Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cov/cor between two metrics #2

Closed
JianyangZhao opened this issue Sep 12, 2016 · 4 comments
Closed

cov/cor between two metrics #2

JianyangZhao opened this issue Sep 12, 2016 · 4 comments
Labels

Comments

@JianyangZhao
Copy link

@JianyangZhao JianyangZhao commented Sep 12, 2016

For example like
(-1 * correlation(open, volume, 10))
If we have two metrics for open and volume each, and each column for a symbol. It will be much more convenient if we can use something like "roll_cor(open, volume, 10)" or "roll_cor(list(open, volume), 10)" rather than transform into vectors then transform back.

@jjf234
Copy link
Owner

@jjf234 jjf234 commented Sep 13, 2016

Thanks for the feedback. Just so I understand what you’re thinking, here’s an example of how I’d approach it using a for loop in R:

library(roll)
library(xts)

n_lists <- 2
n_vars <- 10
n_obs <- 1000
start_date <- as.Date("2013-12-31")
dates <- seq(start_date, length.out = n_obs, by = "day")
tickers <- paste("Ticker", 1:n_vars, sep = "")

# create dummy data
data <- list()
for (i in 1:n_lists) {
  temp <- xts(matrix(rnorm(n_obs * n_vars), nrow = n_obs, ncol = n_vars), dates)
  colnames(temp) <- tickers
  data <- c(data, list(temp))
}

# rolling correlation
result <- list()
for (i in 1:n_vars) {
  temp <- do.call(merge, c(lapply(1:n_lists, function(j) data[[j]][ , i])))
  result <- c(result, list(roll_cor(temp, 252)))
}
names(result) <- tickers

result[["Ticker1"]][ , , n_obs]
result[["Ticker2"]][ , , n_obs]

If so, in addition to price vs volume for many securities, do you have other use cases that come to mind? My sense is more basic applications, such as when there is only one x (e.g. correlation of many securities to one market) or one y (e.g. correlation of one security to many markets), are more common and the approach above would not work.

@uzhao
Copy link

@uzhao uzhao commented Sep 14, 2016

Current i use a similar implement. But if we can have a native cpp solution could it be more efficient in time cost and memory cost. For now we have to use a 2*2 matrix for saving 1 value.

roll_cov2 = function(x, y, n) {
ret = matrix(roll_cov(cbind(as.vector(x), as.vector(y)), n)[1,2,], nrow = nrow(x))
ret[1:(n-1),] = NA
ret
}

@jjf234
Copy link
Owner

@jjf234 jjf234 commented Sep 15, 2016

Thanks, I believe you're simply asking to have the same behavior as in stats::cov(x, y = NULL). Makes sense and it's something I'd also like to include in future versions.

@jjf234
Copy link
Owner

@jjf234 jjf234 commented Jul 29, 2018

This feature was added in the development version:

# install.packages("devtools")
devtools::install_github("jjf234/roll")
@jjf234 jjf234 closed this Jul 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.