use of edu.cmu.tetrad.data.CovarianceMatrix in project tetrad by cmu-phil.
the class TestDeltaTetradTest method getBollenSimulationExampleData.
private CovarianceMatrix getBollenSimulationExampleData() {
double[][] d = new double[][] { { 2.034 }, { 0.113, 1.281 }, { 0.510, 0.093, 1.572 }, { 0.105, 0.857, 0.447, 1.708 }, { 0.998, 0.228, 0.170, 0.345, 1.651 } };
Node y1 = new ContinuousVariable("y1");
Node y2 = new ContinuousVariable("y2");
Node y3 = new ContinuousVariable("y3");
Node y4 = new ContinuousVariable("y4");
Node y5 = new ContinuousVariable("y5");
List<Node> nodes = new ArrayList<>();
nodes.add(y1);
nodes.add(y2);
nodes.add(y3);
nodes.add(y4);
nodes.add(y5);
TetradMatrix matrix = new TetradMatrix(5, 5);
for (int i = 0; i < 5; i++) {
for (int j = 0; j <= i; j++) {
matrix.set(i, j, d[i][j]);
matrix.set(j, i, d[i][j]);
}
}
return new CovarianceMatrix(nodes, matrix, 1000);
}
use of edu.cmu.tetrad.data.CovarianceMatrix in project tetrad by cmu-phil.
the class SemOptimizerRicf method optimize.
// ==============================PUBLIC METHODS========================//
/**
* Optimizes the fitting function of the given Sem using the Powell method
* from Numerical Recipes by adjusting the freeParameters of the Sem.
*/
public void optimize(SemIm semIm) {
if (numRestarts < 1)
numRestarts = 1;
if (numRestarts != 1) {
throw new IllegalArgumentException("Number of restarts must be 1 for this method.");
}
TetradMatrix sampleCovar = semIm.getSampleCovar();
if (sampleCovar == null) {
throw new NullPointerException("Sample covar has not been set.");
}
if (DataUtils.containsMissingValue(sampleCovar)) {
throw new IllegalArgumentException("Please remove or impute missing values.");
}
if (DataUtils.containsMissingValue(sampleCovar)) {
throw new IllegalArgumentException("Please remove or impute missing values.");
}
TetradLogger.getInstance().log("info", "Trying EM...");
// new SemOptimizerEm().optimize(semIm);
CovarianceMatrix cov = new CovarianceMatrix(semIm.getMeasuredNodes(), sampleCovar, semIm.getSampleSize());
SemGraph graph = semIm.getSemPm().getGraph();
Ricf.RicfResult result = new Ricf().ricf(graph, cov, 0.001);
// Ricf.RicfResult result = null;
//
// for (int t = 0; t < 10; t++) {
// Graph graph = semIm.getSemPm().getGraph();
// result = new Ricf().ricf(graph, cov, 0.001);
//
// TetradMatrix bHat = result.getBhat();
// TetradMatrix lHat = result.getLhat();
// TetradMatrix oHat = result.getOhat();
// TetradMatrix sHat = result.getShat();
//
// for (Parameter param : semIm.getFreeParameters()) {
// if (param.getType() == ParamType.COEF) {
// int i = semIm.getSemPm().getVariableNodes().indexOf(param.getNodeA());
// int j = semIm.getSemPm().getVariableNodes().indexOf(param.getNodeB());
// semIm.setEdgeCoef(param.getNodeA(), param.getNodeB(), -bHat.get(j, i));
// }
//
// if (param.getType() == ParamType.VAR) {
// int i = semIm.getSemPm().getVariableNodes().indexOf(param.getNodeA());
// if (lHat.get(i, i) != 0) {
// semIm.setErrVar(param.getNodeA(), lHat.get(i, i));
// } else if (oHat.get(i, i) != 0) {
// semIm.setErrVar(param.getNodeA(), oHat.get(i, i));
// }
// }
// }
//
// if (t < 9) {
// for (Parameter param : semIm.getFreeParameters()) {
// double value = semIm.getParamValue(param);
// double max = Double.NEGATIVE_INFINITY;
// double d;
//
// for (d = value - .5; d <= value + 0.5; d += 0.001) {
// semIm.setParamValue(param, d);
// double fml = semIm.getFml();
// if (fml > max) max = fml;
// }
//
// semIm.setParamValue(param, d);
// }
// }
// }
TetradMatrix bHat = new TetradMatrix(result.getBhat().toArray());
TetradMatrix lHat = new TetradMatrix(result.getLhat().toArray());
TetradMatrix oHat = new TetradMatrix(result.getOhat().toArray());
for (Parameter param : semIm.getFreeParameters()) {
if (param.getType() == ParamType.COEF) {
int i = semIm.getSemPm().getVariableNodes().indexOf(param.getNodeA());
int j = semIm.getSemPm().getVariableNodes().indexOf(param.getNodeB());
semIm.setEdgeCoef(param.getNodeA(), param.getNodeB(), -bHat.get(j, i));
}
if (param.getType() == ParamType.VAR) {
int i = semIm.getSemPm().getVariableNodes().indexOf(param.getNodeA());
if (lHat.get(i, i) != 0) {
semIm.setErrVar(param.getNodeA(), lHat.get(i, i));
} else if (oHat.get(i, i) != 0) {
semIm.setErrVar(param.getNodeA(), oHat.get(i, i));
}
}
if (param.getType() == ParamType.COVAR) {
int i = semIm.getSemPm().getVariableNodes().indexOf(param.getNodeA());
int j = semIm.getSemPm().getVariableNodes().indexOf(param.getNodeB());
if (lHat.get(i, i) != 0) {
semIm.setErrCovar(param.getNodeA(), param.getNodeB(), lHat.get(j, i));
} else if (oHat.get(i, i) != 0) {
semIm.setErrCovar(param.getNodeA(), param.getNodeB(), oHat.get(j, i));
}
}
}
System.out.println(result);
System.out.println(semIm);
}
use of edu.cmu.tetrad.data.CovarianceMatrix in project tetrad by cmu-phil.
the class TestDeltaTetradTest method testBollenExample1.
// Bollen and Ting, Confirmatory Tetrad Analysis, p. 164 Sympathy and Anger.
@Test
public void testBollenExample1() {
CovarianceMatrix cov = getBollenExample1Data();
List<Node> variables = cov.getVariables();
Node v1 = variables.get(0);
Node v2 = variables.get(1);
Node v3 = variables.get(2);
Node v4 = variables.get(3);
Node v5 = variables.get(4);
Node v6 = variables.get(5);
Tetrad t1 = new Tetrad(v1, v2, v3, v4);
Tetrad t2 = new Tetrad(v1, v2, v3, v5);
Tetrad t3 = new Tetrad(v1, v2, v3, v6);
Tetrad t4 = new Tetrad(v1, v4, v5, v6);
Tetrad t5 = new Tetrad(v1, v4, v2, v3);
Tetrad t6 = new Tetrad(v1, v5, v2, v3);
Tetrad t7 = new Tetrad(v1, v6, v2, v3);
Tetrad t8 = new Tetrad(v1, v6, v4, v5);
DeltaTetradTest test = new DeltaTetradTest(cov);
// DeltaTetradTest test = new DeltaTetradTest(new CorrelationMatrix(cov));
double chiSq = test.calcChiSquare(t1, t2, t3, t4, t5, t6, t7, t8);
double pValue = test.getPValue();
// They get chi square = 6.71 p = .57 8 df but using the raw data which they don't provide here.
// Just using the covariance matrix provided, I get chi square = 8.46, p = 0.39, df = 8.
assertEquals(11.42, chiSq, 0.01);
assertEquals(0.18, pValue, 0.01);
}
use of edu.cmu.tetrad.data.CovarianceMatrix in project tetrad by cmu-phil.
the class TestRegression method testCovariance.
/**
* Same problem, using the covariance matrix.
*/
@Test
public void testCovariance() {
setUp();
RandomUtil.getInstance().setSeed(3848283L);
ICovarianceMatrix cov = new CovarianceMatrix(data);
List<Node> nodes = cov.getVariables();
Node target = nodes.get(0);
List<Node> regressors = new ArrayList<>();
for (int i = 1; i < nodes.size(); i++) {
regressors.add(nodes.get(i));
}
Regression regression = new RegressionCovariance(cov);
RegressionResult result = regression.regress(target, regressors);
double[] coeffs = result.getCoef();
assertEquals(0.00, coeffs[0], 0.01);
assertEquals(-.053, coeffs[1], 0.01);
assertEquals(0.036, coeffs[2], 0.01);
assertEquals(.019, coeffs[3], 0.01);
assertEquals(.007, coeffs[4], 0.01);
}
use of edu.cmu.tetrad.data.CovarianceMatrix in project tetrad by cmu-phil.
the class TestSemIm method testCovariancesOfSimulated.
@Test
public void testCovariancesOfSimulated() {
List<Node> nodes = new ArrayList<>();
for (int i = 0; i < 5; i++) {
nodes.add(new ContinuousVariable("X" + (i + 1)));
}
Graph randomGraph = new Dag(GraphUtils.randomGraph(nodes, 0, 8, 30, 15, 15, false));
SemPm semPm1 = new SemPm(randomGraph);
SemIm semIm1 = new SemIm(semPm1);
TetradMatrix implCovarC = semIm1.getImplCovar(true);
implCovarC.toArray();
DataSet dataSet = semIm1.simulateDataRecursive(1000, false);
new CovarianceMatrix(dataSet);
}
Aggregations