various tweaks
This commit is contained in:
parent
f115fd2039
commit
d1e1eb1fc4
18
README.md
18
README.md
|
@ -40,13 +40,23 @@ Remedy: consider the conditional probabilities? But how? Or, relax assumptions u
|
||||||
- [x] Download and format
|
- [x] Download and format
|
||||||
- [x] Read
|
- [x] Read
|
||||||
- [x] Add date of poll
|
- [x] Add date of poll
|
||||||
- [ ] Consider what the standards error should be
|
- [x] Consider what the standards error should be
|
||||||
- [ ] Consider how to aggregate polls?
|
- [x] Consider how to aggregate polls?
|
||||||
- One extreme: Just look at the most recent one
|
- One extreme: Just look at the most recent one
|
||||||
- [x] Another extreme: Aggregate very naïvely, add up all samples together?
|
- [x] Another extreme: Aggregate very naïvely, add up all samples together?
|
||||||
- [ ] Aggregate polls?
|
- [x] Aggregate polls?
|
||||||
- [ ] Exclude polls older than one month?
|
- [x] Exclude polls older than one month?
|
||||||
- [ ] Exclude partisan polls
|
- [ ] Exclude partisan polls
|
||||||
- [ ] ...
|
- [ ] ...
|
||||||
|
|
||||||
https://stats.stackexchange.com/questions/274211/calculating-the-probability-of-someone-winning-from-a-poll
|
https://stats.stackexchange.com/questions/274211/calculating-the-probability-of-someone-winning-from-a-poll
|
||||||
|
|
||||||
|
This time, the republicans win by a mile.
|
||||||
|
|
||||||
|
What is happening?
|
||||||
|
- Trump polling ok in Minessota,
|
||||||
|
- No polls in Iowa yet
|
||||||
|
- Trump polling very well in Pennsylvania, Wisconsin, Arizona, Michigan, Florida, Nevada, Georgia, North Carolina
|
||||||
|
- In part because of the Kennedy/West/Stein vote.
|
||||||
|
- Biden is poling well in Colorado
|
||||||
|
- Not that many polls yet, in general
|
||||||
|
|
54
main.go
54
main.go
|
@ -29,10 +29,12 @@ type Poll struct {
|
||||||
SampleSize int
|
SampleSize int
|
||||||
PollResults map[string]float64
|
PollResults map[string]float64
|
||||||
Date time.Time
|
Date time.Time
|
||||||
|
Partisan string
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Globals */
|
/* Globals */
|
||||||
var r = rand.New(rand.NewPCG(uint64(100), uint64(2224)))
|
var r = rand.New(rand.NewPCG(uint64(100), uint64(2224)))
|
||||||
|
var dev = true
|
||||||
|
|
||||||
/* Load data from csvs */
|
/* Load data from csvs */
|
||||||
func readStates() ([]State, error) {
|
func readStates() ([]State, error) {
|
||||||
|
@ -119,8 +121,9 @@ func readStates() ([]State, error) {
|
||||||
|
|
||||||
poll_id := record[0]
|
poll_id := record[0]
|
||||||
state_name := record[12]
|
state_name := record[12]
|
||||||
candidate_name := record[44]
|
|
||||||
end_date := record[14]
|
end_date := record[14]
|
||||||
|
partisan := record[32]
|
||||||
|
candidate_name := record[44]
|
||||||
|
|
||||||
date_layout := "1/2/06"
|
date_layout := "1/2/06"
|
||||||
parsed_date, err := time.Parse(date_layout, end_date)
|
parsed_date, err := time.Parse(date_layout, end_date)
|
||||||
|
@ -150,6 +153,7 @@ func readStates() ([]State, error) {
|
||||||
SampleSize: sample_size,
|
SampleSize: sample_size,
|
||||||
PollResults: make(map[string]float64),
|
PollResults: make(map[string]float64),
|
||||||
Date: parsed_date,
|
Date: parsed_date,
|
||||||
|
Partisan: partisan,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
poll.PollResults[candidate_name] = percentage
|
poll.PollResults[candidate_name] = percentage
|
||||||
|
@ -202,11 +206,12 @@ func getChanceCandidateWinsFromPoll(candidate_p float64, poll_sample_size float6
|
||||||
|
|
||||||
/* Sample state by state */
|
/* Sample state by state */
|
||||||
func sampleFromState(state State) VotesForEachParty {
|
func sampleFromState(state State) VotesForEachParty {
|
||||||
fmt.Printf("\n\nState: %s", state.Name)
|
if dev {
|
||||||
fmt.Printf("\n\tVotes: %d", state.Votes)
|
fmt.Printf("\n\nState: %s", state.Name)
|
||||||
fmt.Printf("\n\tHistory: %s", state.PresidentialElectoralHistory)
|
fmt.Printf("\n\tVotes: %d", state.Votes)
|
||||||
// fmt.Printf("\n\tPolls: %s", state.Polls)
|
fmt.Printf("\n\tHistory: %s", state.PresidentialElectoralHistory)
|
||||||
|
// fmt.Printf("\n\tPolls: %s", state.Polls)
|
||||||
|
}
|
||||||
switch state.Name {
|
switch state.Name {
|
||||||
case "Nebraska":
|
case "Nebraska":
|
||||||
/*
|
/*
|
||||||
|
@ -241,13 +246,14 @@ func sampleFromState(state State) VotesForEachParty {
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
/* Just considering the base rate for the state */
|
/* Just considering the base rate for the state */
|
||||||
p_republican := 0.0
|
p_baserate_republican := 0.0
|
||||||
for _, party := range state.PresidentialElectoralHistory {
|
for _, party := range state.PresidentialElectoralHistory {
|
||||||
if party == "R" {
|
if party == "R" {
|
||||||
p_republican++
|
p_baserate_republican++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
p_republican = p_republican / float64(len(state.PresidentialElectoralHistory))
|
p_baserate_republican = p_baserate_republican / float64(len(state.PresidentialElectoralHistory))
|
||||||
|
p_republican := p_baserate_republican // if no polls
|
||||||
|
|
||||||
/* Considering polls */
|
/* Considering polls */
|
||||||
var recent_polls []Poll
|
var recent_polls []Poll
|
||||||
|
@ -294,8 +300,10 @@ func sampleFromState(state State) VotesForEachParty {
|
||||||
std_poll := math.Sqrt((normalized_trump_share * normalized_biden_share) / joint_trump_biden_sample_size)
|
std_poll := math.Sqrt((normalized_trump_share * normalized_biden_share) / joint_trump_biden_sample_size)
|
||||||
|
|
||||||
p_trump_more_votes := getProbabilityAboveX(0.5, normalized_trump_share, std_poll)
|
p_trump_more_votes := getProbabilityAboveX(0.5, normalized_trump_share, std_poll)
|
||||||
fmt.Printf("\n\tPoll: %+v", recent_biden_trump_poll)
|
if dev {
|
||||||
fmt.Printf("\n\t\tPoll says chance of R win: %f", p_trump_more_votes)
|
fmt.Printf("\n\tPoll: %+v", recent_biden_trump_poll)
|
||||||
|
fmt.Printf("\n\t\tPoll says chance of R win: %f", p_trump_more_votes)
|
||||||
|
}
|
||||||
|
|
||||||
// Update general tally
|
// Update general tally
|
||||||
num_biden_votes += poll_biden_votes
|
num_biden_votes += poll_biden_votes
|
||||||
|
@ -308,12 +316,18 @@ func sampleFromState(state State) VotesForEachParty {
|
||||||
|
|
||||||
std_all_polls := math.Sqrt((aggregate_trump_share * aggregate_biden_share) / total_sample_size)
|
std_all_polls := math.Sqrt((aggregate_trump_share * aggregate_biden_share) / total_sample_size)
|
||||||
|
|
||||||
p_trump_more_votes := getProbabilityAboveX(0.5, aggregate_trump_share, std_all_polls)
|
p_republican_according_to_polls := getProbabilityAboveX(0.5, aggregate_trump_share, std_all_polls)
|
||||||
fmt.Printf("\n\tAggregating all polls naïvely says chance of R win: %f", p_trump_more_votes)
|
|
||||||
|
if dev {
|
||||||
|
fmt.Printf("\n\tAggregating all polls naïvely says chance of R win: %f", p_republican_according_to_polls)
|
||||||
|
}
|
||||||
|
p_republican = 0.75*p_republican_according_to_polls + 0.25*p_baserate_republican
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("\n\tHistorical base rate: %f", p_republican)
|
if dev {
|
||||||
|
fmt.Printf("\n\tHistorical base rate: %f", p_baserate_republican)
|
||||||
|
}
|
||||||
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 {
|
||||||
|
@ -331,7 +345,9 @@ func simulateElection(states []State) int {
|
||||||
republican_seats += election_sample.Republicans
|
republican_seats += election_sample.Republicans
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("\n\n\n (%d) ", republican_seats)
|
if dev {
|
||||||
|
fmt.Printf("\n\n(%d) ", republican_seats)
|
||||||
|
}
|
||||||
if republican_seats >= 270 {
|
if republican_seats >= 270 {
|
||||||
return 1
|
return 1
|
||||||
} else {
|
} else {
|
||||||
|
@ -347,17 +363,19 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
n_sims := 1
|
n_sims := 10
|
||||||
|
|
||||||
p_republicans := 0.0
|
p_republicans := 0.0
|
||||||
for i := 0; i < n_sims; i++ {
|
for i := 0; i < n_sims; i++ {
|
||||||
result := simulateElection(states)
|
result := simulateElection(states)
|
||||||
fmt.Printf("Election result: %d\n", result)
|
if dev {
|
||||||
|
fmt.Printf("Election result: %d", result)
|
||||||
|
}
|
||||||
if result == 1 {
|
if result == 1 {
|
||||||
p_republicans++
|
p_republicans++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
p_republicans = p_republicans / float64(n_sims)
|
p_republicans = p_republicans / float64(n_sims)
|
||||||
fmt.Printf("%% republicans: %f\n", p_republicans)
|
fmt.Printf("\n\n\n%% republicans: %f\n", p_republicans)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user