Search in sources :

Example 16 with DoubleArrayList

use of cern.colt.list.DoubleArrayList in project onebusaway-application-modules by camsys.

the class RealTimeHistoryServiceImpl method noNans.

private double[] noNans(double[] values) {
    DoubleArrayList vs = new DoubleArrayList();
    for (double v : values) {
        if (!Double.isNaN(v))
            vs.add(v);
    }
    vs.trimToSize();
    return vs.elements();
}
Also used : DoubleArrayList(cern.colt.list.DoubleArrayList)

Example 17 with DoubleArrayList

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

the class LingamPattern2 method getScore1.

// =============================PRIVATE METHODS=========================//
private Score getScore1(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 absoluteStandardizedResiduals = new TetradMatrix(totalSampleSize, numCols);
    for (int i = 0; i < nodes.size(); i++) {
        List<Double> _absoluteStandardizedResiduals = new ArrayList<>();
        for (int j = 0; j < data.size(); j++) {
            Node _target = nodes.get(i);
            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);
            }
            RegressionResult result = regressions.get(j).regress(target, regressors);
            TetradVector residualsColumn = result.getResiduals();
            DoubleArrayList _absoluteStandardizedResidualsColumn = new DoubleArrayList(residualsColumn.toArray());
            double mean = Descriptive.mean(_absoluteStandardizedResidualsColumn);
            double std = Descriptive.standardDeviation(Descriptive.variance(_absoluteStandardizedResidualsColumn.size(), Descriptive.sum(_absoluteStandardizedResidualsColumn), Descriptive.sumOfSquares(_absoluteStandardizedResidualsColumn)));
            for (int i2 = 0; i2 < _absoluteStandardizedResidualsColumn.size(); i2++) {
                _absoluteStandardizedResidualsColumn.set(i2, (_absoluteStandardizedResidualsColumn.get(i2) - mean) / std);
                _absoluteStandardizedResidualsColumn.set(i2, Math.abs(_absoluteStandardizedResidualsColumn.get(i2)));
            }
            for (int k = 0; k < _absoluteStandardizedResidualsColumn.size(); k++) {
                _absoluteStandardizedResiduals.add(_absoluteStandardizedResidualsColumn.get(k));
            }
        }
        DoubleArrayList absoluteStandardResidualsList = new DoubleArrayList(absoluteStandardizedResiduals.getColumn(i).toArray());
        for (int k = 0; k < _absoluteStandardizedResiduals.size(); k++) {
            absoluteStandardizedResiduals.set(k, i, _absoluteStandardizedResiduals.get(k));
        }
        double _mean = Descriptive.mean(absoluteStandardResidualsList);
        double diff = _mean - Math.sqrt(2.0 / Math.PI);
        score += diff * diff;
    }
    for (int j = 0; j < absoluteStandardizedResiduals.columns(); j++) {
        double[] x = absoluteStandardizedResiduals.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 18 with DoubleArrayList

use of cern.colt.list.DoubleArrayList in project Gemma by PavlidisLab.

the class ExpressionDataSVD method imputeMissing.

/**
 * Simple imputation method. Generally (but not always), missing values correspond to "low expression". Therefore
 * imputed values of zero are defensible. However, because at this point the matrix has probably already been
 * filtered, the row mean is better.
 */
private void imputeMissing(DoubleMatrix<CompositeSequence, BioMaterial> matrix) {
    /*
         * keep track of the missing values so they can be re-masked later.
         */
    missingValueInfo = new DenseDoubleMatrix2D(matrix.rows(), matrix.columns());
    for (int i = 0; i < matrix.rows(); i++) {
        DoubleArrayList v = new DoubleArrayList(matrix.getRow(i));
        double m = DescriptiveWithMissing.mean(v);
        for (int j = 0; j < matrix.columns(); j++) {
            double d = matrix.get(i, j);
            if (Double.isNaN(d)) {
                missingValueInfo.set(i, j, Double.NaN);
                matrix.set(i, j, m);
            } else {
                missingValueInfo.set(i, j, 1.0);
            }
        }
    }
}
Also used : DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) DoubleArrayList(cern.colt.list.DoubleArrayList)

Example 19 with DoubleArrayList

use of cern.colt.list.DoubleArrayList in project Gemma by PavlidisLab.

the class ComBat method itSol.

private DoubleMatrix1D[] itSol(DoubleMatrix2D matrix, DoubleMatrix1D gHat, DoubleMatrix1D dHat, double gbar, double t2b, double a, double b) throws ComBatException {
    DoubleMatrix1D n = this.rowNonMissingCounts(matrix);
    DoubleMatrix1D gold = gHat;
    DoubleMatrix1D dold = dHat;
    final double conv = 0.0001;
    double change = 1.0;
    int count = 0;
    int MAXITERS = 500;
    while (change > conv) {
        DoubleMatrix1D gnew = this.postMean(gHat, gbar, n, dold, t2b);
        DoubleMatrix1D sum2 = this.stepSum(matrix, gnew);
        DoubleMatrix1D dnew = this.postVar(sum2, n, a, b);
        DoubleMatrix1D gnewtmp = gnew.copy().assign(gold, Functions.minus).assign(Functions.abs).assign(gold, Functions.div);
        DoubleMatrix1D dnewtmp = dnew.copy().assign(dold, Functions.minus).assign(Functions.abs).assign(dold, Functions.div);
        double gnewmax;
        double dnewmax;
        if (hasMissing) {
            gnewmax = DescriptiveWithMissing.max(new DoubleArrayList(gnewtmp.toArray()));
            dnewmax = DescriptiveWithMissing.max(new DoubleArrayList(dnewtmp.toArray()));
        } else {
            gnewmax = gnewtmp.aggregate(Functions.max, Functions.identity);
            dnewmax = dnewtmp.aggregate(Functions.max, Functions.identity);
        }
        change = Math.max(gnewmax, dnewmax);
        gold = gnew;
        dold = dnew;
        if (count++ > MAXITERS) {
            /*
                 * For certain data sets, we just flail around; for example if there are only two samples. This is a
                 * bailout for exceptional circumstances.
                 */
            throw new ComBatException("Failed to converge within " + MAXITERS + " iterations, last delta was " + String.format("%.2g", change));
        }
    }
    return new DoubleMatrix1D[] { gold, dold };
}
Also used : DoubleMatrix1D(cern.colt.matrix.DoubleMatrix1D) DenseDoubleMatrix1D(cern.colt.matrix.impl.DenseDoubleMatrix1D) DoubleArrayList(cern.colt.list.DoubleArrayList)

Example 20 with DoubleArrayList

use of cern.colt.list.DoubleArrayList in project Gemma by PavlidisLab.

the class ComBat method bPrior.

private DoubleArrayList bPrior(DoubleMatrix2D d) {
    DoubleArrayList result = new DoubleArrayList();
    for (int i = 0; i < d.rows(); i++) {
        DoubleArrayList dd = new DoubleArrayList(d.viewRow(i).toArray());
        double mean = DescriptiveWithMissing.mean(dd);
        double var = DescriptiveWithMissing.sampleVariance(dd, mean);
        result.add((mean * var + Math.pow(mean, 3)) / var);
    }
    return result;
}
Also used : DoubleArrayList(cern.colt.list.DoubleArrayList)

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