Forked from greenido/WorldCup Monte Carlo Simulations.js
Last active
September 3, 2016 00:10
-
-
Save tariqhamid/c9701e4019aa412c8d862c03b36d9442 to your computer and use it in GitHub Desktop.
WorldCup Monte Carlo Simulations in Apps Script (For a post)
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
/*************************************************************************** | |
* This is a Monte Carlo simulation | |
* to see who will win the 2014 World Cup | |
* Data ideas: http://fivethirtyeight.com/interactives/world-cup/ | |
* See: http://goo.gl/NV2OLc for a diagram of the odds. | |
* Author: Ido Green | @greenido | plus.google.com/+greenido | |
* Date: 4th July 2014 | |
* *************************************************************************/ | |
// Decide base on our odds who will win each match | |
function stateMachine(curLine) { | |
var path = ""; | |
var curState = "0:0"; | |
while (curState != "BRA" && | |
curState != "GER") { | |
path += curState + " | "; | |
var rand = Math.random(); | |
switch (curState) { | |
case "0:0": | |
// We always start at this state | |
if (rand < 0.3) { | |
curState = "1:0"; | |
} else if (rand >= 0.3 && rand < 0.6) { | |
curState = "1:1"; | |
} else if (rand >= 0.6 && rand < 0.8) { | |
curState = "0:1"; | |
} else if (rand >= 0.8) { | |
curState = "kicks"; | |
} | |
break; | |
case "1:0": | |
if (rand < 0.3) { | |
curState = "kicks"; | |
} else if (rand >= 0.3 && rand < 0.8) { | |
curState = "2:0"; | |
} else if (rand >= 0.8 && rand < 0.9) { | |
curState = "1:1"; | |
} else if (rand >= 0.9) { | |
curState = "BRA"; | |
} | |
break; | |
case "1:1": | |
if (rand < 0.4) { | |
curState = "2:1"; | |
} else if (rand >= 0.4 && rand < 0.7) { | |
curState = "1:2"; | |
} else if (rand >= 0.7) { | |
curState = "kicks"; | |
} | |
break; | |
case "0:1": | |
if (rand < 0.3) { | |
curState = "2:1"; | |
} else if (rand >= 0.3 && rand < 0.5) { | |
curState = "1:2"; | |
} else if (rand >= 0.5 && rand < 0.7) { | |
curState = "kicks"; | |
} else if (rand >= 0.7) { | |
curState = "GER"; | |
} | |
break; | |
case "2:0": | |
case "2:1": | |
curState = "BRA"; | |
break; | |
case "1:2": | |
case "0:2": | |
curState = "GER"; | |
break; | |
case "kicks": | |
if (rand < 0.4) { | |
curState = "BRA"; | |
} else { | |
curState = "GER"; | |
} | |
break; | |
} // switch | |
} // while | |
var curSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Games1"); | |
var barWon = 1; | |
var gerWon = 0; | |
if (curState != "BRA") { | |
gerWon = 1; | |
barWon = 0; | |
} | |
curSheet.getRange("a"+curLine).setValue(barWon); | |
curSheet.getRange("b"+curLine).setValue(gerWon); | |
curSheet.getRange("c"+curLine).setValue(path); | |
return curState; | |
} | |
// Run on the senarios | |
function runStateSenarios() { | |
var start = new Date().getTime(); | |
var curSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Games1"); | |
var numOfSenarios = curSheet.getRange("e1").getValue(); | |
try { | |
var brazilWon = 0; | |
var germanyWon = 0; | |
for (var i=0; i < numOfSenarios; i++) { | |
var result = stateMachine(i+11); | |
if (result === "BRA") { | |
brazilWon++; | |
} | |
else { | |
germanyWon++; | |
} | |
SpreadsheetApp.flush(); | |
} | |
var percForBra = Math.round((brazilWon / (brazilWon+germanyWon))*100); | |
curSheet.getRange("a2").setValue(brazilWon); | |
curSheet.getRange("b2").setValue(germanyWon); | |
curSheet.getRange("a3").setValue(percForBra); | |
curSheet.getRange("b3").setValue(100 - percForBra); | |
} catch(err) { | |
Logger.log("Error: runSenarios(): " + err); | |
} | |
var end = new Date().getTime(); | |
var execTime = (end - start) / 1000; | |
var outStr = "<ul><li>Brazil won: " + brazilWon + " <li>Gremany won: " + germanyWon + | |
" <li>Brazil get the cup in %" + percForBra + " of times. <li><small>It took: " + | |
execTime + "sec</ul>"; | |
var htmlApp = HtmlService | |
.createHtmlOutput(outStr) | |
.setTitle('Results') | |
.setWidth(350) | |
.setHeight(200); | |
SpreadsheetApp.getActiveSpreadsheet().show(htmlApp); | |
Logger.log(outStr); | |
} | |
// | |
function onOpen() { | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var menuEntries = []; | |
menuEntries.push({name: "Run Simulations", functionName: "runStateSenarios"}); | |
ss.addMenu("WorldCup Simulator", menuEntries); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment