计算百分位数 (Ruby)
def fraction?(number)
number - number.truncate
end
def percentile(param_array, percentage)
another_array = param_array.to_a.sort
r = percentage.to_f * (param_array.size.to_f - 1) + 1
if r <= 1 then return another_array[0]
elsif r >= another_array.size then return another_array[another_array.size - 1]
end
ir = r.truncate
another_array[ir] + fraction?((another_array[ir].to_f - another_array[ir - 1].to_f).abs)
end
用法示例:
test_array = [95.1772, 95.1567, 95.1937, 95.1959, 95.1442, 95.061, 95.1591, 95.1195,
95.1065, 95.0925, 95.199, 95.1682]
test_values = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
test_values.each do |value|
puts value.to_s + ": " + percentile(test_array, value).to_s
end
输出:
0.0: 95.061
0.1: 95.1205
0.2: 95.1325
0.3: 95.1689
0.4: 95.1692
0.5: 95.1615
0.6: 95.1773
0.7: 95.1862
0.8: 95.2102
0.9: 95.1981
1.0: 95.199
这里的问题是第 80 个百分位数高于第 90 个和第 100 个百分位数。但是,据我所知,我的实现如所描述的那样,并且它返回给定示例的正确答案(0.9)。
我的代码中是否存在我没有看到的错误?或者有更好的方法来做到这一点吗?
My code is based on the methods described here and here.
def fraction?(number)
number - number.truncate
end
def percentile(param_array, percentage)
another_array = param_array.to_a.sort
r = percentage.to_f * (param_array.size.to_f - 1) + 1
if r <= 1 then return another_array[0]
elsif r >= another_array.size then return another_array[another_array.size - 1]
end
ir = r.truncate
another_array[ir] + fraction?((another_array[ir].to_f - another_array[ir - 1].to_f).abs)
end
Example usage:
test_array = [95.1772, 95.1567, 95.1937, 95.1959, 95.1442, 95.061, 95.1591, 95.1195,
95.1065, 95.0925, 95.199, 95.1682]
test_values = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
test_values.each do |value|
puts value.to_s + ": " + percentile(test_array, value).to_s
end
Output:
0.0: 95.061
0.1: 95.1205
0.2: 95.1325
0.3: 95.1689
0.4: 95.1692
0.5: 95.1615
0.6: 95.1773
0.7: 95.1862
0.8: 95.2102
0.9: 95.1981
1.0: 95.199
The problem here is that the 80th percentile is higher than the 90th and the 100th. However, as far as I can tell my implementation is as described, and it returns the right answer for the example given (0.9).
Is there an error in my code I'm not seeing? Or is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
脚本
这听起来像是一个家庭作业问题。无论如何,这很有趣。
输出
script
This sounds like a homework problem. Anyway, it was kinda fun to do.
output
成功了。将
-Infinity
添加到数组中,以便我可以使用1 - N
范围内的索引。我还将最后一行中的值乘以错误的变量。可以将
r = ...
行替换为r = Percentage.to_f * (another_array_size + 1)
,以使用第一个链接中的公式而不是 Excel 的公式。输出:
Got it working. Added
-Infinity
to the array so that I could use the indexes in the range1 - N
. I was also multiplying the value in the last line for the wrong variable.The
r = ...
line can be replaced forr = percentage.to_f * (another_array_size + 1)
to use the formula in the first link instead of Excel's.Output:
您还可以 Monkeypatch Enumerable:
之后您将能够执行以下操作:
You could also monkeypatch Enumerable:
After this you would be able to do something like this: