Skip to content

ENH: recurrence relation calculations #4567

Closed
@jreback

Description

@jreback

I think an implementation of a general recurrent relation might be interesting. These
are not able to evaluated in numpy/numexpr directly

see here:
http://stackoverflow.com/questions/9984373/calculate-two-coupled-equation-with-numpy
http://stackoverflow.com/questions/21336794/python-recursive-vectorization-with-timeseries/21338665
http://stackoverflow.com/questions/23996260/python-pandas-using-the-previous-value-in-dataframe/23997030#23997030

R[i<=k] = start(i) 
R[i] = R[i-k]*a(i) + b(i) 

in the case where a(i) and b(i) are constants then this can be completely cythonized
if a and/or b are passed functions then the shell can still be cythonized (with the function called at each iteration)

Example

values = df['input']

In [62]: result = np.empty(len(values))

In [65]: result[0] = values[0]

In [66]: for i in xrange(len(result)):
   ....:     if i > 0:
   ....:         result[i] = result[i-1]*.8
   ....:         

In [67]: result
Out[67]: array([ 5.    ,  4.    ,  3.2   ,  2.56  ,  2.048 ,  1.6384])

I propose that pandas have a recurrence wrapper (that can then call a pure constant cython version and a cython version that takes functions)
Probably just for a 1-dim array atm

In theory this can also be done by a rolling_apply that has memory (or I guess that is sort of the same thing)

related #4546
https://groups.google.com/forum/#!topic/pydata/0MCWhwurOWs

Metadata

Metadata

Assignees

No one assigned

    Labels

    API DesignAlgosNon-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diffEnhancementNumeric OperationsArithmetic, Comparison, and Logical operations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions