/** * 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); }
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; }
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.getName()); } } // 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); }