[英]Series of correlation matrices in R
鉴于以下婴儿片段:
d1=as.Date('April 26, 2001',format='%B %d, %Y')
d2=as.Date('April 27, 2001',format='%B %d, %Y')
d3=as.Date('April 28, 2001',format='%B %d, %Y')
tibble(DATE=c(d1,d1,d2,d2,d3,d3), Symbol=c("A","B","A","B","A","B"), voladj=c(0.2, 0.3, -0.2, -0.1, 0.3, 0.2))
导致
# A tibble: 6 x 3
DATE Symbol voladj
<date> <chr> <dbl>
1 2001-04-26 A 0.2
2 2001-04-26 B 0.3
3 2001-04-27 A -0.2
4 2001-04-27 B -0.1
5 2001-04-28 A 0.3
6 2001-04-28 B 0.2
我尝试在时间 D2 计算一系列相关/协方差矩阵 cor,在时间 D3 计算 cor,等等。理想情况下,数据是指数加权的。 我在 R 中有哪些选择? 为了让事情更辣一点,符号 C 也可能在某个时候出现。 我想在时间 t1、t2、t3 计算外积(秩 1 矩阵),然后使用简单的移动平均值。
潜在的输出可能如下:
DATE cov
<date>
1 2001-04-26 M1
2 2001-04-27 M2
3 2001-04-28 M3
其中 M_i 是矩阵(或帧),例如
M_1 = A B
A 1.0 c1
B c1 1.0
等。显然更有趣,一旦涉及更多的符号
这是一种使用quantmod
从雅虎财经检索 5 只股票三周的方法。 我们将来自xts
对象的Close
变量组合到一个数据框中,使用lubridate::week()
生成week
标识符, lubridate::week()
split()
并使用lapply()
计算每周的协方差矩阵。
library(quantmod)
from.dat <- as.Date("12/03/19",format="%m/%d/%y")
to.dat <- as.Date("12/24/19",format="%m/%d/%y")
theSymbols <- c("AAPL","AXP","BA","CAT","CSCO")
getSymbols(theSymbols,from=from.dat,to=to.dat,src="yahoo")
#combine to single data frame
combinedData <- data.frame(date = as.Date(rownames(as.data.frame(AAPL))),
AAPL$AAPL.Close,
AXP$AXP.Close,
BA$BA.Close,
CAT$CAT.Close,
CSCO$CSCO.Close)
colnames(combinedData) <- c("date","AAPL","AXP","BA","CAT","CSCO")
# split by week
library(lubridate)
combinedData$week <- week(combinedData$date)
symbolsByWeek <- split(combinedData,as.factor(combinedData$week))
covariances <- lapply(symbolsByWeek,function(x){
cov(x[,-c(1,7)])
})
covariances[[1]]
...和输出:
> covariances[[1]]
AAPL AXP BA CAT CSCO
AAPL 19.4962156 7.0959976 3.9093027 5.4158116 -0.66194433
AXP 7.0959976 3.0026695 2.0175793 2.2569625 -0.18793832
BA 3.9093027 2.0175793 10.4511473 1.8555752 0.55619975
CAT 5.4158116 2.2569625 1.8555752 1.8335361 -0.11141911
CSCO -0.6619443 -0.1879383 0.5561997 -0.1114191 0.07287982
>
这是一种使用quantmod
从雅虎财经检索四天的道琼斯指数 30 数据的方法, apply()
和do.call()
与rbind()
将其合并为单个数据帧,然后split()
按天拆分以生成每日协方差矩阵。
library(quantmod)
from.dat <- as.Date("12/02/19",format="%m/%d/%y")
to.dat <- as.Date("12/06/19",format="%m/%d/%y")
theSymbols <- c("AAPL","AXP","BA","CAT","CSCO","CVX","XOM","GS","HD","IBM",
"INTC","JNJ","KO","JPM","MCD","MMM","MRK","MSFT","NKE","PFE","PG",
"TRV","UNH","UTX","VZ","V","WBA","WMT","DIS","DOW")
getSymbols(theSymbols,from=from.dat,to=to.dat,src="yahoo")
# since quantmod::getSymbols() writes named xts objects, need to use
# get() with the symbol names to access each data frame
# e.g. head(get(theSymbols[[1]]))
# convert to list
symbolData <- lapply(theSymbols,function(x){
y <- as.data.frame(get(x))
colnames(y) <- c("open","high","low","close","volume","adjusted")
# add date and symbol name to output data frames
y$date <- rownames(y)
y$symbol <- x
y
})
#combine to single data frame
combinedData <- do.call(rbind,symbolData)
# split by day
symbolsByDay <- split(combinedData,as.factor(combinedData$date))
covariances <- lapply(symbolsByDay,function(x){
cov(x[,1:6]) # only use first 6 columns
})
# print first covariance matrix
covariances[1]
...和输出:
> covariances[1]
$`2019-12-02`
open high low close volume adjusted
open 5956.289 5962.359 5811.514 5818.225 -9.274871e+07 5809.939
high 5962.359 5968.557 5817.580 5824.272 -9.314473e+07 5816.005
low 5811.514 5817.580 5671.809 5678.470 -9.188418e+07 5670.276
close 5818.225 5824.272 5678.470 5685.467 -9.155485e+07 5677.246
volume -92748711.735 -93144729.578 -91884178.312 -91554853.356 4.365841e+13 -90986549.261
adjusted 5809.939 5816.005 5670.276 5677.246 -9.098655e+07 5669.171
>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.