Search in sources :

Example 76 with DoubleArrayList

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

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

the class SemEstimator method setMeans.

/**
 * Sets the means of variables in the SEM IM based on the given data set.
 */
private void setMeans(SemIm semIm, DataSet dataSet) {
    if (dataSet != null) {
        int numColumns = dataSet.getNumColumns();
        for (int j = 0; j < numColumns; j++) {
            double[] column = dataSet.getDoubleData().getColumn(j).toArray();
            DoubleArrayList list = new DoubleArrayList(column);
            double mean = Descriptive.mean(list);
            Node node = dataSet.getVariable(j);
            Node variableNode = semIm.getVariableNode(node.getName());
            semIm.setMean(variableNode, mean);
            double standardDeviation = Descriptive.standardDeviation(Descriptive.variance(list.size(), Descriptive.sum(list), Descriptive.sumOfSquares(list)));
            semIm.setMeanStandardDeviation(variableNode, standardDeviation);
        }
    } else if (getCovMatrix() != null) {
        List<Node> variables = getCovMatrix().getVariables();
        for (Node node : variables) {
            Node variableNode = semIm.getVariableNode(node.getName());
            semIm.setMean(variableNode, 0.0);
        }
    }
}
Also used : Node(edu.cmu.tetrad.graph.Node) DoubleArrayList(cern.colt.list.DoubleArrayList) ArrayList(java.util.ArrayList) List(java.util.List) DoubleArrayList(cern.colt.list.DoubleArrayList)

Example 78 with DoubleArrayList

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

the class Whitener method scale.

private DoubleMatrix2D scale(DoubleMatrix2D x, boolean scale) {
    for (int j = 0; j < x.columns(); j++) {
        DoubleArrayList u = new DoubleArrayList(x.viewColumn(j).toArray());
        double mean = Descriptive.mean(u);
        for (int i = 0; i < x.rows(); i++) {
            x.set(i, j, x.get(i, j) - mean);
        }
        if (scale) {
            double rms = rms(x.viewColumn(j));
            for (int i = 0; i < x.rows(); i++) {
                x.set(i, j, x.get(i, j) / rms);
            }
        }
    }
    return x;
}
Also used : DoubleArrayList(cern.colt.list.DoubleArrayList)

Example 79 with DoubleArrayList

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

the class DataUtils method standardizeData.

public static DoubleArrayList standardizeData(DoubleArrayList data) {
    DoubleArrayList data2 = new DoubleArrayList(data.size());
    double sum = 0.0;
    for (int i = 0; i < data.size(); i++) {
        sum += data.get(i);
    }
    double mean = sum / data.size();
    for (int i = 0; i < data.size(); i++) {
        data2.add(data.get(i) - mean);
    }
    double norm = 0.0;
    for (int i = 0; i < data2.size(); i++) {
        double v = data2.get(i);
        norm += v * v;
    }
    norm = Math.sqrt(norm / (data2.size() - 1));
    for (int i = 0; i < data2.size(); i++) {
        data2.set(i, data2.get(i) / norm);
    }
    return data2;
}
Also used : DoubleArrayList(cern.colt.list.DoubleArrayList)

Example 80 with DoubleArrayList

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

the class TestSemIm method testCholesky.

/**
 * The Cholesky decomposition of a symmetric, positive definite matrix
 * multiplied by the transpose of the Cholesky decomposition should be equal
 * to the original matrix itself.
 */
@Test
public void testCholesky() {
    Graph graph = constructGraph2();
    SemPm semPm = new SemPm(graph);
    SemIm semIm = new SemIm(semPm);
    DataSet dataSet = semIm.simulateData(500, false);
    TetradMatrix data = dataSet.getDoubleData();
    double[][] a = new double[data.columns()][data.columns()];
    for (int i = 0; i < data.columns(); i++) {
        for (int j = 0; j < data.columns(); j++) {
            DoubleArrayList icol = new DoubleArrayList(data.getColumn(i).toArray());
            DoubleArrayList jcol = new DoubleArrayList(data.getColumn(j).toArray());
            a[i][j] = Descriptive.covariance(icol, jcol);
        }
    }
    double[][] l = MatrixUtils.cholesky(a);
    double[][] lT = MatrixUtils.transpose(l);
    double[][] product = MatrixUtils.product(l, lT);
    assertTrue(MatrixUtils.equals(a, product, 1.e-10));
}
Also used : DataSet(edu.cmu.tetrad.data.DataSet) DoubleArrayList(cern.colt.list.DoubleArrayList) Test(org.junit.Test)

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