多维数组超出范围

时间:2019-04-20 15:34:44

标签: python numpy multidimensional-array

我做错了,无法弄清楚。我有一个多维矩阵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)

1 个答案:

答案 0 :(得分:0)

所以我修复了它。我不知道为什么或如何,但是它是固定的

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
        total[j,0] = self.spot
        for i in range(self.steps-1):##--->This was the main reason, dont know why but it was!
            phi = np.random.normal()
            pathwiseS[i+1] = pathwiseS[i]*(1+self.rate*self.dt+self.sigma*phi*np.sqrt(self.dt))
            total[j,i]= pathwiseS[i+1]##--->This as suggested in the comments

        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)