use of edu.cmu.tetrad.util.TetradVector in project tetrad by cmu-phil.
the class TestColtDataSet method testPermuteRows.
@Test
public void testPermuteRows() {
ContinuousVariable x1 = new ContinuousVariable("X1");
ContinuousVariable x2 = new ContinuousVariable("X2");
List<Node> nodes = new ArrayList<>();
nodes.add(x1);
nodes.add(x2);
DataSet dataSet = new ColtDataSet(3, nodes);
RandomUtil randomUtil = RandomUtil.getInstance();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
dataSet.setDouble(i, j, randomUtil.nextDouble());
}
}
ColtDataSet _dataSet = new ColtDataSet((ColtDataSet) dataSet);
dataSet.permuteRows();
I: for (int i = 0; i < dataSet.getNumRows(); i++) {
TetradVector v = _dataSet.getDoubleData().getRow(i);
for (int j = 0; j < dataSet.getNumRows(); j++) {
TetradVector w = dataSet.getDoubleData().getRow(j);
if (v.equals(w)) {
continue I;
}
}
fail("Missing row in permutation.");
}
}
use of edu.cmu.tetrad.util.TetradVector in project tetrad by cmu-phil.
the class IndTestTrekSep method determines.
/**
* If <code>isDeterminismAllowed()</code>, deters to IndTestFisherZD; otherwise throws
* UnsupportedOperationException.
*/
public boolean determines(List<Node> z, Node x) throws UnsupportedOperationException {
int[] parents = new int[z.size()];
for (int j = 0; j < parents.length; j++) {
parents[j] = covMatrix.getVariables().indexOf(z.get(j));
}
int i = covMatrix.getVariables().indexOf(x);
TetradMatrix matrix2D = covMatrix.getMatrix();
double variance = matrix2D.get(i, i);
if (parents.length > 0) {
// Regress z onto i, yielding regression coefficients b.
TetradMatrix Czz = matrix2D.getSelection(parents, parents);
TetradMatrix inverse;
try {
inverse = Czz.inverse();
} catch (Exception e) {
return true;
}
TetradVector Cyz = matrix2D.getColumn(i);
Cyz = Cyz.viewSelection(parents);
TetradVector b = inverse.times(Cyz);
variance -= Cyz.dotProduct(b);
}
return variance < 1e-20;
}
use of edu.cmu.tetrad.util.TetradVector in project tetrad by cmu-phil.
the class KMeans method squaredError.
/**
* The squared error of the kth cluster.
*
* @param k The index of the cluster in question.
* @return this squared error.
*/
private double squaredError(int k) {
double squaredError = 0.0;
for (int i = 0; i < data.rows(); i++) {
if (clusters.get(i) == k) {
TetradVector datum = data.getRow(i);
TetradVector center = centers.getRow(k);
squaredError += metric.dissimilarity(datum, center);
}
}
return squaredError;
}
use of edu.cmu.tetrad.util.TetradVector in project tetrad by cmu-phil.
the class TimeSeriesUtils method sumOfArCoefficients.
public static double sumOfArCoefficients(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);
TetradMatrix residuals = new TetradMatrix(timeLags.getNumRows(), timeSeries.getNumColumns());
double sum = 0.0;
int n = 0;
for (int i = 0; i < timeSeries.getNumColumns(); i++) {
Node target = timeLags.getVariable(i);
RegressionResult result = regression.regress(target, regressors);
double[] coef = result.getCoef();
for (int k = 0; k < coef.length; k++) {
sum += coef[k] * coef[k];
n++;
}
TetradVector residualsColumn = result.getResiduals();
// residuals.viewColumn(i).assign(residualsColumn);
residuals.assignColumn(i, residualsColumn);
}
return sum / n;
}
use of edu.cmu.tetrad.util.TetradVector in project tetrad by cmu-phil.
the class TimeSeriesUtils method ar2.
public static DataSet ar2(DataSet timeSeries, int numLags) {
List<Node> missingVariables = new ArrayList<>();
for (Node node : timeSeries.getVariables()) {
int index = timeSeries.getVariables().indexOf(node);
boolean missing = true;
for (int i = 0; i < timeSeries.getNumRows(); i++) {
if (!Double.isNaN(timeSeries.getDouble(i, index))) {
missing = false;
break;
}
}
if (missing) {
missingVariables.add(node);
}
}
DataSet timeLags = createLagData(timeSeries, numLags);
Regression regression = new RegressionDataset(timeLags);
TetradMatrix residuals = new TetradMatrix(timeLags.getNumRows(), timeSeries.getNumColumns());
for (int i = 0; i < timeSeries.getNumColumns(); i++) {
Node target = timeLags.getVariable(i);
int index = timeSeries.getVariables().indexOf(target);
if (missingVariables.contains(target)) {
for (int i2 = 0; i2 < residuals.rows(); i2++) {
residuals.set(i2, index, Double.NaN);
}
continue;
}
List<Node> regressors = new ArrayList<>();
for (int i2 = timeSeries.getNumColumns(); i2 < timeLags.getNumColumns(); i2++) {
int varIndex = i2 % timeSeries.getNumColumns();
Node var = timeSeries.getVariable(varIndex);
if (missingVariables.contains(var)) {
continue;
}
regressors.add(timeLags.getVariable(i2));
}
RegressionResult result = regression.regress(target, regressors);
TetradVector residualsColumn = result.getResiduals();
residuals.assignColumn(i, residualsColumn);
}
return ColtDataSet.makeContinuousData(timeSeries.getVariables(), residuals);
}
Aggregations