并尝试使用它编码索引和成分之间的相关性计算。基本上遵循此公式并附加of implied correlation。 我的数据文件由样本期内每个日期的股票期权IV列组成。 我尝试使用for循环遍历每一行,并执行相关计算。但是,循环仅产生一个每日值,而不循环所有日期。 我不确定这里哪里做错了,到目前为止,我只编码了分母。
我的代码当前看起来像这样(如果看起来很笨拙,则表示歉意):
import xlrd
import numpy as np
import pandas as pd
#read the 'Beta_Weighted_IV'sheet from the '30day_IV_File.xlsx'file
data=pd.read_excel('30day_IV_File.xlsx',sheet_name='Beta_Weighted_IV')
#remove the first row, first and last col.
data_new=data.iloc[1:,1:16]
#define the row and col count
count_row = data_new.shape[0]
count_col = data_new.shape[1]
#define matrix of 1 in 1x15 and 15x1 dimensions, and name as ones_pre and ones_post
ones_pre=np.ones(15)
ones_post=np.ones((15,1))
Index_Vol=np.matrix(data.iloc[:,16])
#Take the value of the row as a matrix, transpose the row matrix.
#Multiple the product of T1_trans, T1, by ones_pre and ones_post to
#get a 1x1 matrix for each row, named Sum_of_IV. Repeat for all rows.
# Substract diagonal from Sum_of_IV for each row
for r in range(count_row):
T1=np.matrix(data_new.iloc[r])
T1_trans=np.matrix(T1.transpose())
print(T1)
Diagonal=np.matmul(T1,T1_trans)
test=np.matmul(T1_trans,T1)
test1=np.matmul(ones_pre,test)
Sum_of_IV=np.matmul(test1,ones_post)
Sum_of_IV=Sum_of_IV-Diagonal
print(Sum_of_IV)
break