多维数组超出范围

问题描述 投票:-1回答:1

我做错了什么,弄不清楚。我有一个多维矩阵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)
python numpy multidimensional-array
1个回答
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)
© www.soinside.com 2019 - 2024. All rights reserved.