use of edu.cmu.tetrad.util.TetradVector in project tetrad by cmu-phil.
the class TimeSeriesUtils method ar.
/**
* @return the VAR residuals of the given time series with the given number
* of lags. That is, every variable at the model lag is regressed onto every
* variable at previous lags, up to the given number of lags, and the
* residuals of these regressions for each variable are returned.
*/
public static DataSet ar(DataSet timeSeries, int numLags) {
DataSet timeLags = createLagData(timeSeries, numLags);
List<Node> regressors = new ArrayList<>();
for (int i = timeSeries.getNumColumns(); i < timeLags.getNumColumns(); i++) {
regressors.add(timeLags.getVariable(i));
}
Regression regression = new RegressionDataset(timeLags);
// Regression regression = new RegressionDatasetGeneralized(timeLags);
TetradMatrix residuals = new TetradMatrix(timeLags.getNumRows(), timeSeries.getNumColumns());
for (int i = 0; i < timeSeries.getNumColumns(); i++) {
Node target = timeLags.getVariable(i);
RegressionResult result = regression.regress(target, regressors);
TetradVector residualsColumn = result.getResiduals();
// residuals.viewColumn(i).assign(residualsColumn);
residuals.assignColumn(i, residualsColumn);
}
return ColtDataSet.makeContinuousData(timeSeries.getVariables(), residuals);
}
use of edu.cmu.tetrad.util.TetradVector in project tetrad by cmu-phil.
the class TimeSeriesUtils method structuralVar.
public static VarResult structuralVar(DataSet timeSeries, int numLags) {
DataSet timeLags = TimeSeriesUtils.createLagData(timeSeries, numLags);
IKnowledge knowledge = timeLags.getKnowledge().copy();
for (int i = 0; i <= numLags; i++) {
knowledge.setTierForbiddenWithin(i, true);
}
Score score;
if (timeLags.isDiscrete()) {
score = new BDeuScore(timeLags);
} else if (timeLags.isContinuous()) {
SemBicScore semBicScore = new SemBicScore(new CovarianceMatrixOnTheFly(timeLags));
semBicScore.setPenaltyDiscount(2.0);
score = semBicScore;
} else {
throw new IllegalArgumentException("Mixed data set");
}
Fges search = new Fges(score);
search.setKnowledge(knowledge);
Graph graph = search.search();
// want to collapse graph here...
Graph collapsedVarGraph = new EdgeListGraph(timeSeries.getVariables());
for (Edge edge : graph.getEdges()) {
String node1_before = edge.getNode1().getName();
String node2_before = edge.getNode2().getName();
String node1_after = node1_before.substring(0, node1_before.indexOf("."));
String node2_after = node2_before.substring(0, node2_before.indexOf("."));
Node node1 = collapsedVarGraph.getNode(node1_after);
Node node2 = collapsedVarGraph.getNode(node2_after);
Edge _edge = new Edge(node1, node2, edge.getEndpoint1(), edge.getEndpoint2());
if (!collapsedVarGraph.containsEdge(_edge)) {
collapsedVarGraph.addEdge(_edge);
}
}
TetradMatrix residuals = new TetradMatrix(timeLags.getNumRows(), timeSeries.getNumColumns());
Regression regression = new RegressionDataset(timeLags);
for (int i = 0; i < timeSeries.getNumColumns(); i++) {
Node target = timeLags.getVariable(i);
List<Node> regressors = new ArrayList<>();
// timelags.getVariable(i).
for (int j = 0; j <= 0; /*numLags*/
j++) {
Node variable = timeLags.getVariable(i + j * timeSeries.getNumColumns());
regressors.addAll(graph.getParents(variable));
}
RegressionResult result = regression.regress(target, regressors);
TetradVector residualsColumn = result.getResiduals();
// residuals.viewColumn(i).assign(residualsColumn);
residuals.assignColumn(i, residualsColumn);
}
return new VarResult(ColtDataSet.makeContinuousData(timeSeries.getVariables(), residuals), collapsedVarGraph);
}
use of edu.cmu.tetrad.util.TetradVector in project tetrad by cmu-phil.
the class SemUpdater method getUpdatedSemIm.
/**
* See http://en.wikipedia.org/wiki/Multivariate_normal_distribution.
*/
public SemIm getUpdatedSemIm() {
Algebra algebra = new Algebra();
// First manipulate the old semIm.
SemIm manipulatedSemIm = getManipulatedSemIm();
// Get out the means and implied covariances.
double[] means = new double[manipulatedSemIm.getVariableNodes().size()];
for (int i = 0; i < means.length; i++) {
means[i] = manipulatedSemIm.getMean(manipulatedSemIm.getVariableNodes().get(i));
}
DoubleMatrix1D mu = new DenseDoubleMatrix1D(means);
DoubleMatrix2D sigma = new DenseDoubleMatrix2D(manipulatedSemIm.getImplCovar(true).toArray());
// Updating on x2 = a.
SemEvidence evidence = getEvidence();
List nodesInEvidence = evidence.getNodesInEvidence();
int[] x2 = new int[nodesInEvidence.size()];
DoubleMatrix1D a = new DenseDoubleMatrix1D(nodesInEvidence.size());
for (int i = 0; i < nodesInEvidence.size(); i++) {
Node _node = (Node) nodesInEvidence.get(i);
x2[i] = evidence.getNodeIndex(_node);
}
for (int i = 0; i < nodesInEvidence.size(); i++) {
int j = evidence.getNodeIndex((Node) nodesInEvidence.get(i));
a.set(i, evidence.getProposition().getValue(j));
}
// x1 is all the variables.
// int[] x1 = new int[sigma.rows() - x2.length];
int[] x1 = new int[sigma.rows()];
for (int i = 0; i < sigma.rows(); i++) {
x1[i] = i;
}
// int index = -1;
// for (int i = 0; i < sigma.rows(); i++) {
// if (Arrays.binarySearch(x2, i) == -1) {
// x1[++index] = i;
// }
// }
// Calculate sigmaBar. (Don't know how to use it yet.)
// DoubleMatrix2D sigma11 = sigma.viewSelection(x1, x1);
DoubleMatrix2D sigma12 = sigma.viewSelection(x1, x2);
DoubleMatrix2D sigma22 = sigma.viewSelection(x2, x2);
// DoubleMatrix2D sigma21 = sigma.viewSelection(x2, x1);
DoubleMatrix2D inv_sigma22 = algebra.inverse(sigma22);
DoubleMatrix2D temp1 = algebra.mult(sigma12, inv_sigma22);
// DoubleMatrix2D temp2 = algebra.times(temp1, sigma21.copy());
// DoubleMatrix2D sigmaBar = sigma11.copy().assign(temp2, Functions.minus);
// Calculate muBar.
DoubleMatrix1D mu1 = mu.viewSelection(x1);
DoubleMatrix1D mu2 = mu.viewSelection(x2);
DoubleMatrix1D temp4 = a.copy().assign(mu2, Functions.minus);
DoubleMatrix1D temp5 = algebra.mult(temp1, temp4);
DoubleMatrix1D muBar = mu1.copy().assign(temp5, Functions.plus);
// Estimate a SEM with this sigmaBar and muBar.
// List variableNodes = manipulatedSemIm.getVariableNodes();
// String[] varNames = new String[variableNodes.size()];
//
// for (int i = 0; i < variableNodes.size(); i++) {
// varNames[i] = ((Node) variableNodes.get(i)).getNode();
// }
// System.out.println(sigmaBar);
//
// CovarianceMatrix covMatrix = new CovarianceMatrix(varNames,
// sigmaBar, 100);
// SemPm semPm = manipulatedSemIm.getEstIm();
// SemEstimator estimator = new SemEstimator(covMatrix, semPm);
// estimator.estimate();
// SemIm semIm = estimator.getEstimatedSem();
// semIm.setMeanValues(muBar.toArray());
// return semIm;
DoubleMatrix2D sigma2 = new DenseDoubleMatrix2D(manipulatedSemIm.getErrCovar().toArray());
// }
return manipulatedSemIm.updatedIm(new TetradMatrix(sigma2.toArray()), new TetradVector(muBar.toArray()));
}
use of edu.cmu.tetrad.util.TetradVector in project tetrad by cmu-phil.
the class SemOptimizerEm method maximization.
private void maximization() {
List<Node> nodes = graph.getNodes();
for (Node node : graph.getNodes()) {
if (node.getNodeType() == NodeType.ERROR) {
continue;
}
int idx = nodes.indexOf(node);
double variance = expectedCov.get(idx, idx);
if (parents[idx] != null) {
for (int i = 0; i < parents[idx].length; i++) {
int idx2 = parents[idx][i];
nodeParentsCov[idx][i] = expectedCov.get(idx, idx2);
for (int j = i; j < parents[idx].length; j++) {
int idx3 = parents[idx][j];
parentsCov[idx][i][j] = expectedCov.get(idx2, idx3);
parentsCov[idx][j][i] = expectedCov.get(idx3, idx2);
}
}
TetradVector coefs = new TetradMatrix(parentsCov[idx]).inverse().times(new TetradVector(nodeParentsCov[idx]));
for (int i = 0; i < coefs.size(); i++) {
if (semIm.getSemPm().getParameter(nodes.get(parents[idx][i]), node) != null && !semIm.getSemPm().getParameter(nodes.get(parents[idx][i]), node).isFixed()) {
semIm.setEdgeCoef(nodes.get(parents[idx][i]), node, coefs.get(i));
}
}
variance -= new TetradVector(nodeParentsCov[idx]).dotProduct(coefs);
}
if (!semIm.getSemPm().getParameter(errorParent[idx], errorParent[idx]).isFixed()) {
semIm.setErrCovar(errorParent[idx], variance);
}
}
}
use of edu.cmu.tetrad.util.TetradVector in project tetrad by cmu-phil.
the class SemOptimizerRegression method optimize.
// ============================PUBLIC METHODS==========================//
/**
* Fit the freeParameters by doing local regressions.
*/
public void optimize(SemIm semIm) {
if (numRestarts != 1) {
throw new IllegalArgumentException("Number of restarts must be 1 for this method.");
}
TetradMatrix covar = semIm.getSampleCovar();
if (covar == null) {
throw new NullPointerException("Sample covar has not been set.");
}
SemGraph graph = semIm.getSemPm().getGraph();
List<Node> nodes = graph.getNodes();
for (Node node : nodes) {
if (node.getNodeType() != NodeType.MEASURED) {
continue;
}
if (!graph.isParameterizable(node))
continue;
int idx = nodes.indexOf(node);
List<Node> parents = graph.getParents(node);
for (int i = 0; i < parents.size(); i++) {
Node nextParent = parents.get(i);
if (nextParent.getNodeType() == NodeType.ERROR) {
// errorParent = nextParent;
parents.remove(nextParent);
break;
}
}
double variance = covar.get(idx, idx);
if (parents.size() > 0) {
TetradVector nodeParentsCov = new TetradVector(parents.size());
TetradMatrix parentsCov = new TetradMatrix(parents.size(), parents.size());
for (int i = 0; i < parents.size(); i++) {
int idx2 = nodes.indexOf(parents.get(i));
nodeParentsCov.set(i, covar.get(idx, idx2));
for (int j = i; j < parents.size(); j++) {
int idx3 = nodes.indexOf(parents.get(j));
parentsCov.set(i, j, covar.get(idx2, idx3));
parentsCov.set(j, i, covar.get(idx2, idx3));
}
}
TetradVector b = parentsCov.inverse().times(nodeParentsCov);
variance -= nodeParentsCov.dotProduct(b);
for (int i = 0; i < b.size(); i++) {
int idx2 = nodes.indexOf(parents.get(i));
semIm.setParamValue(nodes.get(idx2), node, b.get(i));
}
}
semIm.setParamValue(node, node, variance);
}
TetradLogger.getInstance().log("optimization", "FML = " + semIm.getScore());
}
Aggregations