查找2D阵列的标准偏差

时间:2013-09-29 11:20:03

标签: java arrays math standard-deviation

我正在努力为我的任务找到标准偏差,我已经做了其他所有事情,而我只是在最后一点上苦苦挣扎。我的作业问题简而言之是: 我收到了一组学生的作业结果的文本文件(必须是任何数字)。给出的唯一数据是学生姓名,粉丝及其结果,您需要计算他们的平均成绩和标准差。输出应如下所示:

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

我知道我需要使用的等式,我知道我需要使用哪些数字我只是不知道如何获得它们。我会告诉你我需要做什么,希望你能进一步了解我的代码:

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

我正在使用3个课程,我的主要课程Topic Management,一个Student课程,其中包含一个带有学生姓名和粉丝的数组(两者都与我的问题无关,但我认为我最好设置场景) ,最后是StudentsMarks Class,其中包含我的StdDec方法,如下所示:

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;
}

3 个答案:

答案 0 :(得分:1)

你需要2个循环,因为你需要从每一行中减去平均值。

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);

答案 1 :(得分:0)

以下是我的表现。

您可以在没有数组的情况下进行管理。只需跟踪值的总和,平方值的平方和值的数量。

我只为展示目的添加了成绩列表。不属于均值或方差计算的一部分。

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;
    }
}

答案 2 :(得分:0)

我只想指出,为了估计样本的标准偏差,通常使用等式

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

其中sum是所有二次偏差的总和,N是样本大小。所以在你的例子中,你必须除以9,而不是10.也许这会产生你期望的数字。

(见http://en.wikipedia.org/wiki/Unbiased_estimation_of_standard_deviation