Search in sources :

Example 1 with CovarianceMatrix

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);
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) ArrayList(java.util.ArrayList) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) CovarianceMatrix(edu.cmu.tetrad.data.CovarianceMatrix)

Example 2 with CovarianceMatrix

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);
}
Also used : SemGraph(edu.cmu.tetrad.graph.SemGraph) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) CovarianceMatrix(edu.cmu.tetrad.data.CovarianceMatrix)

Example 3 with CovarianceMatrix

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);
}
Also used : DeltaTetradTest(edu.cmu.tetrad.search.DeltaTetradTest) Tetrad(edu.cmu.tetrad.search.Tetrad) CovarianceMatrix(edu.cmu.tetrad.data.CovarianceMatrix) DeltaTetradTest(edu.cmu.tetrad.search.DeltaTetradTest) Test(org.junit.Test)

Example 4 with CovarianceMatrix

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);
}
Also used : ICovarianceMatrix(edu.cmu.tetrad.data.ICovarianceMatrix) ArrayList(java.util.ArrayList) Regression(edu.cmu.tetrad.regression.Regression) RegressionCovariance(edu.cmu.tetrad.regression.RegressionCovariance) RegressionResult(edu.cmu.tetrad.regression.RegressionResult) CovarianceMatrix(edu.cmu.tetrad.data.CovarianceMatrix) ICovarianceMatrix(edu.cmu.tetrad.data.ICovarianceMatrix) Test(org.junit.Test)

Example 5 with CovarianceMatrix

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);
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) DataSet(edu.cmu.tetrad.data.DataSet) DoubleArrayList(cern.colt.list.DoubleArrayList) ArrayList(java.util.ArrayList) CovarianceMatrix(edu.cmu.tetrad.data.CovarianceMatrix) ICovarianceMatrix(edu.cmu.tetrad.data.ICovarianceMatrix) Test(org.junit.Test)

Aggregations

CovarianceMatrix (edu.cmu.tetrad.data.CovarianceMatrix)20 ICovarianceMatrix (edu.cmu.tetrad.data.ICovarianceMatrix)11 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)10 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)7 DataSet (edu.cmu.tetrad.data.DataSet)6 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)5 DeltaTetradTest (edu.cmu.tetrad.search.DeltaTetradTest)4 Tetrad (edu.cmu.tetrad.search.Tetrad)4 SemPm (edu.cmu.tetrad.sem.SemPm)4 SemIm (edu.cmu.tetrad.sem.SemIm)3 DataModel (edu.cmu.tetrad.data.DataModel)2 Node (edu.cmu.tetrad.graph.Node)2 List (java.util.List)2 DoubleArrayList (cern.colt.list.DoubleArrayList)1 ColtDataSet (edu.cmu.tetrad.data.ColtDataSet)1 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)1 Graph (edu.cmu.tetrad.graph.Graph)1 SemGraph (edu.cmu.tetrad.graph.SemGraph)1 Regression (edu.cmu.tetrad.regression.Regression)1