程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在 Python 中绘制同一投资组合中多个资产的分布大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何在 Python 中绘制同一投资组合中多个资产的分布?

开发过程中遇到如何在 Python 中绘制同一投资组合中多个资产的分布的问题如何解决?下面主要结合日常开发的经验,给出你关于如何在 Python 中绘制同一投资组合中多个资产的分布的解决方法建议,希望对你解决如何在 Python 中绘制同一投资组合中多个资产的分布有所启发或帮助;

我的这个投资组合由以下三项资产组成

# import some librarIEs
from pandas_datareader import data as wb
import pandas as pd
import numpy as np
from datetime import datetime
import matplotlib.pyplot as plt
from scipy.stats import norm
import math
# get the stock symbols in our portfolio
assets = ['AAP','NFLX','BA']

# assign weights to our portfolio 
weights = np.array([0.2,0.3,0.5])
# get the portfolio start date
stockStartDate = '2020-01-01'

# get the portfolio ending date 
today = datetime.today().strftime('%Y-%m-%d')

# create a dataframe to store the adjusted close price of the price
df = pd.DataFrame()

# store the adjusted close price pf the stock into the df
for stock in assets : 
  df[stock] = wb.DataReader(stock,data_source = 'yahoo',start = stockStartDate,end = today)['Adj Close']

# visually show the portfolio

Title = 'Portfolio Adjusted Close Price History'

#get the stocks
my_stocks = df

# create & plot the graph
for c in my_stocks.columns.values:
  plt.plot(my_stocks[c],label = c)

plt.Title(Title)
plt.xlabel('Date',Fontsize = 18)
plt.ylabel('Adj. Price USD($)')
plt.legend(my_stocks.columns.values,loc = 'upper left')
plt.show()

导致这个结果: Portfolio Adjusted Price History

我已经能够使用下面的代码为整个投资组合绘制一张图表

returns = df.pct_change()
df1 = returns

mean = sum(weights)/len(weights)
var = sum(pow(x-mean,2) for x in weights) / len(weights)
std = math.sqrt(var)
x_min = df1.min()*100
x_max = df1.max()*100

mean = mean
std = std
#x_axis = np.arange(x_min,x_max,0.01)
x_axis = np.arange(-20,20,0.01)
plt.plot(x_axis,norm.pdf(x_axis,mean,std))
plt.xlabel('returns')
plt.ylabel('Frequency density')
plt.Title('Returns distributions for N = 1')
plt.show()

以及以下代码:

for d in returns.columns:
  plt.plot(returns[d],label = d)

plt.Title(Title)
plt.xlabel('Date',loc = 'upper left')
plt.show()

但是我想要的结果如下: Asset price distribution 和 Asset gross return

你能帮忙计算这两个图吗?

非常感谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的如何在 Python 中绘制同一投资组合中多个资产的分布全部内容,希望文章能够帮你解决如何在 Python 中绘制同一投资组合中多个资产的分布所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:Python