Example usage for org.apache.commons.math3.stat.descriptive DescriptiveStatistics getMean

List of usage examples for org.apache.commons.math3.stat.descriptive DescriptiveStatistics getMean

Introduction

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

Prototype

public double getMean() 

Source Link

Document

Returns the <a href="http://www.xycoon.com/arithmetic_mean.htm"> arithmetic mean </a> of the available values

Usage

From source file:com.github.jessemull.microflex.stat.statbiginteger.MeanBigIntegerWeightsTest.java

/**
 * Tests set calculation.//ww  w  .ja va2 s.co  m
 */
@Test
public void testSet() {

    for (PlateBigInteger plate : array) {

        Map<WellBigInteger, BigDecimal> resultMap = new TreeMap<WellBigInteger, BigDecimal>();
        Map<WellBigInteger, BigDecimal> returnedMap = mean.set(plate.dataSet(), weights, mc);

        for (WellBigInteger well : plate) {

            double[] input = new double[well.size()];
            int index = 0;

            for (BigInteger bi : well) {
                input[index] = bi.doubleValue() * weights[index];
                index++;
            }

            DescriptiveStatistics stat = new DescriptiveStatistics(input);
            double resultDouble = stat.getMean();

            BigDecimal result = new BigDecimal(resultDouble, mc);

            resultMap.put(well, result);
        }

        for (WellBigInteger well : plate) {

            BigDecimal result = resultMap.get(well);
            BigDecimal returned = returnedMap.get(well);

            BigDecimal[] corrected = correctRoundingErrors(result, returned);

            assertEquals(corrected[0], corrected[1]);
        }
    }

}

From source file:com.github.jessemull.microflexbigdecimal.stat.MeanWeightsTest.java

/**
 * Tests the plate statistics method.//w  w w .j  a  v a  2  s  . c  om
 */
@Test
public void testPlate() {

    for (Plate plate : array) {

        Map<Well, BigDecimal> resultMap = new TreeMap<Well, BigDecimal>();
        Map<Well, BigDecimal> returnedMap = mean.plate(plate, weights, mc);

        for (Well well : plate) {

            double[] input = new double[well.size()];
            int index = 0;

            for (BigDecimal bd : well) {
                input[index] = bd.doubleValue() * weights[index];
                index++;
            }

            DescriptiveStatistics stat = new DescriptiveStatistics(input);
            double resultDouble = stat.getMean();

            BigDecimal result = new BigDecimal(resultDouble, mc);

            resultMap.put(well, result);
        }

        for (Well well : plate) {

            BigDecimal result = resultMap.get(well);
            BigDecimal returned = returnedMap.get(well);

            BigDecimal[] corrected = correctRoundingErrors(result, returned);
            assertEquals(corrected[0], corrected[1]);
        }
    }
}

From source file:com.github.jessemull.microflexbiginteger.stat.MeanWeightsTest.java

/**
 * Tests set calculation.//from   w  w  w. j  ava 2 s .c o m
 */
@Test
public void testSet() {

    for (Plate plate : array) {

        Map<Well, BigDecimal> resultMap = new TreeMap<Well, BigDecimal>();
        Map<Well, BigDecimal> returnedMap = mean.set(plate.dataSet(), weights, mc);

        for (Well well : plate) {

            double[] input = new double[well.size()];
            int index = 0;

            for (BigInteger bi : well) {
                input[index] = bi.doubleValue() * weights[index];
                index++;
            }

            DescriptiveStatistics stat = new DescriptiveStatistics(input);
            double resultDouble = stat.getMean();

            BigDecimal result = new BigDecimal(resultDouble, mc);

            resultMap.put(well, result);
        }

        for (Well well : plate) {

            BigDecimal result = resultMap.get(well);
            BigDecimal returned = returnedMap.get(well);

            BigDecimal[] corrected = correctRoundingErrors(result, returned);

            assertEquals(corrected[0], corrected[1]);
        }
    }

}

From source file:com.github.jessemull.microflex.stat.statbigdecimal.MeanBigDecimalWeightsTest.java

/**
 * Tests set calculation./*from   w w  w  . java 2s  .  co  m*/
 */
@Test
public void testSet() {

    for (PlateBigDecimal plate : array) {

        Map<WellBigDecimal, BigDecimal> resultMap = new TreeMap<WellBigDecimal, BigDecimal>();
        Map<WellBigDecimal, BigDecimal> returnedMap = mean.set(plate.dataSet(), weights, mc);

        for (WellBigDecimal well : plate) {

            double[] input = new double[well.size()];
            int index = 0;

            for (BigDecimal bd : well) {
                input[index] = bd.doubleValue() * weights[index];
                index++;
            }

            DescriptiveStatistics stat = new DescriptiveStatistics(input);
            double resultDouble = stat.getMean();

            BigDecimal result = new BigDecimal(resultDouble, mc);

            resultMap.put(well, result);
        }

        for (WellBigDecimal well : plate) {

            BigDecimal result = resultMap.get(well);
            BigDecimal returned = returnedMap.get(well);

            BigDecimal[] corrected = correctRoundingErrors(result, returned);

            assertEquals(corrected[0], corrected[1]);
        }
    }

}

From source file:com.github.jessemull.microflexbigdecimal.stat.MeanWeightsTest.java

/**
 * Tests set calculation.// ww w  . j a  v a  2s  . co m
 */
@Test
public void testSet() {

    for (Plate plate : array) {

        Map<Well, BigDecimal> resultMap = new TreeMap<Well, BigDecimal>();
        Map<Well, BigDecimal> returnedMap = mean.set(plate.dataSet(), weights, mc);

        for (Well well : plate) {

            double[] input = new double[well.size()];
            int index = 0;

            for (BigDecimal bd : well) {
                input[index] = bd.doubleValue() * weights[index];
                index++;
            }

            DescriptiveStatistics stat = new DescriptiveStatistics(input);
            double resultDouble = stat.getMean();

            BigDecimal result = new BigDecimal(resultDouble, mc);

            resultMap.put(well, result);
        }

        for (Well well : plate) {

            BigDecimal result = resultMap.get(well);
            BigDecimal returned = returnedMap.get(well);

            BigDecimal[] corrected = correctRoundingErrors(result, returned);

            assertEquals(corrected[0], corrected[1]);
        }
    }

}

From source file:gr.iti.mklab.reveal.forensics.maps.dq.DQExtractor.java

public void detectDQDiscontinuities() {

    int imWidth = dcts.length;
    int imHeight = dcts[0].length;

    int[] p_h_avg = new int[maxCoeffs];
    int[] p_h_fft = new int[maxCoeffs];
    int[] p_final = new int[maxCoeffs];

    double[][] pTampered = new double[maxCoeffs][];
    double[][] pUntampered = new double[maxCoeffs][];

    for (int coeffIndex = 0; coeffIndex < maxCoeffs; coeffIndex++) {

        int coe = coeff[coeffIndex];
        int startY = coe % 8 - 1;
        if (startY == -1) {
            startY = 8;//  www  . jav  a  2s  .  c o m
        }
        int startX = (int) Math.floor((coe - 1) / 8);

        List<Integer> selectedCoeffs = new ArrayList<Integer>();
        for (int ii = startX; ii < imWidth; ii += 8) {
            for (int jj = startY; jj < imHeight; jj += 8) {
                selectedCoeffs.add(dcts[ii][jj]);
            }
        }

        int minCoeffValue = Collections.min(selectedCoeffs);
        int maxCoeffValue = Collections.max(selectedCoeffs);
        int s_0;
        Double[] coeffHist = new Double[0];
        if (maxCoeffValue - minCoeffValue > 0) {
            //will be a power of 2 to allow for fft (zero padded)
            int trueHistRange = maxCoeffValue - minCoeffValue + 1;
            //int histLength = trueHistRange;
            int histLength = (int) Math.pow(2, Math.ceil(Math.log(trueHistRange) / Math.log(2)));

            coeffHist = new Double[histLength];

            for (int ii = 0; ii < coeffHist.length; ii++) {
                coeffHist[ii] = 0.0;
            }

            for (Integer selectedCoeff : selectedCoeffs) {
                coeffHist[selectedCoeff - minCoeffValue] += 1;
            }

            List<Double> coeffHistList = Arrays.asList(coeffHist);
            s_0 = coeffHistList.indexOf(Collections.max(coeffHistList));

            List<Double> h = new ArrayList<>();
            DescriptiveStatistics vals;
            for (int coeffInd = 1; coeffInd < coeffHistList.size(); coeffInd++) {
                vals = new DescriptiveStatistics();
                for (int leapInd = s_0; leapInd < coeffHistList.size(); leapInd += coeffInd) {
                    vals.addValue(coeffHistList.get(leapInd));
                }
                for (int leapInd = s_0 - coeffInd; leapInd >= 0; leapInd -= coeffInd) {
                    vals.addValue(coeffHistList.get(leapInd));
                }
                h.add(vals.getMean());
            }
            p_h_avg[coeffIndex] = (h.indexOf(Collections.max(h)));

            FastFourierTransformer fastFourierTransformer = new FastFourierTransformer(
                    DftNormalization.STANDARD);
            Complex[] fft = fastFourierTransformer.transform(ArrayUtils.toPrimitive(coeffHist),
                    TransformType.FORWARD);

            double[] power = new double[fft.length];
            for (int ii = 0; ii < power.length; ii++) {
                power[ii] = fft[ii].abs();
            }

            //Find first local minimum, to bypass DC peak
            double DC = power[0];
            int FreqValley = 1;
            while (FreqValley < power.length - 1 & power[FreqValley] >= power[FreqValley + 1]) {
                FreqValley++;
            }

            int maxFFTInd = 0;
            double maxFFTVal = 0;
            double minFFTVal = Double.MAX_VALUE;
            for (int ii = FreqValley; ii < power.length / 2; ii++) {
                if (power[ii] > maxFFTVal) {
                    maxFFTInd = ii;
                    maxFFTVal = power[ii];
                }
                if (power[ii] < minFFTVal) {
                    minFFTVal = power[ii];
                }
            }
            if (maxFFTInd == 0 | maxFFTVal < (DC / 5) | minFFTVal / maxFFTVal > 0.9) {
                p_h_fft[coeffIndex] = 1;
            } else {
                p_h_fft[coeffIndex] = Math.round(coeffHist.length / maxFFTInd);
            }

        } else {
            p_h_avg[coeffIndex] = 1;
            p_h_fft[coeffIndex] = 1;
            s_0 = 0;
        }
        if (p_h_avg[coeffIndex] < p_h_fft[coeffIndex]) {
            p_final[coeffIndex] = p_h_avg[coeffIndex];
        } else {
            p_final[coeffIndex] = p_h_fft[coeffIndex];
        }

        pTampered[coeffIndex] = new double[selectedCoeffs.size()];
        pUntampered[coeffIndex] = new double[selectedCoeffs.size()];
        int[] adjustedCoeffs = new int[selectedCoeffs.size()];
        int[] period_start = new int[selectedCoeffs.size()];
        int[] period;
        int[] num = new int[selectedCoeffs.size()];
        int[] denom = new int[selectedCoeffs.size()];
        double[] P_u = new double[selectedCoeffs.size()];
        double[] P_t = new double[selectedCoeffs.size()];

        if (p_final[coeffIndex] != 1) {
            for (int ii = 0; ii < adjustedCoeffs.length; ii++) {
                adjustedCoeffs[ii] = selectedCoeffs.get(ii) - minCoeffValue;
                period_start[ii] = adjustedCoeffs[ii] - rem(adjustedCoeffs[ii] - s_0, p_final[coeffIndex]);
            }
            for (int kk = 0; kk < selectedCoeffs.size(); kk++) {
                if (period_start[kk] > s_0) {
                    period = new int[p_final[coeffIndex]];
                    for (int ii = 0; ii < p_final[coeffIndex]; ii++) {
                        period[ii] = period_start[kk] + ii;
                        if (period[ii] >= coeffHist.length) {
                            period[ii] = period[ii] - p_final[coeffIndex];
                        }
                    }
                    num[kk] = (int) coeffHist[adjustedCoeffs[kk]].doubleValue();
                    denom[kk] = 0;
                    for (int ll = 0; ll < period.length; ll++) {
                        denom[kk] = denom[kk] + (int) coeffHist[period[ll]].doubleValue();
                    }
                } else {
                    period = new int[p_final[coeffIndex]];
                    for (int ii = 0; ii < p_final[coeffIndex]; ii++) {
                        period[ii] = period_start[kk] - ii;
                        if (period_start[kk] - p_final[coeffIndex] + 1 <= 0) {
                            if (period[ii] <= 0) {
                                period[ii] = period[ii] + p_final[coeffIndex];
                            }
                        }
                    }
                    num[kk] = (int) coeffHist[adjustedCoeffs[kk]].doubleValue();
                    denom[kk] = 0;
                    for (int ll = 0; ll < period.length; ll++) {
                        denom[kk] = denom[kk] + (int) coeffHist[period[ll]].doubleValue();
                    }
                }

                P_u[kk] = ((double) num[kk] / denom[kk]);
                P_t[kk] = (1.0 / p_final[coeffIndex]);
                if (P_u[kk] + P_t[kk] != 0) {
                    pTampered[coeffIndex][kk] = P_t[kk] / (P_u[kk] + P_t[kk]);
                    pUntampered[coeffIndex][kk] = P_u[kk] / (P_u[kk] + P_t[kk]);

                } else {
                    pTampered[coeffIndex][kk] = 0.5;
                    pUntampered[coeffIndex][kk] = 0.5;
                }
            }

        } else {
            for (int kk = 0; kk < selectedCoeffs.size(); kk++) {
                pTampered[coeffIndex][kk] = 0.5;
                pUntampered[coeffIndex][kk] = 0.5;
            }
        }

    }
    double[] pTamperedOverall = new double[pTampered[0].length];
    double pTamperedProd;
    double pUntamperedProd;

    for (int locationIndex = 0; locationIndex < pTampered[0].length; locationIndex++) {
        pTamperedProd = 1;
        pUntamperedProd = 1;
        for (int coeffIndex = 0; coeffIndex < pTampered.length; coeffIndex++) {
            pTamperedProd = pTamperedProd * pTampered[coeffIndex][locationIndex];
            pUntamperedProd = pUntamperedProd * pUntampered[coeffIndex][locationIndex];
        }
        if (pTamperedProd + pUntamperedProd != 0) {
            pTamperedOverall[locationIndex] = pTamperedProd / (pTamperedProd + pUntamperedProd);
        } else {
            pTamperedOverall[locationIndex] = 0;
        }
    }

    int blocksH = imWidth / 8;
    int blocksV = imHeight / 8;
    double[][] outputMap = new double[blocksV][blocksH];
    for (int kk = 0; kk < pTamperedOverall.length; kk++) {
        outputMap[kk % blocksV][(int) Math.floor(kk / blocksV)] = pTamperedOverall[kk];
        if (pTamperedOverall[kk] > maxProbValue) {
            maxProbValue = pTamperedOverall[kk];
        }
        if (pTamperedOverall[kk] < minProbValue) {
            minProbValue = pTamperedOverall[kk];
        }
    }
    probabilityMap = outputMap;
    BufferedImage outputIm = visualizeWithJet(outputMap);
    // output
    displaySurface = outputIm;
}

From source file:com.github.jessemull.microflex.stat.statbiginteger.MeanBigIntegerWeightsTest.java

/**
 * Tests the aggregated plate statistics method.
 *//*from  w w  w  . j av a 2  s  .  c o  m*/
@Test
public void testAggregatedPlate() {

    for (PlateBigInteger plate : array) {

        List<BigDecimal> resultList = new ArrayList<BigDecimal>();
        BigDecimal aggregatedReturned = mean.platesAggregated(plate, weights, mc);

        for (WellBigInteger well : plate) {

            List<BigDecimal> input = well.toBigDecimal();

            for (int i = 0; i < input.size(); i++) {
                resultList.add(input.get(i).multiply(new BigDecimal(weights[i])));
            }

        }

        double[] inputAggregated = new double[resultList.size()];

        for (int i = 0; i < resultList.size(); i++) {
            inputAggregated[i] = resultList.get(i).doubleValue();
        }

        DescriptiveStatistics statAggregated = new DescriptiveStatistics(inputAggregated);
        double resultAggregatedDouble = statAggregated.getMean();

        BigDecimal aggregatedResult = new BigDecimal(resultAggregatedDouble, mc);

        BigDecimal[] corrected = correctRoundingErrors(aggregatedResult, aggregatedReturned);
        assertEquals(corrected[0], corrected[1]);
    }
}

From source file:com.github.jessemull.microflex.stat.statbiginteger.MeanBigIntegerWeightsTest.java

/**
 * Tests the aggregated plate statistics method.
 */// w w  w  .  j a va 2 s . com
@Test
public void testAggregatedSet() {

    for (PlateBigInteger plate : array) {

        List<BigDecimal> resultList = new ArrayList<BigDecimal>();
        BigDecimal aggregatedReturned = mean.setsAggregated(plate.dataSet(), weights, mc);

        for (WellBigInteger well : plate) {

            List<BigDecimal> input = well.toBigDecimal();

            for (int i = 0; i < input.size(); i++) {
                resultList.add(input.get(i).multiply(new BigDecimal(weights[i])));
            }

        }

        double[] inputAggregated = new double[resultList.size()];

        for (int i = 0; i < resultList.size(); i++) {
            inputAggregated[i] = resultList.get(i).doubleValue();
        }

        DescriptiveStatistics statAggregated = new DescriptiveStatistics(inputAggregated);
        double resultAggregatedDouble = statAggregated.getMean();

        BigDecimal aggregatedResult = new BigDecimal(resultAggregatedDouble, mc);

        BigDecimal[] corrected = correctRoundingErrors(aggregatedResult, aggregatedReturned);
        assertEquals(corrected[0], corrected[1]);
    }
}

From source file:com.github.jessemull.microflexbiginteger.stat.MeanWeightsTest.java

/**
 * Tests the aggregated plate statistics method.
 *///from ww w .java2  s.c o m
@Test
public void testAggregatedPlate() {

    for (Plate plate : array) {

        List<BigDecimal> resultList = new ArrayList<BigDecimal>();
        BigDecimal aggregatedReturned = mean.platesAggregated(plate, weights, mc);

        for (Well well : plate) {

            List<BigDecimal> input = well.toBigDecimal();

            for (int i = 0; i < input.size(); i++) {
                resultList.add(input.get(i).multiply(new BigDecimal(weights[i])));
            }

        }

        double[] inputAggregated = new double[resultList.size()];

        for (int i = 0; i < resultList.size(); i++) {
            inputAggregated[i] = resultList.get(i).doubleValue();
        }

        DescriptiveStatistics statAggregated = new DescriptiveStatistics(inputAggregated);
        double resultAggregatedDouble = statAggregated.getMean();

        BigDecimal aggregatedResult = new BigDecimal(resultAggregatedDouble, mc);

        BigDecimal[] corrected = correctRoundingErrors(aggregatedResult, aggregatedReturned);
        assertEquals(corrected[0], corrected[1]);
    }
}

From source file:com.github.jessemull.microflexbiginteger.stat.MeanWeightsTest.java

/**
 * Tests the aggregated plate statistics method.
 *///from   www  .jav a  2  s .c  om
@Test
public void testAggregatedSet() {

    for (Plate plate : array) {

        List<BigDecimal> resultList = new ArrayList<BigDecimal>();
        BigDecimal aggregatedReturned = mean.setsAggregated(plate.dataSet(), weights, mc);

        for (Well well : plate) {

            List<BigDecimal> input = well.toBigDecimal();

            for (int i = 0; i < input.size(); i++) {
                resultList.add(input.get(i).multiply(new BigDecimal(weights[i])));
            }

        }

        double[] inputAggregated = new double[resultList.size()];

        for (int i = 0; i < resultList.size(); i++) {
            inputAggregated[i] = resultList.get(i).doubleValue();
        }

        DescriptiveStatistics statAggregated = new DescriptiveStatistics(inputAggregated);
        double resultAggregatedDouble = statAggregated.getMean();

        BigDecimal aggregatedResult = new BigDecimal(resultAggregatedDouble, mc);

        BigDecimal[] corrected = correctRoundingErrors(aggregatedResult, aggregatedReturned);
        assertEquals(corrected[0], corrected[1]);
    }
}