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 ParameterLearningTemporal {
public static void main(String[] args) {
Network network = createNetworkStructure();
Variable x1 = network.getVariables().get("X1");
Variable x2 = network.getVariables().get("X2");
ParameterLearning learning = new ParameterLearning(network, new RelevanceTreeInferenceFactory());
ParameterLearningOptions learningOptions = new ParameterLearningOptions();
learningOptions.setTimeSeriesMode(TimeSeriesMode.ROLLING);
String connectionUrl = "<connection url to database goes here>";
DataReaderCommand temporalDataReaderCommand = new DatabaseDataReaderCommand(
connectionUrl,
"Select [Case], Time, X1, X2 From TimeSeriesWalkthrough ORDER BY [Case], Time");
TemporalReaderOptions temporalReaderOptions = new TemporalReaderOptions("Case", "Time", TimeValueType.INDEX);
VariableReference[] temporalVariableReferences = new VariableReference[]
{
new VariableReference(x1, ColumnValueType.VALUE, x1.getName()),
new VariableReference(x2, ColumnValueType.VALUE, x2.getName())
};
EvidenceReaderCommand evidenceReaderCommand = new DefaultEvidenceReaderCommand(
temporalDataReaderCommand,
Arrays.asList(temporalVariableReferences),
temporalReaderOptions);
ParameterLearningOutput result = learning.learn(evidenceReaderCommand, learningOptions);
System.out.println("Log likelihood = " + result.getLogLikelihood());
}
private static Network createNetworkStructure() {
Network network = new Network();
Variable x1 = new Variable("X1", VariableValueType.CONTINUOUS);
Variable x2 = new Variable("X2", VariableValueType.CONTINUOUS);
Node nodeX = new Node("X", new Variable[]{x1, x2});
nodeX.setTemporalType(TemporalType.TEMPORAL);
network.getNodes().add(nodeX);
for (int order = 1; order <= 4; order++) {
network.getLinks().add(new Link(nodeX, nodeX, order));
}
return network;
}
}