use of com.sri.ai.praise.learning.parameterlearning.representation.expression.ExpressionBayesianModel 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);
}
use of com.sri.ai.praise.learning.parameterlearning.representation.expression.ExpressionBayesianModel in project aic-praise by aic-sri-international.
the class ExpressionBayesianModelTest method testEarthquakeBurglaryAlarmModel.
/**
* 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)
*/
@Test
public void testEarthquakeBurglaryAlarmModel() {
ExpressionBayesianModel model = generateEarthquakeBurglaryAlarmModel();
// Dataset - Order of variables for the datapoints: (Alarm, Earthquake, Burglary)
// (1, 0, 1)
int numberOfDatapoints1 = 1;
// (1, 1, 1)
int numberOfDatapoints2 = 2;
DefaultDataset dataset = generateDatasetForEarthquakeBurglaryAlarmModel(numberOfDatapoints1, numberOfDatapoints2);
// Expected values for alarmNode
Expression expectedParam3inF3 = parse("(1 + " + numberOfDatapoints1 + ") / (2 + " + numberOfDatapoints1 + ")");
Expression expectedOneMinusParam3inF3 = parse("1 / (2 + " + numberOfDatapoints1 + ")");
Expression expectedParam1inF1 = parse("(1 + " + numberOfDatapoints2 + ") / (2 + " + numberOfDatapoints2 + ")");
Expression expectedOneMinusParam1inF1 = parse("1 / (2 + " + numberOfDatapoints2 + ")");
Expression expectedAlarmNode = parse("if Earthquake = 0 and Burglary = 1 then if Alarm = 1 then " + expectedParam3inF3 + " else " + expectedOneMinusParam3inF3 + " else if Earthquake = 1 and Burglary = 1 then if Alarm = 1 then " + expectedParam1inF1 + " else " + expectedOneMinusParam1inF1 + " else 0.5");
// Expected values for burglaryNode
Expression expectedParam5inF1burglary = parse("(1 + " + (numberOfDatapoints1 + numberOfDatapoints2) + ") / (2 + " + (numberOfDatapoints1 + numberOfDatapoints2) + ")");
Expression expectedOneMinusParam5inF1burglary = parse("1 / (2 + " + (numberOfDatapoints1 + numberOfDatapoints2) + ")");
Expression expectedBurglaryNode = parse("if Burglary = 1 then " + expectedParam5inF1burglary + " else " + expectedOneMinusParam5inF1burglary);
// Expected value for earthquakeNode (fixed prior probability)
Expression expectedEarthquakeNode = parse("if Earthquake = 1 then 0.01 else 0.99");
// Learning
model = (ExpressionBayesianModel) model.learnModelParametersFromCompleteData(dataset);
ExpressionBayesianNode learnedAlarmNode = model.getNodes().get(0);
ExpressionBayesianNode learnedEarthquakeNode = model.getNodes().get(1);
ExpressionBayesianNode learnedBurglaryNode = model.getNodes().get(2);
// Verifications
Expression alarmNodeVerification = Equality.make(expectedAlarmNode, learnedAlarmNode);
Expression earthquakeNodeVerification = Equality.make(expectedEarthquakeNode, learnedEarthquakeNode);
Expression burglaryNodeVerification = Equality.make(expectedBurglaryNode, learnedBurglaryNode);
alarmNodeVerification = contextForEarthquakeBurglaryAlarmModel.evaluate(alarmNodeVerification);
earthquakeNodeVerification = contextForEarthquakeBurglaryAlarmModel.evaluate(earthquakeNodeVerification);
burglaryNodeVerification = contextForEarthquakeBurglaryAlarmModel.evaluate(burglaryNodeVerification);
// Assertions
assertEquals(Expressions.TRUE, alarmNodeVerification);
assertEquals(Expressions.TRUE, earthquakeNodeVerification);
assertEquals(Expressions.TRUE, burglaryNodeVerification);
}
use of com.sri.ai.praise.learning.parameterlearning.representation.expression.ExpressionBayesianModel in project aic-praise by aic-sri-international.
the class ExpressionBayesianModelTest method testChildParentModel1.
/**
* Only one family having the two parameters
* expressionForChildNode: if Child < 5 then Param1 else Param2
*
* Final families:
* F1: [Condition = true, Parameters: [Param1, Param2]]
*/
@Test
public void testChildParentModel1() {
ExpressionBayesianModel model = generateChildParentModel(parse("if Child < 5 then Param1 else Param2"));
// (1, 2)
int numberOfDatapoints1 = 1;
// (5, 1)
int numberOfDatapoints2 = 2;
DefaultDataset dataset = generateDatasetForChildParentModel(numberOfDatapoints1, numberOfDatapoints2, 0, 0);
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 expectedChildExpression = parse("if Child < 5 then " + expectedParam1inF1 + " else " + expectedParam2inF1);
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);
}
use of com.sri.ai.praise.learning.parameterlearning.representation.expression.ExpressionBayesianModel 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);
}
use of com.sri.ai.praise.learning.parameterlearning.representation.expression.ExpressionBayesianModel 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");
}
}
Aggregations