Created
August 28, 2012 16:24
-
-
Save larsbutler/3499931 to your computer and use it in GitHub Desktop.
weighted_quantile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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