计算投资组合级别返回

时间:2017-09-10 23:32:57

标签: r quantmod stock portfolio performanceanalytics

编辑更新我发现了performanceAnalytics的一篇好文章。这篇文章基本上总结了累积投资组合回报的结果,并且正如作者所说,它非常棘手(他也错了)!这里仅供参考:https://tradeblotter.wordpress.com/2014/09/25/aggregate-portfolio-contributions-through-time/

所以我遇到了一个小问题,我的两个数字应该加起来,但事实并非如此。这是一个示例数据框,其中包含股票选择及其对上下文投资组合的权重:

      stock.choices stock_weights
1          GOOG         0.150
2          AMZN         0.200
3            BA         0.250
4            FB         0.225
5          AAPL         0.175

然后我将使用credit.portfolio函数和wealth.index = TRUE来显示我的投资组合的回报。

Portfolio <- merge.xts(GOOG,AMZN,BA,FB,AAPL)
dollar_growth <- Return.portfolio(Portfolio, weights = stock_weights, wealth.index = TRUE)

我使用dygraph来描绘美元的增长。

dygraph(dollar_growth, main = "Portfolio Growth Base.$1") %>% dyAxis("y", label = "$")%>%
  dyAnnotation("2017-05-01", text = May1, tooltip = "Initial Investment", width = 70, height = 18, tickHeight = -75)%>%
  dyAnnotation(LastDay, text = Today, tooltip = "Percentage Increase",width = 70,  attachAtBottom = TRUE) %>%
  dyAxis("y", label = "Dollars USD")

对于这个例子,我将使用5月1日作为投资的初始点。在这个投资组合中,我从5月1日获得了11.5%的回报 - 以当前价值(1.37美元)计算并除以5月1日(1.23057美元),收益率增长11.33%。

然而,当我使用不同的方法时,我会得到一个不同的答案,这很奇怪,因为我认为第二种方法是计算投资组合回报的准确方法。

首先,我创建一个数据框,其中包含5月1日的股票价值及其当前值。然后我将它们各自的权重乘以投资组合。这是输出:

      May1 Current Stock.Weights    May1C  CurrentC
GOOG 912.57  926.50         0.150 136.8855 138.97500
AMZN 948.23  965.90         0.200 189.6460 193.18000
BA   182.39  238.78         0.250  45.5975  59.69500
FB   152.46  170.95         0.225  34.3035  38.46375
AAPL 146.58  158.63         0.175  25.6515  27.76025

May1C = May1 * Stock.Weights | CurrentC = Current * Stock.Weights

现在,当我将May1C和CurrentC相加时,我得到:

> sum(df$May1C)
[1] 432.084
> sum(df$CurrentC)
[1] 458.074

我认为这将是投资组合的当前价值,因为它是股票选择*各自的权重。这只会增加6.015%。

我的问题是: return.portfolio函数如何返回11.3%,而第二种方法返回6.015%?

编辑回复评论我发现在使用return.portfolio时,verbose = TRUE函数会返回随时间变化的股票权重。此输出显示权重随EOP和BOP而变化。

供参考,这是运行dygraph输出的完整代码:

library(PerformanceAnalytics)
library(quantmod)
library(dygraphs)
library(scales)

daily_stock_returns = function(ticker) {

    symbol <- getSymbols(ticker, src = 'google', auto.assign = FALSE, warnings = FALSE)
    symbol <- xts::last(symbol, "1 year")
    data <- periodReturn(symbol, period = 'daily', type = 'log')
    colnames(data) <- as.character(ticker)
    assign(ticker, data, .GlobalEnv)
}


    daily_stock_returns("GOOG")
    daily_stock_returns("AMZN")
    daily_stock_returns("BA")
    daily_stock_returns("FB")
    daily_stock_returns("AAPL")
    Portfolio <- merge.xts(GOOG,AMZN,BA,FB,AAPL)
    test <- periodReturn(Portfolio, period = 'monthly', type = 'log')
    stock_weights <- c(.15, .20, .25, .225, .175)

    dollar_growth <- Return.portfolio(Portfolio, weights = stock_weights, wealth.index = TRUE)
    May1 <- as.numeric(dollar_growth["2017-05-01"]) 
    format(round(May1, 3), nsmall = 2)
    Today <- as.numeric(xts::last(dollar_growth, "1 day"))
    Today <- ((Today/May1)-1) %>% percent()
    format(round(May1, 3), nsmall = 2)
    LastDay <- xts::last(dollar_growth, "1 day")
    dygraph(dollar_growth, main = "Portfolio Growth Base.$1") 

1 个答案:

答案 0 :(得分:1)

如果您想查看投资组合组件的美元价值和总投资组合,您可以执行以下操作。假设你想要的是在“DayStart(2017-01-01)”上投资一个投资组合,分配“alloc(.15,.20,.25,.225,.175)”,然后在没有重新平衡的情况下让它运行它的路线直到“DayEnd(2017-05-01)”:

对于GOOG,AMZN,BA,FB,AAPL的初始分配(例如1000美元):150,200,250,225,175

拿你的投资组合返回“投资组合”(我拿'离散'回报而非'日''):

startCapital <- c(150, 200, 250, 225, 175)
portDollar <- cumprod(1+Portfolio["::2017-05-01”]) * startCapital
portDollar <- cbind(portDollar,portf=rowSums(portDollar))

您现在可以用美元绘制投资组合值或将其转换回退货。

both(portDollar)
               GOOG     AMZN       BA       FB     AAPL    portf
2017-01-03 151.4052 248.5942 175.7486 201.4256 225.6790 1002.853
2017-01-04 202.0686 224.7743 152.2168 255.6943 175.3316 1010.086
2017-01-05 254.8609 180.1164 203.0709 233.9321 151.0465 1023.027
               GOOG     AMZN       BA       FB     AAPL    portf
2017-04-27 195.9950 241.4572 262.7753 190.4188 309.3954 1200.042
2017-04-28 173.9812 303.9860 206.1689 258.2377 278.1846 1220.558
2017-05-01 233.6613 280.3763 174.3678 327.5105 220.7346 1236.650