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:AverageCost.java

public static void main(String[] args) throws FileNotFoundException, IOException {
    //Directory of the n files
    String directory_path = "/home/gauss/rgrunitzki/Dropbox/Profissional/UFRGS/Doutorado/Artigo TRI15/SF Experiments/IQ-Learning/";
    BufferedReader reader = null;
    //Line to analyse
    String line = "";
    String csvDivisor = ";";
    int totalLines = 1002;
    int totalRows = 532;

    String filesToRead[] = new File(directory_path).list();
    Arrays.sort(filesToRead);/*w ww. ja v a 2s. c o m*/
    System.out.println(filesToRead.length);

    List<List<DescriptiveStatistics>> summary = new ArrayList<>();

    for (int i = 0; i <= totalLines; i++) {
        summary.add(new ArrayList<DescriptiveStatistics>());
        for (int j = 0; j <= totalRows; j++) {
            summary.get(i).add(new DescriptiveStatistics());
        }
    }

    //reads all files
    for (String file : filesToRead) {
        reader = new BufferedReader(new FileReader(directory_path + file));
        int lineCounter = 0;
        //reads all file's line
        while ((line = reader.readLine()) != null) {
            if (lineCounter > 0) {
                String[] rows = line.trim().split(csvDivisor);
                //reads all line's row
                for (int r = 0; r < rows.length; r++) {
                    summary.get(lineCounter).get(r).addValue(Double.parseDouble(rows[r]));
                }
            }
            lineCounter++;
        }

        //System.out.println(file.split("/")[file.split("/").length - 1] + csvDivisor + arithmeticMean(avgCost) + csvDivisor + standardDeviation(avgCost));
    }

    //generate mean and standard deviation;
    for (List<DescriptiveStatistics> summaryLines : summary) {
        System.out.println();
        for (DescriptiveStatistics summaryLineRow : summaryLines) {
            System.out.print(summaryLineRow.getMean() + ";" + summaryLineRow.getStandardDeviation() + ";");
        }
    }
}

From source file:com.fpuna.preproceso.TestApacheMathLibDemo.java

/**
 * @param args// ww w .j a va 2 s. co  m
 */
public static void main(String[] args) {

    RandomGenerator randomGenerator = new JDKRandomGenerator();
    System.out.println(randomGenerator.nextInt());
    System.out.println(randomGenerator.nextDouble());

    /**
     * Descriptive Statistics like MEAN,GP,SD,MAX
    *
     */
    DescriptiveStatistics stats = new DescriptiveStatistics();
    stats.addValue(1);
    stats.addValue(2);
    stats.addValue(3);
    stats.addValue(4);
    stats.addValue(5);
    stats.addValue(6);
    stats.addValue(7);
    System.out.print("Mean : " + stats.getMean() + "\n");
    System.out.print("Standard deviation : " + stats.getStandardDeviation() + "\n");
    System.out.print("Max : " + stats.getMax() + "\n");

    /**
     * Complex number format a+bi
    *
     */
    Complex c1 = new Complex(1, 2);
    Complex c2 = new Complex(2, 3);
    System.out.print("Absolute of c1 " + c1.abs() + "\n");
    System.out.print("Addition : " + (c1.add(c2)) + "\n");
}

From source file:es.upm.oeg.tools.rdfshapes.libdemo.CommonsMathDemo.java

public static void main(String[] args) {

    DescriptiveStatistics stats = new DescriptiveStatistics();

    int inputArray[] = { 3, 4, 5, 6, 2, 3, 4, 3, 3, 4, 3, 6, 3, 2, 3, 1, 2, 1, 1, 1, 3 };

    // Add the data from the array
    for (int i = 0; i < inputArray.length; i++) {
        stats.addValue(inputArray[i]);// w  w w . j a  v  a2  s.co m
    }

    double mean = stats.getMean();
    double std = stats.getStandardDeviation();
    double median = stats.getPercentile(50);

    System.out.println("mean" + stats.getMean());
    System.out.println("standard deviation" + stats.getStandardDeviation());
    System.out.println("skewness" + stats.getSkewness());

}

From source file:DifferentalEvolution.java

public static void main(String[] args) {
    solutions = new ArrayList<Double>(ControlVariables.RUNS_PER_FUNCTION);

    /* An array of the benchmark functions to evalute */
    benchmarkFunctions = new ArrayList<FitnessFunction>();
    benchmarkFunctions.add(new DeJong());
    benchmarkFunctions.add(new HyperEllipsoid());
    benchmarkFunctions.add(new Schwefel());
    benchmarkFunctions.add(new RosenbrocksValley());
    benchmarkFunctions.add(new Rastrigin());

    /* Apply the differential evolution algorithm to each benchmark function */
    for (FitnessFunction benchmarkFunction : benchmarkFunctions) {
        /* Set the fitness function for the current benchmark function */
        fitnessFunction = benchmarkFunction;

        /* Execute the differential evolution algorithm a number of times per function */
        for (int runs = 0; runs < ControlVariables.RUNS_PER_FUNCTION; ++runs) {
            int a;
            int b;
            int c;
            boolean validVector = false;
            Vector noisyVector = null;

            /* Reset the array of the best values found */
            prevAmount = 0;// w  ww  . j  a v  a 2  s .  c  o m
            lowestFit = new LinkedHashMap<Integer, Double>();
            lowestFit.put(prevAmount, Double.MAX_VALUE);

            initPopulation(fitnessFunction.getBounds());

            /* Reset the fitness function NFC each time */
            fitnessFunction.resetNFC();

            while (fitnessFunction.getNFC() < ControlVariables.MAX_FUNCTION_CALLS) {
                for (int i = 0; i < ControlVariables.POPULATION_SIZE; i++) {
                    // Select 3 Mutually Exclusive Parents i != a != b != c
                    while (!validVector) {
                        do {
                            a = getRandomIndex();
                        } while (a == i);

                        do {
                            b = getRandomIndex();
                        } while (b == i || b == a);

                        do {
                            c = getRandomIndex();
                        } while (c == i || c == a || c == b);

                        // Catch invalid vectors
                        try {
                            validVector = true;
                            noisyVector = VectorOperations.mutation(population.get(c), population.get(b),
                                    population.get(a));
                        } catch (IllegalArgumentException e) {
                            validVector = false;
                        }
                    }

                    validVector = false;

                    Vector trialVector = VectorOperations.crossover(population.get(i), noisyVector, random);

                    trialVector.setFitness(fitnessFunction.evaluate(trialVector));

                    population.set(i, VectorOperations.selection(population.get(i), trialVector));

                    /* Get the best fitness value found so far */
                    if (population.get(i).getFitness() < lowestFit.get(prevAmount)) {
                        prevAmount = fitnessFunction.getNFC();
                        bestValue = population.get(i).getFitness();
                        lowestFit.put(prevAmount, bestValue);
                    }
                }
            }

            /* save the best value found for the entire DE algorithm run */
            solutions.add(bestValue);
        }

        /* Display the mean and standard deviation */
        System.out.println("\nResults for " + fitnessFunction.getName());
        DescriptiveStatistics stats = new DescriptiveStatistics(Doubles.toArray(solutions));
        System.out.println("AVERAGE BEST FITNESS: " + stats.getMean());
        System.out.println("STANDARD DEVIATION:   " + stats.getStandardDeviation());

        /* Set the last value (NFC) to the best value found */
        lowestFit.put(ControlVariables.MAX_FUNCTION_CALLS, bestValue);

        /* Plot the best value found vs. NFC */
        PerformanceGraph.plot(lowestFit, fitnessFunction.getName());

        /* Reset the results for the next benchmark function to be evaluated */
        solutions.clear();
        lowestFit.clear();
        bestValue = Double.MAX_VALUE;
    }
}

From source file:FaceRatios.java

@SuppressWarnings("serial")
public static void main(String[] args) {
    int r = FSDK.ActivateLibrary(FACE_SDK_LICENSE);
    if (r == FSDK.FSDKE_OK) {
        FSDK.Initialize();//from  w ww .j  a  v a2  s . c  o  m
        FSDK.SetFaceDetectionParameters(true, true, 384);

        Map<String, Map<String, ArrayList<Double>>> faceProperties = new HashMap<>();

        for (String directory : new File(FACE_DIRECTORY).list()) {
            if (new File(FACE_DIRECTORY + directory).isDirectory()) {
                Map<String, ArrayList<Double>> properties = new HashMap<String, ArrayList<Double>>() {
                    {
                        for (String property : propertyNames)
                            put(property, new ArrayList<Double>());
                    }
                };

                File[] files = new File(FACE_DIRECTORY + directory).listFiles();
                System.out.println("Analyzing " + directory + " with " + files.length + " files\n");
                for (File file : files) {
                    if (file.isFile()) {
                        HImage imageHandle = new HImage();
                        FSDK.LoadImageFromFileW(imageHandle, file.getAbsolutePath());

                        FSDK.TFacePosition.ByReference facePosition = new FSDK.TFacePosition.ByReference();
                        if (FSDK.DetectFace(imageHandle, facePosition) == FSDK.FSDKE_OK) {
                            FSDK_Features.ByReference facialFeatures = new FSDK_Features.ByReference();
                            FSDK.DetectFacialFeaturesInRegion(imageHandle, (FSDK.TFacePosition) facePosition,
                                    facialFeatures);

                            Point[] featurePoints = new Point[FSDK.FSDK_FACIAL_FEATURE_COUNT];
                            for (int i = 0; i < FSDK.FSDK_FACIAL_FEATURE_COUNT; i++) {
                                featurePoints[i] = new Point(0, 0);
                                featurePoints[i].x = facialFeatures.features[i].x;
                                featurePoints[i].y = facialFeatures.features[i].y;
                            }

                            double eyeDistance = featureDistance(featurePoints, FeatureID.LEFT_EYE,
                                    FeatureID.RIGHT_EYE);
                            double rightEyeSize = featureDistance(featurePoints,
                                    FeatureID.RIGHT_EYE_INNER_CORNER, FeatureID.RIGHT_EYE_OUTER_CORNER);
                            double leftEyeSize = featureDistance(featurePoints, FeatureID.LEFT_EYE_INNER_CORNER,
                                    FeatureID.LEFT_EYE_OUTER_CORNER);
                            double averageEyeSize = (rightEyeSize + leftEyeSize) / 2;

                            double mouthLength = featureDistance(featurePoints, FeatureID.MOUTH_RIGHT_CORNER,
                                    FeatureID.MOUTH_LEFT_CORNER);
                            double mouthHeight = featureDistance(featurePoints, FeatureID.MOUTH_BOTTOM,
                                    FeatureID.MOUTH_TOP);
                            double noseHeight = featureDistance(featurePoints, FeatureID.NOSE_BOTTOM,
                                    FeatureID.NOSE_BRIDGE);
                            double chinHeight = featureDistance(featurePoints, FeatureID.CHIN_BOTTOM,
                                    FeatureID.MOUTH_BOTTOM);

                            double chinToBridgeHeight = featureDistance(featurePoints, FeatureID.CHIN_BOTTOM,
                                    FeatureID.NOSE_BRIDGE);

                            double faceContourLeft = (featurePoints[FeatureID.CHIN_BOTTOM.getIndex()].getY()
                                    - featurePoints[FeatureID.FACE_CONTOUR2.getIndex()].getY())
                                    / (featurePoints[FeatureID.CHIN_BOTTOM.getIndex()].getX()
                                            - featurePoints[FeatureID.FACE_CONTOUR2.getIndex()].getX());
                            double faceContourRight = (featurePoints[FeatureID.CHIN_BOTTOM.getIndex()].getY()
                                    - featurePoints[FeatureID.FACE_CONTOUR12.getIndex()].getY())
                                    / (featurePoints[FeatureID.CHIN_BOTTOM.getIndex()].getX()
                                            - featurePoints[FeatureID.FACE_CONTOUR12.getIndex()].getX());

                            double bridgeLeftEyeDistance = featureDistance(featurePoints,
                                    FeatureID.LEFT_EYE_INNER_CORNER, FeatureID.NOSE_BRIDGE);
                            double bridgeRightEyeDistance = featureDistance(featurePoints,
                                    FeatureID.RIGHT_EYE_INNER_CORNER, FeatureID.NOSE_BRIDGE);

                            properties.get("eyeSize/eyeDistance").add(averageEyeSize / eyeDistance);
                            properties.get("eyeSizeDisparity")
                                    .add(Math.abs(leftEyeSize - rightEyeSize) / averageEyeSize);
                            properties.get("bridgeToEyeDisparity")
                                    .add(Math.abs(bridgeLeftEyeDistance - bridgeRightEyeDistance)
                                            / ((bridgeLeftEyeDistance + bridgeRightEyeDistance) / 2));
                            properties.get("eyeDistance/mouthLength").add(eyeDistance / mouthLength);
                            properties.get("eyeDistance/noseHeight").add(eyeDistance / noseHeight);
                            properties.get("eyeSize/mouthLength").add(eyeDistance / mouthLength);
                            properties.get("eyeSize/noseHeight").add(eyeDistance / noseHeight);
                            properties.get("mouthLength/mouthHeight").add(mouthLength / mouthHeight);
                            properties.get("chinHeight/noseHeight").add(chinHeight / noseHeight);
                            properties.get("chinHeight/chinToBridgeHeight")
                                    .add(chinHeight / chinToBridgeHeight);
                            properties.get("noseHeight/chinToBridgeHeight")
                                    .add(noseHeight / chinToBridgeHeight);
                            properties.get("mouthHeight/chinToBridgeHeight")
                                    .add(mouthHeight / chinToBridgeHeight);
                            properties.get("faceCountourAngle")
                                    .add(Math.toDegrees(Math.atan((faceContourLeft - faceContourRight)
                                            / (1 + faceContourLeft * faceContourRight))));
                        }

                        FSDK.FreeImage(imageHandle);
                    }
                }

                System.out.format("%32s\t%8s\t%8s\t%3s%n", "Property", "", "", "c");
                System.out.println(new String(new char[76]).replace("\0", "-"));

                ArrayList<Entry<String, ArrayList<Double>>> propertyList = new ArrayList<>(
                        properties.entrySet());
                Collections.sort(propertyList, new Comparator<Entry<String, ArrayList<Double>>>() {
                    @Override
                    public int compare(Entry<String, ArrayList<Double>> arg0,
                            Entry<String, ArrayList<Double>> arg1) {
                        DescriptiveStatistics dStats0 = new DescriptiveStatistics(listToArray(arg0.getValue()));
                        DescriptiveStatistics dStats1 = new DescriptiveStatistics(listToArray(arg1.getValue()));
                        return new Double(dStats0.getStandardDeviation() / dStats0.getMean())
                                .compareTo(dStats1.getStandardDeviation() / dStats1.getMean());
                    }
                });

                for (Entry<String, ArrayList<Double>> property : propertyList) {
                    DescriptiveStatistics dStats = new DescriptiveStatistics(listToArray(property.getValue()));
                    System.out.format("%32s\t%4f\t%4f\t%3s%n", property.getKey(), dStats.getMean(),
                            dStats.getStandardDeviation(),
                            Math.round(dStats.getStandardDeviation() / dStats.getMean() * 100) + "%");
                }

                System.out.println("\n");
                faceProperties.put(directory, properties);
            }
        }

        for (String propertyName : propertyNames) {
            DefaultBoxAndWhiskerCategoryDataset dataset = new DefaultBoxAndWhiskerCategoryDataset();
            for (Entry<String, Map<String, ArrayList<Double>>> face : faceProperties.entrySet()) {
                dataset.add(face.getValue().get(propertyName), "Default Series", face.getKey());
            }

            PropertyBoxWhisker plot = new PropertyBoxWhisker(propertyName, dataset);
            plot.pack();
            plot.setVisible(true);
        }
    }
}

From source file:mase.deprecated.FastMathTest.java

public static void main(String[] args) {
    double MIN = -10;
    double MAX = 10;
    int N = 10000;
    DescriptiveStatistics diff = new DescriptiveStatistics();
    DescriptiveStatistics diffJava = new DescriptiveStatistics();
    long tFast = 0, tNormal = 0, tBounded = 0, tJava = 0;
    for (int i = 0; i < N; i++) {
        double x = Math.random() * (MAX - MIN) + MIN;
        long t = System.nanoTime();
        double v1 = (1.0 / (1.0 + FastMath.expQuick(-1 * x)));
        tFast += System.nanoTime() - t;
        t = System.nanoTime();/*  ww w  .  j  a v a  2  s .c  o m*/
        double v2 = (1.0 / (1.0 + FastMath.exp(-1 * x)));
        tNormal += System.nanoTime() - t;
        t = System.nanoTime();
        double v3 = (1.0 / (1.0 + BoundMath.exp(-1 * x)));
        tBounded += System.nanoTime() - t;
        t = System.nanoTime();
        double v4 = (1.0 / (1.0 + Math.exp(-1 * x)));
        tJava += System.nanoTime() - t;
        diff.addValue(Math.abs(v1 - v2));
        diffJava.addValue(Math.abs(v3 - v1));
    }

    System.out.println("MAX: " + diff.getMax());
    System.out.println("MEAN: " + diff.getMean());
    System.out.println("MAX JAVA: " + diffJava.getMax());
    System.out.println("MEAN JAVA: " + diffJava.getMean());

    System.out.println("Fast: " + tFast);
    System.out.println("Normal: " + tNormal);
    System.out.println("Bounded: " + tBounded);
    System.out.println("Java: " + tJava);
}

From source file:cc.redberry.core.performance.StableSort.java

/**
 * @param args the command line arguments
 *//*  w ww  .  j  a v  a  2 s .  c  o m*/
public static void main(String[] args) {
    try {

        //burn JVM
        BitsStreamGenerator bitsStreamGenerator = new Well19937c();

        for (int i = 0; i < 1000; ++i)
            nextArray(1000, bitsStreamGenerator);

        System.out.println("!");
        BufferedWriter timMeanOut = new BufferedWriter(
                new FileWriter("/home/stas/Projects/stableSort/timMean.dat"));
        BufferedWriter insertionMeanOut = new BufferedWriter(
                new FileWriter("/home/stas/Projects/stableSort/insertionMean.dat"));

        BufferedWriter timMaxOut = new BufferedWriter(
                new FileWriter("/home/stas/Projects/stableSort/timMax.dat"));
        BufferedWriter insertionMaxOut = new BufferedWriter(
                new FileWriter("/home/stas/Projects/stableSort/insertionMax.dat"));

        BufferedWriter timSigOut = new BufferedWriter(
                new FileWriter("/home/stas/Projects/stableSort/timSig.dat"));
        BufferedWriter insertionSigOut = new BufferedWriter(
                new FileWriter("/home/stas/Projects/stableSort/insertionSig.dat"));

        DescriptiveStatistics timSort;
        DescriptiveStatistics insertionSort;

        int tryies = 200;
        int arrayLength = 0;
        for (; arrayLength < 1000; ++arrayLength) {

            int[] coSort = nextArray(arrayLength, bitsStreamGenerator);

            timSort = new DescriptiveStatistics();
            insertionSort = new DescriptiveStatistics();
            for (int i = 0; i < tryies; ++i) {
                int[] t1 = nextArray(arrayLength, bitsStreamGenerator);
                int[] t2 = t1.clone();

                long start = System.currentTimeMillis();
                ArraysUtils.timSort(t1, coSort);
                long stop = System.currentTimeMillis();
                timSort.addValue(stop - start);

                start = System.currentTimeMillis();
                ArraysUtils.insertionSort(t2, coSort);
                stop = System.currentTimeMillis();
                insertionSort.addValue(stop - start);
            }
            timMeanOut.write(arrayLength + "\t" + timSort.getMean() + "\n");
            insertionMeanOut.write(arrayLength + "\t" + insertionSort.getMean() + "\n");

            timMaxOut.write(arrayLength + "\t" + timSort.getMax() + "\n");
            insertionMaxOut.write(arrayLength + "\t" + insertionSort.getMax() + "\n");

            timSigOut.write(arrayLength + "\t" + timSort.getStandardDeviation() + "\n");
            insertionSigOut.write(arrayLength + "\t" + insertionSort.getStandardDeviation() + "\n");
        }
        timMeanOut.close();
        insertionMeanOut.close();
        timMaxOut.close();
        insertionMaxOut.close();
        timSigOut.close();
        insertionSigOut.close();
    } catch (IOException ex) {
        Logger.getLogger(StableSort.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.weibo.motan.demo.client.DemoRpcClient.java

public static void main(String[] args) throws Exception {
    final DescriptiveStatistics stats = new SynchronizedDescriptiveStatistics();

    int threads = Integer.parseInt(args[0]);

    DubboBenchmark.BenchmarkMessage msg = prepareArgs();
    final byte[] msgBytes = msg.toByteArray();

    int n = 1000000;
    final CountDownLatch latch = new CountDownLatch(n);

    ExecutorService es = Executors.newFixedThreadPool(threads);

    final AtomicInteger trans = new AtomicInteger(0);
    final AtomicInteger transOK = new AtomicInteger(0);

    ApplicationContext ctx = new ClassPathXmlApplicationContext(
            new String[] { "classpath:motan_demo_client.xml" });

    MotanDemoService service = (MotanDemoService) ctx.getBean("motanDemoReferer");

    long start = System.currentTimeMillis();
    for (int i = 0; i < n; i++) {
        es.submit(() -> {//from  w  w  w.  j  ava2s  . c o  m
            try {

                long t = System.currentTimeMillis();
                DubboBenchmark.BenchmarkMessage m = testSay(service, msgBytes);
                t = System.currentTimeMillis() - t;
                stats.addValue(t);

                trans.incrementAndGet();

                if (m != null && m.getField1().equals("OK")) {
                    transOK.incrementAndGet();
                }

            } finally {
                latch.countDown();
            }
        });
    }

    latch.await();

    start = System.currentTimeMillis() - start;

    System.out.printf("sent     requests    : %d\n", n);
    System.out.printf("received requests    : %d\n", trans.get());
    System.out.printf("received requests_OK : %d\n", transOK.get());
    System.out.printf("throughput  (TPS)    : %d\n", n * 1000 / start);

    System.out.printf("mean: %f\n", stats.getMean());
    System.out.printf("median: %f\n", stats.getPercentile(50));
    System.out.printf("max: %f\n", stats.getMax());
    System.out.printf("min: %f\n", stats.getMin());

    System.out.printf("99P: %f\n", stats.getPercentile(90));

}

From source file:mase.deprecated.SelectionBenchmark.java

public static void main(String[] args) {

    int L = 100, N = 10000;
    double[] truncationP = new double[] { 0.25, 0.50, 0.75 };
    int[] tournamentP = new int[] { 2, 5, 7, 10 };

    DescriptiveStatistics[] truncationStat = new DescriptiveStatistics[truncationP.length];
    for (int i = 0; i < truncationStat.length; i++) {
        truncationStat[i] = new DescriptiveStatistics();
    }/*from   w ww  .  j a v  a  2s .  c  o m*/
    DescriptiveStatistics[] tournamentStat = new DescriptiveStatistics[tournamentP.length];
    DescriptiveStatistics[] tournamentStat2 = new DescriptiveStatistics[tournamentP.length];
    for (int i = 0; i < tournamentStat.length; i++) {
        tournamentStat[i] = new DescriptiveStatistics();
        tournamentStat2[i] = new DescriptiveStatistics();
    }
    DescriptiveStatistics rouletteStat = new DescriptiveStatistics();
    DescriptiveStatistics rouletteStat2 = new DescriptiveStatistics();
    DescriptiveStatistics baseStat = new DescriptiveStatistics();

    for (int i = 0; i < N; i++) {
        // generate test vector
        double[] test = new double[L];
        for (int j = 0; j < L; j++) {
            test[j] = Math.random();
        }

        // truncation
        for (int p = 0; p < truncationP.length; p++) {
            double[] v = Arrays.copyOf(test, test.length);
            Arrays.sort(v);
            int nElites = (int) Math.ceil(truncationP[p] * test.length);
            double cutoff = v[test.length - nElites];
            double[] weights = new double[test.length];
            for (int k = 0; k < test.length; k++) {
                weights[k] = test[k] >= cutoff ? test[k] * (1 / truncationP[p]) : 0;
            }
            truncationStat[p].addValue(sum(weights));
        }

        // tournament
        for (int p = 0; p < tournamentP.length; p++) {
            double[] weights = new double[test.length];
            HashSet<Integer> added = new HashSet<Integer>();
            for (int k = 0; k < test.length; k++) {
                int idx = makeTournament(test, tournamentP[p]);
                weights[idx] += test[idx];
                added.add(idx);
            }
            tournamentStat2[p].addValue(added.size());
            tournamentStat[p].addValue(sum(weights));
        }

        // roulette
        double[] weights = new double[test.length];
        HashSet<Integer> added = new HashSet<Integer>();
        for (int k = 0; k < test.length; k++) {
            int idx = roulette(test);
            weights[idx] += test[idx];
            added.add(idx);
        }
        rouletteStat.addValue(sum(weights));
        rouletteStat2.addValue(added.size());

        // base
        baseStat.addValue(sum(test));
    }

    for (int p = 0; p < truncationP.length; p++) {
        System.out.println("Truncation\t" + truncationP[p] + "\t" + truncationStat[p].getMean() + "\t"
                + truncationStat[p].getStandardDeviation() + "\t" + ((int) Math.ceil(L * truncationP[p]))
                + "\t 0");
    }
    for (int p = 0; p < tournamentP.length; p++) {
        System.out.println("Tournament\t" + tournamentP[p] + "\t" + tournamentStat[p].getMean() + "\t"
                + tournamentStat[p].getStandardDeviation() + "\t" + tournamentStat2[p].getMean() + "\t"
                + tournamentStat2[p].getStandardDeviation());
    }
    System.out.println("Roulette\t\t" + rouletteStat.getMean() + "\t" + rouletteStat.getStandardDeviation()
            + "\t" + rouletteStat2.getMean() + "\t" + rouletteStat2.getStandardDeviation());
    System.out.println(
            "Base    \t\t" + baseStat.getMean() + "\t" + baseStat.getStandardDeviation() + "\t " + L + "\t0");
}

From source file:es.upm.oeg.tools.rdfshapes.utils.CadinalityResultGenerator.java

public static void main(String[] args) throws Exception {

    String endpoint = "http://3cixty.eurecom.fr/sparql";

    List<String> classList = Files.readAllLines(Paths.get(classListPath), Charset.defaultCharset());

    String classPropertyQueryString = readFile(classPropertyQueryPath, Charset.defaultCharset());
    String propertyCardinalityQueryString = readFile(propertyCardinalityQueryPath, Charset.defaultCharset());
    String individualCountQueryString = readFile(individualCountQueryPath, Charset.defaultCharset());

    DecimalFormat df = new DecimalFormat("0.0000");

    //Create the Excel workbook and sheet
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sheet = wb.createSheet("Cardinality");

    int currentExcelRow = 0;
    int classStartRow = 0;

    for (String clazz : classList) {

        Map<String, String> litMap = new HashMap<>();
        Map<String, String> iriMap = ImmutableMap.of("class", clazz);

        String queryString = bindQueryString(individualCountQueryString,
                ImmutableMap.of(IRI_BINDINGS, iriMap, LITERAL_BINDINGS, litMap));

        int individualCount;
        List<RDFNode> c = executeQueryForList(queryString, endpoint, "c");
        if (c.size() == 1) {
            individualCount = c.get(0).asLiteral().getInt();
        } else {//from w w  w  . ja  va  2s  . c o  m
            continue;
        }

        // If there are zero individuals, continue
        if (individualCount == 0) {
            throw new IllegalStateException("Check whether " + classListPath + " and " + endpoint + " match.");
        }

        //            System.out.println("***");
        //            System.out.println("### **" + clazz + "** (" + individualCount + ")");
        //            System.out.println("***");
        //            System.out.println();

        classStartRow = currentExcelRow;
        XSSFRow row = sheet.createRow(currentExcelRow);
        XSSFCell cell = row.createCell(0);
        cell.setCellValue(clazz);
        cell.getCellStyle().setAlignment(CellStyle.ALIGN_CENTER);

        queryString = bindQueryString(classPropertyQueryString,
                ImmutableMap.of(IRI_BINDINGS, iriMap, LITERAL_BINDINGS, litMap));

        List<RDFNode> nodeList = executeQueryForList(queryString, endpoint, "p");

        for (RDFNode property : nodeList) {
            if (property.isURIResource()) {

                DescriptiveStatistics stats = new DescriptiveStatistics();

                String propertyURI = property.asResource().getURI();
                //                    System.out.println("* " + propertyURI);
                //                    System.out.println();

                XSSFRow propertyRow = sheet.getRow(currentExcelRow);
                if (propertyRow == null) {
                    propertyRow = sheet.createRow(currentExcelRow);
                }
                currentExcelRow++;

                XSSFCell propertyCell = propertyRow.createCell(1);
                propertyCell.setCellValue(propertyURI);

                Map<String, String> litMap2 = new HashMap<>();
                Map<String, String> iriMap2 = ImmutableMap.of("class", clazz, "p", propertyURI);

                queryString = bindQueryString(propertyCardinalityQueryString,
                        ImmutableMap.of(IRI_BINDINGS, iriMap2, LITERAL_BINDINGS, litMap2));

                List<Map<String, RDFNode>> solnMaps = executeQueryForList(queryString, endpoint,
                        ImmutableSet.of("card", "count"));

                int sum = 0;
                List<CardinalityCount> cardinalityList = new ArrayList<>();
                if (solnMaps.size() > 0) {

                    for (Map<String, RDFNode> soln : solnMaps) {
                        int count = soln.get("count").asLiteral().getInt();
                        int card = soln.get("card").asLiteral().getInt();

                        for (int i = 0; i < count; i++) {
                            stats.addValue(card);
                        }

                        CardinalityCount cardinalityCount = new CardinalityCount(card, count,
                                (((double) count) / individualCount) * 100);
                        cardinalityList.add(cardinalityCount);
                        sum += count;
                    }

                    // Check for zero cardinality instances
                    int count = individualCount - sum;
                    if (count > 0) {
                        for (int i = 0; i < count; i++) {
                            stats.addValue(0);
                        }
                        CardinalityCount cardinalityCount = new CardinalityCount(0, count,
                                (((double) count) / individualCount) * 100);
                        cardinalityList.add(cardinalityCount);
                    }
                }

                Map<Integer, Double> cardMap = new HashMap<>();
                for (CardinalityCount count : cardinalityList) {
                    cardMap.put(count.getCardinality(), count.getPrecentage());
                }

                XSSFCell instanceCountCell = propertyRow.createCell(2);
                instanceCountCell.setCellValue(individualCount);

                XSSFCell minCell = propertyRow.createCell(3);
                minCell.setCellValue(stats.getMin());

                XSSFCell maxCell = propertyRow.createCell(4);
                maxCell.setCellValue(stats.getMax());

                XSSFCell p1 = propertyRow.createCell(5);
                p1.setCellValue(stats.getPercentile(1));

                XSSFCell p99 = propertyRow.createCell(6);
                p99.setCellValue(stats.getPercentile(99));

                XSSFCell mean = propertyRow.createCell(7);
                mean.setCellValue(df.format(stats.getMean()));

                for (int i = 0; i < 21; i++) {
                    XSSFCell dataCell = propertyRow.createCell(8 + i);
                    Double percentage = cardMap.get(i);
                    if (percentage != null) {
                        dataCell.setCellValue(df.format(percentage));
                    } else {
                        dataCell.setCellValue(0);
                    }
                }

                //                    System.out.println("| Min Card. |Max Card. |");
                //                    System.out.println("|---|---|");
                //                    System.out.println("| ? | ? |");
                //                    System.out.println();

            }
        }

        //System.out.println("class start: " + classStartRow + ", class end: " + (currentExcelRow -1));
        //We have finished writting properties of one class, now it's time to merge the cells
        int classEndRow = currentExcelRow - 1;
        if (classStartRow < classEndRow) {
            sheet.addMergedRegion(new CellRangeAddress(classStartRow, classEndRow, 0, 0));
        }

    }

    String filename = "3cixty.xls";
    FileOutputStream fileOut = new FileOutputStream(filename);
    wb.write(fileOut);
    fileOut.close();
}