Skip to content

Instantly share code, notes, and snippets.

@twolodzko
Created October 7, 2019 13:12
Show Gist options
  • Select an option

  • Save twolodzko/38f137544263e4caba0819b20c21a35f to your computer and use it in GitHub Desktop.

Select an option

Save twolodzko/38f137544263e4caba0819b20c21a35f to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
class ECDF:
def __init__(self, arr: np.ndarray, max_points: Optional[int] = None) -> None:
self.arr = np.sort(arr)
n = len(self.arr)
if max_points and n > max_points:
self.arr = self.arr[:: (n // max_points)]
def prob(self, x: np.ndarray) -> np.ndarray:
return (np.searchsorted(self.arr, x, side='right') + 1) / len(self.arr)
def plot(self, ax: Optional[matplotlib.axes.Axes] = None, **kwargs) -> None:
ax = ax or plt.gca()
p = np.arange(1, len(self.arr) + 1) / len(self.arr)
ax.plot(self.arr, p, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment