Search in sources :

Example 1 with DefaultDataset

use of com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDataset 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 DefaultDataset

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

the class ExpressionBayesianModelTest method printEarthquakeBurglaryAlarmModelTest.

/**
 * Printing the test for the Earthquake/Burglary/Alarm model
 *
 * For the alarmNode:
 * We have four families (one for every (Earthquake, Burglary) pair value), and naturally each one with two parameters (one for Alarm = 1 and the other for Alarm = 0)
 *
 * Final families:
 * F1: [Condition: Earthquake = 1 and Burglary = 1, Parameters: [Param1, OneMinusParam1]]
 * F2: [Condition: Earthquake = 1 and Burglary = 0, Parameters: [Param2, OneMinusParam2]]
 * F3: [Condition: Earthquake = 0 and Burglary = 1, Parameters: [Param3, OneMinusParam3]]
 * F4: [Condition: Earthquake = 0 and Burglary = 0, Parameters: [Param4, OneMinusParam4]]
 *
 * For the burglaryNode:
 * Similar and much simpler, we have only one family here, with two parameters (one for Burglary = 1 and the other for Burglary = 0)
 * F1burglary = [Condition: true, Parameters: [Param5, OneMinusParam5]]
 *
 * For the earthquakeNode:
 * Since we have no parameters to learn we have no families here, this expressionFactor is treated as a constant prior probability defined by the user (here, P(Earthquake) is set to 1% in the generateEarthquakeBurglaryAlarmModel method)
 */
public static void printEarthquakeBurglaryAlarmModelTest() {
    ExpressionBayesianModel model = generateEarthquakeBurglaryAlarmModel();
    println("-- Initial Expressions for each node:");
    for (ExpressionBayesianNode node : model.getNodes()) {
        println("\n- " + node.getChildVariable() + " (initial expression)\n" + node);
    }
    // Dataset - Order of variables for the datapoints: (Alarm, Earthquake, Burglary)
    // (1, 0, 1)
    int numberOfDatapoints1 = 2;
    // (1, 1, 1)
    int numberOfDatapoints2 = 0;
    DefaultDataset dataset = generateDatasetForEarthquakeBurglaryAlarmModel(numberOfDatapoints1, numberOfDatapoints2);
    // Learning
    println("\n(Learning ...)");
    long startTime = System.currentTimeMillis();
    model = (ExpressionBayesianModel) model.learnModelParametersFromCompleteData(dataset);
    long stopTime = System.currentTimeMillis();
    long elapsedTime = stopTime - startTime;
    System.out.println("Elapsed time for learning with " + dataset.getDatapoints().size() + " datapoint(s): " + elapsedTime + " miliseconds \n");
    // Printing the learned nodes
    List<ExpressionBayesianNode> learnedNodes = model.getNodes();
    System.out.println("-- Learned nodes:\n");
    for (ExpressionBayesianNode node : learnedNodes) {
        println("- " + node.getChildVariable());
        println("Learned value: " + node);
        println("Families: " + node.getFamilies() + "\n");
    }
}
Also used : ExpressionBayesianModel(com.sri.ai.praise.learning.parameterlearning.representation.expression.ExpressionBayesianModel) ExpressionBayesianNode(com.sri.ai.praise.learning.parameterlearning.representation.expression.ExpressionBayesianNode) DefaultDataset(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDataset) DefaultDatapoint(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint)

Example 3 with DefaultDataset

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

the class ExpressionBayesianModelTest method testChildParentModel2.

/**
 * Two families, one for each parameter
 * expressionForChildNode: if Parent != 5 then Param1 else Param2
 *
 * Final families:
 * F1: [Condition: Parent in {1, 2, 3, 4}, Parameters: [Param1]]
 * F2: [Condition: Parent in {5}, Parameters: [Param2]]
 */
@Test
public void testChildParentModel2() {
    ExpressionBayesianModel model = generateChildParentModel(parse("if Parent != 5 then Param1 else Param2"));
    // (1, 2)
    int numberOfDatapoints1 = 1;
    // (1, 5)
    int numberOfDatapoints4 = 2;
    DefaultDataset dataset = generateDatasetForChildParentModel(numberOfDatapoints1, 0, 0, numberOfDatapoints4);
    model = (ExpressionBayesianModel) model.learnModelParametersFromCompleteData(dataset);
    ExpressionBayesianNode learnedChild = model.getNodes().get(0);
    ExpressionBayesianNode learnedParent = model.getNodes().get(1);
    Expression expectedParam1inF1 = parse("0.2");
    Expression expectedParam2inF2 = parse("0.2");
    Expression expectedChildExpression = parse("if Parent != 5 then " + expectedParam1inF1 + " else " + expectedParam2inF2);
    Expression expectedParentExpression = parse("0.2");
    Expression childVerification = Equality.make(expectedChildExpression, learnedChild);
    Expression parentVerification = Equality.make(expectedParentExpression, learnedParent);
    // println(childVerification); // uncomment this line if you want to see the main equality that is being tested
    childVerification = contextForChildParentModel.evaluate(childVerification);
    parentVerification = contextForChildParentModel.evaluate(parentVerification);
    assertEquals(Expressions.TRUE, childVerification);
    assertEquals(Expressions.TRUE, parentVerification);
}
Also used : ExpressionBayesianModel(com.sri.ai.praise.learning.parameterlearning.representation.expression.ExpressionBayesianModel) Expression(com.sri.ai.expresso.api.Expression) ExpressionBayesianNode(com.sri.ai.praise.learning.parameterlearning.representation.expression.ExpressionBayesianNode) DefaultDataset(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDataset) DefaultDatapoint(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint) Test(org.junit.Test)

Example 4 with DefaultDataset

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

the class ExpressionBayesianModelTest method testChildParentModel3.

/**
 * Two families, one with two of the parameters and the other with the third one
 * expressionForChildNode: if Parent != 5 then if Child < 5 then Param1 else Param2 else Param3
 *
 * Final families:
 * F1: [Condition: Parent in {1, 2, 3, 4}, Parameters: [Param1, Param2]]
 * F2: [Condition: Parent in {5}, Parameters: [Param3]]
 */
@Test
public void testChildParentModel3() {
    ExpressionBayesianModel model = generateChildParentModel(parse("if Parent != 5 then if Child < 5 then Param1 " + "else Param2 " + "else Param3"));
    // (1, 2)
    int numberOfDatapoints1 = 1;
    // (5, 1)
    int numberOfDatapoints2 = 2;
    // (1, 5)
    int numberOfDatapoints4 = 3;
    DefaultDataset dataset = generateDatasetForChildParentModel(numberOfDatapoints1, numberOfDatapoints2, 0, numberOfDatapoints4);
    model = (ExpressionBayesianModel) model.learnModelParametersFromCompleteData(dataset);
    ExpressionBayesianNode learnedChild = model.getNodes().get(0);
    ExpressionBayesianNode learnedParent = model.getNodes().get(1);
    Expression expectedParam1inF1 = parse("( (4 + " + numberOfDatapoints1 + ")/(5 + " + (numberOfDatapoints1 + numberOfDatapoints2) + ") ) / 4");
    Expression expectedParam2inF1 = parse("(1 + " + numberOfDatapoints2 + ")/(5 + " + (numberOfDatapoints1 + numberOfDatapoints2) + ")");
    Expression expectedParam3inF2 = parse("0.2");
    Expression expectedChildExpression = parse("if Parent != 5 then if Child < 5 then " + expectedParam1inF1 + " else " + expectedParam2inF1 + " else " + expectedParam3inF2);
    Expression expectedParentExpression = parse("0.2");
    Expression childVerification = Equality.make(expectedChildExpression, learnedChild);
    Expression parentVerification = Equality.make(expectedParentExpression, learnedParent);
    // println(childVerification); // uncomment this line if you want to see the main equality that is being tested
    childVerification = contextForChildParentModel.evaluate(childVerification);
    parentVerification = contextForChildParentModel.evaluate(parentVerification);
    assertEquals(Expressions.TRUE, childVerification);
    assertEquals(Expressions.TRUE, parentVerification);
}
Also used : ExpressionBayesianModel(com.sri.ai.praise.learning.parameterlearning.representation.expression.ExpressionBayesianModel) Expression(com.sri.ai.expresso.api.Expression) ExpressionBayesianNode(com.sri.ai.praise.learning.parameterlearning.representation.expression.ExpressionBayesianNode) DefaultDataset(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDataset) DefaultDatapoint(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint) Test(org.junit.Test)

Example 5 with DefaultDataset

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

the class ExpressionBayesianModelTest method testChildParentModel5.

/**
 * Three families, case with partial intersection at the beginning and manipulation to generate new completely disjoint families (in terms of their conditions over the Parent)
 * expressionForChildNode: if Parent != 5 then if Child < Parent then Param1 else Param2 else Param3
 *
 * Final families:
 * F1: [Condition: Parent in {1}, Parameters: [Param2]]
 * F2: [Condition: Parent in {5}, Parameters: [Param3]]
 * F3: [Condition: Parent in {2, 3, 4}, Parameters: [Param1, Param2]]
 */
@Test
public void testChildParentModel5() {
    ExpressionBayesianModel model = generateChildParentModel(parse("if Parent != 5 then if Child < Parent then Param1 " + " else Param2 " + " else Param3"));
    // (1, 2)
    int numberOfDatapoints1 = 1;
    // (5, 1)
    int numberOfDatapoints2 = 2;
    // (4, 3)
    int numberOfDatapoints3 = 3;
    // (1, 5)
    int numberOfDatapoints4 = 4;
    DefaultDataset dataset = generateDatasetForChildParentModel(numberOfDatapoints1, numberOfDatapoints2, numberOfDatapoints3, numberOfDatapoints4);
    model = (ExpressionBayesianModel) model.learnModelParametersFromCompleteData(dataset);
    ExpressionBayesianNode learnedChild = model.getNodes().get(0);
    ExpressionBayesianNode learnedParent = model.getNodes().get(1);
    Expression expectedParam2inF1 = parse("0.2");
    Expression expectedParam3inF2 = parse("0.2");
    Expression expectedParam1inF3 = parse("( ((Parent - 1) + " + numberOfDatapoints1 + " ) / ( 5 + " + (numberOfDatapoints1 + numberOfDatapoints3) + ") ) / (Parent - 1)");
    Expression expectedParam2inF3 = parse("( ((6 - Parent) + " + numberOfDatapoints3 + ")/(5 + " + (numberOfDatapoints1 + numberOfDatapoints3) + ") ) / (6 - Parent)");
    Expression expectedChildExpression = parse("if Parent = 1 then " + expectedParam2inF1 + " else if Parent = 5 then " + expectedParam3inF2 + " else if Child < Parent then " + expectedParam1inF3 + " else " + expectedParam2inF3);
    Expression expectedParentExpression = parse("0.2");
    // Generating the childVerification Expression after iteration through all the possible values of Parent
    Expression childVerification;
    Expression andExpression = Expressions.TRUE;
    for (int i = 1; i <= 5; i++) {
        andExpression = And.make(andExpression, Equality.make(expectedChildExpression, learnedChild).replaceAllOccurrences(parentVariable, parse("" + i), contextForChildParentModel));
    }
    childVerification = andExpression;
    // Expression childVerification = Equality.make(expectedChildExpression, learnedChild); // right way to generate the childVerification Expression, but giving errors (related to PRAiSE evaluations for the Parent variable I believe, out of the scope of parameter learning)
    Expression parentVerification = Equality.make(expectedParentExpression, learnedParent);
    // println(childVerification); // uncomment this line if you want to see the main equality that is being tested
    childVerification = contextForChildParentModel.evaluate(childVerification);
    parentVerification = contextForChildParentModel.evaluate(parentVerification);
    assertEquals(Expressions.TRUE, childVerification);
    assertEquals(Expressions.TRUE, parentVerification);
}
Also used : ExpressionBayesianModel(com.sri.ai.praise.learning.parameterlearning.representation.expression.ExpressionBayesianModel) Expression(com.sri.ai.expresso.api.Expression) ExpressionBayesianNode(com.sri.ai.praise.learning.parameterlearning.representation.expression.ExpressionBayesianNode) DefaultDataset(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDataset) DefaultDatapoint(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint) Test(org.junit.Test)

Aggregations

DefaultDatapoint (com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint)13 DefaultDataset (com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDataset)13 ExpressionBayesianModel (com.sri.ai.praise.learning.parameterlearning.representation.expression.ExpressionBayesianModel)8 ExpressionBayesianNode (com.sri.ai.praise.learning.parameterlearning.representation.expression.ExpressionBayesianNode)8 Test (org.junit.Test)8 Expression (com.sri.ai.expresso.api.Expression)7 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 LinkedHashMap (java.util.LinkedHashMap)1