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);
}
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
}
}
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);
}
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;
}
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;
}
Aggregations