我试图用FFT方法预测期权的认沽和赎回权,而我的代码中存在一些问题:
options = pd.read_csv("/Users/***/Documents/relevantoptions2",sep =',',header = 0)
def oneorzero():
L=[]
for i in range( 0,options.shape[0]):
if (options.optiontype[i]=='put'):
L.append(1)
else :
L.append(0)
options.optiontype=L
oneorzero()
options=options.drop_duplicates(subset=None, keep='first', inplace=False)
prices=pd.DataFrame()
prices['settledate']= options.settledate.drop_duplicates(keep='first',
inplace=False)
prices['settle_sp_price']=
options.settle_sp_price.drop_duplicates(keep='first', inplace=False)
prices['return'] = prices.settle_sp_price.pct_change().dropna()
prices['settleVol'] =prices['return'].rolling( window=17).std() *
np.sqrt(17)
prices = prices.fillna(method='ffill').fillna(method='bfill')
prices = prices.reset_index(drop=True)
prices.head()
n=prices.settle_sp_price.size
x=np.arange(0,n)
p1 =sp.polyfit(x, prices.settle_sp_price,3)
pp1 =np.polyval(p1, x)
diff = prices.settle_sp_price-pp1
Y=[]
#for i in range(1,prices.settle_sp_price.size):
# if Y[i] > .1*Y[i]:
# Y[i] = 0
# print(Y[i])
Y=np.fft.fft(diff)
PP=np.fft.ifft(Y)
YY=[0]*(prices.settle_sp_price.size+30)
a=[0]*prices.settle_sp_price.size
b=[0]*prices.settle_sp_price.size
#interpolate the difference for 90days
for n in range(prices.settle_sp_price.size,prices.settle_sp_price.size+25):
YY[n]=0
for k in range(1,prices.settle_sp_price.size):
a[k]=np.real(Y[k])
b[k]=-np.imag(Y[k])
omk=2*math.pi*(k-1)/prices.settle_sp_price.size
YY[n]=YY[n]+a[k]*math.cos(omk*(n-1))+b[k]*math.sin(omk*(n-1))
YY[n]=-YY[n]/prices.settle_sp_price.size
# create one array for the original and interpolated difference
# between the least squares curve and the stock price. for i=1:415
i=range(prices.settle_sp_price.size)
YY[i]=diff[i]
tot= pp1+YY
我对于for循环有第一个问题,但我不明白为什么:
文件“ C:/ Users / *** / Documents / Stage gm3 / FFT3.py”,第91行,在 如果Y [i]> .1 * Y [i]:
IndexError:列表索引超出范围
第二个问题,当我在评论中加入for循环时,我明白了:
KeyError:范围(0,238)
最重要的是第二个问题,因为我可以继续处理第一个问题。
答案 0 :(得分:0)
您遇到的第一个问题是
Y=[]
for i in range(1,prices.settle_sp_price.size):
if Y[i] > .1*Y[i]:
Y[i] = 0
print(Y[i])
数组Y[]
初始化为空,然后在您尝试访问其元素后,Y[i]
只能返回错误。
答案 1 :(得分:0)
当我尝试显示基于x的tot时,我得到的曲线与基于price.settle_sp_price的x完全相同,这是正常的。另一方面,未显示预测,因为在最后一个for循环之前,YY的大小为250,而在与diff相等之后,大小为238,但我希望将其保持在250。问题来自何处,该如何解决?