从数据集中计算标准偏差 [英] Calculating Standard Deviation from a dataset

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

问题描述

您好,我正在编码以计算存储在数据集中的数组的标准偏差(来自服务器的Gotten).该代码能够计算标准差所需的平均值.

但是,我得到的StDev值是错误的. 我知道标准差的公式,但也许我对形式编码有误.另一个可能的错误可能是由于四舍五入.从服务器接收的数据有17个小数点,而我最多只需要6个小数点.我在网上找到了四舍五入小数点的方法,但是我不确定如何对数组中的小数点进行四舍五入.如果某些专业人士可以教我如何对数组中的小数点进行四舍五入,我将不胜感激.

我刚刚开始从在线教程和示例中学习编程.

有人可以在这件事上给我建议吗?非常感谢! :)

Hi, I am coding to calculate standard deviation from an array which is stored in a dataset (Gotten from a server). The code is able to calculate the mean which is needed for standard deviation.

However, the StDev value i gotten is wrong. I know the formula for standard deviation but maybe I have coded the formalu wrongly. Another possible mistake could be due to rounding off. The data received from the server has 17 decimal points and I only need up to 6 decimal points. I have found online on rounding up decimal points but I am unsure how to round up decimal points from an array. I would be thankful if some pros can teach me how to round up decimal places in an array.

I have just started learning programming from tutorials and examples online.

Would someone please advise me on this matter? Thanks alot! :)

Private Function Compute(ByVal data As ArrayList) As Double

    Dim result As Double = 0.0
    Dim result2 As Double = 0.0
    Dim sum As Double = 0.0
    Dim i As Integer = 0
    Dim count As Integer = 0

    'CALCULATE AVERAGE
    If ComputationType.Equals("Average") Then

        For i = 0 To data.Count - 1
            If data(i) Is DBNull.Value Then 'IMPORTANT! SOME RECORD HAS NULL VALUE SO MUST ADD A ZERO
                sum += 0
            Else
                sum += data(i)
            End If

            count += 1
        Next i

        result = sum / data.Count

        'CALCULATE STANDARD DEVIATION
    ElseIf ComputationType.Equals("STDev") Then

        For i = 0 To data.Count - 1
            If data(i) Is DBNull.Value Then 'IMPORTANT! SOME RECORD HAS NULL VALUE SO MUST ADD A ZERO
                sum += 0
            Else
                sum += data(i)
            End If

            count += 1
        Next i

        result2 = sum / data.Count

        For i = 0 To data.Count - 1

            If data(i) Is DBNull.Value Then 'IMPORTANT! SOME RECORD HAS NULL VALUE SO MUST ADD A ZERO
                result += 0
            Else
                result += Sqrt((data(i) - result2) ^ 2) / (data.Count - 1)
            End If

        Next i

        'CALCULATE CPK
    ElseIf ComputationType.Equals("Cpk does not work") Then
        'IMPORTANT: CODING NOT DONE!
        MsgBox("Cpk")
    End If

    Return result

End Function

推荐答案

抱歉,目前我还没有阅读和分析此代码的工作.实际上,找到标准偏差的问题很简单,但是所有数值计算算法都存在一个问题:如果您犯任何错误,没有崩溃,没有引发异常,屏幕不显示图像.使用一些易于检查的简单数据样本对算法进行了调试,等等.同样,该问题过于简单,不会造成真正的麻烦.

我只想告诉您一件重要的事情:永远不要在计算中使用任何舍入方式.始终使用最大的精度.如果计算简单,则不会花费太多时间.如果计算时间很长,那么损失准确性的危险就很大,而性能则更为重要.您不想很快就得出错误的数字. :-)在计算中几乎几乎不需要舍入(我在这里不讨论最终结果.),使用舍入的情况太奇特,无法在此处进行讨论,例如,在用于生成伪随机数的算法.永远记住:中间结果中的舍入误差往往会累积.根据计算算法,您可能对舍入误差有或多或少的明显影响,但是为什么使情况变得更糟?

—SA
Sorry, at this moment I did not take a labor of reading and analysis of this code. Actually, the problem of finding of the standard deviation is way to simple, but all numeric calculation algorithm presents one problem: if you make any mistake, nothing crashes, exception is not thrown, the screen does not show mangles picture. The algorithm is debugged using some simple data samples which are easy to checkup, etc. Again, the problem is too simple to make a real trouble.

I want to tell you only one important thing: never ever use any rounding in calculation. Always use maximum accuracy. If the calculation is simple, it won''t take too much time; if the calculation is very long, the danger of loss of accuracy is way to big and more important then performance. You don''t want to get wrong figures but fast. :-) Rounding is almost never really needed in calculation (I''m not talking in the presentation of final results here.), and the cases the rounding is used are too exotic to discuss them here, for example, rounding is used in the algorithms for generating of pseudo-random numbers. Always remember: rounding errors in intermediate results tens to accumulate. You can have more or less pronounce effect of rounding errors, depending on calculation algorithm, but why making it worse?

—SA


我设法编码了正确的标准偏差,尽管它有点长...

为了避免错误,我将编码分为多个服务器步骤.

I have managed to code the right standard deviation although it is a bit long...

I split the coding into serveral steps to avoid mistakes.

Private Function Compute(ByVal data As ArrayList) As Double

        Dim result As Double = 0.0
        Dim result2 As Double = 0.0
        Dim result3 As Double = 0.0
        Dim result4 As Double = 0.0
        Dim sum As Double = 0.0
        Dim i As Integer = 0
        Dim count As Integer = 0

        'CALCULATE AVERAGE
        If ComputationType.Equals("Average") Then

            For i = 0 To data.Count - 1
                If data(i) Is DBNull.Value Then 'IMPORTANT! SOME RECORD HAS NULL VALUE SO MUST ADD A ZERO
                    sum += 0
                Else
                    sum += data(i)
                End If

                count += 1
            Next i

            result = sum / data.Count

            'CALCULATE STANDARD DEVIATION
        ElseIf ComputationType.Equals("STDev") Then

            For i = 0 To data.Count - 1
                If data(i) Is DBNull.Value Then 'IMPORTANT! SOME RECORD HAS NULL VALUE SO MUST ADD A ZERO
                    sum += 0
                Else
                    sum += data(i)
                End If

                count += 1
            Next i

            result4 = sum / data.Count

            For i = 0 To data.Count - 1

                If data(i) Is DBNull.Value Then 'IMPORTANT! SOME RECORD HAS NULL VALUE SO MUST ADD A ZERO
                    result3 += 0
                Else
                    result3 += (data(i) - result4) ^ 2
                End If

            Next i

            result2 = result3 / (data.Count - 1)

            result = Sqrt(result2)

        End If

        Return result

    End Function


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

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