Search in sources :

Example 96 with TetradMatrix

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);
}
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 97 with TetradMatrix

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;
}
Also used : TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 98 with TetradMatrix

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;
}
Also used : ChiSquaredDistribution(org.apache.commons.math3.distribution.ChiSquaredDistribution) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 99 with TetradMatrix

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;
}
Also used : TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 100 with TetradMatrix

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;
}
Also used : ChiSquaredDistribution(org.apache.commons.math3.distribution.ChiSquaredDistribution) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Aggregations

TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)161 TetradVector (edu.cmu.tetrad.util.TetradVector)46 ArrayList (java.util.ArrayList)43 Node (edu.cmu.tetrad.graph.Node)41 List (java.util.List)12 CovarianceMatrix (edu.cmu.tetrad.data.CovarianceMatrix)10 DepthChoiceGenerator (edu.cmu.tetrad.util.DepthChoiceGenerator)9 SingularMatrixException (org.apache.commons.math3.linear.SingularMatrixException)9 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)8 RegressionResult (edu.cmu.tetrad.regression.RegressionResult)8 Test (org.junit.Test)8 Regression (edu.cmu.tetrad.regression.Regression)7 RegressionDataset (edu.cmu.tetrad.regression.RegressionDataset)7 SemIm (edu.cmu.tetrad.sem.SemIm)7 Graph (edu.cmu.tetrad.graph.Graph)6 SemPm (edu.cmu.tetrad.sem.SemPm)6 Vector (java.util.Vector)6 DoubleArrayList (cern.colt.list.DoubleArrayList)5 DataSet (edu.cmu.tetrad.data.DataSet)5 ICovarianceMatrix (edu.cmu.tetrad.data.ICovarianceMatrix)5