查找投资组合回报的协方差



我创建了一个包含 5 只股票的投资组合,我想找到整个投资组合的年回报率和波动率。这是我的代码:

df1 = pd.read_csv("C:stock_1.csv")
df2 = pd.read_csv("C:stock_2.csv")
df3 = pd.read_csv("C:stock_3.csv")
df4 = pd.read_csv("C:stock_4.csv")
df5 = pd.read_csv("C:stock_5.csv")
frames = [df1,df2,df3,df4,df5]
data = pd.concat(frames,axis=0)
print(data)
data.to_csv("C:final.csv")
data_new = data['Close Price']
data_new.sort_index(inplace=True)
returns = data_new.pct_change()
mean_daily_returns = returns.mean()
cov_matrix = returns.cov()
weights = np.asarray([0.2,0.2,0.2,0.2,0.2])
portfolio_return = round(np.sum(mean_daily_returns * weights) * 252,2)
portfolio_std_dev = round(np.sqrt(np.dot(weights.T,np.dot(cov_matrix, weights))) * np.sqrt(252),2)
print(portfolio_return)
print(portfolio_std_dev)

我收到错误为:

cov_matrix = returns.cov()
TypeError: cov() missing 1 required positional argument: 'other'

在以下代码行中:

cov_matrix = returns.cov()

您要计算pandas.Series的协方差。根据 pandas 文档,您需要指定用于计算协方差的other参数。基本上,您需要一个其他系列来比较'Close Price'

相关内容

  • 没有找到相关文章

最新更新