Search in sources :

Example 11 with Expression

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

the class GeneralizedSemPm method setParameterEstimationInitializationExpression.

/**
 * 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 setParameterEstimationInitializationExpression(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);
    }
    parameterEstimationInitializationExpressions.put(parameter, expression);
    parameterEstimationInitializationExpressionStrings.put(parameter, expressionString);
    startsWithParametersTemplates.put(startsWith, expressionString);
}
Also used : Expression(edu.cmu.tetrad.calculator.expression.Expression) ExpressionParser(edu.cmu.tetrad.calculator.parser.ExpressionParser)

Example 12 with Expression

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

the class TestParser method parseInvalid.

// ============================== Private Methods ===========================//
private static void parseInvalid(ExpressionParser parser, String exp) {
    try {
        Expression e = parser.parseExpression(exp);
        fail("Should not have parsed, " + exp + ", but got " + e);
    } catch (ParseException ex) {
    // Succeeded
    }
}
Also used : Expression(edu.cmu.tetrad.calculator.expression.Expression) ConstantExpression(edu.cmu.tetrad.calculator.expression.ConstantExpression) ParseException(java.text.ParseException)

Example 13 with Expression

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

the class TestParser method testVariables.

/**
 * Tests expressions with variables.
 */
@Test
public void testVariables() {
    ExpressionParser parser = new ExpressionParser(Arrays.asList("x", "y", "z"), ExpressionParser.RestrictionType.MAY_ONLY_CONTAIN);
    TestingContext context = new TestingContext();
    Expression expression = parse(parser, "x");
    context.assign("x", 5.6);
    assertTrue(expression.evaluate(context) == 5.6);
    expression = parse(parser, "(x + y) * z");
    context.assign("x", 1.0);
    context.assign("y", 2.0);
    context.assign("z", 3.0);
    assertTrue(expression.evaluate(context) == 9.0);
    expression = parse(parser, "3 + (x + (3 * y))");
    context.assign("x", 4.0);
    context.assign("y", 2.0);
    assertTrue(expression.evaluate(context) == 13.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 14 with Expression

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

the class GraphWrapper method getStrongestInfluenceGraph.

private static Graph getStrongestInfluenceGraph(GeneralizedSemIm im) {
    GeneralizedSemPm pm = im.getGeneralizedSemPm();
    Graph imGraph = im.getGeneralizedSemPm().getGraph();
    List<Node> nodes = new ArrayList<>();
    for (Node node : imGraph.getNodes()) {
        if (!(node.getNodeType() == NodeType.ERROR)) {
            nodes.add(node);
        }
    }
    Graph graph2 = new EdgeListGraph(nodes);
    for (Edge edge : imGraph.getEdges()) {
        Node node1 = edge.getNode1();
        Node node2 = edge.getNode2();
        if (!graph2.containsNode(node1))
            continue;
        if (!graph2.containsNode(node2))
            continue;
        if (graph2.isAdjacentTo(node1, node2)) {
            continue;
        }
        List<Edge> edges = imGraph.getEdges(node1, node2);
        if (edges.size() == 1) {
            graph2.addEdge(edges.get(0));
        } else {
            Expression expression1 = pm.getNodeExpression(node1);
            Expression expression2 = pm.getNodeExpression(node2);
            String param1 = findParameter(expression1, node2.getName());
            String param2 = findParameter(expression2, node1.getName());
            if (param1 == null || param2 == null) {
                continue;
            }
            double value1 = im.getParameterValue(param1);
            double value2 = im.getParameterValue(param2);
            if (value2 > value1) {
                graph2.addDirectedEdge(node1, node2);
            } else if (value1 > value2) {
                graph2.addDirectedEdge(node2, node1);
            }
        }
    }
    return graph2;
}
Also used : VariableExpression(edu.cmu.tetrad.calculator.expression.VariableExpression) Expression(edu.cmu.tetrad.calculator.expression.Expression) ArrayList(java.util.ArrayList) GeneralizedSemPm(edu.cmu.tetrad.sem.GeneralizedSemPm)

Example 15 with Expression

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

the class GraphWrapper method findParameter.

// ==========================PRIVATE METaHODS===========================//
private static String findParameter(Expression expression, String name) {
    List<Expression> expressions = expression.getExpressions();
    if (expression.getToken().equals("*")) {
        Expression expression1 = expressions.get(1);
        VariableExpression varExpr = (VariableExpression) expression1;
        if (varExpr.getVariable().equals(name)) {
            Expression expression2 = expressions.get(0);
            VariableExpression constExpr = (VariableExpression) expression2;
            return constExpr.getVariable();
        }
    }
    for (Expression _expression : expressions) {
        String param = findParameter(_expression, name);
        if (param != null) {
            return param;
        }
    }
    return null;
}
Also used : VariableExpression(edu.cmu.tetrad.calculator.expression.VariableExpression) Expression(edu.cmu.tetrad.calculator.expression.Expression) VariableExpression(edu.cmu.tetrad.calculator.expression.VariableExpression)

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