【问题标题】:How to calculate a Goodness of Fit using Math.net?如何使用 Math.net 计算拟合优度?
【发布时间】:2014-09-10 21:35:05
【问题描述】:

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

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

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

当我在this website 上输入值对时,它会计算出 16.2 的卡方值和 0.00629567 的 P 值,这表明观察到的结果不太可能与值的预期分布一致六。

我想使用math.net numerics 计算 P 值,但虽然我可以在那里找到 ChiSquared class,但我找不到如何将预期观察值对输入它以获取 P 值。

怎么做?

【问题讨论】:

  • 请解释否决票。这可能有助于解决我的问题!

标签: .net statistics chi-squared math.net


【解决方案1】:

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

'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

【讨论】:

  • chi 变量声明在哪里?
【解决方案2】:

也许这个 C# sn-p 可以帮助你。

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

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

【讨论】:

    猜你喜欢
    • 2014-02-19
    • 2015-06-14
    • 1970-01-01
    • 2012-06-10
    • 1970-01-01
    • 2016-03-16
    • 2018-06-29
    • 1970-01-01
    • 2015-04-15
    相关资源
    最近更新 更多