Search in sources :

Example 31 with TetradVector

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

the class LingamPattern2 method getScore.

// Return the average score.
private Score getScore(Graph dag, List<TetradMatrix> data, List<Node> variables) {
    // System.out.println("Scoring DAG: " + dag);
    int totalSampleSize = 0;
    for (TetradMatrix _data : data) {
        totalSampleSize += _data.rows();
    }
    int numCols = data.get(0).columns();
    List<Node> nodes = dag.getNodes();
    double score = 0.0;
    double[] pValues = new double[nodes.size()];
    TetradMatrix residuals = new TetradMatrix(totalSampleSize, numCols);
    for (int j = 0; j < nodes.size(); j++) {
        List<Double> _residuals = new ArrayList<>();
        Node _target = nodes.get(j);
        List<Node> _regressors = dag.getParents(_target);
        Node target = getVariable(variables, _target.getName());
        List<Node> regressors = new ArrayList<>();
        for (Node _regressor : _regressors) {
            Node variable = getVariable(variables, _regressor.getName());
            regressors.add(variable);
        }
        for (int m = 0; m < data.size(); m++) {
            RegressionResult result = regressions.get(m).regress(target, regressors);
            TetradVector residualsSingleDataset = result.getResiduals();
            DoubleArrayList _residualsSingleDataset = new DoubleArrayList(residualsSingleDataset.toArray());
            double mean = Descriptive.mean(_residualsSingleDataset);
            double std = Descriptive.standardDeviation(Descriptive.variance(_residualsSingleDataset.size(), Descriptive.sum(_residualsSingleDataset), Descriptive.sumOfSquares(_residualsSingleDataset)));
            for (int i2 = 0; i2 < _residualsSingleDataset.size(); i2++) {
                _residualsSingleDataset.set(i2, (_residualsSingleDataset.get(i2) - mean) / std);
            }
            for (int k = 0; k < _residualsSingleDataset.size(); k++) {
                _residuals.add(_residualsSingleDataset.get(k));
            }
            DoubleArrayList f = new DoubleArrayList(_residualsSingleDataset.elements());
            for (int k = 0; k < f.size(); k++) {
                f.set(k, Math.abs(f.get(k)));
            }
            double _mean = Descriptive.mean(f);
            double diff = _mean - Math.sqrt(2.0 / Math.PI);
            score += diff * diff;
        // score += andersonDarlingPASquareStar(target, dag.getParents(target));
        }
        for (int k = 0; k < _residuals.size(); k++) {
            residuals.set(k, j, _residuals.get(k));
        }
    }
    for (int j = 0; j < residuals.columns(); j++) {
        double[] x = residuals.getColumn(j).toArray();
        double p = new AndersonDarlingTest(x).getP();
        pValues[j] = p;
    }
    return new Score(score, pValues);
}
Also used : DoubleArrayList(cern.colt.list.DoubleArrayList) ArrayList(java.util.ArrayList) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) DoubleArrayList(cern.colt.list.DoubleArrayList) TetradVector(edu.cmu.tetrad.util.TetradVector) AndersonDarlingTest(edu.cmu.tetrad.data.AndersonDarlingTest) RegressionResult(edu.cmu.tetrad.regression.RegressionResult)

Example 32 with TetradVector

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

the class LingamPattern2 method andersonDarlingPASquareStar.

private double andersonDarlingPASquareStar(Node node, List<Node> parents) {
    List<Double> _residuals = new ArrayList<>();
    Node _target = node;
    List<Node> _regressors = parents;
    Node target = getVariable(variables, _target.getName());
    List<Node> regressors = new ArrayList<>();
    for (Node _regressor : _regressors) {
        Node variable = getVariable(variables, _regressor.getName());
        regressors.add(variable);
    }
    DATASET: for (int m = 0; m < dataSets.size(); m++) {
        RegressionResult result = regressions.get(m).regress(target, regressors);
        TetradVector residualsSingleDataset = result.getResiduals();
        for (int h = 0; h < residualsSingleDataset.size(); h++) {
            if (Double.isNaN(residualsSingleDataset.get(h))) {
                continue DATASET;
            }
        }
        DoubleArrayList _residualsSingleDataset = new DoubleArrayList(residualsSingleDataset.toArray());
        double mean = Descriptive.mean(_residualsSingleDataset);
        double std = Descriptive.standardDeviation(Descriptive.variance(_residualsSingleDataset.size(), Descriptive.sum(_residualsSingleDataset), Descriptive.sumOfSquares(_residualsSingleDataset)));
        for (int i2 = 0; i2 < _residualsSingleDataset.size(); i2++) {
            // _residualsSingleDataset.set(i2, (_residualsSingleDataset.get(i2) - mean) / std);
            _residualsSingleDataset.set(i2, (_residualsSingleDataset.get(i2) - mean));
        // _residualsSingleDataset.set(i2, (_residualsSingleDataset.get(i2)));
        }
        for (int k = 0; k < _residualsSingleDataset.size(); k++) {
            _residuals.add(_residualsSingleDataset.get(k));
        }
    }
    double[] _f = new double[_residuals.size()];
    for (int k = 0; k < _residuals.size(); k++) {
        _f[k] = _residuals.get(k);
    }
    double p = new AndersonDarlingTest(_f).getASquaredStar();
    System.out.println("Anderson Darling p for " + node + " given " + parents + " = " + p);
    return p;
}
Also used : TetradVector(edu.cmu.tetrad.util.TetradVector) DoubleArrayList(cern.colt.list.DoubleArrayList) ArrayList(java.util.ArrayList) AndersonDarlingTest(edu.cmu.tetrad.data.AndersonDarlingTest) DoubleArrayList(cern.colt.list.DoubleArrayList) RegressionResult(edu.cmu.tetrad.regression.RegressionResult)

Example 33 with TetradVector

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

the class LingamPattern2 method getScore2.

private Score getScore2(Graph dag, List<TetradMatrix> data, List<Node> variables) {
    // System.out.println("Scoring DAG: " + dag);
    List<Regression> regressions = new ArrayList<>();
    for (TetradMatrix _data : data) {
        regressions.add(new RegressionDataset(_data, variables));
    }
    int totalSampleSize = 0;
    for (TetradMatrix _data : data) {
        totalSampleSize += _data.rows();
    }
    int numCols = data.get(0).columns();
    List<Node> nodes = dag.getNodes();
    double score = 0.0;
    double[] pValues = new double[nodes.size()];
    TetradMatrix residuals = new TetradMatrix(totalSampleSize, numCols);
    for (int j = 0; j < nodes.size(); j++) {
        List<Double> _residuals = new ArrayList<>();
        Node _target = nodes.get(j);
        List<Node> _regressors = dag.getParents(_target);
        Node target = getVariable(variables, _target.getName());
        List<Node> regressors = new ArrayList<>();
        for (Node _regressor : _regressors) {
            Node variable = getVariable(variables, _regressor.getName());
            regressors.add(variable);
        }
        for (int m = 0; m < data.size(); m++) {
            RegressionResult result = regressions.get(m).regress(target, regressors);
            TetradVector residualsSingleDataset = result.getResiduals();
            DoubleArrayList _residualsSingleDataset = new DoubleArrayList(residualsSingleDataset.toArray());
            double mean = Descriptive.mean(_residualsSingleDataset);
            double std = Descriptive.standardDeviation(Descriptive.variance(_residualsSingleDataset.size(), Descriptive.sum(_residualsSingleDataset), Descriptive.sumOfSquares(_residualsSingleDataset)));
            for (int i2 = 0; i2 < _residualsSingleDataset.size(); i2++) {
                _residualsSingleDataset.set(i2, (_residualsSingleDataset.get(i2) - mean) / std);
            }
            for (int k = 0; k < _residualsSingleDataset.size(); k++) {
                _residuals.add(_residualsSingleDataset.get(k));
            }
        }
        for (int k = 0; k < _residuals.size(); k++) {
            residuals.set(k, j, _residuals.get(k));
        }
    }
    for (int i = 0; i < nodes.size(); i++) {
        DoubleArrayList f = new DoubleArrayList(residuals.getColumn(i).toArray());
        for (int j = 0; j < f.size(); j++) {
            f.set(j, Math.abs(f.get(j)));
        }
        double _mean = Descriptive.mean(f);
        double diff = _mean - Math.sqrt(2.0 / Math.PI);
        score += diff * diff;
    }
    for (int j = 0; j < residuals.columns(); j++) {
        double[] x = residuals.getColumn(j).toArray();
        double p = new AndersonDarlingTest(x).getP();
        pValues[j] = p;
    }
    return new Score(score, pValues);
}
Also used : Regression(edu.cmu.tetrad.regression.Regression) DoubleArrayList(cern.colt.list.DoubleArrayList) ArrayList(java.util.ArrayList) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) DoubleArrayList(cern.colt.list.DoubleArrayList) RegressionDataset(edu.cmu.tetrad.regression.RegressionDataset) TetradVector(edu.cmu.tetrad.util.TetradVector) AndersonDarlingTest(edu.cmu.tetrad.data.AndersonDarlingTest) RegressionResult(edu.cmu.tetrad.regression.RegressionResult)

Example 34 with TetradVector

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

the class Peter1Score 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();
        int k = 2 * p + 1;
        s2 = ((n) / (double) (n - k)) * s2;
        return -(n) * log(s2) - getPenaltyDiscount() * k * log(n);
    } 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) List(java.util.List) ArrayList(java.util.ArrayList)

Example 35 with TetradVector

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

the class SemBicScoreImages3 method score2.

public double score2(int i, int[] parents) {
    double lik = 0.0;
    for (int k = 0; k < covs.size(); k++) {
        final int a = sampleSizes[k];
        TetradMatrix cov = covs.get(k);
        double residualVariance = cov.get(i, i);
        TetradMatrix covxx = cov.getSelection(parents, parents);
        TetradMatrix covxxInv = covxx.inverse();
        TetradVector covxy = cov.getSelection(parents, new int[] { i }).getColumn(0);
        TetradVector b = covxxInv.times(covxy);
        residualVariance -= covxy.dotProduct(b);
        lik += -(a / 2.0) * log(residualVariance) - (a / 2.0) - (a / 2.0) * log(2 * PI);
    }
    return 2.0 * lik - getPenaltyDiscount() * (parents.length + 1) * log(N);
}
Also used : TetradVector(edu.cmu.tetrad.util.TetradVector) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

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