Search in sources :

Example 66 with TetradMatrix

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

the class SemLikelihood2 method likelihoodJoint.

// The likelihood of the joint over all of these variables, assuming conditional Gaussian,
// continuous and discrete.
private Ret likelihoodJoint(List<ContinuousVariable> X) {
    X = new ArrayList<>(X);
    int k = X.size();
    int[] cols = new int[k];
    for (int j = 0; j < k; j++) cols[j] = nodesHash.get(X.get(j));
    int N = covMatrix.getSampleSize();
    TetradMatrix cov = covMatrix.getSelection(cols, cols);
    double lnL = N * gaussianLikelihood(k, cov);
    final int dof = h(X);
    return new Ret(lnL, dof);
}
Also used : TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 67 with TetradMatrix

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

the class MixedBicScore method getBicLinear.

private double getBicLinear(int i, int[] parents) {
    double residualVariance = getCovariances().getValue(i, i);
    int n = getSampleSize();
    int p = parents.length;
    TetradMatrix covxx = getSelection1(getCovariances(), parents);
    try {
        TetradMatrix covxxInv = covxx.inverse();
        TetradVector covxy = getSelection2(getCovariances(), parents, i);
        TetradVector b = covxxInv.times(covxy);
        residualVariance -= covxy.dotProduct(b);
        if (residualVariance <= 0) {
            if (isVerbose()) {
                out.println("Nonpositive residual varianceY: resVar / varianceY = " + (residualVariance / getCovariances().getValue(i, i)));
            }
            return Double.NaN;
        }
        double c = getPenaltyDiscount();
        return -n * Math.log(residualVariance) - c * (p + 1) * logn;
    } catch (Exception e) {
        boolean removedOne = true;
        while (removedOne) {
            List<Integer> _parents = new ArrayList<>();
            for (int y = 0; y < parents.length; y++) _parents.add(parents[y]);
            _parents.removeAll(forbidden);
            parents = new int[_parents.size()];
            for (int y = 0; y < _parents.size(); y++) parents[y] = _parents.get(y);
            removedOne = printMinimalLinearlyDependentSet(parents, getCovariances());
        }
        return Double.NaN;
    }
}
Also used : TetradVector(edu.cmu.tetrad.util.TetradVector) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 68 with TetradMatrix

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

the class MixedBicScore method printMinimalLinearlyDependentSet.

// Prints a smallest subset of parents that causes a singular matrix exception.
private boolean printMinimalLinearlyDependentSet(int[] parents, ICovarianceMatrix cov) {
    List<Node> _parents = new ArrayList<>();
    for (int p : parents) _parents.add(variables.get(p));
    DepthChoiceGenerator gen = new DepthChoiceGenerator(_parents.size(), _parents.size());
    int[] choice;
    while ((choice = gen.next()) != null) {
        int[] sel = new int[choice.length];
        List<Node> _sel = new ArrayList<>();
        for (int m = 0; m < choice.length; m++) {
            sel[m] = parents[m];
            _sel.add(variables.get(sel[m]));
        }
        TetradMatrix m = cov.getSelection(sel, sel);
        try {
            m.inverse();
        } catch (Exception e2) {
            forbidden.add(sel[0]);
            out.println("### Linear dependence among variables: " + _sel);
            out.println("### Removing " + _sel.get(0));
            return true;
        }
    }
    return false;
}
Also used : DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator) Node(edu.cmu.tetrad.graph.Node) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 69 with TetradMatrix

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

the class Mimbuild2 method search.

// =================================== PUBLIC METHODS =========================================//
public Graph search(List<List<Node>> clustering, List<String> latentNames, ICovarianceMatrix measuresCov) {
    List<String> _latentNames = new ArrayList<>(latentNames);
    List<String> allVarNames = new ArrayList<>();
    for (List<Node> cluster : clustering) {
        for (Node node : cluster) allVarNames.add(node.getName());
    }
    measuresCov = measuresCov.getSubmatrix(allVarNames);
    List<List<Node>> _clustering = new ArrayList<>();
    for (List<Node> cluster : clustering) {
        List<Node> _cluster = new ArrayList<>();
        for (Node node : cluster) {
            _cluster.add(measuresCov.getVariable(node.getName()));
        }
        _clustering.add(_cluster);
    }
    List<Node> latents = defineLatents(_latentNames);
    this.latents = latents;
    // This removes the small clusters and their names.
    removeSmallClusters(latents, _clustering, getMinClusterSize());
    this.clustering = _clustering;
    Node[][] indicators = new Node[latents.size()][];
    for (int i = 0; i < latents.size(); i++) {
        indicators[i] = new Node[_clustering.get(i).size()];
        for (int j = 0; j < _clustering.get(i).size(); j++) {
            indicators[i][j] = _clustering.get(i).get(j);
        }
    }
    TetradMatrix cov = getCov(measuresCov, latents, indicators);
    CovarianceMatrix latentscov = new CovarianceMatrix(latents, cov, measuresCov.getSampleSize());
    this.latentsCov = latentscov;
    Graph graph;
    Cpc search = new Cpc(new IndTestFisherZ(latentscov, alpha));
    search.setKnowledge(knowledge);
    graph = search.search();
    // try {
    // Ges search = new Ges(latentscov);
    // search.setDepErrorsAlpha(penaltyDiscount);
    // search.setKnowledge(knowledge);
    // graph = search.search();
    // } catch (Exception e) {
    // //            e.printStackTrace();
    // CPC search = new CPC(new IndTestFisherZ(latentscov, alpha));
    // search.setKnowledge(knowledge);
    // graph = search.search();
    // }
    this.structureGraph = new EdgeListGraph(graph);
    GraphUtils.fruchtermanReingoldLayout(this.structureGraph);
    return this.structureGraph;
}
Also used : ArrayList(java.util.ArrayList) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) CovarianceMatrix(edu.cmu.tetrad.data.CovarianceMatrix) ICovarianceMatrix(edu.cmu.tetrad.data.ICovarianceMatrix) ArrayList(java.util.ArrayList) List(java.util.List)

Example 70 with TetradMatrix

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

the class MimbuildTrek method search.

// =================================== PUBLIC METHODS =========================================//
public Graph search(List<List<Node>> clustering, List<String> latentNames, ICovarianceMatrix measuresCov) {
    List<String> _latentNames = new ArrayList<>(latentNames);
    List<String> allVarNames = new ArrayList<>();
    for (List<Node> cluster : clustering) {
        for (Node node : cluster) allVarNames.add(node.getName());
    }
    measuresCov = measuresCov.getSubmatrix(allVarNames);
    List<List<Node>> _clustering = new ArrayList<>();
    for (List<Node> cluster : clustering) {
        List<Node> _cluster = new ArrayList<>();
        for (Node node : cluster) {
            _cluster.add(measuresCov.getVariable(node.getName()));
        }
        _clustering.add(_cluster);
    }
    List<Node> latents = defineLatents(_latentNames);
    this.latents = latents;
    // This removes the small clusters and their names.
    removeSmallClusters(latents, _clustering, getMinClusterSize());
    this.clustering = _clustering;
    Node[][] indicators = new Node[latents.size()][];
    for (int i = 0; i < latents.size(); i++) {
        indicators[i] = new Node[_clustering.get(i).size()];
        for (int j = 0; j < _clustering.get(i).size(); j++) {
            indicators[i][j] = _clustering.get(i).get(j);
        }
    }
    TetradMatrix cov = getCov(measuresCov, latents, indicators);
    CovarianceMatrix latentscov = new CovarianceMatrix(latents, cov, measuresCov.getSampleSize());
    this.latentsCov = latentscov;
    Graph graph;
    Cpc search = new Cpc(new IndTestTrekSep(measuresCov, alpha, clustering, latents));
    search.setKnowledge(knowledge);
    graph = search.search();
    // try {
    // Ges search = new Ges(latentscov);
    // search.setCorrErrorsAlpha(penaltyDiscount);
    // search.setKnowledge(knowledge);
    // graph = search.search();
    // } catch (Exception e) {
    // //            e.printStackTrace();
    // CPC search = new CPC(new IndTestFisherZ(latentscov, alpha));
    // search.setKnowledge(knowledge);
    // graph = search.search();
    // }
    this.structureGraph = new EdgeListGraph(graph);
    GraphUtils.fruchtermanReingoldLayout(this.structureGraph);
    return this.structureGraph;
}
Also used : ArrayList(java.util.ArrayList) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) CovarianceMatrix(edu.cmu.tetrad.data.CovarianceMatrix) ICovarianceMatrix(edu.cmu.tetrad.data.ICovarianceMatrix) ArrayList(java.util.ArrayList) List(java.util.List)

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