How to np.vectorize __call__ method

Yaşar Arabacı yasar11732 at gmail.com
Thu Nov 14 18:58:47 EST 2013


I am cross-posting from: http://stackoverflow.com/q/19990863/886669

I am following, [quant-econ](http://quant-econ.net/numpy.html)
tutorial. I am trying the exercise where I am supposed to implement a
[Empirical Cumulative Probability
Funcion](http://en.wikipedia.org/wiki/Empirical_distribution_function)
using vectorized numpy methods.

Here is the **correct** solution to problem:

    class ecdf:

        def __init__(self, observations):
            self.observations = np.asarray(observations)

        def __call__(self, x):
            return np.mean(self.observations <= x)

        def plot(self, a=None, b=None):

            # === choose reasonable interval if [a, b] not specified === #
            if not a:
                a = self.observations.min() - self.observations.std()
            if not b:
                b = self.observations.max() + self.observations.std()

            # === generate plot === #
            x_vals = np.linspace(a, b, num=100)
            f = np.vectorize(self.__call__)
            plt.plot(x_vals, f(x_vals))
            plt.show()

But I am **trying** to do it this way:

    class ecdf(object):

        def __init__(self, observations):
            self.observations = np.asarray(observations)
            self.__call__ = np.vectorize(self.__call__)

        def __call__(self, x):
            return np.mean(self.observations <= x)

So that, `__call__` method is vectorized and instance can be called
with an array and it returns an array of cumulative probabilities for
that array. However, when I try it like this:

    p = ecdf(uniform(0,1,500))
    p([0.2, 0.3])

I am getting this error:

    Traceback (most recent call last):

      File "<ipython-input-34-6a77f18aa54e>", line 1, in <module>
        p([0.2, 0.3])

      File "D:/Users/y_arabaci-ug/Desktop/quant-econ/programs/numpy_exercises.py",
line 50, in __call__
        return np.mean(self.observations <= x)

    ValueError: operands could not be broadcast together with shapes (500) (2)

My question is, how come author could vectorize `self.__call__` and it
works, while my method gives an error?


-- 
http://ysar.net/



More information about the Python-list mailing list