Structural learning in Java

package com.bayesserver.examples;

// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StructuralLearningNonTemporal.cs" company="Bayes Server">
//   Copyright (C) Bayes Server.  All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

import com.bayesserver.Network;
import com.bayesserver.Node;
import com.bayesserver.Variable;
import com.bayesserver.data.*;
import com.bayesserver.learning.structure.*;

import java.util.ArrayList;
import java.util.List;

public class StructuralLearningNonTemporal {

    public static void main(String[] args) {

        Network network = CreateNetworkNoLinks();  // we manually construct the network here, but it could be loaded from a file

        PCStructuralLearning learning = new PCStructuralLearning();

        DataReaderCommand dataReaderCommand = CreateDataReaderCommand();

        List<VariableReference> variableReferences = new ArrayList<VariableReference>();

        for (Variable v : network.getVariables()) {

            variableReferences.add(new VariableReference(v, ColumnValueType.NAME, v.getName()));
        }

        EvidenceReaderCommand evidenceReaderCommand = new DefaultEvidenceReaderCommand(
                dataReaderCommand,
                variableReferences,
                new ReaderOptions());

        PCStructuralLearningOptions options = new PCStructuralLearningOptions();

        PCStructuralLearningOutput output = (PCStructuralLearningOutput) learning.learn(evidenceReaderCommand, network.getNodes(), options);

        for (LinkOutput linkOutput : output.getLinkOutputs()) {

            System.out.println(
                    String.format("Link added from %s -> %s",
                            linkOutput.getLink().getFrom().getName(),
                            linkOutput.getLink().getTo().getName()));
        }

    }

    /// <summary>
    /// Manually construct a network to keep the example simple.
    /// </summary>
    private static Network CreateNetworkNoLinks() {

        Network network = new Network();

        // Instead of manually constructing a network
        // you could also load from a file using
        // network.Load("path-to-file");

        for (String name : new String[]{"A", "B", "C"}) {
            Node node = new Node(name, new String[]{"False", "True"});
            network.getNodes().add(node);
        }

        // We are not adding links here, as we are going to learn them from data.

        return network;
    }

    /// <summary>
    /// Create a data reader command.
    /// </summary>
    /// <remarks>
    /// Normally you would read from a database or spreadsheet, but to keep this example simple
    /// we are hard coding the data.
    /// </remarks>
    private static DataTableDataReaderCommand CreateDataReaderCommand() {

        // See the Parameter learning sample code, for an
        // example of how to read from a database.

        DataTable data = new DataTable();

        DataColumnCollection columns = data.getColumns();
        columns.add("A", String.class);
        columns.add("B", String.class);
        columns.add("C", String.class);

        DataRowCollection rows = data.getRows();
        rows.add("True", "False", "True");
        rows.add("True", "True", "True");
        rows.add("True", "False", "True");
        rows.add("False", "False", "False");
        rows.add("False", "True", "True");
        rows.add("False", "False", "False");
        rows.add("False", "True", "True");
        rows.add("True", "False", "True");
        rows.add("True", "True", "True");
        rows.add("True", "False", "True");
        rows.add("True", "False", "True");
        rows.add("False", "True", "True");
        rows.add("True", "True", "True");
        rows.add("True", "True", "True");
        rows.add("False", "False", "False");
        rows.add("True", "False", "True");
        rows.add("False", "True", "True");
        rows.add("False", "False", "False");
        rows.add("True", "False", "True");
        rows.add("False", "True", "True");
        rows.add("False", "False", "False");
        rows.add("True", "False", "True");
        rows.add("False", "False", "False");
        rows.add("True", "True", "True");
        rows.add("False", "False", "False");
        rows.add("True", "False", "True");
        rows.add("False", "False", "False");
        rows.add("False", "False", "False");
        rows.add("True", "True", "True");
        rows.add("False", "False", "False");
        rows.add("True", "False", "True");
        rows.add("False", "False", "False");
        rows.add("True", "False", "True");
        rows.add("True", "False", "True");
        rows.add("True", "False", "True");
        rows.add("True", "True", "True");
        rows.add("True", "True", "True");
        rows.add("True", "False", "True");
        rows.add("False", "False", "False");
        rows.add("True", "False", "True");
        rows.add("True", "True", "True");
        rows.add("False", "True", "True");
        rows.add("True", "False", "True");
        rows.add("False", "False", "False");
        rows.add("False", "False", "False");
        rows.add("True", "False", "True");
        rows.add("True", "True", "True");
        rows.add("True", "False", "True");
        rows.add("False", "False", "False");
        rows.add("True", "True", "True");
        rows.add("True", "True", "True");
        rows.add("True", "True", "True");
        rows.add("True", "False", "True");
        rows.add("True", "False", "True");
        rows.add("True", "False", "True");
        rows.add("True", "False", "True");
        rows.add("True", "False", "True");
        rows.add("True", "False", "True");
        rows.add("False", "False", "False");
        rows.add("True", "False", "True");
        rows.add("False", "True", "True");
        rows.add("False", "False", "False");
        rows.add("True", "True", "True");
        rows.add("False", "True", "True");
        rows.add("True", "True", "True");
        rows.add("False", "True", "True");
        rows.add("False", "False", "False");
        rows.add("True", "True", "True");
        rows.add("False", "False", "False");
        rows.add("False", "False", "False");
        rows.add("True", "True", "True");
        rows.add("True", "False", "True");
        rows.add("False", "False", "False");
        rows.add("False", "False", "False");
        rows.add("True", "False", "True");
        rows.add("True", "False", "True");
        rows.add("True", "True", "True");
        rows.add("False", "True", "True");
        rows.add("True", "False", "True");
        rows.add("False", "True", "True");
        rows.add("True", "True", "True");
        rows.add("True", "True", "True");
        rows.add("True", "True", "True");
        rows.add("True", "False", "True");
        rows.add("False", "True", "True");
        rows.add("True", "False", "True");
        rows.add("True", "False", "True");
        rows.add("False", "False", "False");
        rows.add("True", "False", "True");
        rows.add("True", "False", "True");
        rows.add("True", "True", "True");
        rows.add("False", "False", "False");
        rows.add("False", "True", "True");
        rows.add("True", "False", "True");
        rows.add("True", "False", "True");
        rows.add("True", "True", "True");
        rows.add("True", "True", "True");
        rows.add("False", "True", "True");
        rows.add("True", "True", "True");
        rows.add("True", "False", "True");

        return new DataTableDataReaderCommand(data);

    }
}