Pacific-Design.com

    
Home Index

1. Algorithms

2. Mean Median Variance

Algorithms / Mean Median Variance /

Mean, Median, Variance and Standard Deviation StdDev

package test;                                                                                                                                                                                                                                                                                                                                                                                                                                                                      

import java.util.ArrayList;
import java.util.List;

public class Test {

    /*---------------------------------------------------------------------*/
    public static void main(String[] args) {

        List<Double> data = new ArrayList<>();
        data.add(600.0);
        data.add(470.0);
        data.add(170.0);
        data.add(430.0);
        data.add(300.0);  
        
        
        Statistics stat = new Statistics(data);
        System.out.println("Mean     : " + stat.getMean());
        System.out.println("Variance : " + stat.getVariance());
        System.out.format("StdDev   : %.3f", stat.getStdDev());
        
        double max = stat.getMean() + stat.getStdDev();
        double min = stat.getMean() - stat.getStdDev();
        
        System.out.println("\n------------------------");
        System.out.format("Max : %.2f\n", max);
        System.out.format("Min : %.2f\n", min);

    }
    /*---------------------------------------------------------------------*/
    
}
/*

Mean     : 394.0
Variance : 21704.0
StdDev   : 147.323
------------------------
Max : 541.32
Min : 246.68

*/

package test;

import java.util.Arrays;
import java.util.List;

public class Statistics {

    List<Double> data;
    double size;

    /*----------------------------------------------------------------------*/
    public Statistics(List<Double> data) {
        this.data = data;
        size = data.size();
    }

    /*----------------------------------------------------------------------*/
    double getMean() {

        double sum = 0.0;
        for (double a : data) {
            sum += a;
        }
        return sum / size;
    }

    /*----------------------------------------------------------------------*/
    double getVariance() {

        double mean = getMean();
        double temp = 0;
        for (double a : data) {
            temp += (mean - a) * (mean - a);
        }
        return temp / size;
    }

    /*----------------------------------------------------------------------*/
    double getStdDev() {
        return Math.sqrt(getVariance());
    }

    /*----------------------------------------------------------------------*/
    public double median() {
        double[] b = new double[data.size()];
        System.arraycopy(data, 0, b, 0, b.length);
        Arrays.sort(b);

        if (data.size() % 2 == 0) {
            return (b[(b.length / 2) - 1] + b[b.length / 2]) / 2.0;
        } else {
            return b[b.length / 2];
        }
    }
    /*----------------------------------------------------------------------*/
}                                                                                                                                                                                                                                                                                                                                                                                                     

import java.util.ArrayList;
import java.util.List;

public class Test {

    /*---------------------------------------------------------------------*/
    public static void main(String[] args) {

        List<Double> data = new ArrayList<>();
        data.add(600.0);
        data.add(470.0);
        data.add(170.0);
        data.add(430.0);
        data.add(300.0);  
        
        
        Statistics stat = new Statistics(data);
        System.out.println("Mean     : " + stat.getMean());
        System.out.println("Variance : " + stat.getVariance());
        System.out.format("StdDev   : %.3f", stat.getStdDev());
        
        double max = stat.getMean() + stat.getStdDev();
        double min = stat.getMean() - stat.getStdDev();
        
        System.out.println("\n------------------------");
        System.out.format("Max : %.2f\n", max);
        System.out.format("Min : %.2f\n", min);

    }
    /*---------------------------------------------------------------------*/
    
}
/*

Mean     : 394.0
Variance : 21704.0
StdDev   : 147.323
------------------------
Max : 541.32
Min : 246.68

*/

package test;

import java.util.Arrays;
import java.util.List;

public class Statistics {

    List<Double> data;
    double size;

    /*----------------------------------------------------------------------*/
    public Statistics(List<Double> data) {
        this.data = data;
        size = data.size();
    }

    /*----------------------------------------------------------------------*/
    double getMean() {

        double sum = 0.0;
        for (double a : data) {
            sum += a;
        }
        return sum / size;
    }

    /*----------------------------------------------------------------------*/
    double getVariance() {

        double mean = getMean();
        double temp = 0;
        for (double a : data) {
            temp += (mean - a) * (mean - a);
        }
        return temp / size;
    }

    /*----------------------------------------------------------------------*/
    double getStdDev() {
        return Math.sqrt(getVariance());
    }

    /*----------------------------------------------------------------------*/
    public double median() {
        double[] b = new double[data.size()];
        System.arraycopy(data, 0, b, 0, b.length);
        Arrays.sort(b);

        if (data.size() % 2 == 0) {
            return (b[(b.length / 2) - 1] + b[b.length / 2]) / 2.0;
        } else {
            return b[b.length / 2];
        }
    }
    /*----------------------------------------------------------------------*/
}