Skip to main content

Structural learning in C#

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

namespace BayesServer.HelpSamples
{
using BayesServer.Data;
using Learning.Structure.PC;
using System;
using System.Data;
using System.Linq;

public static class StructuralLearningNonTemporal
{
public static void Main()
{
var network = CreateNetworkNoLinks(); // we manually construct the network here, but it could be loaded from a file

var learning = new PCStructuralLearning();

var dataReaderCommand = CreateDataReaderCommand();

var variableReferences = network.Variables.Select(v => new VariableReference(v, ColumnValueType.Name, v.Name)).ToArray();

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

var options = new PCStructuralLearningOptions();

var output = learning.Learn(evidenceReaderCommand, network.Nodes, options);

foreach (var linkOutput in output.LinkOutputs)
{
Console.WriteLine("Link added from {0} -> {1}", linkOutput.Link.From.Name, linkOutput.Link.To.Name);
}

}

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

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

foreach (var name in new string[] { "A", "B", "C" })
{
var node = new Node(name, new string[] { "False", "True" });
network.Nodes.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.

var data = new DataTable();
data.Columns.Add("A", typeof(string));
data.Columns.Add("B", typeof(string));
data.Columns.Add("C", typeof(string));

data.Rows.Add("True", "False", "True");
data.Rows.Add("True", "True", "True");
data.Rows.Add("True", "False", "True");
data.Rows.Add("False", "False", "False");
data.Rows.Add("False", "True", "True");
data.Rows.Add("False", "False", "False");
data.Rows.Add("False", "True", "True");
data.Rows.Add("True", "False", "True");
data.Rows.Add("True", "True", "True");
data.Rows.Add("True", "False", "True");
data.Rows.Add("True", "False", "True");
data.Rows.Add("False", "True", "True");
data.Rows.Add("True", "True", "True");
data.Rows.Add("True", "True", "True");
data.Rows.Add("False", "False", "False");
data.Rows.Add("True", "False", "True");
data.Rows.Add("False", "True", "True");
data.Rows.Add("False", "False", "False");
data.Rows.Add("True", "False", "True");
data.Rows.Add("False", "True", "True");
data.Rows.Add("False", "False", "False");
data.Rows.Add("True", "False", "True");
data.Rows.Add("False", "False", "False");
data.Rows.Add("True", "True", "True");
data.Rows.Add("False", "False", "False");
data.Rows.Add("True", "False", "True");
data.Rows.Add("False", "False", "False");
data.Rows.Add("False", "False", "False");
data.Rows.Add("True", "True", "True");
data.Rows.Add("False", "False", "False");
data.Rows.Add("True", "False", "True");
data.Rows.Add("False", "False", "False");
data.Rows.Add("True", "False", "True");
data.Rows.Add("True", "False", "True");
data.Rows.Add("True", "False", "True");
data.Rows.Add("True", "True", "True");
data.Rows.Add("True", "True", "True");
data.Rows.Add("True", "False", "True");
data.Rows.Add("False", "False", "False");
data.Rows.Add("True", "False", "True");
data.Rows.Add("True", "True", "True");
data.Rows.Add("False", "True", "True");
data.Rows.Add("True", "False", "True");
data.Rows.Add("False", "False", "False");
data.Rows.Add("False", "False", "False");
data.Rows.Add("True", "False", "True");
data.Rows.Add("True", "True", "True");
data.Rows.Add("True", "False", "True");
data.Rows.Add("False", "False", "False");
data.Rows.Add("True", "True", "True");
data.Rows.Add("True", "True", "True");
data.Rows.Add("True", "True", "True");
data.Rows.Add("True", "False", "True");
data.Rows.Add("True", "False", "True");
data.Rows.Add("True", "False", "True");
data.Rows.Add("True", "False", "True");
data.Rows.Add("True", "False", "True");
data.Rows.Add("True", "False", "True");
data.Rows.Add("False", "False", "False");
data.Rows.Add("True", "False", "True");
data.Rows.Add("False", "True", "True");
data.Rows.Add("False", "False", "False");
data.Rows.Add("True", "True", "True");
data.Rows.Add("False", "True", "True");
data.Rows.Add("True", "True", "True");
data.Rows.Add("False", "True", "True");
data.Rows.Add("False", "False", "False");
data.Rows.Add("True", "True", "True");
data.Rows.Add("False", "False", "False");
data.Rows.Add("False", "False", "False");
data.Rows.Add("True", "True", "True");
data.Rows.Add("True", "False", "True");
data.Rows.Add("False", "False", "False");
data.Rows.Add("False", "False", "False");
data.Rows.Add("True", "False", "True");
data.Rows.Add("True", "False", "True");
data.Rows.Add("True", "True", "True");
data.Rows.Add("False", "True", "True");
data.Rows.Add("True", "False", "True");
data.Rows.Add("False", "True", "True");
data.Rows.Add("True", "True", "True");
data.Rows.Add("True", "True", "True");
data.Rows.Add("True", "True", "True");
data.Rows.Add("True", "False", "True");
data.Rows.Add("False", "True", "True");
data.Rows.Add("True", "False", "True");
data.Rows.Add("True", "False", "True");
data.Rows.Add("False", "False", "False");
data.Rows.Add("True", "False", "True");
data.Rows.Add("True", "False", "True");
data.Rows.Add("True", "True", "True");
data.Rows.Add("False", "False", "False");
data.Rows.Add("False", "True", "True");
data.Rows.Add("True", "False", "True");
data.Rows.Add("True", "False", "True");
data.Rows.Add("True", "True", "True");
data.Rows.Add("True", "True", "True");
data.Rows.Add("False", "True", "True");
data.Rows.Add("True", "True", "True");
data.Rows.Add("True", "False", "True");

return new DataTableDataReaderCommand(data);

}
}
}