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

标签 java arrays standard-deviation

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

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

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

编辑:我需要找到变量 RT Correct 和 RTin Correct 的方差/标准差(我需要找到每个变量的方差和标准差)。我认为我的程序当前没有保存这些数字,所以我猜我需要将这些数字放入一个数组中以便之后进行分析。

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

最佳答案

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

  • 第一步:求三个变量的平均值,即

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

    a=(totalCorrect-Mean)*(totalCorrect-Mean)
    b=(totalRTCorrect-Mean)*(totalRTCorrect-Mean)
    c=(totalRTIncorrect-Mean)*(totalRTIncorrect-Mean)
    
  • 第三步:计算 a、b 和 c 的平均值

    mean2= (a+b+c)/3.
    
  • 第四步:取mean2的平方根。

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

}
 }

关于java - 如何从我生成的数据中获取标准差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20387989/

相关文章:

javascript - 为什么javascript程序不运行

C++ 标准偏差分配需要帮助更正错误

python - 计算平均值和标准偏差并忽略 0 值

java - 不希望图像作为附件,但需要内联

java - 从 Neo4j 密码查询返回 boolean 值

c - 将二进制文件读入同一个后Unsigned Char为(null)

javascript - 大写字母的函数,错误

python - 使用numpy计算列表上的标准差忽略零

java - 更快地更新图像像素

使用 Maven 导入库时出现 java.lang.UnsatisfiedLinkError (Java3D)