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