move poll filtering to loadStates function
This commit is contained in:
parent
862b7f0c92
commit
693538cdca
70
main.go
70
main.go
|
@ -105,7 +105,7 @@ func readStates() ([]State, error) {
|
|||
defer polls_file.Close()
|
||||
|
||||
// Using a temporary map to group poll results by state and poll ID
|
||||
tmp_polls := make(map[string]map[string]Poll)
|
||||
state_polls_map := make(map[string]map[string]Poll)
|
||||
|
||||
polls_reader := csv.NewReader(polls_file)
|
||||
_, err = polls_reader.Read() // Skip the header
|
||||
|
@ -142,11 +142,11 @@ func readStates() ([]State, error) {
|
|||
continue // If error, skip this record
|
||||
}
|
||||
|
||||
if _, exists := tmp_polls[state_name]; !exists {
|
||||
tmp_polls[state_name] = make(map[string]Poll)
|
||||
if _, exists := state_polls_map[state_name]; !exists {
|
||||
state_polls_map[state_name] = make(map[string]Poll)
|
||||
}
|
||||
|
||||
poll, exists := tmp_polls[state_name][poll_id]
|
||||
poll, exists := state_polls_map[state_name][poll_id]
|
||||
if !exists {
|
||||
poll = Poll{
|
||||
PollId: poll_id,
|
||||
|
@ -157,24 +157,43 @@ func readStates() ([]State, error) {
|
|||
}
|
||||
}
|
||||
poll.PollResults[candidate_name] = percentage
|
||||
tmp_polls[state_name][poll_id] = poll
|
||||
state_polls_map[state_name][poll_id] = poll
|
||||
}
|
||||
|
||||
// Add the aggregated poll data to the respective states
|
||||
for state_name, polls := range tmp_polls {
|
||||
var pollsSlice []Poll
|
||||
for state_name, polls := range state_polls_map {
|
||||
|
||||
var recent_polls []Poll
|
||||
for _, poll := range polls {
|
||||
pollsSlice = append(pollsSlice, poll)
|
||||
if poll.Date.After(time.Now().AddDate(0, 0, -30)) {
|
||||
recent_polls = append(recent_polls, poll)
|
||||
}
|
||||
}
|
||||
var recent_biden_trump_polls []Poll
|
||||
for _, recent_poll := range recent_polls {
|
||||
has_biden := false
|
||||
has_trump := false
|
||||
for candidate_name, _ := range recent_poll.PollResults {
|
||||
if candidate_name == "Biden" {
|
||||
has_biden = true
|
||||
} else if candidate_name == "Trump" {
|
||||
has_trump = true
|
||||
}
|
||||
}
|
||||
if has_biden && has_trump {
|
||||
recent_biden_trump_polls = append(recent_biden_trump_polls, recent_poll)
|
||||
}
|
||||
}
|
||||
|
||||
if state, exists := states[state_name]; exists {
|
||||
state.Polls = pollsSlice
|
||||
state.Polls = recent_biden_trump_polls
|
||||
states[state_name] = state // Not redundant
|
||||
} else {
|
||||
// fmt.Printf("Encountered new state: %s\n", state_name)
|
||||
/*
|
||||
states[state_name] = State{
|
||||
Name: state_name,
|
||||
Polls: pollsSlice,
|
||||
Polls: polls_slice,
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
@ -245,7 +264,7 @@ func sampleFromState(state State) VotesForEachParty {
|
|||
}
|
||||
default:
|
||||
{
|
||||
/* Just considering the base rate for the state */
|
||||
/* Consider the base rate for the state */
|
||||
p_baserate_republican := 0.0
|
||||
for _, party := range state.PresidentialElectoralHistory {
|
||||
if party == "R" {
|
||||
|
@ -255,33 +274,10 @@ func sampleFromState(state State) VotesForEachParty {
|
|||
p_baserate_republican = p_baserate_republican / float64(len(state.PresidentialElectoralHistory))
|
||||
p_republican := p_baserate_republican // if no polls
|
||||
|
||||
/* Considering polls */
|
||||
var recent_polls []Poll
|
||||
for _, poll := range state.Polls {
|
||||
if poll.Date.After(time.Now().AddDate(0, 0, -30)) {
|
||||
recent_polls = append(recent_polls, poll)
|
||||
}
|
||||
}
|
||||
|
||||
var recent_biden_trump_polls []Poll
|
||||
for _, recent_poll := range recent_polls {
|
||||
has_biden := false
|
||||
has_trump := false
|
||||
for candidate_name, _ := range recent_poll.PollResults {
|
||||
if candidate_name == "Biden" {
|
||||
has_biden = true
|
||||
} else if candidate_name == "Trump" {
|
||||
has_trump = true
|
||||
}
|
||||
}
|
||||
if has_biden && has_trump {
|
||||
recent_biden_trump_polls = append(recent_biden_trump_polls, recent_poll)
|
||||
}
|
||||
}
|
||||
|
||||
/* Consider polls */
|
||||
num_biden_votes := 0.0
|
||||
num_trump_votes := 0.0
|
||||
for _, recent_biden_trump_poll := range recent_biden_trump_polls {
|
||||
for _, recent_biden_trump_poll := range state.Polls {
|
||||
biden_share := 0.0
|
||||
trump_share := 0.0
|
||||
for candidate_name, candidate_percentage := range recent_biden_trump_poll.PollResults {
|
||||
|
@ -364,7 +360,7 @@ func main() {
|
|||
return
|
||||
}
|
||||
|
||||
n_sims := 100_000
|
||||
n_sims := 10_000
|
||||
|
||||
p_republicans := 0.0
|
||||
for i := 0; i < n_sims; i++ {
|
||||
|
|
Loading…
Reference in New Issue
Block a user