3

比如说,我有一个骰子,它产生 6 的可能性是 1 的 19 倍,因为它已被篡改。当我将这个骰子掷出 60 次时,六种可能结果的预期频率与观察频率分别为:

1:10、1
2:10、10
3:10、10
4:10、10
5:10、10
6:10:19

我想将这个预期观察到的对提供给算法,以确定骰子确实被篡改的可能性。

当我在这个网站上输入值对时,它计算出的卡方值为 16.2,P 值为 0.00629567,这表明观察到的结果不太可能与值 1 到 6 的预期分布一致。

我想使用math.net numerics计算 P 值,但是虽然我可以在那里找到一个ChiSquared 类,但我找不到如何将预期观察值对提供给它以获得 P 值。

怎么做到呢?

4

2 回答 2

3

我通过反复试验找到了答案,至少部分是这样。

'The constructor takes the freedom, which is number of sides minus one'
Dim chiSquared=New ChiSquared(5) 
Dim pValue=1-chi.CumulativeDistribution(16.2) '0.00629567'    

我必须自己实现代码来计算 16.2 的临界值,但这当然不是很难:

   Public Function CalculateChiSquaredCriticalValue(Of T)(assertionPairs As IEnumerable(Of AssertionPair(Of T))) As Double
        Contracts.Contract.Requires(Of ArgumentNullException)(assertionPairs IsNot Nothing, "assertionPairs")

        Dim totalExpected As Integer
        Dim totalObserved As Integer

        Dim criticalValue As Double
        'The critical value is the sum of each squared difference between the observed' 
        'and the expected value, divided by the expected value.'
        For index = 0 To assertionPairs.Count - 1
            Dim element = assertionPairs(index)
            Dim expected = element.ExpectedValue
            Dim observed = element.ObservedValue
            totalExpected += expected
            totalObserved += observed

            If element.ExpectedValue = 0 Then
                Throw New InvalidOperationException(String.Format("The expected value of outcome {0} is zero.", element.Value))
            End If
            Dim diff = (element.ExpectedValue - element.ObservedValue) * (element.ExpectedValue - element.ObservedValue) / element.ExpectedValue
            criticalValue += diff

        Next

        If totalExpected <> totalObserved Then
            Throw New InvalidOperationException(String.Format("The total number of expected values ({0}) must equal the total number of observed values ({1}).",
                                                              totalExpected, totalObserved))
        End If

        Return criticalValue

    End Function

该函数使用如下 AssertionPair结构:

Namespace Mathematics
''' <summary>
''' Contains a pair of expected and observed probabilities for a given value.
''' </summary>
''' <remarks></remarks>
 Public Structure AssertionPair(Of T)

    ''' <summary>
    ''' Initializes the structure.
    ''' </summary>
    ''' <param name="value">A given value. Can be used for reference.</param>
    ''' <param name="expected">The expected number of times that the given value should be obtained.</param>
    ''' <param name="observed">The actual number of times that the given value was obtained.</param>
    ''' <remarks></remarks>
    Public Sub New(value As T, expected As Integer, observed As Integer)

        Me.Value = value
        Me.ExpectedValue = expected
        Me.ObservedValue = observed
    End Sub

    Private _value As T
    Private _observedValue As Integer
    Private _expectedValue As Integer


    Public Property Value As T
        Get
            Return _value
        End Get
        Private Set(value As T)
            _value = value
        End Set
    End Property

    Public Property ExpectedValue As Integer
        Get
            Return _expectedValue
        End Get
        Private Set(ByVal value As Integer)
            _expectedValue = value
        End Set
    End Property

    Public Property ObservedValue As Integer
        Get
            Return _observedValue
        End Get
        Private Set(ByVal value As Integer)
            _observedValue = value
        End Set
    End Property

    Public Overrides Function ToString() As String
        Return Value
    End Function

 End Structure
End Namespace
于 2014-09-13T05:46:11.683 回答
1

也许这个 C# 片段可以帮助你。

我想你可以用这条线来测量拟合误差:

GoodnessOfFit.RSquared(xdata.Select(x => a+b*x), ydata); // == 1.0

其中1意味着 PERFECT(完全在线)并且0意味着 POOR。

它在该页面上的 Math.NET 文档中进行了描述:

http://numerics.mathdotnet.com/docs/Regression.html#Simple-Regression-Fit-to-a-Line

于 2015-02-22T21:52:36.033 回答