计算标准偏差 [英] calculating standard deviation

查看:99
本文介绍了计算标准偏差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,该程序计算随机产生的温度下的标准偏差.我相信我的算法是正确的,但是代码只使用Temp数组中的第一个数字,然后从均值中减去它,因此我无法弄清楚为什么它不遍历整个数组.我已经尝试过foreach循环,但是总是无法从int类型转换我.这是我到目前为止的代码,对您的帮助将不胜感激:

I am writing a program that calculates standard deviation in randomly generated temperatures. I believe that my algorithm is right, however the code only takes the first number in the Temp array and subtracts it from the mean, and I can''t figure out why it does not go through the whole array. I have tried a foreach loop but always get a cannot convert i from type int. Here''s the code I have so far, and any help would be greatly appreciated:

static void Main(string[] args)
        {
            int[] Temp = new int[4];
            Random num = new Random();
            for (int i = 0; i < Temp.Length; i++)
            {
                Temp[i] = num.Next(10, 50);
                Console.WriteLine(Temp[i]);
            }

            {


                double dev = 0;
                double devsum = 0;

                double total = 0;
                foreach (double i in Temp)
                    total += i;
                double avg = total / Temp.Length;
                Console.WriteLine("Average = {0}\n", avg);
                for (int i = 0; i < Temp.Length; i++)
                {
                    ///// For some reason it only takes the first i in Temp
                    ///// does not work for the other i's in Temp
                    dev = (Temp[i] - avg);
                    dev = Math.Pow(dev,2);
                    devsum = devsum + dev;
                    Console.WriteLine("{0}", dev);
                    dev = Math.Sqrt(devsum / Temp[i] - 1);
                    total++;
                    Console.WriteLine(" The Deviation is {0}\n", dev);
                    Console.ReadLine();
                }

            }
        }
    }
}

推荐答案

首先,仅供参考:"标准偏差"是一个第二时刻 分布:

http://en.wikipedia.org/wiki/Standard_deviation [ http://en.wikipedia.org/wiki/Moment_%28mathematics%29 [ ^ ],
http://en.wikipedia.org/wiki/Distribution_%28mathematics%29 [ ^ ].

让我们检查一下:您正确地填充了分布Range(硬编码的立即常量 4,10和50确实是一件坏事,我希望您在不久的将来将其替换为显式声明的常量或变量;值得称赞的是,我感谢至少您从未两次写过任何这样的常数),您可以正确计算每个值的个体偏差.

从这一点来看,您做错了.您需要总结循环中的单个偏差(平方)(也使用foreach,无需使用for),然后需要在循环后取平方偏差之和的平方根, ,但您是在周期中完成的.您也不应触摸 total在上一个循环中计算.计算avg后,您不需要它.这是一个错误.

因此,在上一个循环中,您应该这样做:
First, just for reference: "standard deviation" is a second moment of distribution:

http://en.wikipedia.org/wiki/Standard_deviation[^],
http://en.wikipedia.org/wiki/Moment_%28mathematics%29[^],
http://en.wikipedia.org/wiki/Distribution_%28mathematics%29[^].

Let''s check up: you correctly populate the distribution Range (hard-coded immediate constants 4, 10 and 50 is really bad thing, I hope you replace it in near future with explicitly declared constants or variable; to your credit, I appreciate that at least you never write any such constants twice), you correctly calculate individual deviations for each value.

From this point, you do it wrong. You need to summarize the individual deviations (squared) in the loop (also use foreach, no need to use for), then you need to take a square root of the sum of squared deviations, after the cycle, but you did it in the cycle. You also should not touch total calculate in the previous loop. You don''t need it after avg is calculated. This is a bug.

So, in your last loop you should do this:
double sum = 0;
foreach(double value in Temp) {
    double dev = value - avg;
    sum += dev * dev;
}
float standardDeviation = Math.Sqrt(sum);

问题已解决.

一些建议:1)切勿尝试在您的入口点(Main)中完成所有工作,将单独的类和方法添加为白色; 2)即使是循环变量,也不要使用诸如"i"之类的简短变量名;使用一些语义:indexvaluecounterelement…3)在几乎不关心您的运行时行为的情况下使用调试器;至少要确保在问像这样的问题之前先做到这一点.



蓝凡发现了另一个错误:您将ReadLine放入了循环中.也许这不是一个错误,但是它可能会阻止您看到正在发生的事情.

不过,我上面写的解决方案首先是正确的.
同样,不要将I/O用于如此简单的调试,请使用Debugger.

—SA

Problem solved.

Some side recommendations: 1) never try to do all the work in your entry point (Main), white a separate class and methods; 2) don''t use such short variable names like "i", even if this is a loop variable; use something semantic: index, value, counter, element… 3) use the Debugger on any slightest concern about your run-time behavior; at least make sure you do it before asking questions like this one.



Lan Fan found another bug: you put ReadLine in the loop. Maybe, this is not a mistake, but it could prevent you from seeing what''s going on.

Nevertheless, my solution I''ve written above was correct in first place.
Again, don''t use I/O for such a simple debugging, use the Debugger.

—SA


这篇关于计算标准偏差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆