Example usage for org.apache.commons.math3.stat.descriptive.moment Variance increment

List of usage examples for org.apache.commons.math3.stat.descriptive.moment Variance increment

Introduction

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

Prototype

@Override
public void increment(final double d) 

Source Link

Document

If all values are available, it is more accurate to use #evaluate(double[]) rather than adding values one at a time using this method and then executing #getResult , since evaluate leverages the fact that is has the full list of values together to execute a two-pass algorithm.

Usage

From source file:eu.crisis_economics.abm.model.configuration.LogNormalDistributionModelParameterConfiguration.java

/**
  * A lightweight test for this {@link ConfigurationComponent}. This snippet
  * creates a {@link Parameter}{@code <Double>} using an instance of
  * {@link LogNormalDistributionModelParameterConfiguration} and then tests
  * whether the logarithm of {@link Double} values drawn from this {@link Parameter}
  * have the expected mean./*w  w  w. j a va2s  . co  m*/
  */
public static void main(String[] args) {
    {
        final LogNormalDistributionModelParameterConfiguration configuration = new LogNormalDistributionModelParameterConfiguration();
        configuration.setMu(5.0);
        configuration.setSigma(0.1);
        final ModelParameter<Double> distribution = configuration.createInjector()
                .getInstance(Key.get(new TypeLiteral<ModelParameter<Double>>() {
                }));
        double mean = 0.;
        final int numSamples = 10000000;
        for (int i = 0; i < numSamples; ++i) {
            final double value = Math.log(distribution.get());
            mean += value;
        }
        mean /= numSamples;
        Assert.assertTrue(Math.abs(mean - 5.) < 1.e-3);
    }
    {
        final LogNormalDistributionModelParameterConfiguration configuration = LogNormalDistributionModelParameterConfiguration
                .create(5., .1);
        final ModelParameter<Double> distribution = configuration.createInjector()
                .getInstance(Key.get(new TypeLiteral<ModelParameter<Double>>() {
                }));
        final Variance variance = new Variance();
        double mean = 0.;
        final int numSamples = 10000000;
        for (int i = 0; i < numSamples; ++i) {
            final double value = distribution.get();
            mean += value;
            variance.increment(value);
        }
        mean /= numSamples;
        final double observedSigma = Math.sqrt(variance.getResult());
        Assert.assertTrue(Math.abs(mean - 5.) < 1.e-3);
        Assert.assertTrue(Math.abs(observedSigma - .1) < 1.e-3);
    }
}

From source file:Clustering.technique.KMeansPlusPlusClusterer.java

/**
 * Get a random point from the {@link Cluster} with the largest distance variance.
 *
 * @param clusters the {@link Cluster}s to search
 * @return a random point from the selected cluster
 * @throws ConvergenceException if clusters are all empty
 *///  w ww  .j ava  2s.c  om
private T getPointFromLargestVarianceCluster(final Collection<CentroidCluster<T>> clusters)
        throws ConvergenceException {

    double maxVariance = Double.NEGATIVE_INFINITY;
    Cluster<T> selected = null;
    for (final CentroidCluster<T> cluster : clusters) {
        if (!cluster.getPoints().isEmpty()) {

            // compute the distance variance of the current cluster
            final Clusterable center = cluster.getCenter();
            final Variance stat = new Variance();
            for (final T point : cluster.getPoints()) {
                stat.increment(distance(point, center));
            }
            final double variance = stat.getResult();

            // select the cluster with the largest variance
            if (variance > maxVariance) {
                maxVariance = variance;
                selected = cluster;
            }

        }
    }

    // did we find at least one non-empty cluster ?
    if (selected == null) {
        throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS);
    }

    // extract a random point from the cluster
    final List<T> selectedPoints = selected.getPoints();
    return selectedPoints.remove(random.nextInt(selectedPoints.size()));

}

From source file:nars.concept.util.BeliefClusterer.java

/**
 * Get a random point from the {@link Cluster} with the largest distance variance.
 *
 * @param clusters the {@link Cluster}s to search
 * @return a random point from the selected cluster
 * @throws ConvergenceException if clusters are all empty
 *///from  w  w  w .  ja  va2 s .c  o  m
@NotNull
private ArrayRealVector getPointFromLargestVarianceCluster(@NotNull final Collection<Cluster> clusters)
        throws ConvergenceException {

    double maxVariance = Double.NEGATIVE_INFINITY;
    Cluster selected = null;
    for (final Cluster cluster : clusters) {
        if (!cluster.getPoints().isEmpty()) {

            // compute the distance variance of the current cluster
            final ArrayRealVector center = cluster.pos;
            final Variance stat = new Variance();
            for (final T point : cluster.getPoints()) {
                stat.increment(distance(point, center.getDataRef()));
            }
            final double variance = stat.getResult();

            // select the cluster with the largest variance
            if (variance > maxVariance) {
                maxVariance = variance;
                selected = cluster;
            }

        }
    }

    // did we find at least one non-empty cluster ?
    if (selected == null) {
        throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS);
    }

    // extract a random point from the cluster
    final List<T> selectedPoints = selected.getPoints();
    return p(selectedPoints.remove(random.nextInt(selectedPoints.size())));

}

From source file:nl.systemsgenetics.genenetworkbackend.hpo.TestDiseaseGenePerformance.java

private static HashMap<String, MeanSd> calculatePathayMeansOfAnnotatedGenes(
        DoubleMatrixDataset<String, String> predictionMatrixSignificant,
        DoubleMatrixDataset<String, String> annotationMatrixSignificant) {

    HashMap<String, MeanSd> pathwayMeanSdMap = new HashMap<>(predictionMatrixSignificant.columns());

    Mean meanCalculator = new Mean();
    Variance varianceCalculator = new Variance();

    for (String pathway : predictionMatrixSignificant.getColObjects()) {

        meanCalculator.clear();/*  w  ww  .  ja v  a 2 s.  co  m*/
        varianceCalculator.clear();

        DoubleMatrix1D pathwayPredictions = predictionMatrixSignificant.getCol(pathway);
        DoubleMatrix1D pathwayAnnotations = annotationMatrixSignificant.getCol(pathway);

        for (int g = 0; g < pathwayPredictions.size(); ++g) {
            if (pathwayAnnotations.get(g) != 0) {
                meanCalculator.increment(pathwayPredictions.getQuick(g));
                varianceCalculator.increment(pathwayPredictions.getQuick(g));
            }
        }

        double v = varianceCalculator.getResult();

        pathwayMeanSdMap.put(pathway, new MeanSd(meanCalculator.getResult(), v * v));

    }

    return pathwayMeanSdMap;

}