Search in sources :

Example 86 with TetradMatrix

use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.

the class Ling method makeDataSet.

private void makeDataSet(GraphWithParameters graphWP) {
    // define the "Gaussian-squared" distribution
    Distribution gp2 = new GaussianPower(2);
    // the coefficients of the error terms  (here, all 1s)
    TetradVector errorCoefficients = getErrorCoeffsIdentity(graphWP.getGraph().getNumNodes());
    // generate data from the SEM
    TetradMatrix inVectors = simulateCyclic(graphWP, errorCoefficients, numSamples, gp2);
    // reformat it
    dataSet = ColtDataSet.makeContinuousData(graphWP.getGraph().getNodes(), new TetradMatrix(inVectors.transpose().toArray()));
}
Also used : TetradVector(edu.cmu.tetrad.util.TetradVector) Distribution(edu.cmu.tetrad.util.dist.Distribution) GaussianPower(edu.cmu.tetrad.util.dist.GaussianPower) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 87 with TetradMatrix

use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.

the class Ling method findCandidateModels.

// given the W matrix, outputs the list of SEMs consistent with the observed distribution.
private StoredGraphs findCandidateModels(List<Node> variables, TetradMatrix matrixW, boolean approximateZeros) {
    TetradMatrix normalizedZldW;
    List<PermutationMatrixPair> zldPerms;
    StoredGraphs gs = new StoredGraphs();
    System.out.println("Calculating zeroless diagonal permutations...");
    TetradLogger.getInstance().log("lingDetails", "Calculating zeroless diagonal permutations.");
    zldPerms = zerolessDiagonalPermutations(matrixW, approximateZeros, variables, dataSet);
    System.out.println("Calculated zeroless diagonal permutations.");
    // for each W~, compute a candidate B, and score it
    for (PermutationMatrixPair zldPerm : zldPerms) {
        TetradLogger.getInstance().log("lingDetails", "" + zldPerm);
        System.out.println(zldPerm);
        normalizedZldW = LingUtils.normalizeDiagonal(zldPerm.getMatrixW());
        // Note: add method to deal with this data
        // B~ = I - W~
        zldPerm.setMatrixBhat(computeBhatTetradMatrix(normalizedZldW, variables));
        TetradMatrix doubleData = zldPerm.getMatrixBhat().getDoubleData();
        boolean isStableTetradMatrix = allEigenvaluesAreSmallerThanOneInModulus(new TetradMatrix(doubleData.toArray()));
        GraphWithParameters graph = new GraphWithParameters(zldPerm.getMatrixBhat());
        gs.addGraph(graph.getGraph());
        gs.addStable(isStableTetradMatrix);
        gs.addData(zldPerm.getMatrixBhat());
    }
    TetradLogger.getInstance().log("stableGraphs", "Stable Graphs:");
    for (int d = 0; d < gs.getNumGraphs(); d++) {
        if (!gs.isStable(d)) {
            continue;
        }
        TetradLogger.getInstance().log("stableGraphs", "" + gs.getGraph(d));
        if (TetradLogger.getInstance().getLoggerConfig() != null && TetradLogger.getInstance().getLoggerConfig().isEventActive("stableGraphs")) {
            TetradLogger.getInstance().log("wMatrices", "" + gs.getData(d));
        }
    }
    TetradLogger.getInstance().log("unstableGraphs", "Unstable Graphs:");
    for (int d = 0; d < gs.getNumGraphs(); d++) {
        if (gs.isStable(d)) {
            continue;
        }
        TetradLogger.getInstance().log("unstableGraphs", "" + gs.getGraph(d));
        if (TetradLogger.getInstance().getLoggerConfig() != null && TetradLogger.getInstance().getLoggerConfig().isEventActive("unstableGraphs")) {
            TetradLogger.getInstance().log("wMatrices", "" + gs.getData(d));
        }
    }
    return gs;
}
Also used : TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 88 with TetradMatrix

use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.

the class Ling method computeBhatTetradMatrix.

// B^ = I - W~'
private static DataSet computeBhatTetradMatrix(TetradMatrix normalizedZldW, List<Node> nodes) {
    // , List<Integer> perm) {
    int size = normalizedZldW.rows();
    TetradMatrix mat = TetradMatrix.identity(size).minus(normalizedZldW);
    return ColtDataSet.makeContinuousData(nodes, new TetradMatrix(mat.toArray()));
}
Also used : TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 89 with TetradMatrix

use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.

the class Ling method getWFastIca.

private TetradMatrix getWFastIca() {
    TetradMatrix A;
    // Using this Fast ICA to get the logging.
    TetradMatrix W;
    TetradMatrix data = new TetradMatrix(dataSet.getDoubleData().toArray());
    FastIca fastIca = new FastIca(data, data.columns());
    fastIca.setVerbose(false);
    fastIca.setAlgorithmType(FastIca.DEFLATION);
    fastIca.setFunction(FastIca.LOGCOSH);
    fastIca.setTolerance(1e-20);
    fastIca.setMaxIterations(500);
    fastIca.setAlpha(1.0);
    FastIca.IcaResult result = fastIca.findComponents();
    A = new TetradMatrix(result.getA().transpose().toArray());
    W = A.inverse();
    return W;
}
Also used : TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 90 with TetradMatrix

use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.

the class Ling method zerolessDiagonalPermutation.

private List<PermutationMatrixPair> zerolessDiagonalPermutation(TetradMatrix ica_W, boolean approximateZeros, List<Node> vars, DataSet dataSet) {
    List<PermutationMatrixPair> permutations = new Vector<>();
    if (approximateZeros) {
        // setInsignificantEntriesToZero(ica_W);
        ica_W = pruneEdgesByResampling(ica_W);
        ica_W = removeZeroRowsAndCols(ica_W, vars);
    }
    // List<PermutationMatrixPair > zldPerms = new ArrayList<PermutationMatrixPair >();
    List<Integer> perm = new ArrayList<>();
    for (int i = 0; i < vars.size(); i++) perm.add(i);
    TetradMatrix matrixW = ica_W.transpose();
    PermutationMatrixPair pair = new PermutationMatrixPair(perm, matrixW);
    permutations.add(pair);
    return permutations;
}
Also used : ArrayList(java.util.ArrayList) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) Vector(java.util.Vector) TetradVector(edu.cmu.tetrad.util.TetradVector)

Aggregations

TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)161 TetradVector (edu.cmu.tetrad.util.TetradVector)46 ArrayList (java.util.ArrayList)43 Node (edu.cmu.tetrad.graph.Node)41 List (java.util.List)12 CovarianceMatrix (edu.cmu.tetrad.data.CovarianceMatrix)10 DepthChoiceGenerator (edu.cmu.tetrad.util.DepthChoiceGenerator)9 SingularMatrixException (org.apache.commons.math3.linear.SingularMatrixException)9 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)8 RegressionResult (edu.cmu.tetrad.regression.RegressionResult)8 Test (org.junit.Test)8 Regression (edu.cmu.tetrad.regression.Regression)7 RegressionDataset (edu.cmu.tetrad.regression.RegressionDataset)7 SemIm (edu.cmu.tetrad.sem.SemIm)7 Graph (edu.cmu.tetrad.graph.Graph)6 SemPm (edu.cmu.tetrad.sem.SemPm)6 Vector (java.util.Vector)6 DoubleArrayList (cern.colt.list.DoubleArrayList)5 DataSet (edu.cmu.tetrad.data.DataSet)5 ICovarianceMatrix (edu.cmu.tetrad.data.ICovarianceMatrix)5