Search in sources :

Example 16 with TetradVector

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

the class Ling method simulateReducedForm.

// check against model in which: A =  ..... / (1 - xyzw)
private static TetradVector simulateReducedForm(TetradMatrix reducedForm, TetradVector errorCoefficients, Distribution distr) {
    int n = reducedForm.rows();
    TetradVector vector = new TetradVector(n);
    TetradVector samples = new TetradVector(n);
    for (int j = 0; j < n; j++) {
        // sample from each noise term
        double sample = distr.nextRandom();
        double errorCoefficient = errorCoefficients.get(j);
        samples.set(j, sample * errorCoefficient);
    }
    for (int i = 0; i < n; i++) {
        // for each observed variable, i.e. dimension
        double sum = 0;
        for (int j = 0; j < n; j++) {
            double coefficient = reducedForm.get(i, j);
            double sample = samples.get(j);
            sum += coefficient * sample;
        }
        vector.set(i, sum);
    }
    return vector;
}
Also used : TetradVector(edu.cmu.tetrad.util.TetradVector)

Example 17 with TetradVector

use of edu.cmu.tetrad.util.TetradVector 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 18 with TetradVector

use of edu.cmu.tetrad.util.TetradVector 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 19 with TetradVector

use of edu.cmu.tetrad.util.TetradVector 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 20 with TetradVector

use of edu.cmu.tetrad.util.TetradVector 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)

Aggregations

TetradVector (edu.cmu.tetrad.util.TetradVector)54 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)44 ArrayList (java.util.ArrayList)19 Node (edu.cmu.tetrad.graph.Node)11 RegressionResult (edu.cmu.tetrad.regression.RegressionResult)9 Regression (edu.cmu.tetrad.regression.Regression)7 RegressionDataset (edu.cmu.tetrad.regression.RegressionDataset)7 DoubleArrayList (cern.colt.list.DoubleArrayList)5 AndersonDarlingTest (edu.cmu.tetrad.data.AndersonDarlingTest)5 SingularMatrixException (org.apache.commons.math3.linear.SingularMatrixException)5 List (java.util.List)4 Test (org.junit.Test)4 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)3 DepthChoiceGenerator (edu.cmu.tetrad.util.DepthChoiceGenerator)2 RandomUtil (edu.cmu.tetrad.util.RandomUtil)2 Vector (java.util.Vector)2 DoubleMatrix1D (cern.colt.matrix.DoubleMatrix1D)1 DoubleMatrix2D (cern.colt.matrix.DoubleMatrix2D)1 DenseDoubleMatrix1D (cern.colt.matrix.impl.DenseDoubleMatrix1D)1 DenseDoubleMatrix2D (cern.colt.matrix.impl.DenseDoubleMatrix2D)1