How to np.vectorize __call__ method

Y

Yaþar Arabacý

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top