use of edu.cmu.tetrad.calculator.parser.ExpressionParser in project tetrad by cmu-phil.
the class TestExpressionParser method test4.
// Formulas that should and should not fail.
@Test
public void test4() {
Map<String, Integer> formulasToOffsets = new LinkedHashMap<>();
// -1 means it should parse.
formulasToOffsets.put("X X", 2);
formulasToOffsets.put("b11 b11", 4);
formulasToOffsets.put("X + Y ++", 7);
formulasToOffsets.put("b1*X1 *+ b2 * X2 +", 17);
formulasToOffsets.put("cos()", 0);
formulasToOffsets.put("2..3 * X", 0);
formulasToOffsets.put("+X1", -1);
formulasToOffsets.put("-X1", -1);
formulasToOffsets.put("A / B", -1);
formulasToOffsets.put("b1*X1 +@**!! b2 * X2", 7);
formulasToOffsets.put("X7", 0);
List<String> otherNodes = new ArrayList<>();
otherNodes.add("X7");
ExpressionParser parser = new ExpressionParser(otherNodes, ExpressionParser.RestrictionType.MAY_NOT_CONTAIN);
for (String formula : formulasToOffsets.keySet()) {
try {
parser.parseExpression(formula);
assertEquals(formulasToOffsets.get(formula).intValue(), -1);
} catch (ParseException e) {
int offset = e.getErrorOffset();
assertEquals(formulasToOffsets.get(formula).intValue(), offset);
}
}
}
use of edu.cmu.tetrad.calculator.parser.ExpressionParser in project tetrad by cmu-phil.
the class TestExpressionParser method test5.
// Test distribution means.
@Test
public void test5() {
final Map<String, Double> values = new HashMap<>();
Context context = new Context() {
public Double getValue(String var) {
return values.get(var);
}
};
Map<String, Double> formulas = new LinkedHashMap<>();
formulas.put("ChiSquare(1)", 1.0);
formulas.put("Gamma(2, .5)", 1.0);
formulas.put("Beta(1, 2)", 0.33);
formulas.put("Normal(2, 3)", 2.0);
formulas.put("N(2, 3)", 2.0);
formulas.put("StudentT(5)", 0.0);
formulas.put("U(0, 1)", 0.5);
formulas.put("Uniform(0, 1)", 0.5);
formulas.put("Split(0, 1, 5, 6)", 3.0);
ExpressionParser parser = new ExpressionParser();
try {
for (String formula : formulas.keySet()) {
Expression expression = parser.parseExpression(formula);
double sum = 0.0;
int sampleSize = 10000;
for (int i = 0; i < sampleSize; i++) {
double value = expression.evaluate(context);
sum += value;
}
double mean = sum / sampleSize;
assertEquals(formulas.get(formula), mean, 0.1);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
use of edu.cmu.tetrad.calculator.parser.ExpressionParser in project tetrad by cmu-phil.
the class GeneralizedSemPm method setErrorsTemplate.
public void setErrorsTemplate(String errorsTemplate) throws ParseException {
if (errorsTemplate == null) {
throw new NullPointerException();
}
// Test to make sure it's parsable.
ExpressionParser parser = new ExpressionParser();
parser.parseExpression(errorsTemplate);
this.errorsTemplate = errorsTemplate;
}
use of edu.cmu.tetrad.calculator.parser.ExpressionParser in project tetrad by cmu-phil.
the class GeneralizedSemPm method setStartsWithParametersTemplate.
public void setStartsWithParametersTemplate(String startsWith, String parametersTemplate) throws ParseException {
if (startsWith == null || startsWith.isEmpty()) {
return;
}
if (parametersTemplate == null) {
throw new NullPointerException();
}
// Test to make sure it's parsable.
ExpressionParser parser = new ExpressionParser();
parser.parseExpression(parametersTemplate);
if (startsWith.contains(" ")) {
throw new IllegalArgumentException("Starts with string contains spaces.");
}
// this.parametersTemplate = parametersTemplate;
this.startsWithParametersTemplates.put(startsWith, parametersTemplate);
}
use of edu.cmu.tetrad.calculator.parser.ExpressionParser in project tetrad by cmu-phil.
the class GeneralizedSemPm method setParameterExpression.
/**
* Sets the expression which should be evaluated when calculating new values for the given
* parameter. These values are used to initialize the freeParameters.
* @param parameter The parameter whose initial value needs to be computed.
* @param expressionString The formula for picking initial values.
* @throws ParseException If the formula cannot be parsed or contains variable names.
*/
public void setParameterExpression(String startsWith, String parameter, String expressionString) throws ParseException {
if (parameter == null) {
throw new NullPointerException("Parameter was null.");
}
if (startsWith == null) {
throw new NullPointerException("StartsWith expression was null.");
}
if (startsWith.contains(" ")) {
throw new IllegalArgumentException("StartsWith expression contains spaces.");
}
if (expressionString == null) {
throw new NullPointerException("Expression string was null.");
}
// Parse the expression. This could throw an ParseException, but that exception needs to handed up the
// chain, because the interface will need it.
ExpressionParser parser = new ExpressionParser();
Expression expression = parser.parseExpression(expressionString);
List<String> parameterNames = parser.getParameters();
if (parameterNames.size() > 0) {
throw new IllegalArgumentException("Initial distribution may not " + "contain parameters: " + expressionString);
}
parameterExpressions.put(parameter, expression);
parameterExpressionStrings.put(parameter, expressionString);
startsWithParametersTemplates.put(startsWith, expressionString);
}
Aggregations