从2个数据帧计算加权股票收益

时间:2019-05-10 03:09:24

标签: python pandas

我有一个由7只股票组成的投资组合,其信息在2个DataFrames中。

市值:

                 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

我想计算每日加权平均投资组合收益。权重是股票总金额占总股票总金额的百分比。

例如,加权收益应为

sum(stock return i * stock cap i)/sum(stock cap i)

如何生成包含整个期间每日收益的新数据框?

1 个答案:

答案 0 :(得分:0)

不确定我是否正确地找到了您,但是呢:

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)