Skip to content

Instantly share code, notes, and snippets.

@robbrit
Created January 8, 2012 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robbrit/1579753 to your computer and use it in GitHub Desktop.
Save robbrit/1579753 to your computer and use it in GitHub Desktop.
Cointegration Finder
# 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