如何从我生成的数据中获取标准偏差

时间:2013-12-04 23:05:00

标签: java arrays standard-deviation

我已经建立了这个相当简单的程序,用于我的荣誉论文(心理学),它基本上模拟了人们认为人类做出决定的一种方式。我有实际的人体数据,我正在从这个程序中得到的结果,以确定这个模型是否准确。我的编程经验相当有限,但我需要能够收集变量rtCorrect和rtIncorrect的标准偏差。我猜这将涉及将数字放入数组中。

我知道你们喜欢引导人们找到正确的答案,而不是仅仅把它送给他们这很好但是请记住这不是编程功课而且我不打算成为一名程序员而且我有一篇重要论文到期本星期!如果有人能给我一些非常具体的建议,那就太棒了,谢谢!

顺便说一下,你现在可以忽略已注释掉的部分(除非你有办法让我在不用2个月的情况下运行它)!

编辑:我需要找到RTcorrect和RTincorrect变量的方差/标准偏差(我需要找到每个的方差和标准差)。我不认为我的程序目前保存这些数字所以我猜我需要将这些数字放在一个数组中以便进行分析。

import java.util.Random;

public class Thesis4 {

    public static void main (String [] args){

        int totalRTIncorrect = 0;
        int totalRTCorrect = 0;
        int totalCorrect = 0;
        int totalIncorrect = 0;

        for (int j = 0; j < 10000;j++){
            int correct = 0;
            int incorrect = 0;
            int incorrect2 = 0;
            int incorrect3 =0;
            int rtCorrect = 0;
            int rtIncorrect = 0;
            int time = 0;

            while(correct<88 && incorrect<88){
                Random rand = new Random();
                int pickedNumber = rand.nextInt(400);

                if (pickedNumber<108){
                    correct++;
                    //incorrect--;
                    //incorrect2--;
                    //incorrect3--;
                    time++;

                }
                else if (pickedNumber>107 && pickedNumber<208){
                    incorrect++;
                    //  correct--;
                    //  incorrect2--;
                    //  incorrect3--;
                    time++;
                }
                else if (pickedNumber>207&&pickedNumber<309){
                    incorrect2++;
                    //  correct--;
                    //  incorrect--;
                    //  incorrect3--;
                    time++;
                }
                else if (pickedNumber>308){
                    incorrect3++;
                    //  correct--;
                    //  incorrect--;
                    //  incorrect2--;
                    time++;
                }
            }
            if (correct == 88){
                rtCorrect = time;
                totalCorrect++;
            }
            else if (incorrect == 88){
                rtIncorrect = time;
                totalIncorrect++;
            }
            else if (incorrect2 == 88){
                rtIncorrect = time;
                totalIncorrect++;
            }
            else if (incorrect3 == 88){
                rtIncorrect=time;
                totalIncorrect++;
            }

            totalRTIncorrect = totalRTIncorrect + rtIncorrect;
            totalRTCorrect = totalRTCorrect + rtCorrect;
        }
        System.out.printf ("Total Correct Responses: %d \nTotal Incorrect Responses: %d", totalCorrect, totalIncorrect);
        System.out.printf ("\nTotal Correct RT's: %d \nTotal Incorrect RT's: %d\n", totalRTCorrect, totalRTIncorrect);
    }
}

1 个答案:

答案 0 :(得分:3)

如果要计算三个变量“totalCorrect”,“totalRTCorrect”和“totalRTIncorrect”之间的标准差,最简单的方法是分四步完成:

  • 步骤1:获取三个变量的平均值,即

    Mean= (totalCorrect+totalRTCorrect+totalRTIncorrect)/3.0
    
  • 第2步:计算值:

    a=(totalCorrect-Mean)*(totalCorrect-Mean)
    b=(totalRTCorrect-Mean)*(totalRTCorrect-Mean)
    c=(totalRTIncorrect-Mean)*(totalRTIncorrect-Mean)
    
  • 步骤3:计算a,b和c的平均值

    mean2= (a+b+c)/3.
    
  • Step4:取平均值2的平方根。

    std=sqrt(mean2)
    

这将是三个变量的标准偏差。 因此,您的代码的新版本应如下所示:

    import java.util.Random;
    import java.lang.Math;

    public class Thesis4 {

public static void main (String [] args){

    int totalRTIncorrect = 0;
    int totalRTCorrect = 0;
    int totalCorrect = 0;
    int totalIncorrect = 0;

    for (int j = 0; j < 10000;j++){
        int correct = 0;
        int incorrect = 0;
        int incorrect2 = 0;
        int incorrect3 =0;
        int rtCorrect = 0;
        int rtIncorrect = 0;
        int time = 0;

        while(correct<88 && incorrect<88){
            Random rand = new Random();
            int pickedNumber = rand.nextInt(400);

            if (pickedNumber<108){
                correct++;
                //incorrect--;
                //incorrect2--;
                //incorrect3--;
                time++;

            }
            else if (pickedNumber>107 && pickedNumber<208){
                incorrect++;
                //  correct--;
                //  incorrect2--;
                //  incorrect3--;
                time++;
            }
            else if (pickedNumber>207&&pickedNumber<309){
                incorrect2++;
                //  correct--;
                //  incorrect--;
                //  incorrect3--;
                time++;
            }
            else if (pickedNumber>308){
                incorrect3++;
                //  correct--;
                //  incorrect--;
                //  incorrect2--;
                time++;
            }
        }
        if (correct == 88){
            rtCorrect = time;
            totalCorrect++;
        }
        else if (incorrect == 88){
            rtIncorrect = time;
            totalIncorrect++;
        }
        else if (incorrect2 == 88){
            rtIncorrect = time;
            totalIncorrect++;
        }
        else if (incorrect3 == 88){
            rtIncorrect=time;
            totalIncorrect++;
        }

        totalRTIncorrect = totalRTIncorrect + rtIncorrect;
        totalRTCorrect = totalRTCorrect + rtCorrect;
    }
    System.out.printf ("Total Correct Responses: %d \nTotal Incorrect Responses: %d", totalCorrect, totalIncorrect);
    System.out.printf ("\nTotal Correct RT's: %d \nTotal Incorrect RT's: %d\n", totalRTCorrect, totalRTIncorrect);

    //computation of the standard deviation of the three variables
    double meanValue=(totalCorrect+totalRTCorrect+totalRTIncorrect)/3.0; //step1

    double a= (totalCorrect-meanValue)*(totalCorrect-meanValue);         //step2
    double b= (totalRTCorrect-meanValue)*(totalRTCorrect-meanValue);
    double c= (totalRTIncorrect-meanValue)*(totalRTIncorrect-meanValue);

    double mean2=(a+b+c)/3.0;                                          //step3

    double standard_deviation=Math.sqrt(mean2);                        //step4
    System.out.printf ("\nThe standard deviation of the three variables is %f\n",standard_deviation);

}
 }