Skip to content

Instantly share code, notes, and snippets.

@mrwacky42
Created December 17, 2014 19:26
Show Gist options
  • Save mrwacky42/62798a4305a66ad6a7e8 to your computer and use it in GitHub Desktop.
Save mrwacky42/62798a4305a66ad6a7e8 to your computer and use it in GitHub Desktop.
class ECDF(object):
"""
One-dimensional empirical distribution function given a vector of
observations.
Parameters
----------
observations : array_like
An array of observations
Attributes
----------
observations : array_like
An array of observations
"""
def __init__(self, observations):
self.observations = np.asarray(observations)
def __call__(self, x):
"""
Evaluates the ecdf at x
Parameters
----------
x : scalar(float)
The x at which the ecdf is evaluated
Returns
-------
scalar(float)
Fraction of the sample less than x
"""
return np.mean(self.observations <= x)
def plot(self, a=None, b=None, *args, **kwargs):
"""
Plot the ecdf on the interval [a, b].
Parameters
----------
a : scalar(float), optional(default=None)
Lower end point of the plot interval
b : scalar(float), optional(default=None)
Upper end point of the plot interval
"""
# === choose reasonable interval if [a, b] not specified === #
if a is None:
a = self.observations.min() - self.observations.std()
if b is None:
b = self.observations.max() + self.observations.std()
# === generate plot === #
x_vals = np.linspace(a, b, num=100)
f = np.vectorize(self.__call__)
return (x_vals, f(x_vals))
#plt.plot(x_vals, f(x_vals), *args, **kwargs)
#plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment