[VBA ] 如何應用二分逼近法和count結合?
先附上式子
Function BS_call_difference(S, K, r, T, call_price, sigma)
d1 = (Log(S / K) + (r + 0.5 * sigma ^ 2) * T) / (sigma * Sqr(T))
d2 = d1 - sigma * Sqr(T)
Sum = S * Application.NormSDist(d1) - K * Exp(-r * T) *
Application.NormSDist(d2)
BS_call_difference = Sum - call_price
End Function
=======================================================================
Function Bisection_BS_call(S, K, r, T, call_price)
tolerance = 10 ^ (-6)
down_limit = 0
up_limit = 1
Count = 0
mid_point = (down_limit + up_limit) / 2
Count = Count + 1
Do While Count < 100 And Abs(BS_call_difference(S, K, r, T, call_price,
mid_point)) > tolerance
If BS_call_difference(S, K, r, T, call_price, down_limit) *
BS_call_difference(S, K, r, T, call_price, mid_point) < 0 Then
up_limit = mid_point
ElseIf BS_call_difference(S, K, r, T, call_price, up_limit) *
BS_call_difference(S, K, r, T, call_price, mid_point) < 0 Then
down_limit = mid_point
End If
mid_point = (down_limit + up_limit) / 2
Loop
Bisection_BS_call = mid_point
End Function
========================================================================
這個式子是要透過二分逼近法的應用套在black&scholes來計算出選擇權的波動度
我的作法是先用BS_call_difference來做二分逼近法的前置
再把BS_call_difference套進Bisection_BS_call來使用
可是把這個式子套進數字後,一直都跳出value來
程式也沒有跑出偵錯,所以也不知道從何修起
加上老師說要加入
count起始值要設為0,每一圈迴圈要將count=count+1
最後一行的Bisection_BS_call 傳回值要區分兩種情形:count>=100 (傳回-1)與 其他 (
傳回mid_point)
這段我也不曉得該放在哪個位置
有對這方面了解的朋友能幫個忙嗎?
弄兩個禮拜都想不出來,不想拖過年啦
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.44.191.230
※ 文章網址: https://www.ptt.cc/bbs/Visual_Basic/M.1483152490.A.9AD.html
Visual_Basic 近期熱門文章
PTT數位生活區 即時熱門文章