【发布时间】:2019-04-20 15:34:44
【问题描述】:
我做错了什么,无法弄清楚。我有一个多维矩阵total 这个商店。我在某个地方弄乱了排序,只是无法理解如何或为什么。错误是IndexError: index 253 is out of bounds for axis 0 with size 253
class pricing_floatinglookback:
def __init__(self, spot, rate, sigma, time, sims, steps):
self.spot = spot
self.rate = rate
self.sigma = sigma
self.time = time
self.sims = sims
self.steps = steps+1
self.dt = self.time / self.steps
def call_floatingstrike(self):
SimPriceMin = np.array([])
SimPriceAtMaturity = np.array([])
call2 = np.array([])
pathwiseS= np.zeros((self.steps,),float)
total = np.zeros((self.sims,self.steps),float)
for j in range(self.sims):
pathwiseS[0] =self.spot## This will be one dimensional array from 0 to 253
total[j,0] = self.spot ## This will be multidimensional array with columns 0 to 800 and rows 0 to 253
for i in range(self.steps):
phi = np.random.normal()
pathwiseS[i+1] = pathwiseS[i]*(1+self.rate*self.dt+self.sigma*phi*np.sqrt(self.dt))## -->This is where i am going wrong.
total[j,i+1]= pathwiseS[i+1] ## -->This is where i am going wrong.
SimPriceAtMaturity = np.append(SimPriceAtMaturity, pathwiseS[self.steps - 1])
call2 = np.append(call2,max((pathwiseS[self.steps - 1])-self.spot,0))
SimPriceMin = np.append(SimPriceMin, min(pathwiseS))
callbsm = np.average(call2)
call = max(np.average(SimPriceAtMaturity) - np.average(SimPriceMin), 0)
return call, total.reshape(self.sims, self.steps), np.average(SimPriceMin), callbsm*np.exp(-self.rate*self.time)
pricelookback = pricing_floatinglookback(100, 0.05, 0.2, 1, 800, 252)
clookback, callmatrix, calmin, callbsm = pricelookback.call_floatingstrike()
print (callbsm)
plt.plot(callmatrix.T)
【问题讨论】:
-
您是否尝试将
total[j,i+1]= pathwiseS[i+1]替换为total[j,i]= pathwiseS[i+1]?通过执行i+1,您正在尝试访问不存在的 253 索引。原因是python中的索引是从0开始的。所以长度为253的数组的最后一个索引是252 -
好吧 - python 是基于 0 的索引,
axis 0 with size 253值的索引从 0...252 - 253 超出范围... -
-
@PatrickArtner 我知道,但如果你往上看,我将值 253 分配给
self.steps而不是 252。
标签: python numpy multidimensional-array