use of edu.cmu.tetrad.regression.RegressionResult in project tetrad by cmu-phil.
the class LingamPattern2 method getScore2.
private Score getScore2(Graph dag, List<TetradMatrix> data, List<Node> variables) {
// System.out.println("Scoring DAG: " + dag);
List<Regression> regressions = new ArrayList<>();
for (TetradMatrix _data : data) {
regressions.add(new RegressionDataset(_data, variables));
}
int totalSampleSize = 0;
for (TetradMatrix _data : data) {
totalSampleSize += _data.rows();
}
int numCols = data.get(0).columns();
List<Node> nodes = dag.getNodes();
double score = 0.0;
double[] pValues = new double[nodes.size()];
TetradMatrix residuals = new TetradMatrix(totalSampleSize, numCols);
for (int j = 0; j < nodes.size(); j++) {
List<Double> _residuals = new ArrayList<>();
Node _target = nodes.get(j);
List<Node> _regressors = dag.getParents(_target);
Node target = getVariable(variables, _target.getName());
List<Node> regressors = new ArrayList<>();
for (Node _regressor : _regressors) {
Node variable = getVariable(variables, _regressor.getName());
regressors.add(variable);
}
for (int m = 0; m < data.size(); m++) {
RegressionResult result = regressions.get(m).regress(target, regressors);
TetradVector residualsSingleDataset = result.getResiduals();
DoubleArrayList _residualsSingleDataset = new DoubleArrayList(residualsSingleDataset.toArray());
double mean = Descriptive.mean(_residualsSingleDataset);
double std = Descriptive.standardDeviation(Descriptive.variance(_residualsSingleDataset.size(), Descriptive.sum(_residualsSingleDataset), Descriptive.sumOfSquares(_residualsSingleDataset)));
for (int i2 = 0; i2 < _residualsSingleDataset.size(); i2++) {
_residualsSingleDataset.set(i2, (_residualsSingleDataset.get(i2) - mean) / std);
}
for (int k = 0; k < _residualsSingleDataset.size(); k++) {
_residuals.add(_residualsSingleDataset.get(k));
}
}
for (int k = 0; k < _residuals.size(); k++) {
residuals.set(k, j, _residuals.get(k));
}
}
for (int i = 0; i < nodes.size(); i++) {
DoubleArrayList f = new DoubleArrayList(residuals.getColumn(i).toArray());
for (int j = 0; j < f.size(); j++) {
f.set(j, Math.abs(f.get(j)));
}
double _mean = Descriptive.mean(f);
double diff = _mean - Math.sqrt(2.0 / Math.PI);
score += diff * diff;
}
for (int j = 0; j < residuals.columns(); j++) {
double[] x = residuals.getColumn(j).toArray();
double p = new AndersonDarlingTest(x).getP();
pValues[j] = p;
}
return new Score(score, pValues);
}
use of edu.cmu.tetrad.regression.RegressionResult in project tetrad by cmu-phil.
the class Lofs2 method regressionCoef.
private double regressionCoef(double[] xValues, double[] yValues) {
List<Node> v = new ArrayList<>();
v.add(new GraphNode("x"));
v.add(new GraphNode("y"));
TetradMatrix bothData = new TetradMatrix(xValues.length, 2);
for (int i = 0; i < xValues.length; i++) {
bothData.set(i, 0, xValues[i]);
bothData.set(i, 1, yValues[i]);
}
Regression regression2 = new RegressionDataset(bothData, v);
RegressionResult result;
try {
result = regression2.regress(v.get(0), v.get(1));
} catch (Exception e) {
return Double.NaN;
}
return result.getCoef()[1];
}
use of edu.cmu.tetrad.regression.RegressionResult in project tetrad by cmu-phil.
the class TimeSeriesUtils method getSelfLoopCoefs.
public static double[] getSelfLoopCoefs(DataSet timeSeries) {
DataSet timeLags = createLagData(timeSeries, 1);
double[] coefs = new double[timeSeries.getNumColumns()];
for (int j = 0; j < timeSeries.getNumColumns(); j++) {
Node target = timeLags.getVariable(j);
Node selfLoop = timeLags.getVariable(j + timeSeries.getNumColumns());
List<Node> regressors = Collections.singletonList(selfLoop);
Regression regression = new RegressionDataset(timeLags);
RegressionResult result = regression.regress(target, regressors);
coefs[j] = result.getCoef()[1];
}
return coefs;
}
use of edu.cmu.tetrad.regression.RegressionResult 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.regression.RegressionResult 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);
}
Aggregations