Search in sources :

Example 21 with Expression

use of edu.cmu.tetrad.calculator.expression.Expression in project tetrad by cmu-phil.

the class TestExpressionParser method test2.

public void test2() {
    final Map<String, Double> values = new HashMap<>();
    values.put("b11", 1.0);
    values.put("X1", 2.0);
    values.put("X2", 3.0);
    values.put("B22", 4.0);
    values.put("B12", 5.0);
    values.put("X4", 6.0);
    values.put("b13", 7.0);
    values.put("X5", 8.0);
    values.put("b10", 9.0);
    values.put("X", 10.0);
    values.put("Y", 11.0);
    values.put("Z", 12.0);
    values.put("W", 13.0);
    values.put("T", 14.0);
    values.put("R", 15.0);
    values.put("s2", 0.0);
    values.put("s3", 1.0);
    Context context = new Context() {

        public Double getValue(String var) {
            return values.get(var);
        }
    };
    List<String> formulas = new ArrayList<>();
    // 
    formulas.add("ChiSquare(s3)");
    formulas.add("Gamma(1, 1)");
    formulas.add("Beta(3, 5)");
    formulas.add("Poisson(5)");
    formulas.add("Indicator(0.3)");
    formulas.add("ExponentialPower(3)");
    // Log normal
    formulas.add("exp(Normal(s2, s3))");
    formulas.add("Normal(0, s3)");
    // Gaussian Power
    formulas.add("abs(Normal(s2, s3) ^ 3)");
    formulas.add("Discrete(3, 1, 5)");
    // Mixture of Gaussians
    formulas.add("0.3 * Normal(-2.0e2, 0.5) + 0.7 * Normal(2.0, 0.5)");
    formulas.add("StudentT(s3)");
    // Single value.
    formulas.add("s3");
    formulas.add("Hyperbolic(5, 3)");
    formulas.add("Uniform(s2, s3)");
    formulas.add("VonMises(s3)");
    formulas.add("Split(0, 1, 5, 6)");
    formulas.add("Mixture(0.5, N(-2, 0.5), 0.5, N(2, 0.5))");
    // 
    ExpressionParser parser = new ExpressionParser();
    try {
        for (String formula : formulas) {
            Expression expression = parser.parseExpression(formula);
            double value = expression.evaluate(context);
        }
    } 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)

Example 22 with Expression

use of edu.cmu.tetrad.calculator.expression.Expression in project tetrad by cmu-phil.

the class TestExpressionParser method test1.

@Test
public void test1() {
    final Map<String, Double> values = new HashMap<>();
    values.put("b11", 1.0);
    values.put("X1", 2.0);
    values.put("X2", 3.0);
    values.put("B22", 4.0);
    values.put("B12", 5.0);
    values.put("X4", 6.0);
    values.put("b13", 7.0);
    values.put("X5", 8.0);
    values.put("b10", 9.0);
    values.put("X", 10.0);
    values.put("Y", 11.0);
    values.put("Z", 12.0);
    values.put("W", 13.0);
    values.put("T", 14.0);
    values.put("R", 15.0);
    Context context = new Context() {

        public Double getValue(String var) {
            return values.get(var);
        }
    };
    Map<String, Double> formulasToEvaluations = new HashMap<>();
    formulasToEvaluations.put("0", 0.0);
    formulasToEvaluations.put("b11*X1 + sin(X2) + B22*X2 + B12*X4+b13*X5", 100.14);
    formulasToEvaluations.put("X5*X4*X4", 288.0);
    formulasToEvaluations.put("sin(b10*X1)", -0.75097);
    formulasToEvaluations.put("((X + ((Y * (Z ^ W)) * T)) + R)", 16476953628377113.0);
    formulasToEvaluations.put("X + Y * Z ^ W * T + R", 16476953628377113.0);
    formulasToEvaluations.put("pow(2, 5)", 32.0);
    formulasToEvaluations.put("2^5", 32.0);
    formulasToEvaluations.put("exp(1)", 2.718);
    formulasToEvaluations.put("sqrt(2)", 1.414);
    formulasToEvaluations.put("cos(0)", 1.0);
    formulasToEvaluations.put("cos(3.14/2)", 0.0);
    formulasToEvaluations.put("sin(0)", 0.0);
    formulasToEvaluations.put("sin(3.14/2)", 1.0);
    formulasToEvaluations.put("tan(1)", 1.56);
    formulasToEvaluations.put("cosh(1)", 1.54);
    formulasToEvaluations.put("sinh(1)", 1.18);
    formulasToEvaluations.put("tanh(1)", 0.76);
    formulasToEvaluations.put("acos(1)", 0.0);
    formulasToEvaluations.put("asin(1)", 1.57);
    formulasToEvaluations.put("atan(1)", 0.78);
    formulasToEvaluations.put("ln(1)", 0.0);
    formulasToEvaluations.put("log10(10)", 1.0);
    formulasToEvaluations.put("ceil(2.5)", 3.0);
    formulasToEvaluations.put("floor(2.5)", 2.0);
    formulasToEvaluations.put("abs(-5)", 5.0);
    formulasToEvaluations.put("max(2, 5, 3, 1, 10, -3)", 10.0);
    formulasToEvaluations.put("min(2, 5, 3, 1, 10, -3)", -3.0);
    // Logical.
    formulasToEvaluations.put("AND(1, 1)", 1.0);
    formulasToEvaluations.put("AND(1, 0)", 0.0);
    formulasToEvaluations.put("AND(0, 1)", 0.0);
    formulasToEvaluations.put("AND(0, 0)", 0.0);
    formulasToEvaluations.put("AND(0, 0.5)", 0.0);
    formulasToEvaluations.put("1 AND 1", 1.0);
    formulasToEvaluations.put("OR(1, 1)", 1.0);
    formulasToEvaluations.put("OR(1, 0)", 1.0);
    formulasToEvaluations.put("OR(0, 1)", 1.0);
    formulasToEvaluations.put("OR(0, 0)", 0.0);
    formulasToEvaluations.put("OR(0, 0.5)", 0.0);
    formulasToEvaluations.put("1 OR 1", 1.0);
    formulasToEvaluations.put("XOR(1, 1)", 0.0);
    formulasToEvaluations.put("XOR(1, 0)", 1.0);
    formulasToEvaluations.put("XOR(0, 1)", 1.0);
    formulasToEvaluations.put("XOR(0, 0)", 0.0);
    formulasToEvaluations.put("XOR(0, 0.5)", 0.0);
    formulasToEvaluations.put("1 XOR 1", 0.0);
    formulasToEvaluations.put("1 AND 0 OR 1 XOR 1 + 1", 1.0);
    formulasToEvaluations.put("1 < 2", 1.0);
    formulasToEvaluations.put("1 < 0", 0.0);
    formulasToEvaluations.put("1 < 1", 0.0);
    formulasToEvaluations.put("1 <= 2", 1.0);
    formulasToEvaluations.put("1 <= 1", 1.0);
    formulasToEvaluations.put("1 <= -1", 0.0);
    formulasToEvaluations.put("1 = 2", 0.0);
    formulasToEvaluations.put("1 = 1", 1.0);
    formulasToEvaluations.put("1 = -1", 0.0);
    formulasToEvaluations.put("1 > 2", 0.0);
    formulasToEvaluations.put("1 > 1", 0.0);
    formulasToEvaluations.put("1 > -1", 1.0);
    formulasToEvaluations.put("1 >= 2", 0.0);
    formulasToEvaluations.put("1 >= 1", 1.0);
    formulasToEvaluations.put("1 >= -1", 1.0);
    formulasToEvaluations.put("IF(1 > 2, 1, 2)", 2.0);
    formulasToEvaluations.put("IF(1 < 2, 1, 2)", 1.0);
    formulasToEvaluations.put("IF(1 < 2 AND 3 < 4, 1, 2)", 1.0);
    ExpressionParser parser = new ExpressionParser();
    try {
        for (String formula : formulasToEvaluations.keySet()) {
            Expression expression = parser.parseExpression(formula);
            double value = expression.evaluate(context);
            assertEquals(formulasToEvaluations.get(formula), value, 0.01);
        }
    } 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 23 with Expression

use of edu.cmu.tetrad.calculator.expression.Expression in project tetrad by cmu-phil.

the class TestParser method testCommutativeOperators.

/**
 * Tests commutative operators
 */
@Test
public void testCommutativeOperators() {
    ExpressionParser parser = new ExpressionParser();
    Expression expression = parse(parser, "1 + 2 + 3");
    assertTrue(expression.evaluate(new TestingContext()) == 6.0);
    expression = parse(parser, "1 + 1 + 1 + (3 * 4)");
    assertTrue(expression.evaluate(new TestingContext()) == 15.0);
    expression = parse(parser, "1 * 1 * 2 * 3 * (1 + 1)");
    assertTrue(expression.evaluate(new TestingContext()) == 12.0);
}
Also used : Expression(edu.cmu.tetrad.calculator.expression.Expression) ConstantExpression(edu.cmu.tetrad.calculator.expression.ConstantExpression) ExpressionParser(edu.cmu.tetrad.calculator.parser.ExpressionParser) Test(org.junit.Test)

Example 24 with Expression

use of edu.cmu.tetrad.calculator.expression.Expression in project tetrad by cmu-phil.

the class TestParser method testBasicExpressions.

/**
 * Tests expressions without variables (mainly used while writing the parser)
 */
@Test
public void testBasicExpressions() {
    ExpressionParser parser = new ExpressionParser();
    Expression expression = parse(parser, "+(1,1)");
    assertTrue(expression.evaluate(new TestingContext()) == 2.0);
    expression = parse(parser, "*(+(1,2), 5)");
    assertTrue(expression.evaluate(new TestingContext()) == 15.0);
    expression = parse(parser, "1 + 2.5");
    assertTrue(expression.evaluate(new TestingContext()) == 3.5);
    expression = parse(parser, "(2 + 3)");
    assertTrue(expression.evaluate(new TestingContext()) == 5.0);
    expression = parse(parser, "1 + (3 + 4)");
    assertTrue(expression.evaluate(new TestingContext()) == 8.0);
    expression = parse(parser, "1 + 2 + 5");
    assertTrue(expression.evaluate(new TestingContext()) == 8.0);
    expression = parse(parser, "1 + (2 * 3)");
    assertTrue(expression.evaluate(new TestingContext()) == 7.0);
    expression = parse(parser, "1 + (2 + (3 * 4))");
    assertTrue(expression.evaluate(new TestingContext()) == 15.0);
    expression = parse(parser, "(2 * 3) + (4 * 5)");
    assertTrue(expression.evaluate(new TestingContext()) == 26.0);
    expression = parse(parser, "((2 * 3) + (1 + (2 + (3 * 4))))");
    assertTrue(expression.evaluate(new TestingContext()) == 21.0);
    expression = parse(parser, "pow(2,3)");
    assertTrue(expression.evaluate(new TestingContext()) == 8.0);
    expression = parse(parser, "sqrt(pow(2,2))");
    assertTrue(expression.evaluate(new TestingContext()) == 2.0);
    expression = parse(parser, ConstantExpression.E.getName());
    assertTrue(expression.evaluate(new TestingContext()) == ConstantExpression.E.evaluate(null));
    expression = parse(parser, ConstantExpression.PI.getName());
    assertTrue(expression.evaluate(new TestingContext()) == ConstantExpression.PI.evaluate(null));
    expression = parse(parser, ConstantExpression.PI.getName() + "+ 2");
    assertTrue(expression.evaluate(new TestingContext()) == Math.PI + 2);
}
Also used : Expression(edu.cmu.tetrad.calculator.expression.Expression) ConstantExpression(edu.cmu.tetrad.calculator.expression.ConstantExpression) ExpressionParser(edu.cmu.tetrad.calculator.parser.ExpressionParser) Test(org.junit.Test)

Aggregations

Expression (edu.cmu.tetrad.calculator.expression.Expression)24 Context (edu.cmu.tetrad.calculator.expression.Context)10 ExpressionParser (edu.cmu.tetrad.calculator.parser.ExpressionParser)10 Test (org.junit.Test)5 ConstantExpression (edu.cmu.tetrad.calculator.expression.ConstantExpression)4 ParseException (java.text.ParseException)4 Node (edu.cmu.tetrad.graph.Node)3 ArrayList (java.util.ArrayList)3 VariableExpression (edu.cmu.tetrad.calculator.expression.VariableExpression)2 GeneralAndersonDarlingTest (edu.cmu.tetrad.data.GeneralAndersonDarlingTest)1 MultiGeneralAndersonDarlingTest (edu.cmu.tetrad.data.MultiGeneralAndersonDarlingTest)1 GeneralizedSemPm (edu.cmu.tetrad.sem.GeneralizedSemPm)1 List (java.util.List)1 MultivariateFunction (org.apache.commons.math3.analysis.MultivariateFunction)1 RealDistribution (org.apache.commons.math3.distribution.RealDistribution)1 InitialGuess (org.apache.commons.math3.optim.InitialGuess)1 MaxEval (org.apache.commons.math3.optim.MaxEval)1 PointValuePair (org.apache.commons.math3.optim.PointValuePair)1 MultivariateOptimizer (org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer)1 ObjectiveFunction (org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction)1