Java Code Examples for org.apache.commons.math3.stat.descriptive.moment.Variance#evaluate()

The following examples show how to use org.apache.commons.math3.stat.descriptive.moment.Variance#evaluate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestDoubleVariancePopAggregation.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
protected Number getExpectedValue(int start, int length)
{
    if (length == 0) {
        return null;
    }

    double[] values = new double[length];
    for (int i = 0; i < length; i++) {
        values[i] = start + i;
    }

    Variance variance = new Variance(false);
    return variance.evaluate(values);
}
 
Example 2
Source File: TestLongVariancePopAggregation.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
protected Number getExpectedValue(int start, int length)
{
    if (length == 0) {
        return null;
    }

    double[] values = new double[length];
    for (int i = 0; i < length; i++) {
        values[i] = start + i;
    }

    Variance variance = new Variance(false);
    return variance.evaluate(values);
}
 
Example 3
Source File: TestDoubleVarianceAggregation.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
protected Number getExpectedValue(int start, int length)
{
    if (length < 2) {
        return null;
    }

    double[] values = new double[length];
    for (int i = 0; i < length; i++) {
        values[i] = start + i;
    }

    Variance variance = new Variance();
    return variance.evaluate(values);
}
 
Example 4
Source File: TestLongVarianceAggregation.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
protected Number getExpectedValue(int start, int length)
{
    if (length < 2) {
        return null;
    }

    double[] values = new double[length];
    for (int i = 0; i < length; i++) {
        values[i] = start + i;
    }

    Variance variance = new Variance();
    return variance.evaluate(values);
}
 
Example 5
Source File: CorrelationExample.java    From Java-Data-Analysis with MIT License 5 votes vote down vote up
static double rho(double[][] data) {
    Variance v = new Variance();
    double varX = v.evaluate(data[0]);
    double sigX = Math.sqrt(varX);
    double varY = v.evaluate(data[1]);
    double sigY = Math.sqrt(varY);
    Covariance c = new Covariance(data);
    double sigXY = c.covariance(data[0], data[1]);
    return sigXY/(sigX*sigY);
}
 
Example 6
Source File: RandomInitHMM.java    From HMMRATAC with GNU General Public License v3.0 4 votes vote down vote up
private List<OpdfMultiGaussian> setVectorPDF(ArrayList<ObservationVector> obs){
	List<OpdfMultiGaussian> opdf = new ArrayList<OpdfMultiGaussian>();
	ObservationVector o = obs.get(0);
	int dim = o.dimension();
	double[] means = new double[dim];
	Mean mu = new Mean();
	Variance v = new Variance();
	double[] vars = new double[dim];
	double[] vals = new double[obs.size()];
	for (int i = 0;i < dim;i++){
		
		for (int a = 0;a < obs.size();a++){
			vals[a] = obs.get(a).value(i);
		}
		means[i] = mu.evaluate(vals, 0, vals.length);
		vars[i] = v.evaluate(vals);
	}
	double[][] Means = new double[numStates][dim];
	double[][] Vars = new double[numStates][dim];
	for (int i = 0; i < means.length;i++){
		double mean = means[i];
		double var = vars[i];
		
		double sd = Math.sqrt(var);
		double meanLower = mean - (3.0*sd);
		double meanUpper = mean + (3.0*sd);
		double varLower = 0.5 * var;
		double varUpper = 3 * var;
		
		
		double meanStep = (meanUpper - meanLower) / ((double)numStates - 1);
		double varStep = (varUpper - varLower) / ((double) numStates - 1);
		//System.out.println((meanUpper-meanLower)+"\t"+meanStep);
		for(int a = 0;a < numStates;a++){
			Means[a][i] = meanLower + (a * meanStep);
			Vars[a][i] = varLower + (a * varStep);
			//System.out.println(Means[a][i]);
			//System.out.println(Vars[a][i]);
		}
		
	}
	for (int i = 0;i < Means.length;i++){
		//System.out.println(Means.length);
		double[][] cov = new double[dim][dim];
		for (int a = 0;a < Means[i].length;a++){
			//System.out.println(Means[i].length);
			cov[a][a] = Vars[i][a];
			//System.out.println(cov[a][a]);
		}
		OpdfMultiGaussian pdf = new OpdfMultiGaussian(Means[i],cov);
		opdf.add(pdf);
	}
	
	return opdf;
	
}
 
Example 7
Source File: CovarianceTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Verify that diagonal entries are consistent with Variance computation and matrix matches
 * column-by-column covariances
 */
@Test
public void testConsistency() {
    final RealMatrix matrix = createRealMatrix(swissData, 47, 5);
    final RealMatrix covarianceMatrix = new Covariance(matrix).getCovarianceMatrix();

    // Variances on the diagonal
    Variance variance = new Variance();
    for (int i = 0; i < 5; i++) {
        Assert.assertEquals(variance.evaluate(matrix.getColumn(i)), covarianceMatrix.getEntry(i,i), 10E-14);
    }

    // Symmetry, column-consistency
    Assert.assertEquals(covarianceMatrix.getEntry(2, 3),
            new Covariance().covariance(matrix.getColumn(2), matrix.getColumn(3), true), 10E-14);
    Assert.assertEquals(covarianceMatrix.getEntry(2, 3), covarianceMatrix.getEntry(3, 2), Double.MIN_VALUE);

    // All columns same -> all entries = column variance
    RealMatrix repeatedColumns = new Array2DRowRealMatrix(47, 3);
    for (int i = 0; i < 3; i++) {
        repeatedColumns.setColumnMatrix(i, matrix.getColumnMatrix(0));
    }
    RealMatrix repeatedCovarianceMatrix = new Covariance(repeatedColumns).getCovarianceMatrix();
    double columnVariance = variance.evaluate(matrix.getColumn(0));
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            Assert.assertEquals(columnVariance, repeatedCovarianceMatrix.getEntry(i, j), 10E-14);
        }
    }

    // Check bias-correction defaults
    double[][] data = matrix.getData();
    TestUtils.assertEquals("Covariances",
            covarianceMatrix, new Covariance().computeCovarianceMatrix(data),Double.MIN_VALUE);
    TestUtils.assertEquals("Covariances",
            covarianceMatrix, new Covariance().computeCovarianceMatrix(data, true),Double.MIN_VALUE);

    double[] x = data[0];
    double[] y = data[1];
    Assert.assertEquals(new Covariance().covariance(x, y),
            new Covariance().covariance(x, y, true), Double.MIN_VALUE);
}
 
Example 8
Source File: CovarianceTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Verify that diagonal entries are consistent with Variance computation and matrix matches
 * column-by-column covariances
 */
@Test
public void testConsistency() {
    final RealMatrix matrix = createRealMatrix(swissData, 47, 5);
    final RealMatrix covarianceMatrix = new Covariance(matrix).getCovarianceMatrix();

    // Variances on the diagonal
    Variance variance = new Variance();
    for (int i = 0; i < 5; i++) {
        Assert.assertEquals(variance.evaluate(matrix.getColumn(i)), covarianceMatrix.getEntry(i,i), 10E-14);
    }

    // Symmetry, column-consistency
    Assert.assertEquals(covarianceMatrix.getEntry(2, 3),
            new Covariance().covariance(matrix.getColumn(2), matrix.getColumn(3), true), 10E-14);
    Assert.assertEquals(covarianceMatrix.getEntry(2, 3), covarianceMatrix.getEntry(3, 2), Double.MIN_VALUE);

    // All columns same -> all entries = column variance
    RealMatrix repeatedColumns = new Array2DRowRealMatrix(47, 3);
    for (int i = 0; i < 3; i++) {
        repeatedColumns.setColumnMatrix(i, matrix.getColumnMatrix(0));
    }
    RealMatrix repeatedCovarianceMatrix = new Covariance(repeatedColumns).getCovarianceMatrix();
    double columnVariance = variance.evaluate(matrix.getColumn(0));
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            Assert.assertEquals(columnVariance, repeatedCovarianceMatrix.getEntry(i, j), 10E-14);
        }
    }

    // Check bias-correction defaults
    double[][] data = matrix.getData();
    TestUtils.assertEquals("Covariances",
            covarianceMatrix, new Covariance().computeCovarianceMatrix(data),Double.MIN_VALUE);
    TestUtils.assertEquals("Covariances",
            covarianceMatrix, new Covariance().computeCovarianceMatrix(data, true),Double.MIN_VALUE);

    double[] x = data[0];
    double[] y = data[1];
    Assert.assertEquals(new Covariance().covariance(x, y),
            new Covariance().covariance(x, y, true), Double.MIN_VALUE);
}
 
Example 9
Source File: CovarianceTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Verify that diagonal entries are consistent with Variance computation and matrix matches
 * column-by-column covariances
 */
@Test
public void testConsistency() {
    final RealMatrix matrix = createRealMatrix(swissData, 47, 5);
    final RealMatrix covarianceMatrix = new Covariance(matrix).getCovarianceMatrix();

    // Variances on the diagonal
    Variance variance = new Variance();
    for (int i = 0; i < 5; i++) {
        Assert.assertEquals(variance.evaluate(matrix.getColumn(i)), covarianceMatrix.getEntry(i,i), 10E-14);
    }

    // Symmetry, column-consistency
    Assert.assertEquals(covarianceMatrix.getEntry(2, 3),
            new Covariance().covariance(matrix.getColumn(2), matrix.getColumn(3), true), 10E-14);
    Assert.assertEquals(covarianceMatrix.getEntry(2, 3), covarianceMatrix.getEntry(3, 2), Double.MIN_VALUE);

    // All columns same -> all entries = column variance
    RealMatrix repeatedColumns = new Array2DRowRealMatrix(47, 3);
    for (int i = 0; i < 3; i++) {
        repeatedColumns.setColumnMatrix(i, matrix.getColumnMatrix(0));
    }
    RealMatrix repeatedCovarianceMatrix = new Covariance(repeatedColumns).getCovarianceMatrix();
    double columnVariance = variance.evaluate(matrix.getColumn(0));
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            Assert.assertEquals(columnVariance, repeatedCovarianceMatrix.getEntry(i, j), 10E-14);
        }
    }

    // Check bias-correction defaults
    double[][] data = matrix.getData();
    TestUtils.assertEquals("Covariances",
            covarianceMatrix, new Covariance().computeCovarianceMatrix(data),Double.MIN_VALUE);
    TestUtils.assertEquals("Covariances",
            covarianceMatrix, new Covariance().computeCovarianceMatrix(data, true),Double.MIN_VALUE);

    double[] x = data[0];
    double[] y = data[1];
    Assert.assertEquals(new Covariance().covariance(x, y),
            new Covariance().covariance(x, y, true), Double.MIN_VALUE);
}
 
Example 10
Source File: CovarianceTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Verify that diagonal entries are consistent with Variance computation and matrix matches
 * column-by-column covariances
 */
@Test
public void testConsistency() {
    final RealMatrix matrix = createRealMatrix(swissData, 47, 5);
    final RealMatrix covarianceMatrix = new Covariance(matrix).getCovarianceMatrix();

    // Variances on the diagonal
    Variance variance = new Variance();
    for (int i = 0; i < 5; i++) {
        Assert.assertEquals(variance.evaluate(matrix.getColumn(i)), covarianceMatrix.getEntry(i,i), 10E-14);
    }

    // Symmetry, column-consistency
    Assert.assertEquals(covarianceMatrix.getEntry(2, 3),
            new Covariance().covariance(matrix.getColumn(2), matrix.getColumn(3), true), 10E-14);
    Assert.assertEquals(covarianceMatrix.getEntry(2, 3), covarianceMatrix.getEntry(3, 2), Double.MIN_VALUE);

    // All columns same -> all entries = column variance
    RealMatrix repeatedColumns = new Array2DRowRealMatrix(47, 3);
    for (int i = 0; i < 3; i++) {
        repeatedColumns.setColumnMatrix(i, matrix.getColumnMatrix(0));
    }
    RealMatrix repeatedCovarianceMatrix = new Covariance(repeatedColumns).getCovarianceMatrix();
    double columnVariance = variance.evaluate(matrix.getColumn(0));
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            Assert.assertEquals(columnVariance, repeatedCovarianceMatrix.getEntry(i, j), 10E-14);
        }
    }

    // Check bias-correction defaults
    double[][] data = matrix.getData();
    TestUtils.assertEquals("Covariances",
            covarianceMatrix, new Covariance().computeCovarianceMatrix(data),Double.MIN_VALUE);
    TestUtils.assertEquals("Covariances",
            covarianceMatrix, new Covariance().computeCovarianceMatrix(data, true),Double.MIN_VALUE);

    double[] x = data[0];
    double[] y = data[1];
    Assert.assertEquals(new Covariance().covariance(x, y),
            new Covariance().covariance(x, y, true), Double.MIN_VALUE);
}
 
Example 11
Source File: CovarianceTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Verify that diagonal entries are consistent with Variance computation and matrix matches
 * column-by-column covariances
 */
@Test
public void testConsistency() {
    final RealMatrix matrix = createRealMatrix(swissData, 47, 5);
    final RealMatrix covarianceMatrix = new Covariance(matrix).getCovarianceMatrix();

    // Variances on the diagonal
    Variance variance = new Variance();
    for (int i = 0; i < 5; i++) {
        Assert.assertEquals(variance.evaluate(matrix.getColumn(i)), covarianceMatrix.getEntry(i,i), 10E-14);
    }

    // Symmetry, column-consistency
    Assert.assertEquals(covarianceMatrix.getEntry(2, 3),
            new Covariance().covariance(matrix.getColumn(2), matrix.getColumn(3), true), 10E-14);
    Assert.assertEquals(covarianceMatrix.getEntry(2, 3), covarianceMatrix.getEntry(3, 2), Double.MIN_VALUE);

    // All columns same -> all entries = column variance
    RealMatrix repeatedColumns = new Array2DRowRealMatrix(47, 3);
    for (int i = 0; i < 3; i++) {
        repeatedColumns.setColumnMatrix(i, matrix.getColumnMatrix(0));
    }
    RealMatrix repeatedCovarianceMatrix = new Covariance(repeatedColumns).getCovarianceMatrix();
    double columnVariance = variance.evaluate(matrix.getColumn(0));
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            Assert.assertEquals(columnVariance, repeatedCovarianceMatrix.getEntry(i, j), 10E-14);
        }
    }

    // Check bias-correction defaults
    double[][] data = matrix.getData();
    TestUtils.assertEquals("Covariances",
            covarianceMatrix, new Covariance().computeCovarianceMatrix(data),Double.MIN_VALUE);
    TestUtils.assertEquals("Covariances",
            covarianceMatrix, new Covariance().computeCovarianceMatrix(data, true),Double.MIN_VALUE);

    double[] x = data[0];
    double[] y = data[1];
    Assert.assertEquals(new Covariance().covariance(x, y),
            new Covariance().covariance(x, y, true), Double.MIN_VALUE);
}
 
Example 12
Source File: CovarianceTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Verify that diagonal entries are consistent with Variance computation and matrix matches
 * column-by-column covariances
 */
@Test
public void testConsistency() {
    final RealMatrix matrix = createRealMatrix(swissData, 47, 5);
    final RealMatrix covarianceMatrix = new Covariance(matrix).getCovarianceMatrix();

    // Variances on the diagonal
    Variance variance = new Variance();
    for (int i = 0; i < 5; i++) {
        Assert.assertEquals(variance.evaluate(matrix.getColumn(i)), covarianceMatrix.getEntry(i,i), 10E-14);
    }

    // Symmetry, column-consistency
    Assert.assertEquals(covarianceMatrix.getEntry(2, 3),
            new Covariance().covariance(matrix.getColumn(2), matrix.getColumn(3), true), 10E-14);
    Assert.assertEquals(covarianceMatrix.getEntry(2, 3), covarianceMatrix.getEntry(3, 2), Double.MIN_VALUE);

    // All columns same -> all entries = column variance
    RealMatrix repeatedColumns = new Array2DRowRealMatrix(47, 3);
    for (int i = 0; i < 3; i++) {
        repeatedColumns.setColumnMatrix(i, matrix.getColumnMatrix(0));
    }
    RealMatrix repeatedCovarianceMatrix = new Covariance(repeatedColumns).getCovarianceMatrix();
    double columnVariance = variance.evaluate(matrix.getColumn(0));
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            Assert.assertEquals(columnVariance, repeatedCovarianceMatrix.getEntry(i, j), 10E-14);
        }
    }

    // Check bias-correction defaults
    double[][] data = matrix.getData();
    TestUtils.assertEquals("Covariances",
            covarianceMatrix, new Covariance().computeCovarianceMatrix(data),Double.MIN_VALUE);
    TestUtils.assertEquals("Covariances",
            covarianceMatrix, new Covariance().computeCovarianceMatrix(data, true),Double.MIN_VALUE);

    double[] x = data[0];
    double[] y = data[1];
    Assert.assertEquals(new Covariance().covariance(x, y),
            new Covariance().covariance(x, y, true), Double.MIN_VALUE);
}
 
Example 13
Source File: CovarianceTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Verify that diagonal entries are consistent with Variance computation and matrix matches
 * column-by-column covariances
 */
@Test
public void testConsistency() {
    final RealMatrix matrix = createRealMatrix(swissData, 47, 5);
    final RealMatrix covarianceMatrix = new Covariance(matrix).getCovarianceMatrix();

    // Variances on the diagonal
    Variance variance = new Variance();
    for (int i = 0; i < 5; i++) {
        Assert.assertEquals(variance.evaluate(matrix.getColumn(i)), covarianceMatrix.getEntry(i,i), 10E-14);
    }

    // Symmetry, column-consistency
    Assert.assertEquals(covarianceMatrix.getEntry(2, 3),
            new Covariance().covariance(matrix.getColumn(2), matrix.getColumn(3), true), 10E-14);
    Assert.assertEquals(covarianceMatrix.getEntry(2, 3), covarianceMatrix.getEntry(3, 2), Double.MIN_VALUE);

    // All columns same -> all entries = column variance
    RealMatrix repeatedColumns = new Array2DRowRealMatrix(47, 3);
    for (int i = 0; i < 3; i++) {
        repeatedColumns.setColumnMatrix(i, matrix.getColumnMatrix(0));
    }
    RealMatrix repeatedCovarianceMatrix = new Covariance(repeatedColumns).getCovarianceMatrix();
    double columnVariance = variance.evaluate(matrix.getColumn(0));
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            Assert.assertEquals(columnVariance, repeatedCovarianceMatrix.getEntry(i, j), 10E-14);
        }
    }

    // Check bias-correction defaults
    double[][] data = matrix.getData();
    TestUtils.assertEquals("Covariances",
            covarianceMatrix, new Covariance().computeCovarianceMatrix(data),Double.MIN_VALUE);
    TestUtils.assertEquals("Covariances",
            covarianceMatrix, new Covariance().computeCovarianceMatrix(data, true),Double.MIN_VALUE);

    double[] x = data[0];
    double[] y = data[1];
    Assert.assertEquals(new Covariance().covariance(x, y),
            new Covariance().covariance(x, y, true), Double.MIN_VALUE);
}
 
Example 14
Source File: CovarianceTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Verify that diagonal entries are consistent with Variance computation and matrix matches
 * column-by-column covariances
 */
@Test
public void testConsistency() {
    final RealMatrix matrix = createRealMatrix(swissData, 47, 5);
    final RealMatrix covarianceMatrix = new Covariance(matrix).getCovarianceMatrix();

    // Variances on the diagonal
    Variance variance = new Variance();
    for (int i = 0; i < 5; i++) {
        Assert.assertEquals(variance.evaluate(matrix.getColumn(i)), covarianceMatrix.getEntry(i,i), 10E-14);
    }

    // Symmetry, column-consistency
    Assert.assertEquals(covarianceMatrix.getEntry(2, 3),
            new Covariance().covariance(matrix.getColumn(2), matrix.getColumn(3), true), 10E-14);
    Assert.assertEquals(covarianceMatrix.getEntry(2, 3), covarianceMatrix.getEntry(3, 2), Double.MIN_VALUE);

    // All columns same -> all entries = column variance
    RealMatrix repeatedColumns = new Array2DRowRealMatrix(47, 3);
    for (int i = 0; i < 3; i++) {
        repeatedColumns.setColumnMatrix(i, matrix.getColumnMatrix(0));
    }
    RealMatrix repeatedCovarianceMatrix = new Covariance(repeatedColumns).getCovarianceMatrix();
    double columnVariance = variance.evaluate(matrix.getColumn(0));
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            Assert.assertEquals(columnVariance, repeatedCovarianceMatrix.getEntry(i, j), 10E-14);
        }
    }

    // Check bias-correction defaults
    double[][] data = matrix.getData();
    TestUtils.assertEquals("Covariances",
            covarianceMatrix, new Covariance().computeCovarianceMatrix(data),Double.MIN_VALUE);
    TestUtils.assertEquals("Covariances",
            covarianceMatrix, new Covariance().computeCovarianceMatrix(data, true),Double.MIN_VALUE);

    double[] x = data[0];
    double[] y = data[1];
    Assert.assertEquals(new Covariance().covariance(x, y),
            new Covariance().covariance(x, y, true), Double.MIN_VALUE);
}