Search in sources :

Example 1 with ExpressionBayesianNode

use of com.sri.ai.praise.learning.parameterlearning.representation.expression.ExpressionBayesianNode 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 2 with ExpressionBayesianNode

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

the class ExpressionBayesianModelTest method generateEarthquakeBurglaryAlarmModel.

/**
 * Simple model with three nodes, Alarm as child and [Earthquake, Burglary] as parents
 *
 * @return the Earthquake/Burglary/Alarm model
 */
private static ExpressionBayesianModel generateEarthquakeBurglaryAlarmModel() {
    // prior probability
    Expression expressionForEarthquakeNode = parse("if Earthquake = 1 then 0.01 else 0.99");
    Expression expressionForBurglaryNode = parse("if Burglary = 1 then Param5 else OneMinusParam5");
    Expression expressionForAlarmNode = parse("if Earthquake = 1 " + "then if Burglary = 1 " + "then if Alarm = 1 then Param1 else OneMinusParam1 " + "else if Alarm = 1 then Param2 else OneMinusParam2 " + "else if Burglary = 1 " + "then if Alarm = 1 then Param3 else OneMinusParam3 " + "else if Alarm = 1 then Param4 else OneMinusParam4");
    ExpressionBayesianNode earthquakeNode = new ExpressionBayesianNode(expressionForEarthquakeNode, contextForEarthquakeBurglaryAlarmModel, earthquake, list(), Util.set());
    ExpressionBayesianNode burglaryNode = new ExpressionBayesianNode(expressionForBurglaryNode, contextForEarthquakeBurglaryAlarmModel, burglary, list(), Util.set(parse("Param5"), parse("OneMinusParam5")));
    ExpressionBayesianNode alarmNode = new ExpressionBayesianNode(expressionForAlarmNode, contextForEarthquakeBurglaryAlarmModel, alarm, list(earthquake, burglary), Util.set(parse("Param1"), parse("Param2"), parse("Param3"), parse("Param4"), parse("OneMinusParam1"), parse("OneMinusParam2"), parse("OneMinusParam3"), parse("OneMinusParam4")));
    ExpressionBayesianModel model = new ExpressionBayesianModel(list(alarmNode, earthquakeNode, burglaryNode));
    return model;
}
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)

Example 3 with ExpressionBayesianNode

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

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

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

the class ExpressionBayesianModelTest method generateChildParentModel.

/**
 * Simple model, only one childNode and one parentNode, taking as input the expression determining the conditional probability for the childNode, for the parentNode the expression is a fixed constant (Param4)
 *
 * @param expressionForChildNode
 * @return the created model
 */
private static ExpressionBayesianModel generateChildParentModel(Expression expressionForChildNode) {
    Expression param1 = parse("Param1");
    Expression param2 = parse("Param2");
    Expression param3 = parse("Param3");
    Expression param4 = parse("Param4");
    // for the parent it is a uniform distribution, only one parameter (param4)
    ExpressionBayesianNode parentNode = new ExpressionBayesianNode(param4, contextForChildParentModel, parentVariable, list(), Util.set(param4));
    ExpressionBayesianNode childNode = new ExpressionBayesianNode(expressionForChildNode, contextForChildParentModel, childVariable, list(parentVariable), Util.set(param1, param2, param3));
    ExpressionBayesianModel model = new ExpressionBayesianModel(list(childNode, parentNode));
    return model;
}
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)

Aggregations

ExpressionBayesianModel (com.sri.ai.praise.learning.parameterlearning.representation.expression.ExpressionBayesianModel)10 ExpressionBayesianNode (com.sri.ai.praise.learning.parameterlearning.representation.expression.ExpressionBayesianNode)10 Expression (com.sri.ai.expresso.api.Expression)9 DefaultDatapoint (com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint)8 DefaultDataset (com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDataset)8 Test (org.junit.Test)6