public TimeLagGraphWrapper(GraphWrapper graphWrapper) { if (graphWrapper == null) { throw new NullPointerException("No graph wrapper."); } TimeLagGraph graph = new TimeLagGraph(); Graph _graph = graphWrapper.getGraph(); for (Node node : _graph.getNodes()) { Node _node = node.like(node.getName() + ":0"); _node.setNodeType(node.getNodeType()); graph.addNode(_node); } for (Edge edge : _graph.getEdges()) { if (!Edges.isDirectedEdge(edge)) { throw new IllegalArgumentException(); } Node from = edge.getNode1(); Node to = edge.getNode2(); Node _from = graph.getNode(from.getName(), 1); Node _to = graph.getNode(to.getName(), 0); graph.addDirectedEdge(_from, _to); } this.graph = graph; }
public void setParameterValue(Edge edge, double value) { if (Edges.isDirectedEdge(edge)) { setEdgeCoefficient(edge.getNode1(), edge.getNode2(), value); } else if (Edges.isBidirectedEdge(edge)) { setErrorCovariance(edge.getNode1(), edge.getNode2(), value); } else { throw new IllegalArgumentException( "Only directed and bidirected edges are supported: " + edge); } }
private void initializeArrowsBackward(Graph graph) { sortedArrows.clear(); lookupArrows.clear(); for (Edge edge : graph.getEdges()) { Node x = edge.getNode1(); Node y = edge.getNode2(); if (!knowledgeEmpty()) { if (!getKnowledge().noEdgeRequired(x.getName(), y.getName())) { continue; } } if (Edges.isDirectedEdge(edge)) { calculateArrowsBackward(x, y, graph); } else { calculateArrowsBackward(x, y, graph); calculateArrowsBackward(y, x, graph); } } }
/** @return a string representation of the coefficients and variances of the model. */ public String toString() { StringBuilder buf = new StringBuilder(); NumberFormat nf = NumberFormatUtil.getInstance().getNumberFormat(); buf.append("\nStandardized SEM:"); buf.append("\n\nEdge coefficients (parameters):\n"); for (Edge edge : edgeParameters.keySet()) { if (!Edges.isDirectedEdge(edge)) { continue; } buf.append("\n" + edge + " " + nf.format(edgeParameters.get(edge))); } buf.append("\n\nError covariances (parameters):\n"); for (Edge edge : edgeParameters.keySet()) { if (!Edges.isBidirectedEdge(edge)) { continue; } buf.append("\n" + edge + " " + nf.format(edgeParameters.get(edge))); } buf.append("\n\nError variances (calculated):\n"); for (Node error : getErrorNodes()) { double variance = getErrorVariance(error); buf.append("\n" + error + " " + (Double.isNaN(variance) ? "Undefined" : nf.format(variance))); } buf.append("\n"); return buf.toString(); }
/** * @param edge a->b or a<->b. * @return the range of the covariance parameter for a->b or a<->b. */ public ParameterRange getParameterRange(Edge edge) { if (Edges.isBidirectedEdge(edge)) { edge = Edges.bidirectedEdge( semGraph.getExogenous(edge.getNode1()), semGraph.getExogenous(edge.getNode2())); } if (!(edgeParameters.keySet().contains(edge))) { throw new IllegalArgumentException("Not an edge in this model: " + edge); } double initial = edgeParameters.get(edge); if (initial == Double.NEGATIVE_INFINITY) { initial = Double.MIN_VALUE; } else if (initial == Double.POSITIVE_INFINITY) { initial = Double.MAX_VALUE; } double value = initial; // look upward for a point that fails. double high = value + 1; while (paramInBounds(edge, high)) { high = value + 2 * (high - value); if (high == Double.POSITIVE_INFINITY) { break; } } // find the boundary using binary search. double rangeHigh; if (high == Double.POSITIVE_INFINITY) { rangeHigh = high; } else { double low = value; while (high - low > 1e-10) { double midpoint = (high + low) / 2.0; if (paramInBounds(edge, midpoint)) { low = midpoint; } else { high = midpoint; } } rangeHigh = (high + low) / 2.0; } // look downard for a point that fails. double low = value - 1; while (paramInBounds(edge, low)) { low = value - 2 * (value - low); if (low == Double.NEGATIVE_INFINITY) { break; } } double rangeLow; if (low == Double.NEGATIVE_INFINITY) { rangeLow = low; } else { // find the boundary using binary search. high = value; while (high - low > 1e-10) { double midpoint = (high + low) / 2.0; if (paramInBounds(edge, midpoint)) { high = midpoint; } else { low = midpoint; } } rangeLow = (high + low) / 2.0; } if (Edges.isDirectedEdge(edge)) { edgeParameters.put(edge, initial); } else if (Edges.isBidirectedEdge(edge)) { edgeParameters.put(edge, initial); } return new ParameterRange(edge, value, rangeLow, rangeHigh); }