Search in sources :

Example 11 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 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)

Example 12 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 testChildParentModel4.

/**
 * Two 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 Child > Parent then Param1 else Param2
 *
 * Final families:
 * F1: [Condition: Parent in {5}, Parameters: [Param2]]
 * F2: [Condition: Parent in {1, 2, 3, 4}, Parameters: [Param1, Param2]]
 */
@Test
public void testChildParentModel4() {
    ExpressionBayesianModel model = generateChildParentModel(parse("if Child > Parent then Param1 " + " else Param2 "));
    // (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 expectedParam2inF1 = parse("0.2");
    Expression expectedParam1inF2 = parse("( ((5 - Parent) + " + numberOfDatapoints2 + ")/(5 + " + (numberOfDatapoints1 + numberOfDatapoints2) + ") ) / (5 - Parent)");
    Expression expectedParam2inF2 = parse("( (Parent + " + numberOfDatapoints1 + ")/(5 + " + (numberOfDatapoints1 + numberOfDatapoints2) + ") ) / Parent");
    Expression expectedChildExpression = parse("if Parent = 5 then " + expectedParam2inF1 + " else if Child > Parent then " + expectedParam1inF2 + " else " + expectedParam2inF2);
    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)

Example 13 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 printChildParentModelTest.

/**
 * Printing the test for a simple Child/Parent Bayesian model, choose one of the five expressionForChildNode below to be tested. The distribution (expression) for the parentNode is fixed to a uniform with only one parameter (as set in generateChildParentModel)
 */
public static void printChildParentModelTest() {
    // Model
    // Expression expressionForChildNode = parse("if Child < 5 then Param1 else Param2");
    // Expression expressionForChildNode = parse("if Parent != 5 then Param1 else Param2");
    // Expression expressionForChildNode = parse("if Parent != 5 then if Child < 5 then Param1 else Param2 else Param3");
    // partial intersection
    Expression expressionForChildNode = parse("if Child > Parent then Param1 else Param2");
    // Expression expressionForChildNode = parse("if Parent != 5 then if Child < Parent then Param1 else Param2 else Param3"); // partial intersection
    ExpressionBayesianModel model = generateChildParentModel(expressionForChildNode);
    // Dataset - Order of variables for the datapoints: (Child, Parent)
    // (1, 2)
    int numberOfDatapoints1 = 0;
    // (5, 1)
    int numberOfDatapoints2 = 0;
    // (4, 3)
    int numberOfDatapoints3 = 0;
    // (1, 5)
    int numberOfDatapoints4 = 0;
    DefaultDataset dataset = generateDatasetForChildParentModel(numberOfDatapoints1, numberOfDatapoints2, numberOfDatapoints3, numberOfDatapoints4);
    println("Initial Expression for childNode = " + expressionForChildNode);
    println("Initial Expression for parentNode = " + model.getNodes().get(1) + "\n");
    // Learning
    println("(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) 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)

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