Search in sources :

Example 71 with TetradMatrix

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

the class SemBicScoreDeterministic 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) ArrayList(java.util.ArrayList) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) SingularMatrixException(org.apache.commons.math3.linear.SingularMatrixException)

Example 72 with TetradMatrix

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

the class SemBicScoreDeterministic method determines.

@Override
public boolean determines(List<Node> z, Node y) {
    int i = variables.indexOf(y);
    int[] parents = new int[z.size()];
    for (int t = 0; t < z.size(); t++) {
        parents[t] = variables.indexOf(z.get(t));
    }
    double small = getDeterminismThreshold();
    try {
        double s2 = getCovariances().getValue(i, i);
        TetradMatrix covxx = getSelection(getCovariances(), parents, parents);
        TetradVector covxy = getSelection(getCovariances(), parents, new int[] { i }).getColumn(0);
        s2 -= covxx.inverse().times(covxy).dotProduct(covxy);
        if (s2 <= small) {
            printDeterminism(i, parents);
            return true;
        }
    } catch (Exception e) {
        printDeterminism(i, parents);
    }
    return false;
}
Also used : TetradVector(edu.cmu.tetrad.util.TetradVector) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) SingularMatrixException(org.apache.commons.math3.linear.SingularMatrixException)

Example 73 with TetradMatrix

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

the class SemBicScoreDeterministic method getMaximalLinearlyDependentSet.

private int[] getMaximalLinearlyDependentSet(int i, int[] parents, ICovarianceMatrix cov) {
    double small = getDeterminismThreshold();
    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[] sel0 = new int[choice.length];
        List<Integer> all = new ArrayList<>();
        for (int w = 0; w < parents.length; w++) all.add(parents[w]);
        for (int w = 0; w < sel0.length; w++) all.remove(sel0[w]);
        int[] sel = new int[all.size()];
        for (int w = 0; w < all.size(); w++) sel[w] = all.get(w);
        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);
        double s2 = getCovariances().getValue(i, i);
        TetradMatrix covxx = getSelection(getCovariances(), parents, parents);
        TetradVector covxy = getSelection(getCovariances(), parents, new int[] { i }).getColumn(0);
        s2 -= covxx.inverse().times(covxy).dotProduct(covxy);
        if (s2 <= small) {
            out.println("### Linear dependence among variables: " + _sel);
            out.println("### Removing " + _sel.get(0));
            return 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 sel;
        }
    }
    return new int[0];
}
Also used : TetradVector(edu.cmu.tetrad.util.TetradVector) DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) SingularMatrixException(org.apache.commons.math3.linear.SingularMatrixException)

Example 74 with TetradMatrix

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

the class SemBicScoreImages2 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) ArrayList(java.util.ArrayList) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 75 with TetradMatrix

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

the class SemBicScoreImages2 method localScore.

/**
 * Calculates the sample likelihood and BIC score for i given its parents in a simple SEM model
 */
public double localScore(int i, int... parents) {
    for (int p : parents) if (forbidden.contains(p))
        return Double.NaN;
    double lik = 0.0;
    for (int k = 0; k < covariances.size(); k++) {
        double residualVariance = getCovariances(k).getValue(i, i);
        TetradMatrix covxx = getSelection1(getCovariances(k), parents);
        try {
            TetradMatrix covxxInv = covxx.inverse();
            TetradVector covxy = getSelection2(getCovariances(k), 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(k).getValue(i, i)));
                }
                return Double.NaN;
            }
            int cols = getCovariances(0).getDimension();
            double q = 2 / (double) cols;
            lik += -sampleSize * Math.log(residualVariance);
        } 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(k));
            }
            return Double.NaN;
        }
    }
    int p = parents.length;
    double c = getPenaltyDiscount();
    return 2 * lik - c * (p + 1) * Math.log(covariances.size() * sampleSize);
}
Also used : TetradVector(edu.cmu.tetrad.util.TetradVector) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) 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