Search in sources :

Example 1 with DefaultDatapoint

use of com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint in project aic-praise by aic-sri-international.

the class ExpressionBayesianModelTest method generateDatasetForChildParentModel.

/**
 * Auxiliar function to generate a dataset for the childParent model based on standard datapoints (designed to test all the different subcases for the model)
 *
 * Order of variables for the datapoints: (Child, Parent)
 * @param numberOfDatapoints1 - (1, 2)
 * @param numberOfDatapoints2 - (5, 1)
 * @param numberOfDatapoints3 - (4, 3)
 * @param numberOfDatapoints4 - (1, 5)
 * @return the dataset with the specified number of datapoints
 */
private static DefaultDataset generateDatasetForChildParentModel(int numberOfDatapoints1, int numberOfDatapoints2, int numberOfDatapoints3, int numberOfDatapoints4) {
    List<ExpressionVariable> variables = list(childVariable, parentVariable);
    DefaultDatapoint datapoint1 = new DefaultDatapoint(variables, list(parse("1"), parse("2")));
    DefaultDatapoint datapoint2 = new DefaultDatapoint(variables, list(parse("5"), parse("1")));
    DefaultDatapoint datapoint3 = new DefaultDatapoint(variables, list(parse("4"), parse("3")));
    DefaultDatapoint datapoint4 = new DefaultDatapoint(variables, list(parse("1"), parse("5")));
    List<DefaultDatapoint> datapoints = list();
    for (int i = 1; i <= numberOfDatapoints1; i++) datapoints.add(datapoint1);
    for (int i = 1; i <= numberOfDatapoints2; i++) datapoints.add(datapoint2);
    for (int i = 1; i <= numberOfDatapoints3; i++) datapoints.add(datapoint3);
    for (int i = 1; i <= numberOfDatapoints4; i++) datapoints.add(datapoint4);
    DefaultDataset dataset = new DefaultDataset(datapoints);
    return dataset;
}
Also used : DefaultDataset(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDataset) DefaultDatapoint(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) ExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable) DefaultDatapoint(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint)

Example 2 with DefaultDatapoint

use of com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint in project aic-praise by aic-sri-international.

the class TableBayesianModelTest method testSickSunColdModelWithDifferentDatapoints.

@Test
public void testSickSunColdModelWithDifferentDatapoints() {
    // Dataset
    List<TableVariable> variables = list(sickVariable, sunVariable, coldVariable);
    List<Integer> variableValues1 = list(1, 0, 1);
    DefaultDatapoint datapoint1 = new DefaultDatapoint(variables, variableValues1);
    List<DefaultDatapoint> datapoints = list();
    int numberOfDatapoints1 = 4;
    for (int i = 1; i <= numberOfDatapoints1; i++) {
        datapoints.add(datapoint1);
    }
    List<Integer> variableValues2 = list(0, 0, 0);
    DefaultDatapoint datapoint2 = new DefaultDatapoint(variables, variableValues2);
    int numberOfDatapoints2 = 2;
    for (int i = 1; i <= numberOfDatapoints2; i++) {
        datapoints.add(datapoint2);
    }
    List<Integer> variableValues3 = list(0, 0, 1);
    DefaultDatapoint datapoint3 = new DefaultDatapoint(variables, variableValues3);
    int numberOfDatapoints3 = 1;
    for (int i = 1; i <= numberOfDatapoints3; i++) {
        datapoints.add(datapoint3);
    }
    DefaultDataset dataset = new DefaultDataset(datapoints);
    // Learning
    sickSunColdModel = (TableBayesianModel) sickSunColdModel.learnModelParametersFromCompleteData(dataset);
    List<? extends TableBayesianNode> learnedNodes = sickSunColdModel.getNodes();
    // Testing
    // For the sickNode first:
    // Expected parameters (2 datapoints1): {(0, [0, 0])=0.5, (1, [0, 0])=0.5, (1, [1, 0])=0.5, (1, [1, 1])=0.5, (0, [1, 1])=0.5, (0, [1, 0])=0.5, (0, [0, 1])=0.25, (1, [0, 1])=0.75}
    TableBayesianNode learnedSickNode = learnedNodes.get(0);
    LinkedHashMap<TableVariable, Integer> variablesAndTheirValues = map();
    variablesAndTheirValues.put(sickVariable, 0);
    variablesAndTheirValues.put(sunVariable, 0);
    variablesAndTheirValues.put(coldVariable, 0);
    // Parameter for (0, [0, 0]):
    Assert.assertEquals(Double.valueOf((1.0 + numberOfDatapoints2) / (2 + numberOfDatapoints2)), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (1, [0, 0]):
    variablesAndTheirValues.put(sickVariable, 1);
    Assert.assertEquals(Double.valueOf(1.0 / (2 + numberOfDatapoints2)), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (1, [1, 0]):
    variablesAndTheirValues.put(sunVariable, 1);
    Assert.assertEquals(Double.valueOf(0.5), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (1, [1, 1]):
    variablesAndTheirValues.put(coldVariable, 1);
    Assert.assertEquals(Double.valueOf(0.5), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (0, [1, 1]):
    variablesAndTheirValues.put(sickVariable, 0);
    Assert.assertEquals(Double.valueOf(0.5), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (0, [1, 0]):
    variablesAndTheirValues.put(coldVariable, 0);
    Assert.assertEquals(Double.valueOf(0.5), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (0, [0, 1]):
    variablesAndTheirValues.put(sunVariable, 0);
    variablesAndTheirValues.put(coldVariable, 1);
    Assert.assertEquals(Double.valueOf((1.0 + numberOfDatapoints3) / (2 + numberOfDatapoints1 + numberOfDatapoints3)), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (1, [0, 1]):
    variablesAndTheirValues.put(sickVariable, 1);
    Assert.assertEquals(Double.valueOf((1.0 + numberOfDatapoints1) / (2 + numberOfDatapoints1 + numberOfDatapoints3)), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // For the sunNode:
    // Expected parameters (2 datapoints): {(0, [])=0.75, (1, [])=0.25}
    TableBayesianNode learnedSunNode = learnedNodes.get(1);
    variablesAndTheirValues = map();
    variablesAndTheirValues.put(sunVariable, 0);
    // Parameter for (0, []):
    Assert.assertEquals(Double.valueOf((1.0 + numberOfDatapoints1 + numberOfDatapoints2 + numberOfDatapoints3) / (2 + numberOfDatapoints1 + numberOfDatapoints2 + numberOfDatapoints3)), learnedSunNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (1, []):
    variablesAndTheirValues.put(sunVariable, 1);
    Assert.assertEquals(Double.valueOf(1.0 / (2 + numberOfDatapoints1 + numberOfDatapoints2 + numberOfDatapoints3)), learnedSunNode.getEntryFor(variablesAndTheirValues));
    // For the coldNode:
    // Expected parameters (2 datapoints): {(0, [])=0.25, (1, [])=0.75}
    TableBayesianNode learnedColdNode = learnedNodes.get(2);
    variablesAndTheirValues = map();
    variablesAndTheirValues.put(coldVariable, 0);
    // Parameter for (0, []):
    Assert.assertEquals(Double.valueOf((1.0 + numberOfDatapoints2) / (2 + numberOfDatapoints1 + numberOfDatapoints2 + numberOfDatapoints3)), learnedColdNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (1, []):
    variablesAndTheirValues.put(coldVariable, 1);
    Assert.assertEquals(Double.valueOf((1.0 + numberOfDatapoints1 + numberOfDatapoints3) / (2 + numberOfDatapoints1 + numberOfDatapoints2 + numberOfDatapoints3)), learnedColdNode.getEntryFor(variablesAndTheirValues));
}
Also used : TableBayesianNode(com.sri.ai.praise.learning.parameterlearning.representation.table.TableBayesianNode) DefaultDataset(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDataset) DefaultDatapoint(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable) DefaultDatapoint(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint) Test(org.junit.Test)

Example 3 with DefaultDatapoint

use of com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint in project aic-praise by aic-sri-international.

the class TableBayesianModelTest method testSickSunColdModel.

@Test
public void testSickSunColdModel() {
    // Dataset
    List<TableVariable> variables = list(sickVariable, sunVariable, coldVariable);
    List<Integer> variableValues = list(1, 0, 1);
    DefaultDatapoint datapoint = new DefaultDatapoint(variables, variableValues);
    List<DefaultDatapoint> datapoints = list();
    int numberOfDatapoints = 2;
    for (int i = 1; i <= numberOfDatapoints; i++) {
        datapoints.add(datapoint);
    }
    DefaultDataset dataset = new DefaultDataset(datapoints);
    // Learning
    sickSunColdModel = (TableBayesianModel) sickSunColdModel.learnModelParametersFromCompleteData(dataset);
    List<? extends TableBayesianNode> learnedNodes = sickSunColdModel.getNodes();
    // Testing
    // For the sickNode first:
    // Expected parameters (2 datapoints): {(0, [0, 0])=0.5, (1, [0, 0])=0.5, (1, [1, 0])=0.5, (1, [1, 1])=0.5, (0, [1, 1])=0.5, (0, [1, 0])=0.5, (0, [0, 1])=0.25, (1, [0, 1])=0.75}
    TableBayesianNode learnedSickNode = learnedNodes.get(0);
    LinkedHashMap<TableVariable, Integer> variablesAndTheirValues = map();
    variablesAndTheirValues.put(sickVariable, 0);
    variablesAndTheirValues.put(sunVariable, 0);
    variablesAndTheirValues.put(coldVariable, 0);
    // Parameter for (0, [0, 0]):
    Assert.assertEquals(Double.valueOf(0.5), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (1, [0, 0]):
    variablesAndTheirValues.put(sickVariable, 1);
    Assert.assertEquals(Double.valueOf(0.5), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (1, [1, 0]):
    variablesAndTheirValues.put(sunVariable, 1);
    Assert.assertEquals(Double.valueOf(0.5), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (1, [1, 1]):
    variablesAndTheirValues.put(coldVariable, 1);
    Assert.assertEquals(Double.valueOf(0.5), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (0, [1, 1]):
    variablesAndTheirValues.put(sickVariable, 0);
    Assert.assertEquals(Double.valueOf(0.5), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (0, [1, 0]):
    variablesAndTheirValues.put(coldVariable, 0);
    Assert.assertEquals(Double.valueOf(0.5), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (0, [0, 1]):
    variablesAndTheirValues.put(sunVariable, 0);
    variablesAndTheirValues.put(coldVariable, 1);
    Assert.assertEquals(Double.valueOf(1.0 / (2 + numberOfDatapoints)), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (1, [0, 1]):
    variablesAndTheirValues.put(sickVariable, 1);
    Assert.assertEquals(Double.valueOf((1.0 + numberOfDatapoints) / (2 + numberOfDatapoints)), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // For the sunNode:
    // Expected parameters (2 datapoints): {(0, [])=0.75, (1, [])=0.25}
    TableBayesianNode learnedSunNode = learnedNodes.get(1);
    variablesAndTheirValues = map();
    variablesAndTheirValues.put(sunVariable, 0);
    // Parameter for (0, []):
    Assert.assertEquals(Double.valueOf((1.0 + numberOfDatapoints) / (2 + numberOfDatapoints)), learnedSunNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (1, []):
    variablesAndTheirValues.put(sunVariable, 1);
    Assert.assertEquals(Double.valueOf(1.0 / (2 + numberOfDatapoints)), learnedSunNode.getEntryFor(variablesAndTheirValues));
    // For the coldNode:
    // Expected parameters (2 datapoints): {(0, [])=0.25, (1, [])=0.75}
    TableBayesianNode learnedColdNode = learnedNodes.get(2);
    variablesAndTheirValues = map();
    variablesAndTheirValues.put(coldVariable, 0);
    // Parameter for (0, []):
    Assert.assertEquals(Double.valueOf(1.0 / (2 + numberOfDatapoints)), learnedColdNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (1, []):
    variablesAndTheirValues.put(coldVariable, 1);
    Assert.assertEquals(Double.valueOf((1.0 + numberOfDatapoints) / (2 + numberOfDatapoints)), learnedColdNode.getEntryFor(variablesAndTheirValues));
}
Also used : TableBayesianNode(com.sri.ai.praise.learning.parameterlearning.representation.table.TableBayesianNode) DefaultDataset(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDataset) DefaultDatapoint(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable) DefaultDatapoint(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint) Test(org.junit.Test)

Example 4 with DefaultDatapoint

use of com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint in project aic-praise by aic-sri-international.

the class TableBayesianModelTest method printSickSunColdModelTest.

public static void printSickSunColdModelTest() {
    // Dataset
    List<TableVariable> variables = list(sickVariable, sunVariable, coldVariable);
    List<Integer> variableValues = list(1, 0, 1);
    DefaultDatapoint datapoint = new DefaultDatapoint(variables, variableValues);
    List<DefaultDatapoint> datapoints = list();
    int numberOfDatapoints = 2;
    for (int i = 1; i <= numberOfDatapoints; i++) {
        datapoints.add(datapoint);
    }
    DefaultDataset dataset = new DefaultDataset(datapoints);
    // Learning
    long startTime = System.currentTimeMillis();
    sickSunColdModel = (TableBayesianModel) sickSunColdModel.learnModelParametersFromCompleteData(dataset);
    long stopTime = System.currentTimeMillis();
    long elapsedTime = stopTime - startTime;
    System.out.println("Elapsed time for learning with " + numberOfDatapoints + " datapoints: " + elapsedTime + " miliseconds \n");
    List<? extends TableBayesianNode> learnedNodes = sickSunColdModel.getNodes();
    // Testing
    String expectedParametersForSick = "{(0, [0, 0])=0.5, (1, [0, 0])=0.5, (0, [0, 1])=0.25, (1, [0, 1])=0.75, (0, [1, 0])=0.5, (1, [1, 0])=0.5, (0, [1, 1])=0.5, (1, [1, 1])=0.5}";
    System.out.println("Expected parameters for sick (with 2 datapoints):\n" + expectedParametersForSick + "\n");
    TableBayesianNode learnedSickNode = learnedNodes.get(0);
    LinkedHashMap<TableVariable, Integer> variablesAndTheirValues = new LinkedHashMap<TableVariable, Integer>();
    variablesAndTheirValues.put(sickVariable, 1);
    variablesAndTheirValues.put(sunVariable, 0);
    variablesAndTheirValues.put(coldVariable, 1);
    System.out.println("Actual entries for sick:");
    System.out.println("entryFor(" + variablesAndTheirValues.get(sickVariable) + ", [" + variablesAndTheirValues.get(sunVariable) + ", " + variablesAndTheirValues.get(coldVariable) + "]) = " + learnedSickNode.getEntryFor(variablesAndTheirValues));
    variablesAndTheirValues.put(sickVariable, 0);
    System.out.println("entryFor(" + variablesAndTheirValues.get(sickVariable) + ", [" + variablesAndTheirValues.get(sunVariable) + ", " + variablesAndTheirValues.get(coldVariable) + "]) = " + learnedSickNode.getEntryFor(variablesAndTheirValues));
    variablesAndTheirValues.put(coldVariable, 0);
    System.out.println("entryFor(" + variablesAndTheirValues.get(sickVariable) + ", [" + variablesAndTheirValues.get(sunVariable) + ", " + variablesAndTheirValues.get(coldVariable) + "]) = " + learnedSickNode.getEntryFor(variablesAndTheirValues));
}
Also used : TableBayesianNode(com.sri.ai.praise.learning.parameterlearning.representation.table.TableBayesianNode) DefaultDatapoint(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint) LinkedHashMap(java.util.LinkedHashMap) DefaultDataset(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDataset) DefaultDatapoint(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable)

Example 5 with DefaultDatapoint

use of com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint in project aic-praise by aic-sri-international.

the class ExpressionBayesianModelTest method generateDatasetForEarthquakeBurglaryAlarmModel.

/**
 * Auxiliar function to generate a dataset for the Earthquake/Burglary/Alarm model based on some standard datapoints
 *
 * Order of variables for the datapoints: (Alarm, Earthquake, Burglary)
 * @param numberOfDatapoints1 - (1, 0, 1)
 * @param numberOfDatapoints2 - (1, 1, 1)
 * @return
 */
private static DefaultDataset generateDatasetForEarthquakeBurglaryAlarmModel(int numberOfDatapoints1, int numberOfDatapoints2) {
    List<ExpressionVariable> variables = list(alarm, earthquake, burglary);
    DefaultDatapoint datapoint1 = new DefaultDatapoint(variables, list(parse("1"), parse("0"), parse("1")));
    DefaultDatapoint datapoint2 = new DefaultDatapoint(variables, list(parse("1"), parse("1"), parse("1")));
    List<DefaultDatapoint> datapoints = list();
    for (int i = 1; i <= numberOfDatapoints1; i++) datapoints.add(datapoint1);
    for (int i = 1; i <= numberOfDatapoints2; i++) datapoints.add(datapoint2);
    DefaultDataset dataset = new DefaultDataset(datapoints);
    return dataset;
}
Also used : DefaultDataset(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDataset) DefaultDatapoint(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) ExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable) DefaultDatapoint(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint)

Aggregations

DefaultDatapoint (com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint)5 DefaultDataset (com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDataset)5 TableVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable)3 TableBayesianNode (com.sri.ai.praise.learning.parameterlearning.representation.table.TableBayesianNode)3 ExpressionVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable)2 DefaultExpressionVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable)2 Test (org.junit.Test)2 LinkedHashMap (java.util.LinkedHashMap)1