Search in sources :

Example 61 with TetradMatrix

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

the class MNLRLikelihood method multipleRegression.

private double multipleRegression(TetradVector Y, TetradMatrix X) {
    int n = X.rows();
    TetradVector r;
    try {
        TetradMatrix Xt = X.transpose();
        TetradMatrix XtX = Xt.times(X);
        r = X.times(XtX.inverse().times(Xt.times(Y))).minus(Y);
    } catch (Exception e) {
        TetradVector ones = new TetradVector(n);
        for (int i = 0; i < n; i++) ones.set(i, 1);
        r = ones.scalarMult(ones.dotProduct(Y) / (double) n).minus(Y);
    }
    double sigma2 = r.dotProduct(r) / n;
    if (sigma2 <= 0) {
        TetradVector ones = new TetradVector(n);
        for (int i = 0; i < n; i++) ones.set(i, 1);
        r = ones.scalarMult(ones.dotProduct(Y) / (double) Math.max(n, 2)).minus(Y);
        sigma2 = r.dotProduct(r) / n;
    }
    double lik = -(n / 2) * (Math.log(2 * Math.PI) + Math.log(sigma2) + 1);
    if (Double.isInfinite(lik) || Double.isNaN(lik)) {
        System.out.println(lik);
    }
    return lik;
}
Also used : TetradVector(edu.cmu.tetrad.util.TetradVector) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 62 with TetradMatrix

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

the class SemBicScore 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;
    try {
        double s2 = getCovariances().getValue(i, i);
        int p = parents.length;
        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 <= 0) {
            if (isVerbose()) {
                out.println("Nonpositive residual varianceY: resVar / varianceY = " + (s2 / getCovariances().getValue(i, i)));
            }
            return Double.NaN;
        }
        int n = getSampleSize();
        return -(n) * log(s2) - getPenaltyDiscount() * log(n);
    // + getStructurePrior(parents.length);// - getStructurePrior(parents.length + 1);
    } catch (Exception e) {
        boolean removedOne = true;
        while (removedOne) {
            List<Integer> _parents = new ArrayList<>();
            for (int parent : parents) _parents.add(parent);
            _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) SingularMatrixException(org.apache.commons.math3.linear.SingularMatrixException)

Example 63 with TetradMatrix

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

the class SemBicScore method partialCorrelation.

private double partialCorrelation(Node x, Node y, List<Node> z) throws SingularMatrixException {
    int[] indices = new int[z.size() + 2];
    indices[0] = indexMap.get(x.getName());
    indices[1] = indexMap.get(y.getName());
    for (int i = 0; i < z.size(); i++) indices[i + 2] = indexMap.get(z.get(i).getName());
    TetradMatrix submatrix = covariances.getSubmatrix(indices).getMatrix();
    return StatUtils.partialCorrelation(submatrix);
}
Also used : TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 64 with TetradMatrix

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

the class SemBicScore 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) SingularMatrixException(org.apache.commons.math3.linear.SingularMatrixException)

Example 65 with TetradMatrix

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

the class SemBicScoreImages3 method cov.

private TetradMatrix cov(DataSet x) {
    TetradMatrix M = x.getDoubleData();
    RealMatrix covarianceMatrix = new Covariance(M.getRealMatrix(), true).getCovarianceMatrix();
    return new TetradMatrix(covarianceMatrix, covarianceMatrix.getRowDimension(), covarianceMatrix.getColumnDimension());
}
Also used : RealMatrix(org.apache.commons.math3.linear.RealMatrix) Covariance(org.apache.commons.math3.stat.correlation.Covariance) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

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