Question

I'm struggling to find the Standard Deviation for my assignment, i've done everything else and i'm just struggling with this last bit. My assignment question in a nutshell is: I am given a text file of assignment results from a group of students (has to be for any number). The only Data given is the students name, fan and their results, you need to calculate their averages grades and the standard deviation. The output should look like:

Student Name     FAN      Part 1 Part 2 Part 3 Part 4 Mark    Grade
Adam Adamson     adam0001 85.4   79.8   82.4   86.1   82.77%  DN
Bethany Bright   brig0001 89.7   85.6   84.2   82.9   84.92%  DN
Cameron Carlson  carl0001 55.45  49.82  60.4   42.27  50.23%  P
David Dawson     daws0001 72.6   78.49  80.2   65.88  74.46%  CR
Evelyn Ellis     elli0001 50.2   35.88  48.41  58.37  46.57%  FA
Frances Fitz     fitz0001 78.9   75.67  82.48  79.1   78.38%  DN
Greg Gregson     greg0001 24.3   32.88  29.72  28.4   30.05%  F
Harriett Hope    hope0001 52.2   58.93  61.5   63.44  60.12%  P
Ivan Indigo      indi0001 88.4   91.23  90.05  92.46  91.08%  HD
Jessica Jones    jone0001 82.33  89.74  81.3   84.85  85.84%  HD
                  Average 67.948 67.804 70.066 68.377 68.44%  CR
                                               StdDev 19.4442

I know the equation I need to use and I know which numbers I need to use I Just don't know how to get them. I'll show you what I need to do so hopefully you will understand my code further down:

the total of the averages divided by 10 = 68.442

sum = (82.77-68.442)^2 + (84.92-68.442)^2 + (50.23-68.442)^2 + ... + (85.84-68.442)^2
StdDev = Math.sqrt(sum/10)

stdDev = 19.4442

I am using 3 classes for this, my main class Topic Management, a Student class, which contains an array with the students name and fan (neither are really relevant to my question but I thought I had better just set the scene), and finally the StudentsMarks Class, which contains my StdDec method displayed below:

public static double StdDev() throws IOException
{
    String [][] marks = StudentMarks.StudentMarks();

    double mean = 0, average = 0, stdDev = 0;
    int row = 0;
    for(row = 0; row < marks.length; row++)
    {                                          //ROW,COL
        double score1 = Double.parseDouble(marks[row][2]);  //parsing the data from the array into a double
        double score2 = Double.parseDouble(marks[row][3]);
        double score3 = Double.parseDouble(marks[row][4]);
        double score4 = Double.parseDouble(marks[row][5]);

        average = score1*0.1 + score2*0.4 + score3*0.2 + score4*0.3;   

        mean = average/row;
        stdDev = Math.sqrt(Math.pow(average - mean,2)/row);   
    }  
    return stdDev;
}
Was it helpful?

Solution

u need 2 loops since u need to subtract average from every row.

double[] sum = new double[marks.length];

for(row = 0; row < marks.length; row++)
{                                          //ROW,COL
    double score1 = Double.parseDouble(marks[row][2]);  //parsing the data from the array into a double
    double score2 = Double.parseDouble(marks[row][3]);
    double score3 = Double.parseDouble(marks[row][4]);
    double score4 = Double.parseDouble(marks[row][5]);

    sum[row] = score1*0.1 + score2*0.4 + score3*0.2 + score4*0.3;   
    average += sum[row];
}
average = average/sum.length; //1
for(row = 0; row < sum.length; row++)
{
        mean += Math.pow(Math.abs(sum[row] - average),2);           
}
mean = mean/marks.length;//2
stdDev = Math.sqrt(mean);

OTHER TIPS

Here's how I'd do it.

You can manage it without an array. Just keep track of the sum of values, sum of values squared, and number of values.

I only added List of grades for display purposes. Not part of mean or variance calculations.

package reportcard;

import org.apache.commons.lang3.StringUtils;

/**
 * Student
 * @author Michael
 * @link http://stackoverflow.com/questions/19077442/finding-the-standard-deviation-from-a-2d-array
 * @since 9/29/13 12:19 PM
 */
public class Student {

    private final String name;

    public Student(String name) {
        if (StringUtils.isBlank(name)) throw new IllegalArgumentException("Name cannot be blank or null");
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        if (!name.equals(student.name)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return name.hashCode();
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("Student{");
        sb.append("name='").append(name).append('\'');
        sb.append('}');
        return sb.toString();
    }
}

package reportcard;

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

/**
 * Transcript
 * @author Michael
 * @link http://stackoverflow.com/questions/19077442/finding-the-standard-deviation-from-a-2d-array
 * @since 9/29/13 12:21 PM
 */
public class Transcript {
    private final Student student;
    private final List<Double> grades;
    private double sum = 0.0;
    private double sumsq = 0.0;
    private int numGrades = 0;

    public static void main(String[] args) {
        Student student = new Student("Fatty Arbuckle");
        Transcript transcript = new Transcript(student);
        double [] grades = { 2,  4,  4,  4,  5,  5,  7,  9 };
        for (double grade : grades) {
            transcript.addGrade(grade);
        }
        System.out.println(transcript);

    }

    public Transcript(Student student) {
        if (student == null) throw new IllegalArgumentException("Student cannot be null");
        this.student = student;
        this.grades = new ArrayList<Double>();
    }

    public void addGrade(double grade) {
        if (grade > 0.0) {
            this.grades.add(grade);
            ++this.numGrades;
            this.sum += grade;
            this.sumsq += grade*grade;
        }
    }

    public double getMean() {
        double mean = 0.0;
        if (this.numGrades > 0) {
            mean = this.sum/this.numGrades;
        }
        return mean;
    }

    public double getVariance() {
        double stddev = 0.0;
        if (this.numGrades > 0) {
            stddev = Math.sqrt((this.sumsq-this.sum*this.sum/this.numGrades)/this.numGrades);
        }
        return  stddev;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("Transcript{");
        sb.append("student=").append(student);
        sb.append(", grades=").append(grades);
        sb.append(", sum=").append(sum);
        sb.append(", sumsq=").append(sumsq);
        sb.append(", numGrades=").append(numGrades);
        sb.append(", mean=").append(this.getMean());
        sb.append(", variance=").append(this.getVariance());
        sb.append('}');
        return sb.toString();
    }
}

package reportcard;

/**
 * Grade encapsulates grades with values
 * @author Michael
 * @link http://stackoverflow.com/questions/16744116/java-grade-class-letter-grade-to-number/16744210#16744210
 * @since 5/24/13 6:01 PM
 */
public enum Grade {
    APLUS("A+", 4.3),
    A("A", 4.0),
    AMINUS("A-", 3.7),
    BPLUS("B+", 3.3),
    B("B", 3.0),
    BMINUS("B-", 2.7),
    CPLUS("C+", 2.3),
    C("C", 2.0),
    CMINUS("C-", 1.7),
    DPLUS("D+", 1.3),
    D("D", 1.0),
    DMINUS("D-", 0.7),
    F("F", 0.0);

    private final String symbol;
    private final double value;

    Grade(String symbol, double value) {
        this.symbol = symbol;
        this.value = value;
    }

    public String getSymbol() {
        return symbol;
    }

    public double getValue() {
        return value;
    }
}

I just would like to note that to estimate the standard deviation from a sample, one normally uses the equation

stdDev=Math.sqrt(sum/N-1);

where sum is the sum over all quadratic deviations and N is the sample size. So in your example, you would have to divide by 9, not 10. Maybe this produces the numbers you expect.

(see http://en.wikipedia.org/wiki/Unbiased_estimation_of_standard_deviation)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top