Last active
October 31, 2016 08:17
-
-
Save owenallenaz/6ab41a29517c3b1cad9f499c037e349f to your computer and use it in GitHub Desktop.
Margin of error in polls with introduced bias
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
------------ | |
test { populationSize: 100000000, | |
sampleSize: 1000, | |
bias: 50, | |
marginOfError: 0.030990321069650117 } | |
inMarginOfError { yes: 940, no: 60 } | |
predictedElections { wins: 513, ties: 34, losses: 453 } | |
------------ | |
test { populationSize: 100000000, | |
sampleSize: 1000, | |
bias: 55, | |
marginOfError: 0.030990321069650117 } | |
inMarginOfError { yes: 121, no: 879 } | |
predictedElections { wins: 999, ties: 0, losses: 1 } | |
------------ | |
test { populationSize: 100000000, | |
sampleSize: 1000, | |
bias: 55, | |
marginOfError: 0.075 } | |
inMarginOfError { yes: 950, no: 50 } | |
predictedElections { wins: 998, ties: 0, losses: 2 } | |
------------ | |
test { populationSize: 100000000, | |
sampleSize: 1000, | |
bias: 45, | |
marginOfError: 0.030990321069650117 } | |
inMarginOfError { yes: 98, no: 902 } | |
predictedElections { wins: 0, ties: 0, losses: 1000 } | |
------------ | |
test { populationSize: 100000000, | |
sampleSize: 1000, | |
bias: 45, | |
marginOfError: 0.075 } | |
inMarginOfError { yes: 940, no: 60 } | |
predictedElections { wins: 3, ties: 0, losses: 997 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var randomBetween = function(min, max) { | |
return Math.floor(Math.random() * (max - min + 1) + min); | |
} | |
var testRuns = [ | |
{ populationSize : 100000000, sampleSize : 1000, bias : 50, marginOfError : .98 / Math.sqrt(1000) }, | |
{ populationSize : 100000000, sampleSize : 1000, bias : 55, marginOfError : .98 / Math.sqrt(1000) }, | |
{ populationSize : 100000000, sampleSize : 1000, bias : 55, marginOfError : .075 }, | |
{ populationSize : 100000000, sampleSize : 1000, bias : 45, marginOfError : .98 / Math.sqrt(1000) }, | |
{ populationSize : 100000000, sampleSize : 1000, bias : 45, marginOfError : .075 }, | |
] | |
var runTest = function(test) { | |
var all = []; | |
var results; | |
for(var i = 0; i < 1000; i++) { | |
results = { | |
0 : 0, | |
1 : 0 | |
} | |
for(var j = 0; j < test.sampleSize; j++) { | |
var temp = randomBetween(1, 100); | |
var vote = temp > test.bias ? 0 : 1; | |
results[vote]++; | |
} | |
all.push({ diff : Math.abs(results[1] - results[0]) / test.sampleSize / 2, off : results[1] - results[0] }); | |
} | |
var inMarginOfError = { | |
yes : 0, | |
no : 0 | |
} | |
var electionResults = { | |
wins : 0, | |
ties : 0, | |
losses : 0 | |
} | |
all.forEach(function(val, i) { | |
if (val.diff < test.marginOfError) { | |
inMarginOfError.yes++; | |
} else { | |
inMarginOfError.no++; | |
} | |
if (val.off > 0) { electionResults.wins++; } | |
if (val.off === 0) { electionResults.ties++; } | |
if (val.off < 0) { electionResults.losses++; } | |
}); | |
console.log("------------"); | |
console.log("test", test); | |
console.log("inMarginOfError", inMarginOfError); | |
console.log("electionResults", electionResults); | |
} | |
testRuns.forEach(function(test, i) { | |
runTest(test); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment