Skip to content

Instantly share code, notes, and snippets.

@tvladeck
Created July 18, 2020 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tvladeck/ad40df59744b7b54989289478cc2b769 to your computer and use it in GitHub Desktop.
Save tvladeck/ad40df59744b7b54989289478cc2b769 to your computer and use it in GitHub Desktop.
economist forecast consistency check
p_over_time =
read_csv("https://cdn.economistdatateam.com/us-2020-forecast/data/president/electoral_college_probability_over_time.csv") %>%
# convert win probabilites to logit scale
mutate(win_prob_logit = boot::logit(win_prob)) %>%
# look only at one side
filter(party == "democratic")
# take the variance of the differences
var_p_over_time = var(diff(p_over_time$win_prob_logit))
days_until_election = as.integer(ymd(20201103) -
ymd(last(p_over_time$date)))
# what does the forecast say today?
current_logit_prob = last(p_over_time$win_prob_logit)
# the standard deviation the changes between now and the election
assumed_sd = sqrt(days_until_election * var_p_over_time)
# 95% confidence on the logit scale
confint_95_logit = current_logit_prob + assumed_sd * 1.96 * c(-1, 1)
# 95% confidence on the probability scale
confint_95_prob = boot::inv.logit(confint_95_logit)
confint_95_prob
# [1] 0.7331208 0.9832501
consistency_check = function() {
v = 0:100 / 100
l = boot::logit(v)
d = pnorm(l, mean = current_logit_prob, sd = assumed_sd)
dd = d[2:101] - d[1:100]
sum(v[c(-1)] * dd)
}
consistency_check()
# [1] 0.9142117
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment