List of usage examples for org.apache.commons.math3.stat.descriptive.moment Variance getResult
@Override public double getResult()
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./*from w w w .ja v a 2s. 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:creative.framework.main.ApparelContext.java
private StringBuilder variancesToString(List<Variance> variances) { StringBuilder v = new StringBuilder(); NumberFormat formatter = new DecimalFormat("#0.00"); for (Variance variance : variances) { v.append(formatter.format(variance.getResult())).append(" "); }/*from w ww . j a va 2s . c o m*/ return v; }
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 w w .j a v a 2 s .co m 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 ww.j ava 2 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();//from w w w . jav a 2s .c o 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; }
From source file:org.apereo.portal.events.aggr.stat.JpaStatisticalSummary.java
/** * Returns the <a href="http://en.wikibooks.org/wiki/Statistics/Summary/Variance"> * population variance</a> of the values that have been added. * * <p>Double.NaN is returned if no values have been added.</p> * * @return the population variance//ww w. j a v a 2s . com */ public double getPopulationVariance() { Variance populationVariance = new Variance(_getSecondMoment()); populationVariance.setBiasCorrected(false); return populationVariance.getResult(); }
From source file:org.eclipse.dataset.AbstractDataset.java
@Override public Number variance(boolean isDatasetWholePopulation) { SummaryStatistics stats = getStatistics(false); if (isDatasetWholePopulation) { Variance newVar = (Variance) stats.getVarianceImpl().copy(); newVar.setBiasCorrected(false);/*from w ww . jav a2 s . c om*/ return newVar.getResult(); } return stats.getVariance(); }