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:org.wattdepot.server.http.api.DepositoryAverageValuesServer.java

/**
 * retrieve the depository measurement list for a sensor.
 * //from ww  w  .j a v a 2s .  com
 * @return measurement list.
 */
public InterpolatedValueList doRetrieve() {
    getLogger().log(Level.INFO,
            "GET /wattdepot/{" + orgId + "}/" + Labels.DEPOSITORY + "/{" + depositoryId + "}/" + Labels.VALUES
                    + "/" + Labels.AVERAGE + "/?" + Labels.SENSOR + "={" + sensorId + "}&" + Labels.START + "={"
                    + start + "}&" + Labels.END + "={" + end + "}&" + Labels.INTERVAL + "={" + interval + "}&"
                    + Labels.VALUE_TYPE + "={" + dataType + "}");
    if (isInRole(orgId)) {
        if (start != null && end != null && interval != null) {
            InterpolatedValueList ret = new InterpolatedValueList();
            try {
                Depository depository = depot.getDepository(depositoryId, orgId, true);
                XMLGregorianCalendar startRange = DateConvert.parseCalString(start);
                XMLGregorianCalendar endRange = DateConvert.parseCalString(end);
                int intervalMinutes = Integer.parseInt(interval);
                List<XMLGregorianCalendar> rangeList = Tstamp.getTimestampList(startRange, endRange,
                        intervalMinutes);
                if (rangeList != null) {
                    for (int i = 1; i < rangeList.size(); i++) {
                        Date valueDate = DateConvert.convertXMLCal(rangeList.get(i));
                        XMLGregorianCalendar startInterval = rangeList.get(i - 1);
                        XMLGregorianCalendar endInterval = rangeList.get(i);
                        List<XMLGregorianCalendar> intervalList = Tstamp.getNTimestampList(24, startInterval,
                                endInterval);
                        DescriptiveStatistics stats = new DescriptiveStatistics();
                        Date previous = null;
                        for (int j = 0; j < intervalList.size(); j++) {
                            Date timestamp = DateConvert.convertXMLCal(intervalList.get(j));
                            Double value = null;
                            if ("point".equals(dataType)) {
                                value = getValue(depositoryId, orgId, sensorId, timestamp);
                            } else {
                                if (previous != null) {
                                    value = getValue(depositoryId, orgId, sensorId, previous, timestamp);
                                }
                                previous = timestamp;
                            }
                            if (value != null) {
                                stats.addValue(value);
                            }
                        }

                        if (!Double.isNaN(stats.getMean())) {
                            ret.getInterpolatedValues().add(new InterpolatedValue(sensorId, stats.getMean(),
                                    depository.getMeasurementType(), valueDate));
                        }
                    }
                }
            } catch (IdNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (DatatypeConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return ret;
        } else {
            setStatus(Status.CLIENT_ERROR_BAD_REQUEST, "Missing start and/or end times or interval.");
            return null;
        }
    } else {
        setStatus(Status.CLIENT_ERROR_BAD_REQUEST, "Bad credentials.");
        return null;
    }
}

From source file:org.wildfly.swarm.proc.CSVCollector.java

public void onFinish(String id) {

    List<Object> record = new ArrayList<>();
    record.add(id);/* w ww. jav  a  2  s .  c  om*/
    record.add(Paths.get(id).getFileName());
    for (Measure m : Measure.values()) {
        if (!results.containsKey(m)) {
            throw new RuntimeException("Measurement is missing " + m);
        }

        DescriptiveStatistics stats = results.get(m);
        record.add(stats.getN());
        record.add(stats.getMin());
        record.add(stats.getMax());
        record.add(stats.getMean());
        record.add(stats.getStandardDeviation());
        record.add(stats.getPercentile(50));
        record.add(stats.getPercentile(75));
    }

    try {
        csvOutput.printRecord(record);
        csvOutput.flush();
    } catch (IOException e) {
        throw new RuntimeException("Failed to write data", e);
    }
}

From source file:org.wso2.carbon.ml.database.internal.MLDatabaseService.java

/**
 * Create the JSON string with summary statistics for a column.
 *
 * @param type Data-type of the column/*from www .  ja  va 2  s .  c o  m*/
 * @param graphFrequencies Bin frequencies of the column
 * @param missing Number of missing values in the column
 * @param unique Number of unique values in the column
 * @param descriptiveStats DescriptiveStats object of the column
 * @return JSON representation of the summary statistics of the column
 */
private JSONArray createJson(String type, SortedMap<?, Integer> graphFrequencies, int missing, int unique,
        DescriptiveStatistics descriptiveStats) throws JSONException {

    JSONObject json = new JSONObject();
    JSONArray freqs = new JSONArray();
    Object[] categoryNames = graphFrequencies.keySet().toArray();
    // Create an array with intervals/categories and their frequencies.
    for (int i = 0; i < graphFrequencies.size(); i++) {
        JSONArray temp = new JSONArray();
        temp.put(categoryNames[i].toString());
        temp.put(graphFrequencies.get(categoryNames[i]));
        freqs.put(temp);
    }
    // Put the statistics to a json object
    json.put("unique", unique);
    json.put("missing", missing);

    DecimalFormat decimalFormat = new DecimalFormat("#.###");
    if (descriptiveStats.getN() != 0) {
        json.put("mean", decimalFormat.format(descriptiveStats.getMean()));
        json.put("min", decimalFormat.format(descriptiveStats.getMin()));
        json.put("max", decimalFormat.format(descriptiveStats.getMax()));
        json.put("median", decimalFormat.format(descriptiveStats.getPercentile(50)));
        json.put("std", decimalFormat.format(descriptiveStats.getStandardDeviation()));
        if (type.equalsIgnoreCase(FeatureType.NUMERICAL)) {
            json.put("skewness", decimalFormat.format(descriptiveStats.getSkewness()));
        }
    }
    json.put("values", freqs);
    json.put("bar", true);
    json.put("key", "Frequency");
    JSONArray summaryStatArray = new JSONArray();
    summaryStatArray.put(json);
    return summaryStatArray;
}

From source file:org.wso2.carbon.ml.dataset.internal.DatabaseHandler.java

/**
 * Create the JSON string with summary statistics for a column.
 *
 * @param type              Data-type of the column
 * @param graphFrequencies  Bin frequencies of the column
 * @param missing           Number of missing values in the column
 * @param unique            Number of unique values in the column
 * @param descriptiveStats  DescriptiveStats object of the column
 * @return                  JSON representation of the summary statistics of the column
 *//*w  w  w.ja va  2s  .c  om*/
private JSONArray createJson(String type, SortedMap<?, Integer> graphFrequencies, int missing, int unique,
        DescriptiveStatistics descriptiveStats) {
    JSONObject json = new JSONObject();
    JSONArray freqs = new JSONArray();
    Object[] categoryNames = graphFrequencies.keySet().toArray();
    // Create an array with intervals/categories and their frequencies.
    for (int i = 0; i < graphFrequencies.size(); i++) {
        JSONArray temp = new JSONArray();
        temp.put(categoryNames[i].toString());
        temp.put(graphFrequencies.get(categoryNames[i]));
        freqs.put(temp);
    }
    // Put the statistics to a json object
    json.put("unique", unique);
    json.put("missing", missing);

    DecimalFormat decimalFormat = new DecimalFormat("#.###");
    if (descriptiveStats.getN() != 0) {
        json.put("mean", decimalFormat.format(descriptiveStats.getMean()));
        json.put("median", decimalFormat.format(descriptiveStats.getPercentile(50)));
        json.put("std", decimalFormat.format(descriptiveStats.getStandardDeviation()));
        if (type.equalsIgnoreCase(FeatureType.NUMERICAL)) {
            json.put("skewness", decimalFormat.format(descriptiveStats.getSkewness()));
        }
    }
    json.put("values", freqs);
    json.put("bar", true);
    json.put("key", "Frequency");
    JSONArray summaryStatArray = new JSONArray();
    summaryStatArray.put(json);
    return summaryStatArray;
}

From source file:org.wso2.extension.siddhi.execution.var.backtest.Backtest.java

private void runStandardCoverageTest() {

    int numberOfExceptions = 0;

    for (int i = 0; i < VAR_PER_SAMPLE; i++) {
        if (actualVarList.get(i) <= calculatedVarList.get(i)) {
            numberOfExceptions++;//from   www.  ja  v a  2  s.c o  m
            meanViolations.add(actualVarList.get(i) - calculatedVarList.get(i));
        }
    }

    DescriptiveStatistics dsLoss = new DescriptiveStatistics(
            actualVarList.stream().mapToDouble(Double::doubleValue).toArray());
    DescriptiveStatistics dsVaR = new DescriptiveStatistics(
            calculatedVarList.stream().mapToDouble(Double::doubleValue).toArray());
    DescriptiveStatistics dsMV = new DescriptiveStatistics(
            meanViolations.stream().mapToDouble(Double::doubleValue).toArray());

    System.out.println("Loss mean : " + dsLoss.getMean());
    System.out.println("VaR mean  : " + dsVaR.getMean());
    System.out.println("No. of violations : " + numberOfExceptions);
    System.out.println("Violation mean  : " + dsMV.getMean());
    System.out.println("Violation Rate : " + (((double) numberOfExceptions) / (VAR_PER_SAMPLE)) * 100 + "%");
}

From source file:org.wso2.extension.siddhi.execution.var.backtest.BacktestIncremental.java

public void runTest() throws FileNotFoundException {

    Formatter formatter = new Formatter(new File("MonteCarloBacktestResults.csv"));
    formatter.format("%s%n", "date,varclose,varavg,varmax,corrloss,varmedian,varmode,lossclose,lossavg,lossmax,"
            + "corrvar,lossmedian,lossmode");
    String[] dates = { "jan-23", "jan-24", "jan-25", "jan-26", "jan-27", "jan-30", "jan-31", "feb-1", "feb-2",
            "feb-3" };
    String write = "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s";
    //        VaRCalculator varCalculator = new HistoricalVaRCalculator(BATCH_SIZE, VAR_CI);
    VaRCalculator varCalculator = new ParametricVaRCalculator(BATCH_SIZE, VAR_CI);
    //        VaRCalculator varCalculator = new MonteCarloVarCalculator(BATCH_SIZE, VAR_CI, 2500, 100, 0.01);

    Map<String, Integer> assets = initPortfolio();
    Portfolio portfolio = varCalculator.createPortfolio("1", assets);
    varCalculator.addPortfolio("1", portfolio);

    for (int d = START_DATE; d <= END_DATE; d++) {
        System.out.println("\nDAY : " + dates[d - START_DATE] + "\n");
        ArrayList<Event> list = readBacktestData(d);
        HashMap<Integer, Double> varMap = new HashMap();
        HashMap<Integer, Double> lossMap = new HashMap();
        double var = 0;
        double loss = 0;
        String corrVar;/*from w  w  w .  j  a  v  a2 s  .co m*/
        String corrLoss;

        int counter = 0;
        for (int i = 0; i < list.size(); i++) {

            //System.out.print("Event " + (i + 1) + " : ");
            String jsonString = (String) varCalculator.calculateValueAtRisk(list.get(i));
            currentPortfolioValue = portfolio.getTotalPortfolioValue();

            if (jsonString != null) {

                JSONObject jsonObject = new JSONObject(jsonString);
                double tempVar = (Double) jsonObject.get(PORTFOLIO_KEY); // hardcoded for portfolio ID 1
                if (tempVar < 0) {
                    var = tempVar;
                    varMap.put(counter, var);
                    //System.out.printf("Var : %.4f", tempVar);
                }

                double tempLoss = currentPortfolioValue - previousPortfolioValue;
                if (tempLoss < 0) {
                    loss = tempLoss;
                    lossMap.put(counter, loss);
                    //System.out.printf(" Loss : %.4f", tempLoss);
                }
                counter++;
            }

            previousPortfolioValue = currentPortfolioValue;
            //System.out.println();
        }

        double[] vars = Stream.of(varMap.values().toArray(new Double[varMap.size()]))
                .mapToDouble(Double::doubleValue).toArray();
        DescriptiveStatistics statVar = new DescriptiveStatistics(vars);

        double[] losses = Stream.of(lossMap.values().toArray(new Double[lossMap.size()]))
                .mapToDouble(Double::doubleValue).toArray();
        DescriptiveStatistics statLoss = new DescriptiveStatistics(losses);

        System.out.println("Daily VaR CLOSE  : " + var);
        System.out.println("Daily VaR AVG    : " + statVar.getMean());

        Double min = statVar.getMin();
        System.out.println("Daily VaR MAX    : " + min);

        Integer minIndex = null;
        for (Map.Entry<Integer, Double> e : varMap.entrySet()) {
            if (e.getValue().equals(min)) {
                minIndex = e.getKey();
            }
        }

        if (lossMap.get(minIndex) == null) {
            corrLoss = "NO LOSS";
        } else {
            corrLoss = lossMap.get(minIndex).toString();
        }

        System.out.println("Crspng Loss      : " + corrLoss);
        System.out.println("Daily VaR MEDIAN : " + statVar.getPercentile(50));
        System.out.println("Daily VaR MODE   : " + mode(statVar.getValues()));

        System.out.println();

        System.out.println("Daily Loss CLOSE   : " + loss);
        System.out.println("Daily Loss AVG     : " + statLoss.getMean());

        min = statLoss.getMin();
        System.out.println("Daily Loss MAX     : " + min);

        for (Map.Entry<Integer, Double> e : lossMap.entrySet()) {
            if (e.getValue().equals(min)) {
                minIndex = e.getKey();
            }
        }

        if (varMap.get(minIndex) == null) {
            corrVar = "NO VAR";
        } else {
            corrVar = varMap.get(minIndex).toString();
        }

        System.out.println("Crspng VaR         : " + corrVar);
        System.out.println("Daily Loss MEDIAN  : " + statLoss.getPercentile(50));
        System.out.println("Daily Loss MODE    : " + mode(statLoss.getValues()));

        formatter.format("%s%n",
                String.format(write, dates[d - START_DATE], var, statVar.getMean(), statVar.getMin(), corrLoss,
                        statVar.getPercentile(50), mode(statVar.getValues()), loss, statLoss.getMean(),
                        statLoss.getMin(), corrVar, statLoss.getPercentile(50), mode(statLoss.getValues())));

    }

    formatter.close();
}

From source file:org.wso2.extension.siddhi.execution.var.backtest.BacktestIncrementalTest.java

private void runViolationTest() {
    Formatter formatter = null;//from   w ww.  j a v a  2  s .  c  o  m
    try {
        formatter = new Formatter(new File(METHOD + "-" + DATA_SET + "-" + VAR_CI + ".csv"));
        formatter.format("%s,%s%n", "VaR", "Loss");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    DescriptiveStatistics lossStat = new DescriptiveStatistics(
            lossList.stream().mapToDouble(Double::doubleValue).toArray());
    DescriptiveStatistics varStat = new DescriptiveStatistics(
            varList.stream().mapToDouble(Double::doubleValue).toArray());
    DescriptiveStatistics violationStat = new DescriptiveStatistics();
    int failCount = 0;
    int numberOfData = varList.size();
    for (int i = 0; i < numberOfData - 1; i++) {
        formatter.format("%.3f,%.3f%n", varList.get(i), lossList.get(i + 1));
        if (lossList.get(i + 1) < varList.get(i)) {
            violationStat.addValue(varList.get(i));
            failCount++;
        }
    }
    formatter.close();
    System.out.println("Number of data : " + numberOfData);
    System.out.printf("Loss Mean : %.2f\n", lossStat.getMean());
    System.out.printf("Var Mean : %.2f\n", varStat.getMean());
    System.out.printf("Number of violations : %d\n", failCount);
    System.out.printf("Mean Violation : %.2f\n", violationStat.getMean());
    System.out.printf("Violation Rate : %.2f%%\n", ((double) failCount) / (numberOfData - 1) * 100);
}

From source file:performancestatisticsasset.DistributionSet.java

public void setDistributionSet(String targetGroup, String targetTask, String targetMeasure,
        RecordList dataSet) {/*from  www. j a v a 2s.c o  m*/
    //Determine the number of trials selected by group and by task
    int trials = 0;
    int len = dataSet.records.size();
    for (int i = 0; i < len; i++) {
        if (dataSet.records.get(i).getTrialNumber() > trials)
            trials = dataSet.records.get(i).getTrialNumber();
    }

    Distribution tempDist;
    DescriptiveStatistics tempStat;
    //For each trial of the set do
    for (int i = 0; i < trials; i++) {
        tempDist = new Distribution();
        tempStat = new DescriptiveStatistics();
        //Select data
        for (int j = 0; j < len; j++) {
            //If the current record is of the correct trial
            if ((dataSet.records.get(j).getTrialNumber() == i + 1)
                    && (targetGroup.equals(dataSet.records.get(j).getGroupID()))
                    && (targetTask.equals(dataSet.records.get(j).getTaskID()))) {
                //Fill distribution
                switch (targetMeasure) {
                case "time":
                    tempStat.addValue(dataSet.records.get(j).getTimeToComplete());
                    break;
                case "perf":
                    tempStat.addValue(dataSet.records.get(j).getPerformance());
                    break;
                }
            }
        }
        //Transfer the computed statistics to tempDist
        tempDist.max = tempStat.getMax();
        tempDist.min = tempStat.getMin();
        tempDist.sum = tempStat.getSum();
        tempDist.variance = tempStat.getVariance();
        tempDist.mean = tempStat.getMean();
        tempDist.stdDev = tempStat.getStandardDeviation();
        tempDist.skewness = tempStat.getSkewness();
        tempDist.kurtosis = tempStat.getKurtosis();
        tempDist.n = tempStat.getN();

        //Add tempDist to distributionSet
        distributionSet.add(tempDist);
    }
}

From source file:performancestatisticsasset.DistributionSet.java

public void setDistributionSet(String targetTask, String targetMeasure, RecordList dataSet) {
    //Determine the number of trials selected by task
    int trials = 0;
    int len = dataSet.records.size();
    for (int i = 0; i < len; i++) {
        if (dataSet.records.get(i).getTrialNumber() > trials)
            trials = dataSet.records.get(i).getTrialNumber();
    }//from  w ww . j a  va 2  s . c  o m

    Distribution tempDist;
    DescriptiveStatistics tempStat;
    //For each trial of the set do
    for (int i = 0; i < trials; i++) {
        tempDist = new Distribution();
        tempStat = new DescriptiveStatistics();
        //Select data
        for (int j = 0; j < len; j++) {
            //If the current record is of the correct trial
            if ((dataSet.records.get(j).getTrialNumber() == i + 1)
                    && (targetTask.equals(dataSet.records.get(j).getTaskID()))) {
                //Fill distribution
                switch (targetMeasure) {
                case "time":
                    tempStat.addValue(dataSet.records.get(j).getTimeToComplete());
                    break;
                case "perf":
                    tempStat.addValue(dataSet.records.get(j).getPerformance());
                    break;
                }
            }
        }
        //Transfer the computed statistics to tempDist
        tempDist.max = tempStat.getMax();
        tempDist.min = tempStat.getMin();
        tempDist.sum = tempStat.getSum();
        tempDist.variance = tempStat.getVariance();
        tempDist.mean = tempStat.getMean();
        tempDist.stdDev = tempStat.getStandardDeviation();
        tempDist.skewness = tempStat.getSkewness();
        tempDist.kurtosis = tempStat.getKurtosis();
        tempDist.n = tempStat.getN();

        //Add tempDist to distributionSet
        distributionSet.add(tempDist);
    }
}

From source file:Pooling.collectStatsTime.java

public static void main(String[] args) throws IOException {
    /*         int maxN = 1000;
             double[] prob = {0.25,0.5,0.75,1.00};
             int[] maxPoolSize = {35};//from ww  w  .ja  va  2  s.co  m
             int nTests = 50;
                     
             String folder = "randGraphsSimRes";
             for (int t = 0; t < maxPoolSize.length; t++)
             {
     FileWriter fw = new FileWriter(folder + File.separator + "statsDesignTime" + maxPoolSize[t] + ".txt");
     for (int n = 10; n <= maxN; n+=10)
     {
         DescriptiveStatistics stats = new DescriptiveStatistics();
         for (int i = 1; i <= nTests; i++)
         {
             String resfile = "testResults_" + n + "_" + maxPoolSize[t] + "_" + i + ".txt";
             BufferedReader br = new BufferedReader(new FileReader(folder + File.separator + resfile));
             System.out.println(resfile);
             String s = br.readLine();
             StringTokenizer st = new StringTokenizer(s," ");
             st.nextToken();
             int k = Integer.parseInt(st.nextToken());
             stats.addValue(((double)k)/1000);
         }
         fw.write(n + " " + stats.getMean() + " " + (stats.getStandardDeviation()/Math.sqrt(nTests)) +  "\n");
     }
     fw.close();
             } */

    int[] ns = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150 };
    int[] maxPoolSizes = { 15, 25, 35 };
    int nTests = 10;
    for (int sz = 0; sz < maxPoolSizes.length; sz++) {
        FileWriter fw = new FileWriter("statisticsTime_" + maxPoolSizes[sz] + ".txt");
        for (int in = 0; in < ns.length; in++) {
            DescriptiveStatistics stats = new DescriptiveStatistics();
            for (int it = 0; it < nTests; it++) {
                String outdir = "Test_" + maxPoolSizes[sz] + "_" + ns[in] + "_" + it;
                String repFile = outdir + File.separator + "report.txt";
                System.out.println(outdir);
                BufferedReader br = new BufferedReader(new FileReader(repFile));
                String s = "";
                for (int i = 0; i < 6; i++)
                    s = br.readLine();
                StringTokenizer st = new StringTokenizer(s, ":");
                st.nextToken();
                double tm = Double.parseDouble(st.nextToken());
                stats.addValue(tm);
            }
            double maxtime = stats.getMax();
            DescriptiveStatistics stats1 = new DescriptiveStatistics();
            for (int i = 0; i < nTests; i++)
                if (stats.getElement(i) != maxtime)
                    stats1.addValue(stats.getElement(i));
            fw.write(ns[in] + " " + stats1.getMean() + " "
                    + (stats1.getStandardDeviation() / Math.sqrt((nTests - 1))) + "\n");
        }
        fw.close();
    }
}