Skip to content

Commit

Permalink
May edition
Browse files Browse the repository at this point in the history
  • Loading branch information
technocrat committed Jun 19, 2024
1 parent f3f813c commit 9ae142a
Show file tree
Hide file tree
Showing 82 changed files with 732 additions and 201 deletions.
Binary file modified _assets/img/models/AZ_2020.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _assets/img/models/AZ_apr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _assets/img/models/AZ_mar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _assets/img/models/AZ_may.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _assets/img/models/GA_2020.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _assets/img/models/GA_apr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _assets/img/models/GA_mar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _assets/img/models/MI_2020.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _assets/img/models/MI_apr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _assets/img/models/MI_mar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _assets/img/models/MI_may.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _assets/img/models/NC_2020.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _assets/img/models/NC_apr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _assets/img/models/NC_mar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _assets/img/models/NC_may.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _assets/img/models/NV_2020.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _assets/img/models/NV_mar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _assets/img/models/NV_may.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _assets/img/models/PA_2020.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _assets/img/models/PA_apr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _assets/img/models/PA_mar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _assets/img/models/PA_may.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _assets/img/models/WI_2020.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _assets/img/models/WI_apr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _assets/img/models/WI_mar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _assets/img/models/WI_may.png
Binary file removed _assets/meta_cohort_2020.bson
Binary file not shown.
Binary file removed _assets/meta_cohort_2022.bson
Binary file not shown.
Binary file modified _assets/objs/AZ_2020_p_sample.bson
Binary file not shown.
Binary file modified _assets/objs/AZ_apr_p_sample.bson
Binary file not shown.
Binary file modified _assets/objs/AZ_mar_p_sample.bson
Binary file not shown.
Binary file added _assets/objs/AZ_may_p_sample.bson
Binary file not shown.
Binary file modified _assets/objs/GA_2020_p_sample.bson
Binary file not shown.
Binary file modified _assets/objs/GA_apr_p_sample.bson
Binary file not shown.
Binary file modified _assets/objs/GA_mar_p_sample.bson
Binary file not shown.
Binary file modified _assets/objs/MI_2020_p_sample.bson
Binary file not shown.
Binary file modified _assets/objs/MI_apr_p_sample.bson
Binary file not shown.
Binary file modified _assets/objs/MI_mar_p_sample.bson
Binary file not shown.
Binary file added _assets/objs/MI_may_p_sample.bson
Binary file not shown.
Binary file modified _assets/objs/NC_2020_p_sample.bson
Binary file not shown.
Binary file modified _assets/objs/NC_apr_p_sample.bson
Binary file not shown.
Binary file modified _assets/objs/NC_mar_p_sample.bson
Binary file not shown.
Binary file added _assets/objs/NC_may_p_sample.bson
Binary file not shown.
Binary file modified _assets/objs/NV_2020_p_sample.bson
Binary file not shown.
Binary file modified _assets/objs/NV_mar_p_sample.bson
Binary file not shown.
Binary file added _assets/objs/NV_may_p_sample.bson
Binary file not shown.
Binary file modified _assets/objs/PA_2020_p_sample.bson
Binary file not shown.
Binary file modified _assets/objs/PA_apr_p_sample.bson
Binary file not shown.
Binary file modified _assets/objs/PA_mar_p_sample.bson
Binary file not shown.
Binary file added _assets/objs/PA_may_p_sample.bson
Binary file not shown.
Binary file modified _assets/objs/WI_2020_p_sample.bson
Binary file not shown.
Binary file modified _assets/objs/WI_apr_p_sample.bson
Binary file not shown.
Binary file modified _assets/objs/WI_mar_p_sample.bson
Binary file not shown.
Binary file added _assets/objs/WI_may_p_sample.bson
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
st,biden_pop,tot
st,num_wins,num_votes
AZ,1672143,3333829
GA,2473633,4935487
MI,2804040,5453892
Expand Down
Binary file added _assets/objs/may_polls.bson
Binary file not shown.
2 changes: 1 addition & 1 deletion _assets/scripts/election_priors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ election_priors = election_priors[:,[1,2,3]]
election_priors.tot = election_priors.biden_pop .+ election_priors.trump_pop
election_priors = election_priors[:,[:st,:biden_pop,:tot]]
rename!(election_priors,["st","num_wins","num_votes"])
# CSV.write("../objs/election_priors.jl",election_priors)
# CSV.write("../objs/election_priors",election_priors)
56 changes: 28 additions & 28 deletions _assets/scripts/first_posterior.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,43 @@ include("first_posterior_forepart.jl")
# Set the number of votes and wins
last_election = CSV.read("../objs/election_priors.csv", DataFrame)


ST = last_election[1, :st]
num_wins = last_election[1, :num_wins]
num_votes = last_election[1, :num_votes]
#------------------------------------------------------------------
# row indexed to state in alpha order; repeat for each of 1:7
#------------------------------------------------------------------
ST = last_election[4, :st]
num_wins = last_election[4, :num_wins]
num_votes = last_election[4, :num_votes]
margins = CSV.read("../objs/margins.csv", DataFrame)
margin = first(margins[margins.st .== ST, :pct])

chain = sample(election_model(num_votes, num_wins), sampler, num_samples, init_params=init_params)

p_intv = quantile(chain[:p], [0.025, 0.975])
p_mean = summarystats(chain)[1, :mean]
p_mcse = summarystats(chain)[1, :mcse]
p_rhat = summarystats(chain)[1, :rhat]
p_df = DataFrame(median = median(chain[:p]),
mean = mean(chain[:p]),
mode = mode(chain[:p]),
q025 = p_intv[1],
q975 = p_intv[2],
mcse = summarystats(chain)[1, :mcse],
rhat = summarystats(chain)[1, :rhat])

# Extract the :p parameter from the chain object
p_samples = chain[:p]

# Flatten the p_samples array into a 1D vector
p_vec = vec(p_samples)

# Compute the density estimate
chain = sample(election_model(num_votes, num_wins), sampler,
num_samples, init_params=init_params)

p_intv = quantile(chain[:p], [0.025, 0.975])
p_mean = summarystats(chain)[1, :mean]
p_mcse = summarystats(chain)[1, :mcse]
p_rhat = summarystats(chain)[1, :rhat]
p_df = DataFrame(median = median(chain[:p]),
mean = mean(chain[:p]),
mode = mode(chain[:p]),
q025 = p_intv[1],
q975 = p_intv[2],
mcse = summarystats(chain)[1, :mcse],
rhat = summarystats(chain)[1, :rhat])

p_samples = chain[:p]
p_vec = vec(p_samples)
kde_result = kde(p_vec)

include("first_posterior_aftpart.jl")
# Display posterior density plot

posterior_interval = p_intv
fig = draw_density()
deep = deepcopy(chain)
fig = draw_density()
deep = deepcopy(chain)

@save ("../objs/$ST" * "_2020_p_sample.bson") deep
save(("../img/models/$ST" * "_2020.png"), fig)

out = Vector(p_df[1,:])
out = round.(out,digits = 4)
p_df[1,:] = out
Expand Down
2 changes: 1 addition & 1 deletion _assets/scripts/first_posterior_aftpart.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function draw_density()
fig = Figure(size = (600, 400))

# Add an axis to the figure
ax = Axis(fig[1, 1], xlabel = "Likelihood of Biden win", ylabel = "Number of draws", title = "Model: Biden wins in $ST from 2020 election before polling")
ax = Axis(fig[1, 1], xlabel = "Likelihood of Biden win", ylabel = "Number of draws", title = "Model: Biden results in $ST from 2020 election before polling")

# Plot the full density curve
lines!(ax, kde_result.x, kde_result.density, color = "#a3b35c", linewidth = 3, strokewidth = 4, strokecolor = GREENBAR, label = "Draws")
Expand Down
10 changes: 3 additions & 7 deletions _assets/scripts/first_posterior_forepart.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,16 @@ const LORANGE = "rgb(225, 170, 110)"
# Define the model
@model function election_model(num_votes::Int64, num_wins::Int64)
# Prior: Beta(2, 2) equivalent to a close race going in
p ~ Beta(2, 2)

# Likelihood: Binomial(num_votes, p)
p ~ Beta(2,2)
# Likelihood
num_wins ~ Binomial(num_votes, p)
end

# Set up the sampler
sampler = NUTS(0.65)

# Specify the number of samples and chains
num_samples = 10000
num_chains = 4

# Sample from the posterior

init_params = [Dict(:p => 0.5)]

Month_names = Dict(
Expand Down
50 changes: 28 additions & 22 deletions _assets/scripts/insert_may.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using Statistics
using StatsPlots
using Turing

const DESIGN_ERROR = 0.012
# const DESIGN_ERROR = 0.012

@enum Month mar apr may jun jul aug sep oct nov

Expand All @@ -28,14 +28,18 @@ const DESIGN_ERROR = 0.012
fo3
hi2
hi3
hp2
ma2
ma3
mi2
mi3
mr2
mr3
ny2
qi2
qi3
si2
si3
sp2
sp3
su2
Expand All @@ -58,28 +62,30 @@ end

@load "../objs/apr_polls.bson" months

"""
months[may][AZ][bl2] = [Poll(43,49, 801)]
months[may][AZ][ec2] = [Poll(44,48,1000)]
months[may][GA][bl2] = [Poll(45,47, 802)]
months[may][GA][ec2] = [Poll(44,47,1000)]
months[may][GA][fo2] = [Poll(45,51,1128)]
months[may][MI][bi2] = [Poll(43,51, 708)]
months[may][MI][ec2] = [Poll(44,45,1000)]
months[may][MI][fo2] = [Poll(46,49,1106)]
months[may][MI][mr2] = [Poll(46,42, 600)]
months[may][NC][ec2] = [Poll(42,47,1000)]
months[may][NC][bl2] = [Poll(41,51, 703)]
months[may][NC][ma2] = [Poll(43,49, 645)]
months[may][NC][qi2] = [Poll(46,48,1401)]
months[may][NV][ec2] = [Poll(44,45,1000)]
months[may][PA][bi2] = [Poll(46,47, 803)]
months[may][PA][ec2] = [Poll(45,47,1000)]
months[may][WI][bl2] = [Poll(44,48, 703)]
months[may][WI][ec2] = [Poll(45,47,1000)]
"""
months[may][AZ][bl2] = [Poll(44,49, 795)]
months[may][GA][bl2] = [Poll(44,47, 795)]
months[may][MI][bl3] = [Poll(40,44, 704)]
months[may][NC][bl2] = [Poll(42,49, 704)]
months[may][WI][bl2] = [Poll(46,47, 693)]
months[may][NV][bl2] = [Poll(47,47, 459)]
months[may][PA][bl2] = [Poll(46,48, 812)]
months[may][AZ][ec2] = [Poll(42,49, 626)]
months[may][GA][ec2] = [Poll(39,49, 604)]
months[may][MI][ec2] = [Poll(36,38, 616)]
months[may][NV][ec2] = [Poll(38,50, 614)]
months[may][PA][ec2] = [Poll(44,47,1023)]
months[may][WI][ec2] = [Poll(47,45, 614)]
months[may][NC][hp2] = [Poll(43,48,1002)]
months[may][WI][qi2] = [Poll(50,44,1497)]
months[may][AZ][si2] = [Poll(42,49, 626)]
months[may][GA][si2] = [Poll(39,49, 604)]
months[may][MI][si3] = [Poll(36,38, 616)]
months[may][NV][si2] = [Poll(38,50, 614)]
months[may][PA][si2] = [Poll(44,47,1023)]
months[may][WI][si2] = [Poll(47,45, 614)]

# @save "../objs/may_polls.bson" months

# needs to be done manually rather than with include()
#@save "../objs/may_polls.bson" months


20 changes: 20 additions & 0 deletions _assets/scripts/mar_poll.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@enum State PA GA NC MI AZ WI NV
@enum Month mar apr may jun jul aug sep oct


STATE = State
prior_month = "mar"
mon = mar
MON = "mar"
Mon = "mar"
st = "NV"
ST = NV

include("mar_poll_head.jl")
prior_poll = BSON.load("../objs/"*"$st"*"_2020_p_sample.bson")
@load "../objs/"*"$MON"*"_Polls.bson" months

current_month = remove_empties(months[mon])

include("polls_foot.jl")

72 changes: 72 additions & 0 deletions _assets/scripts/mar_poll_foot.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

@model function poll_model(num_votes::Int64, num_wins::Int64, prior_dist::Distribution)
# Define the prior using the informed prior distribution
p ~ prior_dist
# Define the likelihood with additional uncertainty
num_wins ~ Binomial(num_votes, p)
end

processed_polls = Dict(state =>
Dict(pollster =>
process_polls(polls) for (pollster, polls) in pollsters)
for (state, pollsters) in current_month)

processed_polls_totals = Dict(state =>
Dict("num_wins" =>
sum(first(values(polls)) for polls in values(pollsters)),
"num_votes" =>
sum(last(values(polls)) for polls in values(pollsters)))
for (state, pollsters) in processed_polls)

num_wins = processed_polls_totals[ST]["num_wins"]
num_votes = processed_polls_totals[ST]["num_votes"]

poll_posterior = prior_poll

posterior_mean = mean(poll_posterior[:deep][:p])
posterior_var = var(poll_posterior[:deep][:p])
prior_alpha = posterior_mean *
(posterior_mean * (1 - posterior_mean) / posterior_var - 1)
prior_beta = (1 - posterior_mean) * (posterior_mean *
(1 - posterior_mean) / posterior_var - 1)
prior_dist = Beta(prior_alpha, prior_beta)

model = poll_model(num_votes, num_wins, prior_dist)
sampler = NUTS(0.65)
num_samples = 10000
num_chains = 4
init_params = [Dict(:p => 0.5)]
chain = sample(poll_model(num_votes, num_wins, prior_dist),
sampler, num_samples, init_params=init_params)

p_intv = quantile(chain[:p], [0.025, 0.975])
p_mean = summarystats(chain)[1,:mean]
p_mcse = summarystats(chain)[1,:mcse]
p_rhat = summarystats(chain)[1,:rhat]
p_df = DataFrame(median = median(chain[:p]),
mean = mean(chain[:p]),
mode = mode(chain[:p]),
q025 = p_intv[1],
q975 = p_intv[2],
mcse = summarystats(chain)[1,:mcse],
rhat = summarystats(chain)[1,:rhat])

p_samples = chain[:p]
p_vec = vec(p_samples)
kde_result = kde(p_vec)

posterior_interval = p_intv
fig = draw_density()
save(("../img/models/"*"$st"*"_"*"$mon"*".png"), fig)

deep = deepcopy(chain)
@save "../objs/"*"$st"*"_"*"$mon"*"_p_sample.bson" deep

out = Vector(p_df[1,:])
out = round.(out,digits = 4)
p_df[1,:] = out
pretty_table(p_df,backend=Val(:html),show_subheader = false)
chain
summarystats(chain)
autocor(chain)
hpd(chain)
Loading

0 comments on commit 9ae142a

Please sign in to comment.