From 1c1c32a83de96680714a3dfa010837bf136e05d9 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sun, 14 Apr 2024 15:07:21 -0400 Subject: [PATCH] finish state printing function --- main.go | 54 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index 9916ee2..35961e8 100644 --- a/main.go +++ b/main.go @@ -251,14 +251,45 @@ func printStates(states []State) { fmt.Printf("\n\nState: %s", state.Name) fmt.Printf("\n\tVotes: %d", state.Votes) fmt.Printf("\n\tHistory: %s", state.PresidentialElectoralHistory) - // fmt.Printf("\n\tPolls: %s", state.Polls) + p_baserate_republican := 0.0 + for _, party := range state.PresidentialElectoralHistory { + if party == "R" { + p_baserate_republican++ + } + } + fmt.Printf("\n\tHistorical base rate of R win: %f", p_baserate_republican) + + // Individual poll for _, poll := range state.Polls { p_republican_win_poll := getChanceRepublicanWinFromPoll(poll) fmt.Printf("\n\tPoll: %+v", poll) fmt.Printf("\n\t\tPoll says chance of R win: %f", p_republican_win_poll) } + // Aggregate poll + num_biden_votes := 0.0 + num_trump_votes := 0.0 + for _, poll := range state.Polls { + biden_percentage, biden_exists := poll.PollResults["Biden"] + trump_percentage, trump_exists := poll.PollResults["Trump"] + if !biden_exists || !trump_exists { + panic("PollResults of poll filtered to have Biden/Trump doesn't have Biden/Trump") + } + num_biden_votes += (biden_percentage / 100.0) * float64(poll.SampleSize) + num_trump_votes += (trump_percentage / 100.0) * float64(poll.SampleSize) + } + aggregate_sample_size := num_biden_votes + num_trump_votes + if aggregate_sample_size != 0.0 { + var aggregate_poll = Poll{SampleSize: int(aggregate_sample_size), PollResults: make(map[string]float64)} + aggregate_poll.PollResults["Biden"] = 100.0 * num_biden_votes / aggregate_sample_size + aggregate_poll.PollResults["Trump"] = 100.0 * num_trump_votes / aggregate_sample_size + + p_republican_win_aggregate_polls := getChanceRepublicanWinFromPoll(aggregate_poll) + fmt.Printf("\n\tAggregate poll: %+v", aggregate_poll) + fmt.Printf("\n\t\tAggregate Poll says chance of R win: %f", p_republican_win_aggregate_polls) + } + } } @@ -325,23 +356,14 @@ func sampleFromState(state State) VotesForEachParty { aggregate_sample_size := num_biden_votes + num_trump_votes if aggregate_sample_size != 0.0 { - aggregate_trump_share := num_trump_votes / (num_trump_votes + num_biden_votes) - aggregate_biden_share := num_biden_votes / (num_trump_votes + num_biden_votes) - - std_mean_aggregate_polls := math.Sqrt((aggregate_trump_share * aggregate_biden_share) / aggregate_sample_size) - p_republican_aggregate_polls := getProbabilityAboveX(0.5, aggregate_trump_share, std_mean_aggregate_polls) - - if dev { - fmt.Printf("\n\tAggregating all polls naïvely says chance of R win: %f", p_republican_aggregate_polls) - } + var aggregate_poll = Poll{SampleSize: int(aggregate_sample_size), PollResults: make(map[string]float64)} + aggregate_poll.PollResults["Biden"] = 100.0 * num_biden_votes / aggregate_sample_size + aggregate_poll.PollResults["Trump"] = 100.0 * num_trump_votes / aggregate_sample_size + p_republican_win_aggregate_polls := getChanceRepublicanWinFromPoll(aggregate_poll) weight_polls := 1.0 - p_republican = weight_polls*p_republican_aggregate_polls + (1.0-weight_polls)*p_baserate_republican - + p_republican = weight_polls*p_republican_win_aggregate_polls + (1.0-weight_polls)*p_baserate_republican } - if dev { - fmt.Printf("\n\tHistorical base rate: %f", p_baserate_republican) - } if r.Float64() < p_republican { return VotesForEachParty{Democrats: 0, Republicans: state.Votes} } else { @@ -379,6 +401,8 @@ func main() { n_sims := 10_000 + printStates(states) + p_republicans := 0.0 for i := 0; i < n_sims; i++ { result := simulateElection(states)