Created
January 8, 2012 21:25
-
-
Save robbrit/1579753 to your computer and use it in GitHub Desktop.
Cointegration Finder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# calculates the best vector of cointegration for a group of series. | |
# | |
# @param series A matrix with the series in columns | |
# | |
# @return The best vector of cointegration. | |
# | |
get_coint = function(series){ | |
# does this by performing a regression of time on | |
# a linear combination of the series, but only take | |
# the slope parameter | |
m = dim(series)[1] | |
t = 1:m - (1 + m) / 2 | |
sst = t %*% t | |
func = function(alpha){ | |
# get the square of the slope parameter | |
(( t %*% (series %*% alpha) ) / sst) ** 2 | |
} | |
# initial value is a vector of ones | |
start = seq(1, 1, length = dim(series)[2]) | |
# give the parameters alpha that minimize the square of the slope | |
optim(start, func)$par | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment