假设我有一个骰子,它产生 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 值。
如何才能做到?
我通过反复试验找到了答案,至少部分是这样。
'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
也许这个 C# 代码片段可以帮助您。
我想你可以用这条线来测量拟合误差:
GoodnessOfFit.RSquared(xdata.Select(x => a+b*x), ydata); // == 1.0
其中
1
表示完美(完全正确),0
表示较差。
Math.NET 文档中对此进行了描述: