Let me explain what I'm trying to do. I have data like
const dataByState = {
'Washington' : { ElectoralVotes : 12, RChance: 54, DChance: 46 },
'Oregon': { ElectoralVotes: 7, RChance: 51, DChance: 49 },
.
.
.
'Hawaii' : { ElectoralVotes: 4, RChance : 40, DChance: 60 }
};
where one of the above key-value pairs like
'Hawaii' : { ElectoralVotes: 4, RChance : 40, DChance: 60 }
means "In the state Hawaii, which has 4 electoral votes, there is a 40% chance of the Republican Candidate winning and a 60% chance of the Democrat candidate winning". What I'm ultimately trying to do is calculate the chance of each candidate winning the election. How this would be done in a perfect world is
- Iterate through all
2^51
combinations of states - For each combination
c
, its combined electoral votes are greater than or equal to270
, add it to a collectionC
of collecions of states - For the Republican candidate, sum up the probabilities of winning each combination of states in
C
; call that valuer
. That's his/her chance of winning. The Democrat's chance is1 - r
.
But since I can't go through all 2^51
, what I'm doing is choosing some N
smaller than 51
and doing
- Find a random
2^N
combinations of states whose combined electoral votes sum to greater than or equal to 270; call this combinationC
. - For the Republican candidate, sum up the probabilities of winning each combination of states in
C
; call that valuer
. Multiplyr
by2^(51-N)
. That's approximately his/her chance of winning. The Democrat's chance is1 - r
.
Anyhow, this doesn't seem to be working and I'm wondering whether my logic is wrong (I haven't taken statistics since college 3 years ago) or if I'm running into rounding errors. I'm getting a near 100% of the Republican winning (i.e. America being made great again) when I make the chance even in every state, which is wrong because it should calculate to about 50/50.
Code dump: https://jsfiddle.net/pqhnwek9/