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

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

Introduction

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

Prototype

public Variance() 

Source Link

Document

Constructs a Variance with default (true) isBiasCorrected property.

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./*from   ww w.jav a  2  s.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:cz.cuni.mff.d3s.spl.utils.StatisticsUtils.java

/** Compute variance of given data with bias correction.
 * //www  . ja v  a2  s  .  c  om
 * @param values Array of values to compute the variance from.
 * @return Varince of the provided values.
 */
public static double variance(double... values) {
    Variance var = new Variance();
    return var.evaluate(values);
}

From source file:br.unicamp.ic.recod.gpsi.measures.gpsiNormalBhattacharyyaDistanceScore.java

@Override
public double score(double[][][] input) {

    Mean mean = new Mean();
    Variance var = new Variance();

    double mu0, sigs0, mu1, sigs1;
    double dist[][] = new double[2][];

    dist[0] = MatrixUtils.createRealMatrix(input[0]).getColumn(0);
    dist[1] = MatrixUtils.createRealMatrix(input[1]).getColumn(0);

    mu0 = mean.evaluate(dist[0]);/*w  ww .  jav a 2s . co m*/
    sigs0 = var.evaluate(dist[0]) + Double.MIN_VALUE;
    mu1 = mean.evaluate(dist[1]);
    sigs1 = var.evaluate(dist[1]) + Double.MIN_VALUE;

    double distance = (Math.log((sigs0 / sigs1 + sigs1 / sigs0 + 2) / 4)
            + (Math.pow(mu1 - mu0, 2.0) / (sigs0 + sigs1))) / 4;

    return distance == Double.POSITIVE_INFINITY ? 0 : distance;

}

From source file:hyperheuristics.algorithm.moeadfrrmab.UCBSelectorVariance.java

protected double calcVariance(double[] values) {
    Variance variancia = new Variance();
    return variancia.evaluate(values);
}

From source file:com.facebook.presto.operator.aggregation.TestDoubleVarianceAggregation.java

@Override
public Number getExpectedValue(int start, int length) {
    if (length < 2) {
        return null;
    }/*w  w w  .  j  av  a  2  s .c o  m*/

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

    Variance variance = new Variance();
    return variance.evaluate(values);
}

From source file:GeMSE.GS.Analysis.Stats.TwoSampleTTestPanel.java

public TwoSampleTTestPanel() {
    initComponents();//from   w ww  .  j  ava 2 s  . c  om
    _sample1 = new double[0];
    _sample2 = new double[0];

    _decFor = new DecimalFormat("#.#########");
    _decFor.setRoundingMode(RoundingMode.CEILING);
    DecimalFormatSymbols decFors = _decFor.getDecimalFormatSymbols();
    decFors.setNaN("NaN");
    decFors.setInfinity("");
    _decFor.setDecimalFormatSymbols(decFors);

    _variance = new Variance();
    _studentTest = new TTest();
}

From source file:cz.cuni.mff.d3s.spl.data.BenchmarkRunSummary.java

/** Compute variance of the samples.
 * /*ww  w. j ava2 s  .co  m*/
 * @return Variance of the data in the original benchmark run.
 */
public synchronized double getVariance() {
    if (cacheVariance == null) {
        Variance mean = new Variance();
        cacheVariance = mean.evaluate(data);
    }
    return cacheVariance;
}

From source file:com.github.pitzcarraldo.dissimilar.Dissimilar.java

/**
 * Calculate the SSIM value on a window of pixels
 * @param pLumaOne luma values for first image
 * @param pLumaTwo luma values for second image
 * @param pWidth width of the two images
 * @param pHeight height of the two images
 * @param pWindowSize window size// w  w w  .  j a  v a  2s . com
 * @param pStartX start x coordinate for the window
 * @param pStartY start y coordinate for the window
 * @return SSIM for the window
 */
private static double calcSSIMOnWindow(final double[] pLumaOne, final double[] pLumaTwo, final int pWidth,
        final int pHeight, final int pWindowSize, final int pStartX, final int pStartY) {

    final double k1 = 0.01;
    final double k2 = 0.03;
    final double L = Math.pow(2, 8) - 1;//255 (at least for the moment all pixel values are 8-bit)
    final double c1 = Math.pow(k1 * L, 2);
    final double c2 = Math.pow(k2 * L, 2);

    final int windowWidth = ((pWindowSize + pStartX) > pWidth) ? (pWidth - pStartX) : (pWindowSize);
    final int windowHeight = ((pWindowSize + pStartY) > pHeight) ? (pHeight - pStartY) : (pWindowSize);

    if (windowWidth <= 0 || windowHeight <= 0) {
        System.out.println(pWidth + " " + pStartX + " " + windowWidth + " " + windowHeight);
        System.out.println(pHeight + " " + pStartY + " " + windowWidth + " " + windowHeight);
    }

    //store a temporary copy of the pixels
    double[] pixelsOne = new double[windowWidth * windowHeight];
    double[] pixelsTwo = new double[windowWidth * windowHeight];

    for (int h = 0; h < windowHeight; h++) {
        for (int w = 0; w < windowWidth; w++) {
            pixelsOne[(h * windowWidth) + w] = pLumaOne[(pStartY + h) * pWidth + pStartX + w];
            pixelsTwo[(h * windowWidth) + w] = pLumaTwo[(pStartY + h) * pWidth + pStartX + w];
        }
    }

    final double ux = new Mean().evaluate(pixelsOne, 0, pixelsOne.length);
    final double uy = new Mean().evaluate(pixelsTwo, 0, pixelsTwo.length);
    final double o2x = new Variance().evaluate(pixelsOne);
    final double o2y = new Variance().evaluate(pixelsTwo);
    final double oxy = new Covariance().covariance(pixelsOne, pixelsTwo);

    final double num = (2 * ux * uy + c1) * (2 * oxy + c2);
    final double den = (ux * ux + uy * uy + c1) * (o2x + o2y + c2);

    final double ssim = num / den;

    return ssim;
}

From source file:aml.filter.InteractiveSelector.java

private boolean disagreement(int sourceId, int targetId) {
    double[] signatureVector = new double[aligns.size()];
    int index = 0;
    for (Alignment a : aligns)
        signatureVector[index++] = a.getSimilarity(sourceId, targetId);

    Variance v = new Variance();
    double variance = v.evaluate(signatureVector);

    if (variance < 0.041) {
        previousAgreement = false;/*from  w w w  . ja  v a  2s.  c om*/
        previousFeedback = 0;
        previousSignatureVector = signatureVector;
        return false;
    } else if (previousAgreement == true && previousFeedback == -1
            && previousSignatureVector == signatureVector)
    //vectorsMatch(previousSignatureVector,signatureVector))
    {
        previousAgreement = true;
        previousFeedback = -1;
        previousSignatureVector = signatureVector;
        return false;
    } else {
        previousAgreement = true;
        previousSignatureVector = signatureVector;
        return true;
    }
}

From source file:com.github.pitzcarraldo.dissimilar.Dissimilar.java

/**
 * Calculate the SSIM; see http://en.wikipedia.org/wiki/Structural_similarity
 * @param pOne array of integer pixel values for first image
 * @param pTwo array of integer pixel values for second image
 * @param pWidth width of the two images
 * @param pHeight height of the two images
 * @param pGreyscale if the images are greyscale
 * @param pHeatMapFilename filename for the ssim heatmap image to be saved to (png)
 * @param pMin list to hold return value for ssim-minimum
 * @param pVariance list to hold return value for ssim-variance
 * @return SSIM//from w  ww  .  ja  v a  2s.c  om
 */
public static double calcSSIM(final int[] pOne, final int[] pTwo, final int pWidth, final int pHeight,
        final boolean pGreyscale, final String pHeatMapFilename, List<Double> pMin, List<Double> pVariance) {

    if (!checkPair(pOne, pTwo))
        return -1;

    double[] lumaOne = null;
    double[] lumaTwo = null;

    //if the image is greyscale then don't extract the luma
    if (pGreyscale) {
        System.out.println("=> Greyscale");
        lumaOne = new double[pOne.length];
        lumaTwo = new double[pTwo.length];
        for (int i = 0; i < lumaOne.length; i++) {
            //all rgb values are the same
            lumaOne[i] = (pOne[i] >> 0) & 0xFF;
            lumaTwo[i] = (pTwo[i] >> 0) & 0xFF;
        }
    } else {
        lumaOne = calcLuma(pOne);
        lumaTwo = calcLuma(pTwo);
    }

    final int windowSize = SSIMWINDOWSIZE;

    final int windowsH = (int) Math.ceil((double) pHeight / windowSize);
    final int windowsW = (int) Math.ceil((double) pWidth / windowSize);

    double[] mssim = new double[windowsH * windowsW];

    double mean = 0;
    double min = 1;

    for (int height = 0; height < windowsH; height++) {
        for (int width = 0; width < windowsW; width++) {
            final int window = (height * windowsW) + width;
            mssim[window] = calcSSIMOnWindow(lumaOne, lumaTwo, pWidth, pHeight, windowSize, width * windowSize,
                    height * windowSize);
            mean += mssim[window];
            if (mssim[window] < min) {
                min = mssim[window];
            }
        }
    }

    final double variance = new Variance().evaluate(mssim);

    mean /= (windowsH * windowsW);

    //if(variance>0.001) System.out.println("warning: high variance");

    if (null != pHeatMapFilename) {
        dumpSSIMHeatMap(mssim, windowsH, windowsW, pHeatMapFilename);
    }

    if (null != pMin) {
        pMin.add(0, new Double(min));
    }
    if (null != pVariance) {
        pVariance.add(0, new Double(variance));
    }

    return mean;
}