说,我有一个骰子产生六个一个的可能性是19倍,因为它已被篡改。 当我将这个模具丢弃60次时,预期与观察到的六种可能结果的频率是:
1:10,1 2:10,10 3:10,10 4:10,10 5:10,10 6:10:19
我想将这些预期观察对提供给算法,以确定骰子确实被篡改的可能性。
当我在this website上输入值对时,它计算出卡方值为16.2且P值为0.00629567,表明观察到的结果不太可能与值的预期分布一致一到六个。
我想使用math.net numerics计算P值,但是虽然我可以在那里找到ChiSquared class,但我找不到如何将预期观察值对提供给它以便获得P值。
怎么做?
答案 0 :(得分: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
答案 1 :(得分:1)
也许这个C#代码段可以帮到你。
我支持您可以使用此行来衡量拟合误差:
GoodnessOfFit.RSquared(xdata.Select(x => a+b*x), ydata); // == 1.0
其中1
表示完美(完全在线上),0
表示不良。
在该页面上的Math.NET文档中对其进行了描述:
http://numerics.mathdotnet.com/docs/Regression.html#Simple-Regression-Fit-to-a-Line