JavaScript What is wrong with my method for predicting the election?

  • Thread starter Thread starter SlurrerOfSpeech
  • Start date Start date
  • Tags Tags
    Method
AI Thread Summary
The discussion centers on a method for calculating the probabilities of Trump winning the electoral votes based on combinations of states. The user identifies 210 subcollections of states that total 270 electoral votes or more and sums the probabilities of Trump winning each collection. However, they express concern that the results consistently yield a probability around 0.99 instead of the expected 0.50, given equal chances for Republican and Democrat candidates.The issue arises from the method of extrapolation, where a small sample size is compared to a much larger number, leading to skewed results. The user is advised to consider using statistical measures like medians or means instead of iterating through all possible outcomes. This approach could help control variance and provide a more accurate estimate of the probabilities involved.
SlurrerOfSpeech
Messages
141
Reaction score
11
Given the fact that there are 51 states and districts, there are 251 subcollections of the 51 states, which I can't possibly iterate over entirely. So what I do is find 210 subcollections of states whose electoral votes summed are 270 or greater. I then sum together the probabilities of Trump winning each of those 210 collection of states. Finally, I multiply that sum by 241.

Anything wrong with that? Because I'm getting a results that seems wrong.
 
Technology news on Phys.org
You're extrapolating a small sample - comparing to (##2^{51}##), by a huge number (##2^{41}##) so the result is way off.
 
Can you help me figure out why this is always calculating to ~0.99? It should be calculating to ~0.50 since I put an equal chance of an R or D winning.

Code:
const VOTES_TO_WIN = 270;

// State name, number of electoral votes, and Republican and
// Democrat nominee polling percent average taken from
// RealClearPolitics.com, rounded to nearest integer
const dataByState = {
    'Washington' : { ElectoralVotes : 12, RChance: 50, DChance: 50 },
    'Oregon': { ElectoralVotes: 7, RChance: 50, DChance: 50 },
    'California': { ElectoralVotes: 55, RChance: 50, DChance: 50 },
    'Idaho' : { ElectoralVotes: 4, RChance : 50, DChance: 50 },
    'Nevada' : { ElectoralVotes: 6, RChance : 50, DChance: 50 },
    'Montana' : { ElectoralVotes: 3, RChance : 50, DChance: 50 },
    'Wyoming' : { ElectoralVotes: 3, RChance : 50, DChance: 50 },
    'Colorado' : { ElectoralVotes: 9, RChance : 50, DChance: 50 },
    'New Mexico' : { ElectoralVotes: 5, RChance : 50, DChance: 50 },
    'Utah' : { ElectoralVotes: 6, RChance : 50, DChance: 50 },
    'Arizona' : { ElectoralVotes: 11, RChance : 50, DChance: 50 },
    'North Dakota' : { ElectoralVotes: 3, RChance : 50, DChance: 50 },
    'South Dakota' : { ElectoralVotes: 3, RChance : 50, DChance: 50 },
    'Nebraska' : { ElectoralVotes: 5, RChance : 50, DChance: 50 },
    'Kansas' : { ElectoralVotes: 6, RChance : 50, DChance: 50 },
    'Oklahoma' : { ElectoralVotes: 7, RChance : 50, DChance: 50 },
    'Texas' : { ElectoralVotes: 38, RChance : 50, DChance: 50 },
    'Minnesota' : { ElectoralVotes: 10, RChance : 50, DChance: 50 },
    'Iowa' : { ElectoralVotes: 6, RChance : 50, DChance: 50 },
    'Missouri' : { ElectoralVotes: 10, RChance : 50, DChance: 50 },
    'Arkansas' : { ElectoralVotes: 6, RChance : 50, DChance: 50 },
    'Lousiana' : { ElectoralVotes: 8, RChance : 50, DChance: 50 },
    'Wisconsin' : { ElectoralVotes: 10, RChance : 50, DChance: 50 },
    'Illinois' : { ElectoralVotes: 20, RChance : 50, DChance: 50 },
    'Tennessee' : { ElectoralVotes: 11, RChance : 50, DChance: 50 },
    'Mississippi' : { ElectoralVotes: 6, RChance : 50, DChance: 50 },
    'Alabama' : { ElectoralVotes: 9, RChance : 50, DChance: 50 },
    'Michigan' : { ElectoralVotes: 16, RChance : 50, DChance: 50 },
    'Indiana' : { ElectoralVotes: 11, RChance : 50, DChance: 50 },
    'Kentucky' : { ElectoralVotes: 8, RChance : 50, DChance: 50 },
    'Ohio' : { ElectoralVotes: 18, RChance : 50, DChance: 50 },
    'West Virginia' : { ElectoralVotes: 5, RChance : 50, DChance: 50 },
    'Virginia' : { ElectoralVotes: 13, RChance : 50, DChance: 50 },
    'North Carolina' : { ElectoralVotes: 15, RChance : 50, DChance: 50 },
    'South Carolina' : { ElectoralVotes: 9, RChance : 50, DChance: 50 },
    'Georgia' : { ElectoralVotes: 16, RChance : 50, DChance: 50 },
    'Florida' : { ElectoralVotes: 29, RChance : 50, DChance: 50 },
    'D.C.' : { ElectoralVotes: 3, RChance : 50, DChance: 50 },
    'Maryland' : { ElectoralVotes: 10, RChance : 50, DChance: 50 },
    'Delaware' : { ElectoralVotes: 3, RChance : 50, DChance: 50 },
    'New Jersey' : { ElectoralVotes: 14, RChance : 50, DChance: 50 },
    'Pennsylvania' : { ElectoralVotes: 20, RChance : 50, DChance: 50 },
    'Connectuicut' : { ElectoralVotes: 7, RChance : 50, DChance: 50 },
    'Rhode Island' : { ElectoralVotes: 4, RChance : 50, DChance: 50 },
    'Massachusetts' : { ElectoralVotes: 11, RChance : 50, DChance: 50 },
    'New York' : { ElectoralVotes: 29, RChance : 50, DChance: 50 },
    'Vermont' : { ElectoralVotes: 3, RChance : 50, DChance: 50 },
    'New Hampshire' : { ElectoralVotes: 4, RChance : 50, DChance: 50 },
    'Maine' : { ElectoralVotes: 4, RChance : 50, DChance: 50 },
    'Alaska' : { ElectoralVotes: 3, RChance : 50, DChance: 50 },
    'Hawaii' : { ElectoralVotes: 4, RChance : 50, DChance: 50 }
};

const states = Object.keys(dataByState);

// Helper for using the above map to get a chance of winning
// from a given poll difference
const calcWinChance = (diff) => {
    const conv = pollConversion.find((x) =>
        x.DiffRange[0] <= diff && x.DiffRange[1] >= diff
    );
    return conv && conv.ChanceWin;
}// Helper returns true or false depending on whether a given
// collection of states has enough combined electoral votes
// to win an election
const hasSufficientVotes = (states) => {
    const votes = states.reduce((sum,state) =>
        sum += dataByState[state].ElectoralVotes
    , 0);
    return votes >= VOTES_TO_WIN;
};

// Helper for getting the combination of states corresponding
// to the inputted bit pattern i
const getCombo = (i) => {
    let combo = [];
    for(var j = 0; j < states.length; ++j)
       if((i >> j) & 1)
           combo.push(states[j]);  
    return combo;      
}

// To be filled out, combos will be a map of a bit pattern
// to the corresponding array of state names
let combos = {};

// Set time limit on
const now = new Date();
const timeout = now.setSeconds(now.getSeconds() + 1); // 1 seconds

// Run simulation
const rangetop = Math.pow(2,states.length) + 1;
while(new Date() < timeout)
{
    const rand = Math.floor(Math.random() * rangetop); 
    if(!combos.hasOwnProperty(rand))
        combos[rand] = getCombo(rand);
}

// Sum up the probabilites of the R candidate winning each
// combination of states that add up to a succifient number
// of electoral votes
let RSum = 0;
const keynums = Object.keys(combos);
console.log("num combos = " + keynums.length);//TEST
keynums.forEach((num) => {
  
    const comboStates = combos[num];
  
    // state combo not counted if it doesn't add up
    // to enough electoral votes
    if(!hasSufficientVotes(comboStates))
        return;
  
    // mutltipy together the probabilities of the R
    // winning the state combo
    let RProb = 1;
    comboStates.forEach((state) => {
        RProb *= dataByState[state].RChance / 100;
    });
    const otherStates = states.filter((state) => !comboStates.includes(state));
    otherStates.forEach((state) => {
        RProb *= dataByState[state].DChance / 100; 
    });

    RSum += RProb;
});

const multiplier = Math.pow(2, states.length + 1) / keynums.length;
RSum *= multiplier;

alert("RSum = " + RSum);
 
Hey SlurrerOfSpeech.

Are you trying to do a probabilistic/statistical estimate?

If so you could look at doing medians or means with a distribution as opposed to iterating over every outcome and use probabilities to gauge whether an outcome for some group/organization will occur.

The issue you should have is controlling the variance and depending on the constraints you have, you can shrink them dramatically if the information exists.
 
chiro said:
Hey SlurrerOfSpeech.

Are you trying to do a probabilistic/statistical estimate?

If so you could look at doing medians or means with a distribution as opposed to iterating over every outcome and use probabilities to gauge whether an outcome for some group/organization will occur.

The issue you should have is controlling the variance and depending on the constraints you have, you can shrink them dramatically if the information exists.

I will try this. Thanks. :)
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Back
Top