在某些迭代優化過程中,用於計算雙變量正態CDF的以下VBA代碼有時會在while
循環內的z = hx * hy * c
行上引發溢出錯誤上部功能。當乘法結果大於double可容納的值時,忽略溢出錯誤
我調試了代碼,當乘數相乘的數字大於double可以容納的數字時,就會發生溢出。
你能告訴我如何處理這個問題嗎?忽略這個高值的循環迭代 - 我想這是唯一可行的解決方案(?)。我在乘法之前用On Error Goto nextiteration行嘗試自己,並在Wend之前放置下一個跳躍點,但錯誤仍然存在。
Function tetrachoric(x As Double, y As Double, rho As Double) As Double
Const FACCURACY As Double = 0.0000000000001
Const MinStopK As Integer = 20
Dim k As Integer
Dim c As Double
Dim z As Double
Dim s As Double
Dim hx As Double
Dim hx1 As Double
Dim hx2 As Double
Dim hy As Double
Dim hy1 As Double
Dim hy2 As Double
Dim CheckPass As Integer
hx = 1
hy = 1
hx1 = 0
hy1 = 0
k = 0
c = rho
z = c
s = z
CheckPass = 0
While CheckPass < MinStopK
k = k + 1
hx2 = hx1
hy2 = hy1
hx1 = hx
hy1 = hy
hx = x * hx1 - (k - 1) * hx2
hy = y * hy1 - (k - 1) * hy2
c = c * rho/(k + 1)
z = hx * hy * c
s = s + z
If Abs(z/s) < FACCURACY Then
CheckPass = CheckPass + 1
Else
CheckPass = 0
End If
Wend
tetrachoric = s
End Function
Public Function bivnor(x As Double, y As Double, rho As Double) As Double
'
' bivnor function
' Calculates bivariat normal CDF F(x,y,rho) for a pair of standard normal
' random variables with correlation RHO
'
If rho = 0 Then
bivnor = Application.WorksheetFunction.NormSDist(x) * _
Application.WorksheetFunction.NormSDist(y)
Else
bivnor = Application.WorksheetFunction.NormSDist(x) * _
Application.WorksheetFunction.NormSDist(y) + _
Application.WorksheetFunction.NormDist(x, 0, 1, False) * _
Application.WorksheetFunction.NormDist(y, 0, 1, False) * _
tetrachoric(x, y, rho)
End If
End Function
來源:可供下載http://michael.marginalq.com/
剛剛嘗試過,並且與我自己的嘗試完全相同(除了「On Error Goto 0」之外的確與您的嘗試類似),溢出錯誤仍然存在。任何其他想法? – Steve06 2011-04-12 20:31:24
如果你在'z = hx * hy * c'之前的行上放了一個'Debug.Print hx,hy,c',當你得到這個溢出時,這三個變量的值是多少?雙打的限制是非常難以擊中的。 – mwolfe02 2011-04-12 20:52:24
我剛剛發佈的更新代碼運行時沒有生成錯誤消息,但似乎在溢出情況下輸出了'-1。#IND'(不確定)作爲結果值。這可能是一種致命的缺陷,你必須在@弗拉季斯拉夫的答案中探索選項。 – mwolfe02 2011-04-12 21:34:22