Board logo

標題: [轉貼] F分佈的分佈函數--VB 程式碼 [打印本頁]

作者: laigs218    時間: 2021-7-19 13:34     標題: F分佈的分佈函數--VB 程式碼

Attribute VB_Name = "modMethod"
Option Explicit
'計算F分佈的分佈函數
'n1:自由度,已知
'n2:自由度,已知
'F:F值,已知
'p:下側概率,所求
'd:概率密度,所求
Public Sub F_DIST(n1 As Integer, n2 As Integer, F As Double, _
            p As Double, d As Double)
    Dim x As Double, u As Double, Lu As Double
    Dim IAI As Integer, IBI As Integer, nn1 As Integer, nn2 As Integer
    Dim I As Integer
    Const PI As Double = 3.14159265359
    If F = 0 Then
        p = 0: d = 0: Exit Sub
    End If
    x = n1 * F / (n2 + n1 * F)
    If (n1 \ 2) * 2 = n1 Then
        If (n2 \ 2) * 2 = n2 Then
            u = x * (1 - x): p = x: IAI = 2: IBI = 2
        Else
            u = x * Sqr(1 - x) / 2: p = 1 - Sqr(1 - x): IAI = 2: IBI = 1
        End If
    Else
        If (n2 \ 2) * 2 = n2 Then
            p = Sqr(x): u = p * (1 - x) / 2: IAI = 1: IBI = 2
        Else
            u = Sqr(x * (1 - x)) / PI
            p = 1 - 2 * Atn(Sqr((1 - x) / x)) / PI: IAI = 1: IBI = 1
        End If
    End If
    nn1 = n1 - 2: nn2 = n2 - 2
    If u = 0 Then
        d = u / F
        Exit Sub
    Else
        Lu = Log(u)
    End If
    If IAI = n1 Then GoTo LL1
    For I = IAI To nn1 Step 2
        p = p - 2 * u / I
        Lu = Lu + Log((1 + IBI / I) * x)
        u = Exp(Lu)
    Next I
LL1:
    If IBI = n2 Then
        d = u / F: Exit Sub
    End If
    For I = IBI To nn2 Step 2
        p = p + 2 * u / I
        Lu = Lu + Log((1 + n1 / I) * (1 - x))
        u = Exp(Lu)
    Next I
    d = u / F
End Sub




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)