[Numpy-discussion] Time series: lag function

Sven Schreiber svetosch at gmx.net
Wed Dec 27 19:17:35 EST 2006


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




More information about the NumPy-Discussion mailing list