简体   繁体   English

在R中为投资组合分配权重

[英]Assigning weights to a portfolio in R

I am trying to show in R how I can assign weights to the portfolio and then calculate the daily return. 我试图在R中展示如何为投资组合分配权重,然后计算每日收益。 Below is the data for daily return and I have assigned 25% weight to each of them for making an equal weight portfolio and to calculate the returns. 以下是每日收益的数据,我为每个收益分配了25%的权重,以制作权重相等的投资组合并计算收益。 (There are 4 company stocks out of which i have just provided data for 2 stocks, hence 25% weight to each stock has been given) However, I am unbale to configure the same in R. (有4个公司股票,其中我刚刚提供了2个股票的数据,因此,给了每只股票25%的权重)。但是,我很乐意在R中配置相同的股票。

Workings: 

25% = 636.99*25% = 159.25
      633.79*25% = 158.45

Returns = Closing - opening 
        = 159.25-158.45
        = -0.80

Is there any way I can show this in R?


    AMZN     25%    Returns  GOOG     25%   Returns
    636.99  159.25          741.84  185.46  
    633.79  158.45  -0.80   742.58  185.65   0.18
    632.65  158.16  -0.28   743.62  185.90   0.26
    607.94  151.99  -6.18   726.39  181.60  -4.31
    607.05  151.76  -0.22   714.47  178.62  -2.98
    617.74  154.43   2.67   716.03  179.01   0.39
    617.89  154.47   0.04   726.07  181.52   2.51
    581.81  145.45  -9.02   700.56  175.14  -6.38

Returns should be divided, not subtracted. 收益应除而不是相减。 Your 1-day return for Amazon should be calculated as 633.79 / 636.69 - 1 = -0.0046 . 您对亚马逊的1天退货应计算为633.79 / 636.69 - 1 = -0.0046 An equal-weighted portfolio could just take the mean of these four returns on day 2. 权重相等的投资组合可以在第2天获得这四个收益的平均值。

Beyond day 2, you need to think about how often your portfolio is rebalanced. 在第二天之后,您需要考虑投资组合重新调整的频率。 This is more a topic for quant.stackexchange.com, and not a programming question. 这更多是quantum.stackexchange.com的主题,而不是编程问题。 But I'll give you both the (unrealistic) case where you assume the portfolio is perfectly equal-weighted at the start of each day, and the (simple & realistic) case where we set the portfolio to be equal weighted at the start and we forget about it. 但是,我会给您两种情况(不现实),在这种情况下,您假设投资组合在每天开始时的权重是完全相等的;而在(简单而现实)情况下,我们将投资组合在开始时和开始时设置为相等权重。我们忘记了。

As I'd mentioned in your question about stock price returns , a good R package for collecting and manipulating stock prices is quantmod . 正如我在关于股票价格回报的问题中提到的那样,一个好的用来收集和处理股票价格的R包是quantmod Try: 尝试:

library(quantmod)
symbols <- c("GOOG", "AMZN", "FB", "AAPL")
getSymbols(symbols, src = 'google')
closing.prices <- merge.xts(GOOG[,4], AMZN[,4], FB[,4], AAPL[,4])["2016-12-30/"]

Note that everything will be done using xts time series. 请注意,所有操作都将使用xts时间序列完成。 The time series closing.prices will be prices since the end of 2016, and can be converted to a series of returns using ROC from the TTR package: 时间序列closing.prices是自2016年底以来的价格,可以使用TTR包中的ROC转换为一系列收益:

library(TTR)
price.returns = ROC(closing.prices)

For our portfolio which is 25% equal-weighted at the start of every day, the portfolio returns will be the means: 对于每天开始时权重为25%的投资组合,投资组合收益将是以下方法:

rowMeans(price.returns)

The more realistic case where a portfolio was 25% equal-weighted at the start of the year, you might compute the overall returns of each stock, then compute the mean of the four: 更为现实的情况是,年初投资组合的权重为25%,您可以计算每只股票的整体收益,然后计算这四只股票的均值:

mean(as.numeric(closing.prices["2017-09-12"]) / as.numeric(closing.prices["2016-12-30"]) - 1)

where closing.prices["2017-09-12"] is the closing price today, closing.prices["2016-12-30"] was the closing price at the start of the year. 其中closing.prices["2017-09-12"]是今天的收盘价, closing.prices["2016-12-30"]是年初的收盘价。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM