Search in sources :

Example 1 with ExpressionParser

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);
        }
    }
}
Also used : ExpressionParser(edu.cmu.tetrad.calculator.parser.ExpressionParser) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 2 with ExpressionParser

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();
    }
}
Also used : Context(edu.cmu.tetrad.calculator.expression.Context) Expression(edu.cmu.tetrad.calculator.expression.Expression) ExpressionParser(edu.cmu.tetrad.calculator.parser.ExpressionParser) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 3 with ExpressionParser

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;
}
Also used : ExpressionParser(edu.cmu.tetrad.calculator.parser.ExpressionParser)

Example 4 with ExpressionParser

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);
}
Also used : ExpressionParser(edu.cmu.tetrad.calculator.parser.ExpressionParser)

Example 5 with ExpressionParser

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);
}
Also used : Expression(edu.cmu.tetrad.calculator.expression.Expression) ExpressionParser(edu.cmu.tetrad.calculator.parser.ExpressionParser)

Aggregations

ExpressionParser (edu.cmu.tetrad.calculator.parser.ExpressionParser)22 Expression (edu.cmu.tetrad.calculator.expression.Expression)10 Test (org.junit.Test)8 ParseException (java.text.ParseException)7 ConstantExpression (edu.cmu.tetrad.calculator.expression.ConstantExpression)3 Context (edu.cmu.tetrad.calculator.expression.Context)3 Node (edu.cmu.tetrad.graph.Node)2