I am tryin to convert some of my time-series code written in Ox to scipy/numpy (e.g., unit root tests, IRFs, cointegration, etc). Two key functions I need for this are 'lag' and 'diff'. 'diff' is available but 'lag' is apparently not. Below is my attempt at a lag function. I tried to be somewhat consistent with the diff function which is part of numpy (also listed for convenience). It seems to work fine for a 2-d array but not for a 1-d or 3-d array (see tests at bottom of email). I'd appreciate any suggestions you may have. Thanks, Vincent from numpy import * def diff(a, n=1, axis=-1): """Calculate the nth order discrete difference along given axis. """ if n == 0: return a if n < 0: raise ValueError, 'order must be non-negative but got ' + repr(n) a = asanyarray(a) nd = len(a.shape) slice1 = [slice(None)]*nd slice2 = [slice(None)]*nd slice1[axis] = slice(1, None) slice2[axis] = slice(None, -1) slice1 = tuple(slice1) slice2 = tuple(slice2) if n > 1: return diff(a[slice1]-a[slice2], n-1, axis=axis) else: return a[slice1]-a[slice2] def lag(a, n=1, lag_axis=0, concat_axis=1): """ Calculate the nth order discrete lag along given axis. Note: axis=-1 means 'last dimension'. This is the default for the diff function. However, the first dimension (0) may be preferred for time-series analysis. """ a = asanyarray(a) n = ravel(n) # convert input to an array nmax = n.max() # determines length of array to be returned nd = len(a.shape) # number of dimentions in array a s = [slice(None)]*nd # lag for 1st element in n s[lag_axis] = slice(nmax-n[0],-n[0]) ret_a = a[tuple(s)] # array to be returned # lags for other elements in n for i in n[1:]: s[lag_axis] = slice(nmax-i,-i) ret_a = concatenate((ret_a,a[tuple(s)]), concat_axis) return ret_a # testing lag function # test 1 data = arange(10) print "=" * 30 print "test 1 - data" print data print "\nlag 2" print lag(data,2) print "\nlag 1,2,3" print lag(data,range(1,4)) print "=" * 30 + "\n" # test 2 data = arange(10) data = vstack((data,data)).T print "=" * 30 print "test 2 - data" print data print "\nlag 2" print lag(data,2) print "\nlag 1,2,3" print lag(data,range(1,4)) print "=" * 30 + "\n" # test 3 data = arange(10) data = vstack((data,data)).T data = dstack((data,data)) print "=" * 30 print "test 3 - data" print data print "\nlag 2" print lag(data,2) print "\nlag 1,2,3," print lag(data,range(1,4)) print "=" * 30 + "\n"
Vincent Nijs schrieb:
I am tryin to convert some of my time-series code written in Ox to scipy/numpy (e.g., unit root tests, IRFs, cointegration, etc). Two key functions I need for this are 'lag' and 'diff'. 'diff' is available but 'lag' is apparently not.
Below is my attempt at a lag function. I tried to be somewhat consistent with the diff function which is part of numpy (also listed for convenience). It seems to work fine for a 2-d array but not for a 1-d or 3-d array (see tests at bottom of email). I'd appreciate any suggestions you may have.
Great to see somebody converting from Ox to numpy, I see synergies ahead!
def lag(a, n=1, lag_axis=0, concat_axis=1): """ Calculate the nth order discrete lag along given axis. Note: axis=-1 means 'last dimension'. This is the default for the diff function. However, the first dimension (0) may be preferred for time-series analysis. """ a = asanyarray(a)
n = ravel(n) # convert input to an array
why don't you leave n as an integer? maybe you're trying to be too clever here. I think it's a good idea to have lag resemble the existing diff function, and then a single number n should be enough. (And I'm not sure about your concat_axis, e.g. what does axis=1 mean for a 1-d array?) Do you get your errors also for integer n? cheers, sven
Sven: I simplified the function to create lags only along axis 0 (see attached). I am using c_ now which seems to play nice with 1- and 2-d array's. The reason I am using 'n = ravel(n)' in the code is that I want to be able to pass integers as well as lists. For example, I want each of the following to work: lag(a,2) lag(a,range(1,3)) lag(a,[1]+range(4,6)) I'd actually also like the following to work: lag(a,1,3,6,8) I could do that with *n but then I don't think I can use range(x,y) in the same function call. For example, lag(a,1,3,range(6,9)). You probably don't need this flexibility when calling diff() since, at least in TS applications, I only ever need diff(a,1) or diff(a,2). Thanks, Vincent On 12/27/06 6:17 PM, "Sven Schreiber" <svetosch@gmx.net> wrote:
Vincent Nijs schrieb:
I am tryin to convert some of my time-series code written in Ox to scipy/numpy (e.g., unit root tests, IRFs, cointegration, etc). Two key functions I need for this are 'lag' and 'diff'. 'diff' is available but 'lag' is apparently not.
Below is my attempt at a lag function. I tried to be somewhat consistent with the diff function which is part of numpy (also listed for convenience). It seems to work fine for a 2-d array but not for a 1-d or 3-d array (see tests at bottom of email). I'd appreciate any suggestions you may have.
Great to see somebody converting from Ox to numpy, I see synergies ahead!
def lag(a, n=1, lag_axis=0, concat_axis=1): """ Calculate the nth order discrete lag along given axis. Note: axis=-1 means 'last dimension'. This is the default for the diff function. However, the first dimension (0) may be preferred for time-series analysis. """ a = asanyarray(a)
n = ravel(n) # convert input to an array
why don't you leave n as an integer? maybe you're trying to be too clever here. I think it's a good idea to have lag resemble the existing diff function, and then a single number n should be enough.
(And I'm not sure about your concat_axis, e.g. what does axis=1 mean for a 1-d array?)
Do you get your errors also for integer n?
cheers, sven
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
--
participants (2)
-
Sven Schreiber
-
Vincent Nijs