Example usage for org.apache.commons.math.stat.descriptive.moment Variance evaluate

List of usage examples for org.apache.commons.math.stat.descriptive.moment Variance evaluate

Introduction

In this page you can find the example usage for org.apache.commons.math.stat.descriptive.moment Variance evaluate.

Prototype

@Override
public double evaluate(final double[] values) 

Source Link

Document

Returns the variance of the entries in the input array, or Double.NaN if the array is empty.

Usage

From source file:com.discursive.jccook.math.StatExample.java

public static void main(String[] args) {
    double[] values = new double[] { 2.3, 5.4, 6.2, 7.3, 23.3 };

    System.out.println("min: " + StatUtils.min(values));
    System.out.println("max: " + StatUtils.max(values));
    System.out.println("mean: " + StatUtils.mean(values));
    System.out.println("product: " + StatUtils.product(values));
    System.out.println("sum: " + StatUtils.sum(values));
    System.out.println("variance: " + StatUtils.variance(values));

    // Measures from previous example
    Min min = new Min();
    System.out.println("min: " + min.evaluate(values));
    Max max = new Max();
    System.out.println("max: " + max.evaluate(values));
    Mean mean = new Mean();
    System.out.println("mean: " + mean.evaluate(values));
    Product product = new Product();
    System.out.println("product: " + product.evaluate(values));
    Sum sum = new Sum();
    System.out.println("sum: " + sum.evaluate(values));
    Variance variance = new Variance();
    System.out.println("variance: " + variance.evaluate(values));

    // New measures
    Percentile percentile = new Percentile();
    System.out.println("80 percentile value: " + percentile.evaluate(values, 80.0));
    GeometricMean geoMean = new GeometricMean();
    System.out.println("geometric mean: " + geoMean.evaluate(values));
    StandardDeviation stdDev = new StandardDeviation();
    System.out.println("standard dev: " + stdDev.evaluate(values));
    Skewness skewness = new Skewness();
    System.out.println("skewness: " + skewness.evaluate(values));
    Kurtosis kurtosis = new Kurtosis();
    System.out.println("kurtosis: " + kurtosis.evaluate(values));

}

From source file:laboGrid.graphs.replication.ReplicationGraphHeuristicGenerator.java

protected static void balanceMatrix(int[][] edges, int[] peerSize, double stopCriterion) {
    double[] normIn = new double[peerSize.length];
    for (int i = 0; i < peerSize.length; ++i) {
        for (int j = 0; j < peerSize.length; ++j) {
            normIn[j] += edges[i][j];//from w w w.  j  a v a 2 s  . c om
        }
    }
    for (int j = 0; j < peerSize.length; ++j) {
        normIn[j] /= peerSize[j];
    }
    Variance v = new Variance();
    double var1 = v.evaluate(normIn);
    System.out.println("Initial variance on DA incoming edges is " + var1);
    double var2 = 0;

    int k = 0;
    int maxJ = 1, minJ = 1;
    boolean updateVar1 = false;
    while ((var1 - var2) >= stopCriterion) {

        // First line (to ignore element (0,0))
        double maxNorm = normIn[1], minNorm = normIn[1];
        maxJ = 1;
        minJ = 1;
        for (int j = 1; j < peerSize.length; ++j) {
            if (normIn[j] > maxNorm) {
                maxJ = j;
                maxNorm = normIn[j];
            }

            if (normIn[j] < minNorm) {
                minJ = j;
                minNorm = normIn[j];
            }
        }

        if (minJ != maxJ) {
            balanceRow(0, edges, peerSize, minJ, maxJ, normIn);
        }

        // Other lines
        for (int i = 1; i < peerSize.length; ++i) {
            maxNorm = normIn[0];
            minNorm = normIn[0];
            maxJ = 0;
            minJ = 0;
            for (int j = 0; j < peerSize.length; ++j) {
                if (normIn[j] > maxNorm && i != j) {
                    maxJ = j;
                    maxNorm = normIn[j];
                }

                if (normIn[j] < minNorm && i != j) {
                    minJ = j;
                    minNorm = normIn[j];
                }
            }

            if (minJ != maxJ) {
                balanceRow(i, edges, peerSize, minJ, maxJ, normIn);
            }
        }

        double tmpVar = v.evaluate(normIn);
        if (updateVar1)
            var1 = var2;
        else
            updateVar1 = true;
        var2 = tmpVar;

        System.out.println(
                "After pass " + k + ", variance on DA incoming edges is " + var2 + " (vs. " + var1 + ")");
        ++k;

    }
}

From source file:com.clican.pluto.dataprocess.dpl.function.impl.Beta.java

private double getBeta(double[] referValueList, double[] estimateValueList) {
    Variance var = new Variance(false);
    Covariance cov = new Covariance();
    double varValue = var.evaluate(referValueList);
    double covValue = cov.covariance(referValueList, estimateValueList, false);
    if (log.isDebugEnabled()) {
        log.debug("Covariance=[" + covValue + "],Variance=[" + varValue + "]");
    }// w  w w  .java2  s  .com
    Double beta = covValue / varValue;
    return beta;
}

From source file:Covariance.java

/**
 * Compute a covariance matrix from a matrix whose columns represent 
 * covariates. /*from   w w w. jav a 2s .  c  o m*/
 * @param matrix input matrix (must have at least two columns and two rows) 
 * @param biasCorrected determines whether or not covariance estimates are bias-corrected 
 * @return covariance matrix 
 */
protected RealMatrix computeCovarianceMatrix(RealMatrix matrix, boolean biasCorrected) {
    int dimension = matrix.getColumnDimension();
    Variance variance = new Variance(biasCorrected);
    RealMatrix outMatrix = new BlockRealMatrix(dimension, dimension);
    for (int i = 0; i < dimension; i++) {
        for (int j = 0; j < i; j++) {
            double cov = covariance(matrix.getColumn(i), i, matrix.getColumn(j), j, biasCorrected);
            outMatrix.setEntry(i, j, cov);
            outMatrix.setEntry(j, i, cov);
        }
        outMatrix.setEntry(i, i, variance.evaluate(matrix.getColumn(i)));
    }
    return outMatrix;
}

From source file:org.beedraz.semantics_II.MathBugDemonstration.java

/**
 * @param args//  w w w. j  av a  2 s .com
 */
public static void main(String[] args) {

    // difference between getResult and evaluate
    //      double[] values = new double[] {1.0, 2.0, Double.POSITIVE_INFINITY};
    //      Variance var1 = new Variance();
    //      double value1 = var1.evaluate(values);
    //      Variance var2 = new Variance();
    //      var2.incrementAll(values);
    //      double value2 = var2.getResult();
    //      System.out.println(value1);
    //      System.out.println(value2);

    // difference between getResult and evaluate
    double[] values = new double[] { 1.0, 2.0, Double.NEGATIVE_INFINITY };
    Variance var1 = new Variance();
    double value1 = var1.evaluate(values);
    Variance var2 = new Variance();
    var2.incrementAll(values);
    double value2 = var2.getResult();
    var2.evaluate(values);
    System.out.println(value1);
    System.out.println(value2);

    //    // evaluate method changes internal representation
    //    double[] values = new double[] {1.0, 2.0, Double.NEGATIVE_INFINITY};
    //    Variance var1 = new Variance();
    //    var1.incrementAll(values);
    //    System.out.println(var1.getN());
    //    System.out.println(var1.getResult());
    //    double value1 = var1.evaluate(values);
    //    System.out.println(var1.getN());
    //    System.out.println(var1.getResult());
    //    System.out.println(value1);

}

From source file:org.gwaspi.statistics.ChiSqrBoundaryCalculator.java

protected static void calculateChisqrBoundaryByVariance() throws IOException, MathException {

    FileWriter repFW = new FileWriter(boundaryPath);
    BufferedWriter repBW = new BufferedWriter(repFW);

    NetcdfFile ncfile = NetcdfFile.open(netCDFFile);
    List<Dimension> dims = ncfile.getDimensions();
    Dimension sizeDim = dims.get(0);
    Dimension simsDim = dims.get(1);

    String varName = "distributions";
    Variable distributions = ncfile.findVariable(varName);

    try {/*from w ww.j  a  v a 2s.  com*/
        for (int i = 0; i < pointsNb; i++) {
            // distributions(i:i:1, 0:simsNb:1)
            ArrayDouble.D2 rdDoubleArrayD2 = (ArrayDouble.D2) distributions
                    .read(i + ":" + i + ":1, 0:" + (simsDim.getLength() - 1) + ":1");
            ArrayDouble.D1 rdDoubleArrayD1 = (D1) rdDoubleArrayD2.reduce();

            double sampleSize = rdDoubleArrayD2.getSize();
            double currentTot = 0;

            double[] allValues = new double[(int) sampleSize];
            for (int j = 0; j < sampleSize; j++) {
                allValues[j] = rdDoubleArrayD1.get(j);
                currentTot += rdDoubleArrayD1.get(j);
            }

            Variance variance = new Variance();
            double varianceValue = variance.evaluate(allValues);

            double currentAvg = currentTot / simNb;

            double low95 = currentAvg - varianceValue;
            double top95 = currentAvg + varianceValue;

            StringBuilder sb = new StringBuilder();
            sb.append(top95);
            sb.append(",");
            sb.append(currentAvg);
            sb.append(",");
            sb.append(low95);
            repBW.append(sb + "\n");
        }
    } catch (IOException ex) {
        log.error("Cannot read data", ex);
    } catch (InvalidRangeException ex) {
        log.error("Cannot read data", ex);
    }

    repBW.close();
    repFW.close();

    log.info("Confidence boundary created for {} points", N);
}