2024-election-modelling

A walking stick to Nate Silver's sportscar
Log | Files | Refs | README

commit ca7401a6edc20cc65e96ac0241ee6a192018f779
parent e25942b2c30eb1b4dee31049a7f9c42dc09b074a
Author: NunoSempere <nuno.semperelh@protonmail.com>
Date:   Sun, 14 Apr 2024 09:58:18 -0400

add poll time

Diffstat:
MREADME.md | 5+++--
Mmain.go | 46++++++++++++++++++++++++++++------------------
2 files changed, 31 insertions(+), 20 deletions(-)

diff --git a/README.md b/README.md @@ -32,8 +32,9 @@ Remedy: consider the conditional probabilities? But how? Or, relax assumptions u ## Second round: just consider polls -- [ ] Download and format -- [ ] Read +- [x] Download and format +- [x] Read +- [ ] Add date of poll - [ ] Consider what the standards error should be - [ ] Aggregate polls? - [ ] Exclude polls older than one month? diff --git a/main.go b/main.go @@ -6,15 +6,16 @@ import ( rand "math/rand/v2" "os" "strconv" + "time" // "strings" ) /* Structs */ type State struct { - Name string - Votes int - VictoriousPartyPerElection map[string]string - Polls []Poll + Name string + Votes int + PresidentialElectoralHistory map[string]string + Polls []Poll } type VotesForEachParty struct { @@ -26,6 +27,7 @@ type Poll struct { PollId string SampleSize int PollResults map[string]float64 + Date time.Time } // type src = *rand.Rand @@ -60,7 +62,7 @@ func readStates() ([]State, error) { } state := csv_record[0] if _, exists := states[state]; !exists { - states[state] = State{Name: state, Votes: votes, VictoriousPartyPerElection: make(map[string]string)} + states[state] = State{Name: state, Votes: votes, PresidentialElectoralHistory: make(map[string]string)} } } @@ -87,7 +89,7 @@ func readStates() ([]State, error) { continue // State not found in votes map, skip } // Update the party winning in the specific year - data.VictoriousPartyPerElection[year] = party + data.PresidentialElectoralHistory[year] = party states[state] = data } @@ -115,17 +117,24 @@ func readStates() ([]State, error) { if err != nil { break // EOF or an error } - state_name := record[12] - // fmt.Printf("State: %s", state_name) + poll_id := record[0] + state_name := record[12] + candidate_name := record[44] + end_date := record[14] - sampleSize, err := strconv.Atoi(record[22]) + date_layout := "1/2/06" + parsed_date, err := time.Parse(date_layout, end_date) + if err != nil { + fmt.Println("Error parsing date: ", err) + } + + sample_size, err := strconv.Atoi(record[22]) if err != nil { continue // If error, skip this record } - candidateName := record[44] - pct, err := strconv.ParseFloat(record[47], 64) // pct is in the 42nd column + percentage, err := strconv.ParseFloat(record[47], 64) // percentage is in the 42nd column if err != nil { fmt.Printf("Error parsing percentage") continue // If error, skip this record @@ -139,11 +148,12 @@ func readStates() ([]State, error) { if !exists { poll = Poll{ PollId: poll_id, - SampleSize: sampleSize, + SampleSize: sample_size, PollResults: make(map[string]float64), + Date: parsed_date, } } - poll.PollResults[candidateName] = pct + poll.PollResults[candidate_name] = percentage tmp_polls[state_name][poll_id] = poll } @@ -177,7 +187,7 @@ func readStates() ([]State, error) { } func sampleFromState(state State) VotesForEachParty { - fmt.Printf("%s\n\n", state) + switch state.Name { case "Nebraska": // 2000: R @@ -208,12 +218,12 @@ func sampleFromState(state State) VotesForEachParty { default: { p_republican := 0.0 - for _, party := range state.VictoriousPartyPerElection { + for _, party := range state.PresidentialElectoralHistory { if party == "R" { p_republican++ } } - p_republican = p_republican / float64(len(state.VictoriousPartyPerElection)) + p_republican = p_republican / float64(len(state.PresidentialElectoralHistory)) if r.Float64() < p_republican { return VotesForEachParty{Democrats: 0, Republicans: state.Votes} } else { @@ -227,12 +237,12 @@ func simulateElection(states []State) int { republican_seats := 0 for _, state := range states { - // fmt.Printf("%s\n", state) + fmt.Printf("\n\nState: %s\n\tVotes: %d\n\tHistory: %s\n\tPolls: %s", state.Name, state.Votes, state.PresidentialElectoralHistory, state.Polls) election_sample := sampleFromState(state) republican_seats += election_sample.Republicans /* fmt.Printf("%s: Votes: %d,\n\tWinners: ", state.Name, state.Votes) - for year, party := range state.VictoriousPartyPerElection { + for year, party := range state.PresidentialElectoralHistory { fmt.Printf("[%s: %s] ", year, party) }