Skip to content

Instantly share code, notes, and snippets.

@larsbutler
Created August 28, 2012 16:24
Show Gist options
  • Save larsbutler/3499931 to your computer and use it in GitHub Desktop.
Save larsbutler/3499931 to your computer and use it in GitHub Desktop.
weighted_quantile
def weighted_quantile(weights, curves, quantile):
"""
:param weights:
Array of weights, 1 for each input curve.
:param curves:
2D array of curve PoEs. Each row represents the PoEs for a single curve
:param quantile:
Quantile value to calculate. Should in the range [0.0, 1.0].
"""
# Each curve needs to be associated with a weight:
assert len(weights) == len(curves)
result_curve = []
for i, poes in enumerate(curves.transpose()):
sorted_poe_idxs = numpy.argsort(poes)
sorted_weights = weights[sorted_poe_idxs]
sorted_poes = poes[sorted_poe_idxs]
# cumulatative sum of weights:
cum_weights = numpy.cumsum(sorted_weights)
result_curve.append(numpy.interp(quantile, cum_weights, sorted_poes))
return result_curve
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment