📚 The CoCalc Library - books, templates and other resources
License: OTHER
from __future__ import division1import numpy as np23__author__ = "Eric Chiang"4__email__ = "eric[at]yhathq.com"56"""78Measurements inspired by Philip Tetlock's "Expert Political Judgment"910Equations take from Yaniv, Yates, & Smith (1991):11"Measures of Descrimination Skill in Probabilistic Judgement"1213"""141516def calibration(prob,outcome,n_bins=10):17"""Calibration measurement for a set of predictions.1819When predicting events at a given probability, how far is frequency20of positive outcomes from that probability?21NOTE: Lower scores are better2223prob: array_like, float24Probability estimates for a set of events2526outcome: array_like, bool27If event predicted occurred2829n_bins: int30Number of judgement categories to prefrom calculation over.31Prediction are binned based on probability, since "descrete"32probabilities aren't required.3334"""35prob = np.array(prob)36outcome = np.array(outcome)3738c = 0.039# Construct bins40judgement_bins = np.arange(n_bins + 1) / n_bins41# Which bin is each prediction in?42bin_num = np.digitize(prob,judgement_bins)43for j_bin in np.unique(bin_num):44# Is event in bin45in_bin = bin_num == j_bin46# Predicted probability taken as average of preds in bin47predicted_prob = np.mean(prob[in_bin])48# How often did events in this bin actually happen?49true_bin_prob = np.mean(outcome[in_bin])50# Squared distance between predicted and true times num of obs51c += np.sum(in_bin) * ((predicted_prob - true_bin_prob) ** 2)52return c / len(prob)5354def discrimination(prob,outcome,n_bins=10):55"""Discrimination measurement for a set of predictions.5657For each judgement category, how far from the base probability58is the true frequency of that bin?59NOTE: High scores are better6061prob: array_like, float62Probability estimates for a set of events6364outcome: array_like, bool65If event predicted occurred6667n_bins: int68Number of judgement categories to prefrom calculation over.69Prediction are binned based on probability, since "descrete"70probabilities aren't required.7172"""73prob = np.array(prob)74outcome = np.array(outcome)7576d = 0.077# Base frequency of outcomes78base_prob = np.mean(outcome)79# Construct bins80judgement_bins = np.arange(n_bins + 1) / n_bins81# Which bin is each prediction in?82bin_num = np.digitize(prob,judgement_bins)83for j_bin in np.unique(bin_num):84in_bin = bin_num == j_bin85true_bin_prob = np.mean(outcome[in_bin])86# Squared distance between true and base times num of obs87d += np.sum(in_bin) * ((true_bin_prob - base_prob) ** 2)88return d / len(prob)899091