本文整理汇总了Java中org.apache.commons.math3.stat.descriptive.moment.Variance类的典型用法代码示例。如果您正苦于以下问题:Java Variance类的具体用法?Java Variance怎么用?Java Variance使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Variance类属于org.apache.commons.math3.stat.descriptive.moment包,在下文中一共展示了Variance类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: compare
import org.apache.commons.math3.stat.descriptive.moment.Variance; //导入依赖的package包/类
@Override
public int compare(ClusterModelStats stats1, ClusterModelStats stats2) {
double[] stat1 = stats1.utilizationMatrix()[RawAndDerivedResource.LEADER_NW_IN.ordinal()];
double meanPreLeaderBytesIn = new Mean().evaluate(stat1, 0, stat1.length);
double threshold = meanPreLeaderBytesIn * _balancingConstraint.balancePercentage(Resource.NW_IN);
if (Arrays.stream(stat1).noneMatch(v -> v > threshold)) {
return 1;
}
double[] stat2 = stats2.utilizationMatrix()[RawAndDerivedResource.LEADER_NW_IN.ordinal()];
double variance1 = new Variance().evaluate(stat1);
double variance2 = new Variance().evaluate(stat2);
int result = AnalyzerUtils.compare(Math.sqrt(variance2), Math.sqrt(variance1), Resource.NW_IN);
if (result < 0) {
_reasonForLastNegativeResult = String.format("Violated leader bytes in balancing. preVariance: %.3f "
+ "postVariance: %.3f.", variance2, variance1);
}
return result;
}
示例2: getModel
import org.apache.commons.math3.stat.descriptive.moment.Variance; //导入依赖的package包/类
/**
* Berechne das AR-Modell indem das Yule-Walker Gleichungssystem aufgebaut und gelöst wird.
*
* @param timeSeries Die Zeitreihe, für die das Modell erstellt werden soll.
* @param p Der Grad des AR-Modells.
* @return Das AR-Modell für die Zeitreihe.
*/
@Override
public ARModel getModel(final double[] timeSeries, final int p) {
// Vgl. Schlittgen, Rainer & Streitberg, Bernd (2001). Zeitreihenanalyse (9. Aufl.) Seite 254 für das Yule-Walke-Gleichungssystem
if (p > timeSeries.length - 2) {
throw new IllegalArgumentException("Maximaler Grad für angegebene Zeitreihe ist " + (timeSeries.length - 2));
}
if (new Variance(false).evaluate(timeSeries) == 0) {
return new ARModel(new double[p], timeSeries);
}
// Baue das Yule-Walker Gleichungssystem auf
final RealMatrix yuleWalkerMatrix = buildYuleWalkerMatrix(timeSeries, p);
final RealVector coeffizientVector = new ArrayRealVector(p);
for (int i = 0; i < coeffizientVector.getDimension(); i++) {
coeffizientVector.setEntry(i, getAutocorrelation(timeSeries, i + 1));
}
//Löse das Gleichungsystem
final RealVector solved = new LUDecomposition(yuleWalkerMatrix).getSolver().solve(coeffizientVector);
return new ARModel(solved.toArray(), timeSeries);
}
示例3: score
import org.apache.commons.math3.stat.descriptive.moment.Variance; //导入依赖的package包/类
/** {@inheritDoc} */
@Override
public double score(final List<? extends Cluster<T>> clusters) {
double varianceSum = 0.0;
for (final Cluster<T> cluster : clusters) {
if (!cluster.getPoints().isEmpty()) {
final Clusterable center = centroidOf(cluster);
// compute the distance variance of the current cluster
final Variance stat = new Variance();
for (final T point : cluster.getPoints()) {
stat.increment(distance(point, center));
}
varianceSum += stat.getResult();
}
}
return varianceSum;
}
示例4: computeCovarianceMatrix
import org.apache.commons.math3.stat.descriptive.moment.Variance; //导入依赖的package包/类
/**
* Compute a covariance matrix from a matrix whose columns represent
* covariates.
* @param matrix input matrix (must have at least one column and two rows)
* @param biasCorrected determines whether or not covariance estimates are bias-corrected
* @return covariance matrix
* @throws MathIllegalArgumentException if the matrix does not contain sufficient data
*/
protected RealMatrix computeCovarianceMatrix(RealMatrix matrix, boolean biasCorrected)
throws MathIllegalArgumentException {
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), matrix.getColumn(j), biasCorrected);
outMatrix.setEntry(i, j, cov);
outMatrix.setEntry(j, i, cov);
}
outMatrix.setEntry(i, i, variance.evaluate(matrix.getColumn(i)));
}
return outMatrix;
}
示例5: score
import org.apache.commons.math3.stat.descriptive.moment.Variance; //导入依赖的package包/类
@Override
public double score(final List<? extends Cluster<T>> clusters) {
double varianceSum = 0.0;
for (final Cluster<T> cluster : clusters) {
if (!cluster.getPoints().isEmpty()) {
final Clusterable center = centroidOf(cluster);
// compute the distance variance of the current cluster
final Variance stat = new Variance();
for (final T point : cluster.getPoints()) {
stat.increment(distance(point, center));
}
varianceSum += stat.getResult();
}
}
return varianceSum;
}
示例6: testSliceSamplingOfMonotonicBetaDistribution
import org.apache.commons.math3.stat.descriptive.moment.Variance; //导入依赖的package包/类
/**
* Test slice sampling of a monotonic beta distribution as an example of sampling of a bounded random variable.
* Checks that input mean and variance are recovered by 10000 samples to a relative error of 0.5% and 2%,
* respectively.
*/
@Test
public void testSliceSamplingOfMonotonicBetaDistribution() {
rng.setSeed(RANDOM_SEED);
final double alpha = 10.;
final double beta = 1.;
final BetaDistribution betaDistribution = new BetaDistribution(alpha, beta);
final Function<Double, Double> betaLogPDF = betaDistribution::logDensity;
final double xInitial = 0.5;
final double xMin = 0.;
final double xMax = 1.;
final double width = 0.1;
final int numSamples = 10000;
final SliceSampler betaSampler = new SliceSampler(rng, betaLogPDF, xMin, xMax, width);
final double[] samples = Doubles.toArray(betaSampler.sample(xInitial, numSamples));
final double mean = betaDistribution.getNumericalMean();
final double variance = betaDistribution.getNumericalVariance();
final double sampleMean = new Mean().evaluate(samples);
final double sampleVariance = new Variance().evaluate(samples);
Assert.assertEquals(relativeError(sampleMean, mean), 0., 0.005);
Assert.assertEquals(relativeError(sampleVariance, variance), 0., 0.02);
}
示例7: testSliceSamplingOfPeakedBetaDistribution
import org.apache.commons.math3.stat.descriptive.moment.Variance; //导入依赖的package包/类
/**
* Test slice sampling of a peaked beta distribution as an example of sampling of a bounded random variable.
* Checks that input mean and variance are recovered by 10000 samples to a relative error of 0.5% and 2%,
* respectively.
*/
@Test
public void testSliceSamplingOfPeakedBetaDistribution() {
rng.setSeed(RANDOM_SEED);
final double alpha = 10.;
final double beta = 4.;
final BetaDistribution betaDistribution = new BetaDistribution(alpha, beta);
final Function<Double, Double> betaLogPDF = betaDistribution::logDensity;
final double xInitial = 0.5;
final double xMin = 0.;
final double xMax = 1.;
final double width = 0.1;
final int numSamples = 10000;
final SliceSampler betaSampler = new SliceSampler(rng, betaLogPDF, xMin, xMax, width);
final double[] samples = Doubles.toArray(betaSampler.sample(xInitial, numSamples));
final double mean = betaDistribution.getNumericalMean();
final double variance = betaDistribution.getNumericalVariance();
final double sampleMean = new Mean().evaluate(samples);
final double sampleVariance = new Variance().evaluate(samples);
Assert.assertEquals(relativeError(sampleMean, mean), 0., 0.005);
Assert.assertEquals(relativeError(sampleVariance, variance), 0., 0.02);
}
示例8: computeCovarianceMatrix
import org.apache.commons.math3.stat.descriptive.moment.Variance; //导入依赖的package包/类
/**
* Compute a covariance matrix from a matrix whose columns represent
* covariates.
* @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
* @throws MathIllegalArgumentException if the matrix does not contain sufficient data
*/
protected RealMatrix computeCovarianceMatrix(RealMatrix matrix, boolean biasCorrected)
throws MathIllegalArgumentException {
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), matrix.getColumn(j), biasCorrected);
outMatrix.setEntry(i, j, cov);
outMatrix.setEntry(j, i, cov);
}
outMatrix.setEntry(i, i, variance.evaluate(matrix.getColumn(i)));
}
return outMatrix;
}
示例9: getStatisticalSpectrumDescriptor
import org.apache.commons.math3.stat.descriptive.moment.Variance; //导入依赖的package包/类
private double[] getStatisticalSpectrumDescriptor(double[] dataRow) {
int N = dataRow.length;
double ssd[] = new double[N_STATISTICAL_DESCRIPTORS];
ssd[0] = new Mean().evaluate(dataRow);
ssd[1] = new Variance().evaluate(dataRow);
ssd[2] = new Skewness().evaluate(dataRow);
ssd[3] = new Kurtosis().evaluate(dataRow);
// NOTE: be careful, sort changes data!
// (as sonogram.getRow() above copies the data anyway, in this case there is no problem)
// (otherwise, use dataRow.clone(); )
Arrays.sort(dataRow);
// median
if (N % 2 == 0) {
ssd[4] = (dataRow[(N / 2) - 1] + dataRow[(N / 2)]) / 2;
} else {
ssd[4] = dataRow[(N - 1) / 2];
}
ssd[5] = dataRow[0]; // min value
ssd[6] = dataRow[N - 1]; // max value
return ssd;
}
示例10: estBetaDist
import org.apache.commons.math3.stat.descriptive.moment.Variance; //导入依赖的package包/类
public static double[] estBetaDist(double[] betaValues) {
Mean mean = new Mean();
double mu = mean.evaluate(betaValues,0,betaValues.length);
Variance variance = new Variance();
double var = variance.evaluate(betaValues, mu);
double alpha = -mu*(var+mu*mu-mu)/var;
double beta = (mu-1)*(var+mu*mu-mu)/var;
return new double[] {alpha, beta, mu, FastMath.sqrt(var)};
}
示例11: getPointFromLargestVarianceCluster
import org.apache.commons.math3.stat.descriptive.moment.Variance; //导入依赖的package包/类
/**
* 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
*/
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()));
}
示例12: getPointFromLargestVarianceCluster
import org.apache.commons.math3.stat.descriptive.moment.Variance; //导入依赖的package包/类
/**
* 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
*/
private T getPointFromLargestVarianceCluster(final Collection<Cluster<T>> clusters)
throws ConvergenceException {
double maxVariance = Double.NEGATIVE_INFINITY;
Cluster<T> selected = null;
for (final Cluster<T> cluster : clusters) {
if (!cluster.getPoints().isEmpty()) {
// compute the distance variance of the current cluster
final T center = cluster.getCenter();
final Variance stat = new Variance();
for (final T point : cluster.getPoints()) {
stat.increment(point.distanceFrom(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()));
}
示例13: testSummaryConsistency
import org.apache.commons.math3.stat.descriptive.moment.Variance; //导入依赖的package包/类
@Test
public void testSummaryConsistency() {
final DescriptiveStatistics dstats = new DescriptiveStatistics();
final SummaryStatistics sstats = new SummaryStatistics();
final int windowSize = 5;
dstats.setWindowSize(windowSize);
final double tol = 1E-12;
for (int i = 0; i < 20; i++) {
dstats.addValue(i);
sstats.clear();
double[] values = dstats.getValues();
for (int j = 0; j < values.length; j++) {
sstats.addValue(values[j]);
}
TestUtils.assertEquals(dstats.getMean(), sstats.getMean(), tol);
TestUtils.assertEquals(new Mean().evaluate(values), dstats.getMean(), tol);
TestUtils.assertEquals(dstats.getMax(), sstats.getMax(), tol);
TestUtils.assertEquals(new Max().evaluate(values), dstats.getMax(), tol);
TestUtils.assertEquals(dstats.getGeometricMean(), sstats.getGeometricMean(), tol);
TestUtils.assertEquals(new GeometricMean().evaluate(values), dstats.getGeometricMean(), tol);
TestUtils.assertEquals(dstats.getMin(), sstats.getMin(), tol);
TestUtils.assertEquals(new Min().evaluate(values), dstats.getMin(), tol);
TestUtils.assertEquals(dstats.getStandardDeviation(), sstats.getStandardDeviation(), tol);
TestUtils.assertEquals(dstats.getVariance(), sstats.getVariance(), tol);
TestUtils.assertEquals(new Variance().evaluate(values), dstats.getVariance(), tol);
TestUtils.assertEquals(dstats.getSum(), sstats.getSum(), tol);
TestUtils.assertEquals(new Sum().evaluate(values), dstats.getSum(), tol);
TestUtils.assertEquals(dstats.getSumsq(), sstats.getSumsq(), tol);
TestUtils.assertEquals(new SumOfSquares().evaluate(values), dstats.getSumsq(), tol);
TestUtils.assertEquals(dstats.getPopulationVariance(), sstats.getPopulationVariance(), tol);
TestUtils.assertEquals(new Variance(false).evaluate(values), dstats.getPopulationVariance(), tol);
}
}
示例14: testOverrideVarianceWithMathClass
import org.apache.commons.math3.stat.descriptive.moment.Variance; //导入依赖的package包/类
/**
* JIRA: MATH-691
*/
@Test
public void testOverrideVarianceWithMathClass() {
double[] scores = {1, 2, 3, 4};
SummaryStatistics stats = new SummaryStatistics();
stats.setVarianceImpl(new Variance(false)); //use "population variance"
for(double i : scores) {
stats.addValue(i);
}
Assert.assertEquals((new Variance(false)).evaluate(scores),stats.getVariance(), 0);
}
示例15: rowStdDevs
import org.apache.commons.math3.stat.descriptive.moment.Variance; //导入依赖的package包/类
/**
* Calculates the standard deviation per row from a matrix.
* @param matrix the input matrix.
* @return never {@code null}, an array with as many positions as rows in {@code matrix}.
* @throws IllegalArgumentException if {@code matrix} is {@code null}.
*/
public static double[] rowStdDevs(final RealMatrix matrix) {
Utils.nonNull(matrix);
final Variance varianceEvaluator = new Variance();
return IntStream.range(0, matrix.getRowDimension())
.mapToDouble(r -> Math.sqrt(varianceEvaluator.evaluate(matrix.getRow(r)))).toArray();
}