函数值返回null

时间:2016-10-16 17:54:47

标签: python numpy assert

我尝试运行这段代码,我得到的维度不同。此外,当我尝试打印Strikes和option_value_seqs的值时,我没有得到“option_value_seqs”的回报

import numpy as np
def bsm_mcs_valuation(strike):
    s0=100;T=1.0;r=0.05;vol=0.2;M=50;I=200
    dt=T/M
    rand= np.random.standard_normal((M+1,I))
    s=np.zeros((M+1,I));s[0]=100
    for t in range(1,M+1):
        s[t]= s[t-1]*np.exp((r - 0.5 * vol **2 ) * dt 
                        + vol *  np.sqrt(dt) * rand[t])

    value = (np.exp(-r*t)*np.sum(np.maximum(s[-1]-strike,0))/I)
    return value

def seq_value(n):
    strikes = np.linspace(80,120,n)
    print strikes
    option_values = []
    for strike in strikes:
        print strike
        print "option value is "; 
        option_values.append(bsm_mcs_valuation(strike))
    return strikes, option_values

n=100
%time strikes,option_values_seq=seq_value(n)


import matplotlib.pyplot as plt
%matplotlib inline
plt.figure(figsize=(8,4))
plt.plot(strikes,option_values_seq,'b')
plt.plot(strikes,option_values_seq,'r')

我收到以下错误

221         y = _check_1d(y)
222         if x.shape[0] != y.shape[0]:
223             raise ValueError("x and y must have same first dimension")


ValueError: x and y must have same first dimension

0 个答案:

没有答案