Search in sources :

Example 71 with DoubleArrayList

use of cern.colt.list.DoubleArrayList in project tetrad by cmu-phil.

the class Lofs 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)));
        // of the individual columns. http://en.wikipedia.org/wiki/Mixture_distribution#Finite_and_countable_mixtures
        for (int i2 = 0; i2 < _residualsSingleDataset.size(); i2++) {
            // _residualsSingleDataset.set(i2, (_residualsSingleDataset.get(i2)) / std);
            if (isMeanCenterResiduals()) {
                _residualsSingleDataset.set(i2, (_residualsSingleDataset.get(i2) - mean));
            }
        }
        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);
    }
    return new AndersonDarlingTest(_f).getASquaredStar();
}
Also used : DoubleArrayList(cern.colt.list.DoubleArrayList) AndersonDarlingTest(edu.cmu.tetrad.data.AndersonDarlingTest) DoubleArrayList(cern.colt.list.DoubleArrayList) RegressionResult(edu.cmu.tetrad.regression.RegressionResult)

Example 72 with DoubleArrayList

use of cern.colt.list.DoubleArrayList in project tetrad by cmu-phil.

the class Lofs method pValue.

private double pValue(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);
            if (isMeanCenterResiduals()) {
                _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);
    }
    return new AndersonDarlingTest(_f).getP();
}
Also used : DoubleArrayList(cern.colt.list.DoubleArrayList) AndersonDarlingTest(edu.cmu.tetrad.data.AndersonDarlingTest) DoubleArrayList(cern.colt.list.DoubleArrayList) RegressionResult(edu.cmu.tetrad.regression.RegressionResult)

Example 73 with DoubleArrayList

use of cern.colt.list.DoubleArrayList in project tetrad by cmu-phil.

the class Lofs method localScoreA.

// =============================PRIVATE METHODS=========================//
private double localScoreA(Node node, List<Node> parents) {
    double score = 0.0;
    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);
        }
        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);
    }
    DoubleArrayList f = new DoubleArrayList(_f);
    for (int k = 0; k < _residuals.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;
    return score;
}
Also used : DoubleArrayList(cern.colt.list.DoubleArrayList) DoubleArrayList(cern.colt.list.DoubleArrayList) RegressionResult(edu.cmu.tetrad.regression.RegressionResult)

Example 74 with DoubleArrayList

use of cern.colt.list.DoubleArrayList 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 75 with DoubleArrayList

use of cern.colt.list.DoubleArrayList 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)

Aggregations

DoubleArrayList (cern.colt.list.DoubleArrayList)82 RegressionResult (edu.cmu.tetrad.regression.RegressionResult)11 ArrayList (java.util.ArrayList)9 AndersonDarlingTest (edu.cmu.tetrad.data.AndersonDarlingTest)8 IntArrayList (cern.colt.list.IntArrayList)6 DenseDoubleMatrix2D (cern.colt.matrix.impl.DenseDoubleMatrix2D)5 TetradVector (edu.cmu.tetrad.util.TetradVector)5 Test (org.junit.Test)5 DoubleMatrix2D (cern.colt.matrix.DoubleMatrix2D)4 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)4 DoubleMatrix1D (cern.colt.matrix.DoubleMatrix1D)3 DenseDoubleMatrix1D (cern.colt.matrix.impl.DenseDoubleMatrix1D)3 Regression (edu.cmu.tetrad.regression.Regression)3 RegressionDataset (edu.cmu.tetrad.regression.RegressionDataset)3 StopWatch (org.apache.commons.lang3.time.StopWatch)2 CoordinatePoint (org.onebusaway.geospatial.model.CoordinatePoint)2 Record (org.onebusaway.transit_data.model.realtime.CurrentVehicleEstimateQueryBean.Record)2 ScheduledBlockLocation (org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation)2 BlockLocation (org.onebusaway.transit_data_federation.services.realtime.BlockLocation)2 ByteArrayConverter (ubic.basecode.io.ByteArrayConverter)2