Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to calculate the weighted percentile? #59

Open
hsluoyz opened this issue Feb 4, 2021 · 2 comments
Open

How to calculate the weighted percentile? #59

hsluoyz opened this issue Feb 4, 2021 · 2 comments

Comments

@hsluoyz
Copy link

hsluoyz commented Feb 4, 2021

See: https://en.wikipedia.org/wiki/Percentile#The_weighted_percentile_method

My current code is as follows. But I don't know how to support weighted percentile?

func TestPercentile(t *testing.T) {
	values := []float64{4, 5, 3, 1, 2}
	percentiles := []float64{}
	for i := 1; i <= 100; i++ {
		percentile, err := stats.PercentileNearestRank(values, float64(i))
		if err != nil {
			panic(err)
		}
		percentiles = append(percentiles, percentile)
		fmt.Printf("%d%%: %f, ", i, percentile)
	}
	println()
	println()

	for f := 0.0; f <= 5; f += 0.1 {
		index := sort.SearchFloat64s(percentiles, f + 0.00000001)
		fmt.Printf("%f: %d%%, ", f, index)
	}
	println()
	println()
}
@montanaflynn
Copy link
Owner

There is no built in weighted percentile function in this library. The actual algorithm to use depends on what you're looking for or why you need it.

Do you have an example array and associated weights or can you describe the problem you're trying to solve?

@hsluoyz
Copy link
Author

hsluoyz commented Feb 4, 2021

@montanaflynn thanks for answering. See something similar in Python: https://stackoverflow.com/questions/21844024/weighted-percentile-using-numpy

from statsmodels.stats.weightstats import DescrStatsW
wq = DescrStatsW(data=np.array([1, 2, 9, 3.2, 4]), weights=np.array([0.0, 0.5, 1.0, 0.3, 0.5]))
wq.quantile(probs=np.array([0.1, 0.9]), return_pandas=False)
# array([2., 9.])

So I want something like:

percentile, err := stats.PercentileNearestRankWithWeight(values, valueWeights, float64(i))

valueWeights is the weight slice for values. For the original PercentileNearestRank() function, we can view its valueWeights is an all-one slice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants