gpt4 book ai didi

python - 从 2 个数据帧计算加权股票 yield

转载 作者:太空宇宙 更新时间:2023-11-03 11:58:24 25 4
gpt4 key购买 nike

我有一个包含 7 只股票的投资组合,它们的信息在 2 个 DataFrame 中。

市值:

                 AAPL      GOOGL       AMZN     FB     IBM    MSFT        ORCL
2018-06-04 942870.9225 795721.6978 808033.8064 559683.8431 131306.1865 781150.6901 193175.0512
2018-06-05 950145.3268 795208.7988 823114.6586 558699.2999 131912.0456 785145.9528 192399.4117
2018-06-06 953438.4692 792862.4102 822823.5225 554066.1556 132839.1936 787450.9121 194930.4458
2018-06-07 950882.5975 783977.2272 819693.8089 544915.6954 133435.8730 775080.9641 194644.6838
2018-06-08 942231.9546 782312.8482 817117.2541 547579.7534 134151.8882 780843.3622 196685.8403

和投资组合股票价格:

                AAPL       FB        GOOG        AMZN        IBM         MSFT        ORCL


2018-06-04 189.6813 193.28 1139.29 1665.27 136.5008 100.4157 46.5525
2018-06-05 191.1447 192.94 1139.66 1696.35 137.1307 100.9293 46.3656
2018-06-06 191.8072 191.34 1136.88 1695.75 138.0945 101.2256 46.9755
2018-06-07 191.2930 188.18 1123.86 1689.30 138.7148 99.6354 46.9067

我想计算每日加权平均投资组合返回。权重是股票上限占总上限的百分比。

比如加权 yield 应该是

sum(股票 yield i * 股票市值 i)/sum(股票市值 i)

我如何生成包含整个期间每日返回的新数据框?

最佳答案

不确定我是否理解正确,但是这个怎么样:

import pandas as pd

pd.set_option('display.max_columns',30)
pd.set_option('display.width',1000)
pd.set_option('precision', 4)

mc="""date AAPL GOOG AMZN FB IBM MSFT ORCL
2018-06-04 942870.9225 795721.6978 808033.8064 559683.8431 131306.1865 781150.6901 193175.0512
2018-06-05 950145.3268 795208.7988 823114.6586 558699.2999 131912.0456 785145.9528 192399.4117
2018-06-06 953438.4692 792862.4102 822823.5225 554066.1556 132839.1936 787450.9121 194930.4458
2018-06-07 950882.5975 783977.2272 819693.8089 544915.6954 133435.8730 775080.9641 194644.6838
2018-06-08 942231.9546 782312.8482 817117.2541 547579.7534 134151.8882 780843.3622 196685.8403
"""
sp="""date AAPL FB GOOG AMZN IBM MSFT ORCL
2018-06-04 189.6813 193.28 1139.29 1665.27 136.5008 100.4157 46.5525
2018-06-05 191.1447 192.94 1139.66 1696.35 137.1307 100.9293 46.3656
2018-06-06 191.8072 191.34 1136.88 1695.75 138.0945 101.2256 46.9755
2018-06-07 191.2930 188.18 1123.86 1689.30 138.7148 99.6354 46.9067
"""
marketcap=pd.read_csv(pd.compat.StringIO(mc),header=0,sep="\s+",parse_dates=True,index_col=0)
prices=pd.read_csv(pd.compat.StringIO(sp),header=0,sep="\s+",parse_dates=True,index_col=0)
portfolioReturns=pd.DataFrame()

def weightedReturn(key):
dailyReturns=prices[key].pct_change(1)
weights=marketcap[key]
portfolioReturns[key]=dailyReturns*weights

[weightedReturn(key) for key in prices.columns]
print(portfolioReturns)

产量:

                 AAPL         FB       GOOG        AMZN       IBM        MSFT       ORCL
date
2018-06-04 NaN NaN NaN NaN NaN NaN NaN
2018-06-05 7330.4151 -982.8113 258.2549 15362.3158 608.7246 4015.8159 -772.4494
2018-06-06 3304.5802 -4594.7230 -1934.0483 -291.0332 933.6379 2311.7341 2564.1441
2018-06-07 -2549.1422 -8999.3394 -8978.4177 -3117.8093 599.3741 -12176.1071 -285.0753
2018-06-08 NaN NaN NaN NaN NaN NaN NaN

当然 portfolioReturns.mean(axis=1) 将返回投资组合的平均每日返回。

或者,作为一行代码:print(prices.pct_change()).multiply(marketcap).mean(axis=1)

关于python - 从 2 个数据帧计算加权股票 yield ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56070170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com