Search in sources :

Example 6 with ExpressionParser

use of edu.cmu.tetrad.calculator.parser.ExpressionParser 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 7 with ExpressionParser

use of edu.cmu.tetrad.calculator.parser.ExpressionParser 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 8 with ExpressionParser

use of edu.cmu.tetrad.calculator.parser.ExpressionParser in project tetrad by cmu-phil.

the class GeneralizedSemPm method setParametersTemplate.

public void setParametersTemplate(String parametersTemplate) throws ParseException {
    if (parametersTemplate == null) {
        throw new NullPointerException();
    }
    // Test to make sure it's parsable.
    ExpressionParser parser = new ExpressionParser();
    Expression expression = parser.parseExpression(parametersTemplate);
    List<String> parameterNames = parser.getParameters();
    if (!parameterNames.isEmpty()) {
        throw new IllegalArgumentException("Initial distribution for a parameter may not " + "contain parameters: " + expression.toString());
    }
    this.parametersTemplate = parametersTemplate;
}
Also used : Expression(edu.cmu.tetrad.calculator.expression.Expression) ExpressionParser(edu.cmu.tetrad.calculator.parser.ExpressionParser)

Example 9 with ExpressionParser

use of edu.cmu.tetrad.calculator.parser.ExpressionParser in project tetrad by cmu-phil.

the class GeneralizedSemPm method setStartsWithParametersEstimationInitializaationTemplate.

public void setStartsWithParametersEstimationInitializaationTemplate(String startsWith, String parametersEstimationInitializationTemplate) 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(parametersEstimationInitializationTemplate);
    if (startsWith.contains(" ")) {
        throw new IllegalArgumentException("Starts with string contains spaces.");
    }
    // this.parametersTemplate = parametersTemplate;
    this.startsWithParametersEstimationInitializationTemplates.put(startsWith, parametersEstimationInitializationTemplate);
}
Also used : ExpressionParser(edu.cmu.tetrad.calculator.parser.ExpressionParser)

Example 10 with ExpressionParser

use of edu.cmu.tetrad.calculator.parser.ExpressionParser in project tetrad by cmu-phil.

the class GeneralizedSemPm method setNodeExpression.

public void setNodeExpression(Node node, String expressionString) throws ParseException {
    if (node == null) {
        throw new NullPointerException("Node was null.");
    }
    if (expressionString == null) {
        // return;
        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();
    // Make a list of parent names.
    List<Node> parents = this.graph.getParents(node);
    List<String> parentNames = new LinkedList<>();
    for (Node parent : parents) {
        parentNames.add(parent.getName());
    }
    // List<String> _params = new ArrayList<String>(parameterNames);
    // _params.retainAll(variableNames);
    // _params.removeAll(parentNames);
    // 
    // if (!_params.isEmpty()) {
    // throw new IllegalArgumentException("Conditioning on a variable other than the parents: " + node);
    // }
    // Make a list of parameter names, by removing from the parser's list of freeParameters any that correspond
    // to parent variables. If there are any variable names (including error terms) that are not among the list of
    // parents, that's a time to throw an exception. We must respect the graph! (We will not complain if any parents
    // are missing.)
    parameterNames.removeAll(variableNames);
    for (Node variable : nodes) {
        if (parameterNames.contains(variable.getName())) {
            parameterNames.remove(variable.getName());
        // throw new IllegalArgumentException("The list of parameter names may not include variables: " + variable.getNode());
        }
    }
    // Remove old parameter references.
    List<String> parametersToRemove = new LinkedList<>();
    for (String parameter : this.referencedParameters.keySet()) {
        Set<Node> nodes = this.referencedParameters.get(parameter);
        if (nodes.contains(node)) {
            nodes.remove(node);
        }
        if (nodes.isEmpty()) {
            parametersToRemove.add(parameter);
        }
    }
    for (String parameter : parametersToRemove) {
        this.referencedParameters.remove(parameter);
        this.parameterExpressions.remove(parameter);
        this.parameterExpressionStrings.remove(parameter);
        this.parameterEstimationInitializationExpressions.remove(parameter);
        this.parameterEstimationInitializationExpressionStrings.remove(parameter);
    }
    // Add new parameter references.
    for (String parameter : parameterNames) {
        if (this.referencedParameters.get(parameter) == null) {
            this.referencedParameters.put(parameter, new HashSet<Node>());
        }
        Set<Node> nodes = this.referencedParameters.get(parameter);
        nodes.add(node);
        setSuitableParameterDistribution(parameter);
    }
    // Remove old node references.
    List<Node> nodesToRemove = new LinkedList<>();
    for (Node _node : this.referencedNodes.keySet()) {
        Set<Node> nodes = this.referencedNodes.get(_node);
        if (nodes.contains(node)) {
            nodes.remove(node);
        }
        if (nodes.isEmpty()) {
            nodesToRemove.add(_node);
        }
    }
    for (Node _node : nodesToRemove) {
        this.referencedNodes.remove(_node);
    }
    // Add new freeParameters.
    for (String variableString : variableNames) {
        Node _node = getNode(variableString);
        if (this.referencedNodes.get(_node) == null) {
            this.referencedNodes.put(_node, new HashSet<Node>());
        }
        for (String s : parentNames) {
            if (s.equals(variableString)) {
                Set<Node> nodes = this.referencedNodes.get(_node);
                nodes.add(node);
            }
        }
    }
    // Finally, save the parsed expression and the original string that the user entered. No need to annoy
    // the user by changing spacing.
    nodeExpressions.put(node, expression);
    nodeExpressionStrings.put(node, 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