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
- [ ] 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?

48
main.go
View File

@ -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]
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 {
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)
}