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();
}
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);
}
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);
}
}
}
}
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 };
}
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;
}
Aggregations