larray.wrap_elementwise_array_func¶
-
larray.
wrap_elementwise_array_func
(func)[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.
- Returns
- function
A function taking LArray arguments and returning LArrays.
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 # doctest: +SKIP >>> hpfilter = wrap_elementwise_array_func(hpfilter) # doctest: +SKIP
hpfilter is now a function taking a one dimensional LArray as input and returning a one dimensional LArray 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") # doctest: +SKIP >>> arr # doctest: +SKIP 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") # doctest: +SKIP >>> trend # doctest: +SKIP sex\year 2016 2017 2018 M -0.61 -0.52 -0.52 F -0.37 -0.61 -0.87