Example usage for org.apache.commons.math3.stat.correlation Covariance Covariance

List of usage examples for org.apache.commons.math3.stat.correlation Covariance Covariance

Introduction

In this page you can find the example usage for org.apache.commons.math3.stat.correlation Covariance Covariance.

Prototype

public Covariance(RealMatrix matrix, boolean biasCorrected) throws MathIllegalArgumentException 

Source Link

Document

Create a covariance matrix from a matrix whose columns represent covariates.

Usage

From source file:bigdataproject.PCA.java

public double[][] reduceDimensions() {
    BlockRealMatrix matrix = new BlockRealMatrix(dataSet);
    Covariance cov = new Covariance(matrix, false);
    RealMatrix covarianceMatrix = cov.getCovarianceMatrix();
    EigenDecomposition dec = new EigenDecomposition(covarianceMatrix);
    RealVector principalEigenVector = dec.getEigenvector(0);
    RealVector secondEigenVector = dec.getEigenvector(1);
    BlockRealMatrix pca = new BlockRealMatrix(principalEigenVector.getDimension(), 2);
    pca.setColumnVector(0, principalEigenVector);
    pca.setColumnVector(1, secondEigenVector);
    BlockRealMatrix pcaTranspose = pca.transpose();
    BlockRealMatrix columnVectorMatrix = matrix.transpose();
    BlockRealMatrix matrix2D = pcaTranspose.multiply(columnVectorMatrix);
    return matrix2D.getData();
}

From source file:GeMSE.GS.Analysis.Stats.OneSampleCovariancePanel.java

private void RunAnalysis() {
    if (_data == null)
        return;//from ww w.j  av  a2s . c  om
    _covariance = new Covariance(_data, _biasCorrected);
    double[][] matrix = (_covariance.getCovarianceMatrix()).getData();
    Plot(matrix);
    GridView(matrix);
}

From source file:edu.cmu.tetrad.search.ConditionalGaussianLikelihood.java

private Ret getJointLikelihood(List<ContinuousVariable> X, List<DiscreteVariable> A) {
    int p = X.size();

    //        List<List<Integer>> cells = getCellsOriginal(A);
    List<List<Integer>> cells = adTree.getCellLeaves(A);

    int[] continuousCols = new int[p];
    for (int j = 0; j < p; j++)
        continuousCols[j] = nodesHash.get(X.get(j));
    int N = dataSet.getNumRows();
    double lik = 0;

    for (List<Integer> cell : cells) {
        int r = cell.size();

        if (A.size() > 0) {
            if (r > 0) {
                double prob = r / (double) N;
                lik += r * Math.log(prob);
            }// ww w . j a va2s.  com
        }

        if (X.size() > 0) {
            if (r > 3 * p) {
                TetradMatrix subset = new TetradMatrix(r, p);

                for (int i = 0; i < r; i++) {
                    for (int j = 0; j < p; j++) {
                        subset.set(i, j, continuousData[continuousCols[j]][cell.get(i)]);
                    }
                }

                TetradMatrix Sigma = new TetradMatrix(
                        new Covariance(subset.getRealMatrix(), false).getCovarianceMatrix());
                double det = Sigma.det();
                lik -= 0.5 * r * Math.log(det);
            }

            lik -= 0.5 * r * p * (1.0 + Math.log(2.0 * Math.PI));
        }
    }

    int dof = f(A) * h(X) + f(A);

    return new Ret(lik, dof);
}

From source file:org.meteoinfo.math.stats.StatsUtil.java

/**
 * Computes covariances for pairs of arrays or columns of a matrix.
 *
 * @param x X data//from  ww  w  . ja  v a 2  s  . com
 * @param y Y data
 * @param bias If true, returned value will be bias-corrected
 * @return The covariance matrix
 */
public static Array cov(Array x, Array y, boolean bias) {
    int m = x.getShape()[0];
    int n = 1;
    if (x.getRank() == 2)
        n = x.getShape()[1];
    double[][] aa = new double[m][n * 2];
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n * 2; j++) {
            if (j < n) {
                aa[i][j] = x.getDouble(i * n + j);
            } else {
                aa[i][j] = y.getDouble(i * n + j - n);
            }
        }
    }
    RealMatrix matrix = new Array2DRowRealMatrix(aa, false);
    Covariance cov = new Covariance(matrix, bias);
    RealMatrix mcov = cov.getCovarianceMatrix();
    m = mcov.getColumnDimension();
    n = mcov.getRowDimension();
    Array r = Array.factory(DataType.DOUBLE, new int[] { m, n });
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            r.setDouble(i * n + j, mcov.getEntry(i, j));
        }
    }

    return r;
}