use of edu.cmu.tetrad.util.TetradMatrix 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 edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class Mimbuild2 method impliedCovariance.
private TetradMatrix impliedCovariance(int[][] indicatorIndices, double[][] loadings, TetradMatrix cov, TetradMatrix loadingscov, double[] delta) {
TetradMatrix implied = new TetradMatrix(cov.rows(), cov.columns());
for (int i = 0; i < loadings.length; i++) {
for (int j = 0; j < loadings.length; j++) {
for (int k = 0; k < loadings[i].length; k++) {
for (int l = 0; l < loadings[j].length; l++) {
double prod = loadings[i][k] * loadings[j][l] * loadingscov.get(i, j);
implied.set(indicatorIndices[i][k], indicatorIndices[j][l], prod);
}
}
}
}
for (int i = 0; i < implied.rows(); i++) {
implied.set(i, i, implied.get(i, i) + delta[i]);
}
return implied;
}
use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class Mimbuild2 method getCov.
private TetradMatrix getCov(ICovarianceMatrix _measurescov, List<Node> latents, Node[][] indicators) {
if (latents.size() != indicators.length) {
throw new IllegalArgumentException();
}
TetradMatrix measurescov = _measurescov.getMatrix();
TetradMatrix latentscov = new TetradMatrix(latents.size(), latents.size());
for (int i = 0; i < latentscov.rows(); i++) {
for (int j = i; j < latentscov.columns(); j++) {
if (i == j)
latentscov.set(i, j, 1.0);
else {
double v = .5;
latentscov.set(i, j, v);
latentscov.set(j, i, v);
}
}
}
double[][] loadings = new double[indicators.length][];
for (int i = 0; i < indicators.length; i++) {
loadings[i] = new double[indicators[i].length];
}
for (int i = 0; i < indicators.length; i++) {
loadings[i] = new double[indicators[i].length];
for (int j = 0; j < indicators[i].length; j++) {
loadings[i][j] = .5;
}
}
int[][] indicatorIndices = new int[indicators.length][];
List<Node> measures = _measurescov.getVariables();
for (int i = 0; i < indicators.length; i++) {
indicatorIndices[i] = new int[indicators[i].length];
for (int j = 0; j < indicators[i].length; j++) {
indicatorIndices[i][j] = measures.indexOf(indicators[i][j]);
}
}
// Variances of the measures.
double[] delta = new double[measurescov.rows()];
for (int i = 0; i < delta.length; i++) {
delta[i] = 1;
}
int numNonMeasureVarianceParams = 0;
for (int i = 0; i < latentscov.rows(); i++) {
for (int j = i; j < latentscov.columns(); j++) {
numNonMeasureVarianceParams++;
}
}
for (int i = 0; i < indicators.length; i++) {
numNonMeasureVarianceParams += indicators[i].length;
}
double[] allParams1 = getAllParams(indicators, latentscov, loadings, delta);
optimizeNonMeasureVariancesQuick(indicators, measurescov, latentscov, loadings, indicatorIndices);
// for (int i = 0; i < 10; i++) {
// optimizeNonMeasureVariancesConditionally(indicators, measurescov, latentscov, loadings, indicatorIndices, delta);
// optimizeMeasureVariancesConditionally(measurescov, latentscov, loadings, indicatorIndices, delta);
//
// double[] allParams2 = getAllParams(indicators, latentscov, loadings, delta);
// if (distance(allParams1, allParams2) < epsilon) break;
// allParams1 = allParams2;
// }
this.numParams = allParams1.length;
// // Very slow but could be done alone.
optimizeAllParamsSimultaneously(indicators, measurescov, latentscov, loadings, indicatorIndices, delta);
double N = _measurescov.getSampleSize();
int p = _measurescov.getDimension();
int df = (p) * (p + 1) / 2 - (numParams);
double x = (N - 1) * minimum;
this.pValue = 1.0 - new ChiSquaredDistribution(df).cumulativeProbability(x);
return latentscov;
}
use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class MimbuildTrek method impliedCovariance.
private TetradMatrix impliedCovariance(int[][] indicatorIndices, double[][] loadings, TetradMatrix cov, TetradMatrix loadingscov, double[] delta) {
TetradMatrix implied = new TetradMatrix(cov.rows(), cov.columns());
for (int i = 0; i < loadings.length; i++) {
for (int j = 0; j < loadings.length; j++) {
for (int k = 0; k < loadings[i].length; k++) {
for (int l = 0; l < loadings[j].length; l++) {
double prod = loadings[i][k] * loadings[j][l] * loadingscov.get(i, j);
implied.set(indicatorIndices[i][k], indicatorIndices[j][l], prod);
}
}
}
}
for (int i = 0; i < implied.rows(); i++) {
implied.set(i, i, implied.get(i, i) + delta[i]);
}
return implied;
}
use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class MimbuildTrek method getCov.
private TetradMatrix getCov(ICovarianceMatrix _measurescov, List<Node> latents, Node[][] indicators) {
if (latents.size() != indicators.length) {
throw new IllegalArgumentException();
}
TetradMatrix measurescov = _measurescov.getMatrix();
TetradMatrix latentscov = new TetradMatrix(latents.size(), latents.size());
for (int i = 0; i < latentscov.rows(); i++) {
for (int j = i; j < latentscov.columns(); j++) {
if (i == j)
latentscov.set(i, j, 1.0);
else {
double v = .5;
latentscov.set(i, j, v);
latentscov.set(j, i, v);
}
}
}
double[][] loadings = new double[indicators.length][];
for (int i = 0; i < indicators.length; i++) {
loadings[i] = new double[indicators[i].length];
}
for (int i = 0; i < indicators.length; i++) {
loadings[i] = new double[indicators[i].length];
for (int j = 0; j < indicators[i].length; j++) {
loadings[i][j] = .5;
}
}
int[][] indicatorIndices = new int[indicators.length][];
List<Node> measures = _measurescov.getVariables();
for (int i = 0; i < indicators.length; i++) {
indicatorIndices[i] = new int[indicators[i].length];
for (int j = 0; j < indicators[i].length; j++) {
indicatorIndices[i][j] = measures.indexOf(indicators[i][j]);
}
}
// Variances of the measures.
double[] delta = new double[measurescov.rows()];
for (int i = 0; i < delta.length; i++) {
delta[i] = 1;
}
int numNonMeasureVarianceParams = 0;
for (int i = 0; i < latentscov.rows(); i++) {
for (int j = i; j < latentscov.columns(); j++) {
numNonMeasureVarianceParams++;
}
}
for (int i = 0; i < indicators.length; i++) {
numNonMeasureVarianceParams += indicators[i].length;
}
double[] allParams1 = getAllParams(indicators, latentscov, loadings, delta);
optimizeNonMeasureVariancesQuick(indicators, measurescov, latentscov, loadings, indicatorIndices);
// for (int i = 0; i < 10; i++) {
// optimizeNonMeasureVariancesConditionally(indicators, measurescov, latentscov, loadings, indicatorIndices, delta);
// optimizeMeasureVariancesConditionally(measurescov, latentscov, loadings, indicatorIndices, delta);
//
// double[] allParams2 = getAllParams(indicators, latentscov, loadings, delta);
// if (distance(allParams1, allParams2) < epsilon) break;
// allParams1 = allParams2;
// }
this.numParams = allParams1.length;
// // Very slow but could be done alone.
optimizeAllParamsSimultaneously(indicators, measurescov, latentscov, loadings, indicatorIndices, delta);
double N = _measurescov.getSampleSize();
int p = _measurescov.getDimension();
int df = (p) * (p + 1) / 2 - (numParams);
double x = (N - 1) * minimum;
this.pValue = 1.0 - new ChiSquaredDistribution(df).cumulativeProbability(x);
return latentscov;
}
Aggregations