Click here to Skip to main content
15,914,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello

I am trying to calculate the standard variation in Java. This is a school assignment and I have worked out most of what I need except I can't get the std dev.

I know I need to:

Get the count, calculate the sum, get the average, square each deviation, add up the squares and divide by -1. I have much of that, I just can't seem to pull it together.

Here is what I have.

Java
/**
This class is used to calculate the average and standard deviation
of a data set.
*/

public class DataSet{
	
	private double sum;
	private double sumSquare;
	private int counter;
	
	/**Constructs a DataSet object to hold the
	 * total number of inputs, sum and square
	 */
	
	public DataSet(){
		
		sum = 0;
		sumSquare = 0;
		counter = 0;
	}
	
	/**Adds a value to this data set
	 * @param x the input value
	 */
	
	public void add(double x){
		
		sum = sum + x;
		sumSquare = sumSquare + x * x;
		counter++;
		
	}
	
	/**Calculate average of dataset
	 * @return average, the average of the set
	 */
	
	public double getAverage(){
		
		double avg = sum / counter;
		
		return avg;
		
	}
	
	/**Get the total inputs values
	 * @return n, the total number of inputs
	 */
	
	public int getCount(){
		
		return counter;
					
	}
	
	
	public double getStandardDeviation(){
		
		
		
	}
}
Posted
Updated 28-Jul-13 15:12pm
v2
Comments
[no name] 28-Jul-13 21:19pm    
What exactly does "I just can't seem to pull it together" mean? Sounds to me that you know exactly what you need to do. You just need to re-read exactly what you wrote here, take a deep breath, relax, and do one thing at a time.

1 solution

If there's no performance penalty incurred by holding the data set in memory and calculating this on the fly, this example might point you in the right direction;

Java
public class DataSet {

    private final List<Double> values = new ArrayList<Double>();


    public void add(final double value) {
        values.add(value);
    }

    public double getSum() {
        double sum = 0;
        for(double value : values)
            sum += value;
    }

    public double getAverage() {
        if (values.size() == 0)
            return 0;

        return getSum() / values.size();
    }

    public double getPopulationStandardDeviation() {
        final double average = getAverage();
        double sum = 0;

        for(double value : values) {
            final double v = (value - average);
            sum += v * v;
        }

        return sum;
    }

    public double getStandardDeviation() {
        if (values.size() == 0)
            return 0;

        // TODO: You probably want to divide by N-1 here rather than N (where N is values.size()) if values are a random set from
        // a larger population
        return Math.sqrt(getPopulationStandardDeviation() / values.size());
    }
}


Hope this helps,
Fredrik
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900