import math
import numpy as np
def ExplicitMethod(S0, K, r, q, T, Sigma, M, N, Option):
M = int(M)
N = int(N)
dt = T / N
K = float(K)
Smax = 2 * K
dS = Smax / N
FGrid = np.zeros(shape=(N+1, M+1))
if Option == 'Call':
FGrid[-1, :] = np.maximum(np.arange(0, M+1) * dS - K, 0)
elif Option == 'Put':
FGrid[-1, :] = np.maximum(K - np.arange(0, M+1) * dS, 0)
A = np.zeros(shape=(M+1, M+1))
A[0,0] = 1
A[-1,-1] = 1
for j in range(1, M-1):
A[j, (j-1,j, j+1)] = \
[0.5 * dt * (Sigma ** 2 * j ** 2 - (r - q) * j ), \
1 - dt * (Sigma ** 2 * j ** 2 + r), \
0.5 * dt * (Sigma **2 * j ** 2 + (r - q) * j)]
for i in range(N):
Fhat = FGrid [i, ]
FGrid[i, :] = np.dot(A , Fhat)
Fhat[0] = 0
Fhat[-1] = Smax - K * math.exp(-r * (N - i) * dt)
k = math.floor(S0 / dS)
V = FGrid[0, k] + (FGrid[0, k] - FGrid[0, k]) / dS * [(S0 - k * dS)]
print (V)
ExplicitMethod (50, 30, 0.1, 0.05, 2, 0.3, 400, 800, Option = 'Call')
我收到了这个错误:
IndexError Traceback(最近一次调用 最后)in() 37打印(V) 38 ---> 39明确方法(50,30,0.1,0.05,2,3.3,400,800,Option ='Call')
在ExplicitMethod中的(S0,K,r,q,T, Sigma,M,N,Option) 33 k = math.floor(S0 / dS) 34 ---> 35 V = FGrid [0,k] +(FGrid [0,k] - FGrid [0,k])/ dS * [(S0-k * dS)] 36 37 print(V)
IndexError:索引666超出了轴1的大小为401
的范围
我正在编制期权定价显式方法:
请帮助,谢谢!
答案 0 :(得分:2)
您已将FGrid
初始化为(801,401)。由于某种原因,我们告诉我们k=666
k = math.floor(S0 / dS)
V = FGrid[0, k] + (FGrid[0, k] - FGrid[0, k]) / dS * [(S0 - k * dS)]
您需要优化k
的设置方式。也许数学错了。至少你需要确保它不会超过400。
In [200]: S0=50; K=30; N=800
In [201]: dS = 2*K/N
In [202]: dS
Out[202]: 0.075
In [203]: S0/dS
Out[203]: 666.6666666666667
你的666
。