How to calculate the goodness of fit with Math.net?
Let's say I have a die that is 19 times more likely to produce six as one because it was tampered with. When I roll this stamp 60 times, the expected and observed frequencies of six possible outcomes are:
1:10, 1
2: 10, 10
3: 10, 10
4: 10, 10
5: 10, 10
6: 10: 19
I want to pass these expected pairs to an algorithm to determine how likely it is that the bones were indeed tampered with.
When I enter pairs of values into this website , it calculates a square Chi value of 16.2 and a P value of 0.00629567, indicating that it is very unlikely that the observed results match the expected distribution of values from one to six.
I want to calculate the P value using the math.net number , but although I can find the ChiSquared class , I cannot find how to feed the expected value pairs for it to get the P value.
How can I do that?
source to share
I found the answer by trial and error, in part at least.
'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'
I had to implement some code to calculate the critical value 16.2, but it's not very difficult:
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
This function uses the structure AssertionPair
as follows:
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
source to share
Maybe this C # snippet can help you.
I can use this line to measure the match error:
GoodnessOfFit.RSquared(xdata.Select(x => a+b*x), ydata); // == 1.0
where 1
means PERFECT (exactly on the line), and 0
means POOR.
it is described in the Math.NET documentation on this page:
http://numerics.mathdotnet.com/docs/Regression.html#Simple-Regression-Fit-to-a-Line
source to share