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:com.analog.lyric.dimple.test.model.TestJunctionTreeTransform.java

/**
 * Assert that source and target graphs in {@code transformMap} represent the same
 * joint distribution down to some level of precision.
 * /* w  w  w . j  a v a  2s . c om*/
 * @param transformMap
 */
@SuppressWarnings("null")
private void assertModelsEquivalent(JunctionTreeTransformMap transformMap) {
    if (transformMap.isIdentity()) {
        return;
    }

    final FactorGraph source = transformMap.source();
    final FactorGraph target = transformMap.target();

    GibbsSolver gibbs = new GibbsSolver();
    GibbsSolverGraph sourceGibbs = source.setSolverFactory(gibbs);
    GibbsSolverGraph targetGibbs = target.setSolverFactory(gibbs);
    targetGibbs.initialize();

    final int nSamples = 100;

    final double[] differences = new double[nSamples];
    for (int n = 0; n < nSamples; ++n) {
        // Generate a sample on the source graph
        source.solve();

        // Copy sample values to new graph
        for (Variable sourceVar : source.getVariables()) {
            Variable targetVar = transformMap.sourceToTargetVariable(sourceVar);

            ISolverVariableGibbs sourceSVar = sourceGibbs.getSolverVariable(sourceVar);
            ISolverVariableGibbs targetSVar = targetGibbs.getSolverVariable(targetVar);

            targetSVar.setCurrentSample(sourceSVar.getCurrentSampleValue());
        }

        // Update values of added variables
        for (AddedJointVariable<?> added : transformMap.addedJointVariables()) {
            final ISolverVariableGibbs addedSVar = targetGibbs.getSolverVariable(added.getVariable());
            final Value value = addedSVar.getCurrentSampleValue();
            final Value[] inputs = new Value[added.getInputCount()];
            for (int i = inputs.length; --i >= 0;) {
                final Variable inputVar = added.getInput(i);
                final ISolverVariableGibbs inputSVar = targetGibbs.getSolverVariable(inputVar);
                inputs[i] = inputSVar.getCurrentSampleValue();
            }

            added.updateValue(value, inputs);
        }

        // Compare the joint likelihoods
        final double sourceEnergy = sourceGibbs.getSampleScore();
        final double targetEnergy = targetGibbs.getSampleScore();
        final double difference = sourceEnergy - targetEnergy;
        if (Math.abs(difference) > 1e-10) {
            Misc.breakpoint();
        }
        differences[n] = difference;
    }

    double variance = new Variance().evaluate(differences);
    assertEquals(0.0, variance, 1e-10);
}

From source file:feature.lowLevel.audio.FeatureExtractor.java

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);//from w w  w  .j av a  2 s .c o  m

    // 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;
}

From source file:gamlss.utilities.WLSMultipleLinearRegression.java

/**
* Calculates the variance of the y values.
*
* @return Y variance or Double.NaN if no applicable
*///from  w w w .  j  ava  2  s.c  o  m
@Override
protected double calculateYVariance() {
    if (copyOriginal) {
        return new Variance().evaluate(y.toArray());
    }
    return Double.NaN;
}

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 .  ja v a 2s  .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  w w.  ja va2s. co 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.eqtlinteractionanalyser.eqtlinteractionanalyser.TestEQTLDatasetForInteractions.java

private void correctExpressionDataForInteractions(String[] covsToCorrect, ExpressionDataset datasetCovariates,
        ExpressionDataset datasetGenotypes, int nrSamples, ExpressionDataset datasetExpression,
        OLSMultipleLinearRegression regression, HashMultimap<String, String> qtlProbeSnpMultiMap)
        throws MathIllegalArgumentException, Exception {

    System.out.println("Correcting expression data for predefined gene environment interaction effects: "
            + Arrays.toString(covsToCorrect));
    int[] covsToCorrectIndex = new int[covsToCorrect.length];
    for (int c = 0; c < covsToCorrect.length; c++) {
        covsToCorrectIndex[c] = ((Integer) datasetCovariates.hashProbes.get(covsToCorrect[c])).intValue();

    }/*from  ww  w . jav a 2 s .  c o  m*/

    HashMap<String, Integer> snpMap = new HashMap<String, Integer>(datasetGenotypes.nrProbes);
    for (Map.Entry<String, Integer> snpEntry : datasetGenotypes.hashProbes.entrySet()) {
        try {
            snpMap.put(snpEntry.getKey().substring(0, snpEntry.getKey().indexOf('_')), snpEntry.getValue());
        } catch (Exception e) {
            System.out.println(snpEntry.getKey());
            throw e;
        }
    }

    Variance v = new Variance();

    for (int p = 0; p < datasetExpression.nrProbes; p++) {

        String probe = datasetExpression.probeNames[p].substring(0,
                datasetExpression.probeNames[p].lastIndexOf('_'));
        Set<String> probeQtls = qtlProbeSnpMultiMap.get(probe);

        if (probeQtls.isEmpty()) {
            throw new Exception("No eQTLs found for: " + probe);
        }

        int snpsInData = 0;
        HashSet<String> excludedSnps = new HashSet<String>();
        for (String snp : probeQtls) {

            Integer s = snpMap.get(snp);
            if (s != null) {

                if (v.evaluate(datasetGenotypes.rawData[s]) > 0) {
                    ++snpsInData;
                } else {
                    excludedSnps.add(snp);
                }

            }

        }

        //boolean foundPisS = false;
        double[][] valsX = new double[nrSamples][snpsInData + covsToCorrect.length * 2]; //store genotypes, covariates, interactions
        int k = 0;
        for (String snp : probeQtls) {

            if (excludedSnps.contains(snp)) {
                continue;
            }

            Integer s = snpMap.get(snp);
            if (s == null) {
                //throw new Exception("Snp " + snp + " not found");
                continue;
            }
            //            if(s.intValue() == p){
            //               foundPisS = true;
            //            }
            double[] snpData = datasetGenotypes.rawData[s];
            for (int i = 0; i < datasetGenotypes.nrSamples; ++i) {
                valsX[i][k] = snpData[i];
            }

            k++;
        }
        //         if(!foundPisS){
        //            
        //            System.out.println("Expected snp: " + datasetGenotypes.probeNames[p] + " at index: " + p);
        //            
        //            for(String qtlSnp : probeQtls = qtlProbeSnpMultiMap.get(probe)){
        //               System.out.println("QTL snp: " + qtlSnp + " found at index: " + snpMap.get(qtlSnp));
        //            }
        //            
        //            throw new Exception("Error 2");
        //         }
        for (int c = 0; c < covsToCorrect.length; c++) {
            double[] covData = datasetCovariates.rawData[covsToCorrectIndex[c]];
            double[] snpData = datasetGenotypes.rawData[p];

            for (int s = 0; s < nrSamples; s++) {
                valsX[s][c * 2 + snpsInData] = covData[s]; //covariate
                valsX[s][c * 2 + snpsInData + 1] = snpData[s] * covData[s]; //interction
            }
        }
        double[] valsY = datasetExpression.rawData[p];
        regression.newSampleData(valsY, valsX);
        try {
            datasetExpression.rawData[p] = regression.estimateResiduals();
        } catch (Exception up) {
            System.err.println(
                    "Error correcting for interactions: " + probe + " - " + datasetGenotypes.probeNames[p]);
        }
    }
}

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  w w .j a  va2s.com
        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.hawkular.datamining.forecast.stats.AutoCorrelationFunction.java

private static double[] evaluate(final double[] x, int maxLag, boolean correlation) {
    // max lag = length - 1
    if (maxLag >= x.length) {
        throw new IllegalArgumentException("Lag is higher than ");
    }/*from  www.j a  v a2 s . c om*/

    double mean = new Mean().evaluate(x);
    double var = correlation ? new Variance().evaluate(x, mean) : 1;
    int lengthForMean = correlation ? x.length - 1 : x.length;

    double[] acf = new double[maxLag + 1];
    for (int lag = 0; lag < maxLag + 1; lag++) {

        double sum = 0;
        for (int i = 0; i + lag < x.length; i++) {

            sum += (x[i] - mean) * (x[i + lag] - mean);
        }

        sum /= lengthForMean;
        acf[lag] = sum / var;
    }

    return acf;
}

From source file:pt.minha.calibration.AbstractBenchmark.java

protected Result stop(boolean interarrival) {
    double cpu = ((double) (mxbean.getCurrentThreadCpuTime() - cputime)) / idx;
    if (interarrival) {
        for (int i = idx - 1; i > 0; i--)
            times[i] -= times[i - 1];/*from   w ww .  ja  v  a2s  .  co m*/
        begin = 1;
    }
    Mean mean = new Mean();
    double m = mean.evaluate(times, begin, idx - begin);
    Variance var = new Variance();
    double v = var.evaluate(times, m, begin, idx - begin);
    return new Result(m, v, cpu);
}