Skip to content

Commit

Permalink
add weighted quantile and median.
Browse files Browse the repository at this point in the history
  • Loading branch information
skuschel committed Feb 17, 2015
1 parent 9c55f05 commit cb59032
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions postpic/analyzer/particles.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,25 @@ def var(self, func, weights=1.0):
m = np.average(data, weights=w)
return np.average((data - m)**2, weights=w)

def quantile(self, func, q, weights=1.0):
'''
The qth-quantile of the distribution.
'''
if q < 0 or q > 1:
raise ValueError('Quantile q ({:}) must be in range [0, 1]'.format(q))
w = self.weight() * weights
data = func(self)
sortidx = np.argsort(data)
wcs = np.cumsum(w[sortidx])
idx = np.searchsorted(wcs, wcs[-1]*np.array(q))
return data[sortidx[idx]]

def median(self, func, weights=1.0):
'''
The median
'''
return self.quantile(func, 0.5, weights=weights)

# ---- Functions to create a Histogram. ---

def createHistgram1d(self, scalarfx, optargsh={'bins': 300},
Expand Down

0 comments on commit cb59032

Please sign in to comment.