[SciPy-User] Can I create a 3 argument UFunc easily?

Wes McKinney wesmckinn at gmail.com
Thu Jun 10 13:26:49 EDT 2010


On Tue, Jun 8, 2010 at 7:27 PM, Robert Kern <robert.kern at gmail.com> wrote:
> On Tue, Jun 8, 2010 at 18:46, John Salvatier <jsalvati at u.washington.edu> wrote:
>> Hello,
>>
>> I would like to make a 3 argument UFunc that finds the weighted average of
>> two of the arguments using the 3rd argument as the weight. This way, the
>> .accumulate method of the ufunc can be used as an exponentially weighted
>> moving average function.
>>
>> Unfortunately I am not very familiar with the Numpy C API, so I was hoping
>> to use the Cython hack for making UFuncs
>> (http://wiki.cython.org/MarkLodato/CreatingUfuncs). However, looking at the
>> UFunc C API doc
>> (http://docs.scipy.org/doc/numpy/reference/c-api.ufunc.html), it looks like
>> numpy only has 2 argument "generic functions". Is there a simple way to
>> create a "generic function" that takes 3 arguments that will still work with
>> accumulate? Is there another way to create the sort of UFunc I want?
>
> While you can make n-argument ufuncs (scipy.special has many of them),
> .accumulate() only works for 2-argument ufuncs.
>
> All in all, it's a lot easier and more performant to simply code up an
> EWMA in C rather than "tricking" the general ufunc machinery into
> achieving a specific effect.
>
> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless
> enigma that is made terrible by our own mad attempt to interpret it as
> though it had an underlying truth."
>  -- Umberto Eco
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>

I can point out a couple EWMA implementations present in
scikits.timeseries (C) and pandas (Cython), that you could co-opt if
they do what you want. I'm sure there are others out there

http://svn.scipy.org/svn/scikits/trunk/timeseries/scikits/timeseries/src/c_tseries.c
http://code.google.com/p/pandas/source/browse/trunk/pandas/lib/src/moments.pyx#161



More information about the SciPy-User mailing list