R:用向量乘以XTS对象

时间:2018-01-06 00:13:01

标签: r time-series matrix-multiplication xts finance

我有以下XTS对象:

> head(stocks)
           UA.Close F.Close ORCL.Close GE.Close
2015-07-01    84.26   14.99      40.24    26.66
2015-07-02    84.59   14.87      40.36    26.78
2015-07-06    84.58   14.79      40.15    26.31
2015-07-07    84.94   14.85      40.16    26.47
2015-07-08    84.39   14.37      39.77    25.89
2015-07-09    84.49   14.33      40.18    26.02

我的想法是使用历史性能找到包含这些股票的所有可能投资组合。为此,我计算了投资组合中所有可能的股票组合:

> # Get all possible combinations
> part <- partitions::compositions(n = 100, m = 4, include.zero = T)/100
> part <- matrix(part, ncol = 4, byrow = T)
> head(part)
     [,1] [,2] [,3] [,4]
[1,] 1.00 0.00    0    0
[2,] 0.99 0.01    0    0
[3,] 0.98 0.02    0    0
[4,] 0.97 0.03    0    0
[5,] 0.96 0.04    0    0
[6,] 0.95 0.05    0    0

我希望将part向量的每一行乘以stock xts对象,以便通过决定每个股票进入投资组合的数量来获得所有历史性能。但是,当我将stocks乘以part的第一行时,我得到以下内容:

> head(stocks*part[1,])
           UA.Close F.Close ORCL.Close GE.Close
2015-07-01    84.26    0.00      40.24     0.00
2015-07-02     0.00    0.00       0.00     0.00
2015-07-06     0.00   14.79       0.00    26.31
2015-07-07     0.00    0.00       0.00     0.00
2015-07-08    84.39    0.00      39.77     0.00
2015-07-09     0.00    0.00       0.00     0.00

但是,我期待以下内容:

           UA.Close F.Close ORCL.Close GE.Close
2015-07-01    84.26   0.00      0.00    0.00
2015-07-02    84.59   0.00      0.00    0.00
2015-07-06    84.58   0.00      0.00    0.00
2015-07-07    84.94   0.00      0.00    0.00
2015-07-08    84.39   0.00      0.00    0.00
2015-07-09    84.49   0.00      0.00    0.00

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

此操作:

head(stocks*part[1,])

将股票中的矩阵值乘以矩阵部分的第一行,使用R 回收规则进行矩阵乘法。

您真正想要做的是将part乘以与stock相同的尺寸。

如果coredata(stocks)*parthead的尺寸相同,则stocks只会按预期工作(您可以part)。

以下是可以帮助您的可重复示例:

library(quantmod)

getSymbols("AMD")
# using OHLC will return 4 columns, which matches the 4 in part:
stocks <- OHLC(AMD[1:50, ])
y <- seq(1, to = 0, by = -0.01)
part <- matrix(c(y, rev(y), rep(0, length(y)), rep(0, length(y))), ncol = 4)

dim(stocks)
#[1] 50  4

out <- lapply(1:NROW(part), function(i, stocks, part) {
  mm <- matrix(rep(part[i, ], times = NROW(stocks)), byrow = TRUE, ncol = 4)
  res <- coredata(stocks) * mm
  x <- xts(res, index(stocks))
  x
}, stocks = stocks, part = part)

length(out)
#[1] 101

head(out[[90]])
#            AMD.Open AMD.High AMD.Low AMD.Close
# 2007-01-03   2.2088  18.1560       0         0
# 2007-01-04   2.1626  17.6754       0         0
# 2007-01-05   2.1494  17.7199       0         0
# 2007-01-08   2.1681  17.6754       0         0
# 2007-01-09   2.1395  17.5419       0         0
# 2007-01-10   2.1604  17.8178       0         0

你在做什么(不是你想要的)是这样的:

part[1, ]
#[1] 1.00 0.51 0.00 0.00

head(stocks * part[1, ], 14)
#            AMD.Open AMD.High   AMD.Low AMD.Close
# 2007-01-03  20.0800   0.0000 19.350000  0.000000
# 2007-01-04  10.0266   0.0000  9.853200  0.000000
# 2007-01-05   0.0000  19.9100  0.000000 19.709999
# 2007-01-08   0.0000  10.1286  0.000000  9.929699
# 2007-01-09  19.4500   0.0000 19.370001  0.000000
# 2007-01-10  10.0164   0.0000  9.945000  0.000000
# 2007-01-11   0.0000  20.6300  0.000000 20.180000
# 2007-01-12   0.0000   9.5727  0.000000  9.312600
# 2007-01-16  18.1500   0.0000 17.950001  0.000000
# 2007-01-17   9.1290   0.0000  9.129000  0.000000
# 2007-01-18   0.0000  18.1800  0.000000 17.920000
# 2007-01-19   0.0000   9.1698  0.000000  9.042300
# 2007-01-22  17.5900   0.0000 17.260000  0.000000
# 2007-01-23   8.9760   0.0000  8.858701  0.000000

查看每列中的每4个观察结果(列顺序方式)如何重复权重序列乘以该栏中的相应价格