Python中的正确评分规则
项目描述
Proper scoring rules for evaluating probabilistic forecasts in Python. Evaluation methods that are “strictly proper” cannot be artificially improved through hedging, which makes them fair methods for accessing the accuracy of probabilistic forecasts. In particular, these rules are often used for evaluating weather forecasts.
properscoring可在Python 2和3上运行。它需要NumPy(1.8或更高版本)和SciPy(任何较新版本都应没问题)。Numba是可选的,但强烈推荐:它为crps_ensemble和threshold_brier_score提供了显著的加速(例如,20倍更快)。
要安装,请使用pip: pip install properscoring。
示例:计算CRPS的五种方法
这个库关注的是密切相关联的连续排名概率得分 (CRPS) 和 Brier得分。我们喜欢这些得分,因为它们都是可解释的(例如,CRPS是平均绝对误差的推广)并且可以从概率分布的有限样本中轻松计算。
我们将演示如何使用高斯随机变量给出的预报来计算CRPS。首先,导入properscoring
import numpy as np import properscoring as ps from scipy.stats import norm
使用crps_gaussian进行精确计算(这是最快的方法)
>>>> ps.crps_gaussian(0, mu=0, sig=1) 0.23369497725510913
使用 crps_quadrature 进行数值积分
>>> ps.crps_quadrature(0, norm) array(0.23369497725510724)
从具有 crps_ensemble 的有限样本
>>> ensemble = np.random.RandomState(0).randn(1000) >>> ps.crps_ensemble(0, ensemble) 0.2297109370729622
使用 crps_ensemble 的 PDF 值进行加权
>>> x = np.linspace(-5, 5, num=1000) >>> ps.crps_ensemble(0, x, weights=norm.pdf(x)) 0.23370047937569616
基于 CRPS 的阈值分解,该分解使用 threshold_brier_score
>>> threshold_scores = ps.threshold_brier_score(0, ensemble, threshold=x) >>> (x[1] - x[0]) * threshold_scores.sum(axis=-1) 0.22973090090090081
在这个例子中,我们只对一个观测/预报对进行了评分。但为了可靠地评估预报模型,您需要对这些评分进行平均。幸运的是,properscoring 中的所有评分规则都愉快地接受并返回多维数组作为观测
>>> ps.crps_gaussian([-2, -1, 0, 1, 2], mu=0, sig=1) array([ 1.45279182, 0.60244136, 0.23369498, 0.60244136, 1.45279182])
一旦计算出平均评分,通常将它们相对于基线预报进行归一化,以计算所谓的“技能评分”,其中 0 表示没有超过基线,1 表示完美预报。例如,假设我们的基线预报是始终预测 0
>>> obs = [-2, -1, 0, 1, 2] >>> baseline_score = ps.crps_ensemble(obs, [0, 0, 0, 0, 0]).mean() >>> forecast_score = ps.crps_gaussian(obs, mu=0, sig=1).mean() >>> skill = (baseline_score - forecast_score) / baseline_score >>> skill 0.27597311068630859
标准正态分布在这五个观测上预测得更好,提高了 28%
API
properscoring 包含针对评分概率预报进行了优化和广泛测试的程序。这些函数目前分为两大类
连续排名概率得分 (CRPS)
对于集合预报: crps_ensemble
对于高斯分布: crps_gaussian
对于任意累积分布函数: crps_quadrature
布里尔分数
对于二元概率预报: brier_score
对于使用集合预报的阈值超出: threshold_brier_score
所有函数都健壮地处理用浮点值 NaN 表示的缺失值。
历史
这个库是由 The Climate Corporation 的研究人员编写的。原始作者包括 Leon Barrett、Stephan Hoyer、Alex Kleeman 和 Drew O’Kane。
许可证
版权 2015 The Climate Corporation
根据 Apache License,版本 2.0(“许可证”);除非您遵守许可证,否则不得使用此文件。您可以在以下位置获得许可证的副本:
https://apache.ac.cn/licenses/LICENSE-2.0
除非适用法律要求或书面同意,否则在许可证下分发的软件按“原样”分发,不提供任何明示或暗示的保证或条件。有关许可证的具体语言,请参阅许可证。
贡献
欢迎外部贡献(与正确评分规则相关的错误修复或新功能)!请打开 GitHub 问题以讨论您的计划。
项目详情
下载文件
下载您平台的文件。如果您不确定选择哪个,请了解有关 安装软件包 的更多信息。