威尔逊置信区间
由于正态区间对于小样本并不可靠,因而,1927年,美国数学家 Edwin Bidwell Wilson提出了一个修正公式,被称为“威尔逊区间”,很好地解决了小样本的准确性问题。
根据离散型随机变量的均值和方差定义:
μ=E(X)=0*(1-p)+1*p=p
σ=D(X)=(0-E(X))2(1-p)+(1-E(X))2p=p2(1-p)+(1-p)2p=p2-p3+p3-2p2+p=p-p2=p(1-p)
因此上面的威尔逊区间公式可以简写成:
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | def wilson_score(pos, total, p_z = 2. ): """ 威尔逊得分计算函数 参考:https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval :param pos: 正例数 :param total: 总数 :param p_z: 正太分布的分位数 :return: 威尔逊得分 """ pos_rat = pos * 1. / total * 1. # 正例比率 score = (pos_rat + (np.square(p_z) / ( 2. * total)) - ((p_z / ( 2. * total)) * np.sqrt( 4. * total * ( 1. - pos_rat) * pos_rat + np.square(p_z)))) / \ ( 1. + np.square(p_z) / total) return score |
SQL实现代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #wilson_score SELECT widget_id, ((positive + 1.9208) / (positive + negative) - 1.96 * SQRT((positive * negative) / (positive + negative) + 0.9604) / (positive + negative)) / (1 + 3.8416 / (positive + negative)) AS ci_lower_bound FROM widgets WHERE positive + negative > 0 ORDER BY ci_lower_bound DESC ; # SELECT widget_id, (positive - negative) AS net_positive_ratings FROM widgets ORDER BY net_positive_ratings DESC ; # SELECT widget_id, positive / (positive + negative) AS average_rating FROM widgets ORDER BY average_rating DESC ; |
excel实现代码:
1 2 3 | =IFERROR((([@[Up Votes]] + 1.9208) / ([@[Up Votes]] + [@[Down Votes]]) - 1.96 * SQRT(([@[Up Votes]] * [@[Down Votes]]) / ([@[Up Votes]] + [@[Down Votes]]) + 0.9604) / ([@[Up Votes]] + [@[Down Votes]])) / (1 + 3.8416 / ([@[Up Votes]] + [@[Down Votes]])),0) |
星级评价排名
Reddit的话题排序算法叫做(thehot ranking),实现代码如下:
log(10, max{abs(up-down), 1}) + sign(up>down) * seconds / 45000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #Rewritten code from /r2/r2/lib/db/_sorts.pyx from datetime import datetime, timedelta from math import log epoch = datetime( 1970 , 1 , 1 ) def epoch_seconds(date): """Returns the number of seconds from the epoch to date.""" td = date - epoch return td.days * 86400 + td.seconds + ( float (td.microseconds) / 1000000 ) def score(ups, downs): return ups - downs def hot(ups, downs, date): """The hot formula. Should match the equivalent function in postgres.""" s = score(ups, downs) order = log( max ( abs (s), 1 ), 10 ) sign = 1 if s > 0 else - 1 if s < 0 else 0 seconds = epoch_seconds(date) - 1134028003 return round (order + sign * seconds / 45000 , 7 ) |
imdb top 250用的是贝叶斯统计的算法得出的加权分(Weighted Rank-WR),公式如下:
weighted rank (WR) = (v ÷ (v+m)) × R + (m ÷ (v+m)) × C
WR=( v / (v+m)) (R-C) +C
- WR, 加权得分(weighted rating)。
- R,该电影的用户投票的平均得分(Rating)。
- v,该电影的投票人数(votes)。
- m,排名前250名的电影的最低投票数(现在为3000)。
- C, 所有电影的平均得分(现在为6.9)。
参考资料:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术