多维数组超出范围

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多维数组超出范围相关的知识,希望对你有一定的参考价值。

我做错了什么,弄不清楚。我有一个多维矩阵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)
答案

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

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)

以上是关于多维数组超出范围的主要内容,如果未能解决你的问题,请参考以下文章

如何在swift 3中的不同部分下显示多维数组

活动结果片段索引超出范围:0x20001

如何按列对多维数组进行排序?

VBScript 创建一个多维数组并添加到它?

从firebase删除时数组索引超出范围

为啥我没有得到数组超出范围错误? [复制]