Example usage for org.apache.commons.math3.stat.correlation StorelessCovariance getData

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

Introduction

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

Prototype

public double[][] getData() throws NumberIsTooSmallException 

Source Link

Document

Return the covariance matrix as two-dimensional array.

Usage

From source file:joinery.impl.Aggregation.java

public static <V> DataFrame<Number> cov(final DataFrame<V> df) {
    DataFrame<Number> num = df.numeric();
    StorelessCovariance cov = new StorelessCovariance(num.size());

    // row-wise copy to double array and increment
    double[] data = new double[num.size()];
    for (List<Number> row : num) {
        for (int i = 0; i < row.size(); i++) {
            data[i] = row.get(i).doubleValue();
        }/*  w  w w .java  2  s  . co  m*/
        cov.increment(data);
    }

    // row-wise copy results into new data frame
    double[][] result = cov.getData();
    DataFrame<Number> r = new DataFrame<>(num.columns());
    List<Number> row = new ArrayList<>(num.size());
    for (int i = 0; i < result.length; i++) {
        row.clear();
        for (int j = 0; j < result[i].length; j++) {
            row.add(result[i][j]);
        }
        r.append(row);
    }

    return r;
}

From source file:org.drugis.mtc.summary.MCMCMultivariateNormalSummary.java

private void calculateResults() {
    if (!isReady()) {
        return;//from  w  w w. ja  v  a2 s  .c  o m
    }
    List<List<Double>> sampleCache = new ArrayList<List<Double>>();
    for (int i = 0; i < getParameters().length; ++i) {
        List<Double> samples = SummaryUtil.getAllChainsLastHalfSamples(d_results, getParameters()[i]);
        sampleCache.add(samples);
        d_means[i] = SummaryUtil.evaluate(new Mean(), samples);
    }
    StorelessCovariance cov = new StorelessCovariance(getParameters().length);
    double[] rowData = new double[getParameters().length];
    for (int row = 0; row < sampleCache.get(0).size(); ++row) {
        for (int col = 0; col < getParameters().length; ++col) {
            rowData[col] = sampleCache.get(col).get(row);
        }
        cov.increment(rowData);
    }
    d_covMatrix = cov.getData();
    boolean wasDefined = d_isDefined;
    d_isDefined = true;
    firePropertyChange(PROPERTY_DEFINED, wasDefined, d_isDefined);
    firePropertyChange(PROPERTY_MEAN_VECTOR, null, d_means);
    firePropertyChange(PROPERTY_COVARIANCE_MATRIX, null, d_covMatrix);
}