use of com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.api.ExpressionBasedSolver in project aic-praise by aic-sri-international.
the class ExpressionBasedSolverTest method testAPI.
@Test
public void testAPI() {
// IMPORTANT: this test is reproduced in the User Guide as an example,
// so it should be kept in sync with it.
String modelString = "" + "random earthquake: Boolean;" + "random burglary: Boolean;" + "random alarm: Boolean;" + "" + "earthquake 0.01;" + "burglary 0.1;" + "" + "if earthquake" + " then if burglary" + " then alarm 0.95" + " else alarm 0.6" + " else if burglary" + " then alarm 0.9" + " else alarm 0.01;" + " " + "not alarm;" + "";
Expression evidence = parse("not alarm");
// can be any boolean expression
boolean exploitFactorization = true;
// exploit factorization (that is, employ ExpressionVariable Elimination,
// as opposed to summing over the entire joint probability distribution).
DefaultExpressionBasedProblem problem;
Expression marginal;
HOGMExpressionBasedModel model = new HOGMExpressionBasedModel(modelString);
model = model.getConditionedModel(evidence);
Expression queryExpression;
ExpressionBasedSolver solver = new EvaluationExpressionBasedSolver(exploitFactorization);
queryExpression = parse("not earthquake");
// can be any boolean expression, or any random variable
problem = new DefaultExpressionBasedProblem(queryExpression, model);
marginal = solver.solve(problem);
System.out.println("Marginal is " + marginal);
queryExpression = parse("earthquake");
problem = new DefaultExpressionBasedProblem(queryExpression, model);
marginal = solver.solve(problem);
System.out.println("Marginal is " + marginal);
}
use of com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.api.ExpressionBasedSolver in project aic-praise by aic-sri-international.
the class RegularSymbolicParameterEstimation method buildGradient.
private Vector<Expression> buildGradient(FeatureBasedModel featureBasedModel) {
Vector<Expression> gradient = new Vector<Expression>();
for (Expression condition : featureBasedModel.mapConditionToWeight.keySet()) {
Expression parameter = featureBasedModel.mapConditionToWeight.get(condition);
System.out.println("parameter : " + parameter);
ExpressionBasedSolver solver = new ExactBPExpressionBasedSolver();
Expression marginal = solver.solve(condition, expressionBasedModel);
Expression probabilityOfCondition = convertExpression(marginal);
Expression probabilityOfConditionWithSigmoidTrick = applySigmoidTrick(probabilityOfCondition, expressionBasedModel.getContext());
System.out.println("probabilityOfConditionWithSigmoidTrick : " + probabilityOfCondition);
int numberOfOccurences = countOccurencesGivenParameter(parameter);
int numberOfEvidences = evidences.size();
Expression secondTerm = apply(DIVISION, makeSymbol(numberOfOccurences), makeSymbol(numberOfEvidences));
Expression ithTermOfgradient = apply(MINUS, probabilityOfConditionWithSigmoidTrick, secondTerm);
System.out.println(ithTermOfgradient);
gradient.add(ithTermOfgradient);
}
return gradient;
}
use of com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.api.ExpressionBasedSolver in project aic-praise by aic-sri-international.
the class ExpressionBasedSolverTest method runTest.
private void runTest(Expression queryExpression, ExpressionBasedModel model, Expression expected, boolean useFactorization) throws AssertionError {
ExpressionBasedProblem problem = new DefaultExpressionBasedProblem(queryExpression, model);
ExpressionBasedSolver[] solvers = new ExpressionBasedSolver[] { // new EvaluationExpressionBasedSolver(useFactorization),
new ExactBPExpressionBasedSolver() };
for (ExpressionBasedSolver solver : solvers) {
Expression marginal = solver.solve(problem);
checkResult(problem, expected, marginal, solver);
}
}
use of com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.api.ExpressionBasedSolver in project aic-praise by aic-sri-international.
the class ParameterEstimationForExpressionBasedModel method getMarginalBeforeTransformation.
/**
* Return the "raw" marginal, before log and sigmoid transformation.
*/
private Expression getMarginalBeforeTransformation(Entry<Pair<Expression, Expression>, Integer> entry) {
Pair<Expression, Expression> pairEvidenceQuery = entry.getKey();
ExpressionBasedSolver solver = new ExactBPExpressionBasedSolver();
Expression query = pairEvidenceQuery.first;
Expression evidence = pairEvidenceQuery.second;
ExpressionBasedModel conditionedModel = model.getConditionedModel(evidence);
Expression marginal = solver.solve(query, conditionedModel);
return marginal;
}
use of com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.api.ExpressionBasedSolver in project aic-praise by aic-sri-international.
the class GradientLoglikelihoodToOptimize method buildGradient.
/**
* Build the gradient following the formula described in "Graphical Models in a Nutshell".
*/
private Vector<Expression> buildGradient(Map<Expression, Double> mapParametersToValue) {
Vector<Expression> gradient = new Vector<Expression>();
for (Expression condition : featureBasedModel.mapConditionToWeight.keySet()) {
Expression parameter = featureBasedModel.mapConditionToWeight.get(condition);
ExpressionBasedSolver solver = new ExactBPExpressionBasedSolver();
ExpressionBasedModel newExpressionBasedModel = UsefulOperationsParameterEstimation.buildOptimizedExpressionBasedModel(mapParametersToValue, expressionBasedModel);
Expression marginal = solver.solve(condition, newExpressionBasedModel);
// System.out.println("marginal : " + marginal);
Expression probabilityOfConditionWithSigmoidTrick = applySigmoid(marginal, expressionBasedModel.getContext());
// System.out.println("probabilityOfConditionWithSigmoidTrick : " + probabilityOfConditionWithSigmoidTrick);
probabilityOfConditionWithSigmoidTrick = convertExpression(probabilityOfConditionWithSigmoidTrick);
// System.out.println("probabilityOfConditionWithSigmoidTrick after conversion : " + probabilityOfConditionWithSigmoidTrick);
int numberOfOccurences = countOccurencesGivenParameter(parameter);
int numberOfEvidences = evidences.size();
Expression secondTerm = apply(DIVISION, makeSymbol(numberOfOccurences), makeSymbol(numberOfEvidences));
Expression ithTermOfgradient = apply(MINUS, probabilityOfConditionWithSigmoidTrick, secondTerm);
gradient.add(ithTermOfgradient);
}
return gradient;
}
Aggregations