Skip to content

Instantly share code, notes, and snippets.

@egafni
Created April 3, 2019 22:32
Show Gist options
  • Select an option

  • Save egafni/2c8d7780aeccb4fbd014871096975287 to your computer and use it in GitHub Desktop.

Select an option

Save egafni/2c8d7780aeccb4fbd014871096975287 to your computer and use it in GitHub Desktop.

Revisions

  1. egafni created this gist Apr 3, 2019.
    18 changes: 18 additions & 0 deletions poisson_outliers.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    def poisson_outliers(arr, min_pval=.01, min_size_to_filter=5):
    """
    >>> poisson_outliers([1,1,1,1,1,2,3])
    array([False, False, False, False, False, False, False], dtype=bool)
    >>> poisson_outliers([1,1,1,1,1,2,10])
    array([False, False, False, False, False, False, True], dtype=bool)
    >>> poisson_outliers([1,1,10])
    array([False, False, False], dtype=bool)
    """
    arr = np.asarray(arr)
    mu = np.median(arr)
    pvals = poisson(mu).sf(arr)
    # outliers have pvalues that are too unlikely given our sample size
    if len(arr) >= min_size_to_filter:
    is_outlier = pvals < min(1.0 / len(arr), min_pval)
    else:
    is_outlier = np.zeros_like(pvals).astype(bool)
    return is_outlier