add poll time

This commit is contained in:
NunoSempere 2024-04-14 09:58:18 -04:00
parent e25942b2c3
commit ca7401a6ed
2 changed files with 32 additions and 21 deletions

View File

@ -32,8 +32,9 @@ Remedy: consider the conditional probabilities? But how? Or, relax assumptions u
## Second round: just consider polls ## Second round: just consider polls
- [ ] Download and format - [x] Download and format
- [ ] Read - [x] Read
- [ ] Add date of poll
- [ ] Consider what the standards error should be - [ ] Consider what the standards error should be
- [ ] Aggregate polls? - [ ] Aggregate polls?
- [ ] Exclude polls older than one month? - [ ] Exclude polls older than one month?

42
main.go
View File

@ -6,6 +6,7 @@ import (
rand "math/rand/v2" rand "math/rand/v2"
"os" "os"
"strconv" "strconv"
"time"
// "strings" // "strings"
) )
@ -13,7 +14,7 @@ import (
type State struct { type State struct {
Name string Name string
Votes int Votes int
VictoriousPartyPerElection map[string]string PresidentialElectoralHistory map[string]string
Polls []Poll Polls []Poll
} }
@ -26,6 +27,7 @@ type Poll struct {
PollId string PollId string
SampleSize int SampleSize int
PollResults map[string]float64 PollResults map[string]float64
Date time.Time
} }
// type src = *rand.Rand // type src = *rand.Rand
@ -60,7 +62,7 @@ func readStates() ([]State, error) {
} }
state := csv_record[0] state := csv_record[0]
if _, exists := states[state]; !exists { 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 continue // State not found in votes map, skip
} }
// Update the party winning in the specific year // Update the party winning in the specific year
data.VictoriousPartyPerElection[year] = party data.PresidentialElectoralHistory[year] = party
states[state] = data states[state] = data
} }
@ -115,17 +117,24 @@ func readStates() ([]State, error) {
if err != nil { if err != nil {
break // EOF or an error break // EOF or an error
} }
state_name := record[12]
// fmt.Printf("State: %s", state_name)
poll_id := record[0]
sampleSize, err := strconv.Atoi(record[22]) poll_id := record[0]
state_name := record[12]
candidate_name := record[44]
end_date := record[14]
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 { if err != nil {
continue // If error, skip this record continue // If error, skip this record
} }
candidateName := record[44] percentage, err := strconv.ParseFloat(record[47], 64) // percentage is in the 42nd column
pct, err := strconv.ParseFloat(record[47], 64) // pct is in the 42nd column
if err != nil { if err != nil {
fmt.Printf("Error parsing percentage") fmt.Printf("Error parsing percentage")
continue // If error, skip this record continue // If error, skip this record
@ -139,11 +148,12 @@ func readStates() ([]State, error) {
if !exists { if !exists {
poll = Poll{ poll = Poll{
PollId: poll_id, PollId: poll_id,
SampleSize: sampleSize, SampleSize: sample_size,
PollResults: make(map[string]float64), PollResults: make(map[string]float64),
Date: parsed_date,
} }
} }
poll.PollResults[candidateName] = pct poll.PollResults[candidate_name] = percentage
tmp_polls[state_name][poll_id] = poll tmp_polls[state_name][poll_id] = poll
} }
@ -177,7 +187,7 @@ func readStates() ([]State, error) {
} }
func sampleFromState(state State) VotesForEachParty { func sampleFromState(state State) VotesForEachParty {
fmt.Printf("%s\n\n", state)
switch state.Name { switch state.Name {
case "Nebraska": case "Nebraska":
// 2000: R // 2000: R
@ -208,12 +218,12 @@ func sampleFromState(state State) VotesForEachParty {
default: default:
{ {
p_republican := 0.0 p_republican := 0.0
for _, party := range state.VictoriousPartyPerElection { for _, party := range state.PresidentialElectoralHistory {
if party == "R" { if party == "R" {
p_republican++ p_republican++
} }
} }
p_republican = p_republican / float64(len(state.VictoriousPartyPerElection)) p_republican = p_republican / float64(len(state.PresidentialElectoralHistory))
if r.Float64() < p_republican { if r.Float64() < p_republican {
return VotesForEachParty{Democrats: 0, Republicans: state.Votes} return VotesForEachParty{Democrats: 0, Republicans: state.Votes}
} else { } else {
@ -227,12 +237,12 @@ func simulateElection(states []State) int {
republican_seats := 0 republican_seats := 0
for _, state := range states { 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) election_sample := sampleFromState(state)
republican_seats += election_sample.Republicans republican_seats += election_sample.Republicans
/* /*
fmt.Printf("%s: Votes: %d,\n\tWinners: ", state.Name, state.Votes) 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) fmt.Printf("[%s: %s] ", year, party)
} }