package com.bayesserver.examples;
import com.bayesserver.*;
import com.bayesserver.data.*;
import com.bayesserver.inference.*;
import com.bayesserver.learning.parameters.*;
import java.util.Arrays;
public class ParameterLearningNonTemporal {
public static void main(String[] args) {
Network network = CreateNetworkStructure();
Variable x = network.getVariables().get("X");
Variable y = network.getVariables().get("Y");
ParameterLearning learning = new ParameterLearning(network, new RelevanceTreeInferenceFactory());
ParameterLearningOptions learningOptions = new ParameterLearningOptions();
String connectionUrl = "<connection url to database goes here>";
DataReaderCommand dataReaderCommand = new DatabaseDataReaderCommand(
connectionUrl,
"Select X, Y From MixtureModel");
ReaderOptions readerOptions = new ReaderOptions(null);
VariableReference[] variableReferences = new VariableReference[]
{
new VariableReference(x, ColumnValueType.VALUE, x.getName()),
new VariableReference(y, ColumnValueType.VALUE, y.getName())
};
EvidenceReaderCommand evidenceReaderCommand = new DefaultEvidenceReaderCommand(
dataReaderCommand,
Arrays.asList(variableReferences),
readerOptions);
ParameterLearningOutput result = learning.learn(evidenceReaderCommand, learningOptions);
System.out.println("Log likelihood = " + result.getLogLikelihood());
}
private static Network CreateNetworkStructure() {
Network network = new Network();
Node nodeCluster = new Node("Cluster", new String[]{"Cluster1", "Cluster2", "Cluster3"});
network.getNodes().add(nodeCluster);
Variable x = new Variable("X", VariableValueType.CONTINUOUS);
Variable y = new Variable("Y", VariableValueType.CONTINUOUS);
Node nodePosition = new Node("Position", new Variable[]{x, y});
network.getNodes().add(nodePosition);
network.getLinks().add(new Link(nodeCluster, nodePosition));
return network;
}
}