larray.wrap_elementwise_array_func

larray.wrap_elementwise_array_func(func, doc=None)[source]

Wrap a function using numpy arrays to work with LArray arrays instead.

Parameters
funcfunction

A function taking numpy arrays as arguments and returning numpy arrays of the same shape. If the function takes several arguments, this wrapping code assumes the result will have the combination of all axes present. In numpy talk, arguments will be broadcasted to each other.

docstr, optional

The documentation (docstring) for the new function. Defaults to the documentation of the original function, if any.

Returns
function

A function taking larray.Array arguments and returning larray.Arrays.

Examples

For example, if we want to apply the Hodrick-Prescott filter from statsmodels we can use this:

>>> from statsmodels.tsa.filters.hp_filter import hpfilter         
>>> hpfilter = wrap_elementwise_array_func(hpfilter)               

hpfilter is now a function taking a one dimensional Array as input and returning a one dimensional Array as output

Now let us suppose we have a ND array such as:

>>> from larray.random import normal
>>> arr = normal(axes="sex=M,F;year=2016..2018")                   
>>> arr                                                            
sex\year   2016   2017   2018
       M  -1.15   0.56  -1.06
       F  -0.48  -0.39  -0.98

We can apply an Hodrick-Prescott filter to it by using:

>>> # 6.25 is the recommended smoothing value for annual data
>>> cycle, trend = arr.apply(hpfilter, 6.25, axes="year")          
>>> trend                                                          
sex\year   2016   2017   2018
       M  -0.61  -0.52  -0.52
       F  -0.37  -0.61  -0.87