Skip to content

Instantly share code, notes, and snippets.

@davidwhogg
Created June 7, 2016 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidwhogg/842ab7e5ace1c239a31641676bc5230e to your computer and use it in GitHub Desktop.
Save davidwhogg/842ab7e5ace1c239a31641676bc5230e to your computer and use it in GitHub Desktop.
import numpy as np
from scipy.interpolate import interp1d
def weighted_median(xs, ws=None):
if ws is None:
ws = np.ones_like(xs)
totalw = np.sum(ws)
sindx = np.argsort(xs) # expensive
cs = np.cumsum(ws[sindx]) / totalw
symcumsum = 0.5 * (cs + np.append(0., cs[0:-1]))
print(symcumsum, xs[sindx])
return (interp1d(symcumsum, xs[sindx]))(0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment