Skip to content

Instantly share code, notes, and snippets.

@greenido
Created October 14, 2013 06:56
Show Gist options
  • Save greenido/6971812 to your computer and use it in GitHub Desktop.
Save greenido/6971812 to your computer and use it in GitHub Desktop.
This is a simple Monte Carlo simulation to see whether our sale person should execute a strategy of 'many' big deals and 'few' small ones or vis versa.
/***************************************************************************
*
* This is a simple Monte Carlo simulation to see whether our sale
* person should execute a strategy of 'many' big deals and 'few' small ones
* or vis versa.
*
* Author: Ido Green | plus.google.com/+greenido
* Date: 16 July 2013
*
* *************************************************************************/
//Below are the important assumptions that drive the simulation
// The % chance of making 'big' deal
var bigDealPerc = 0.4;
// The % chance of making 'small' deal
var smallDealPerc = 0.5;
// the max amounts of deals we can do in X days
var amountOfDealsExecution = 50;
var amountBigDeals = 25;
var amountSmallDeals = 50;
// values per deal
var bigDeal = 30000;
var smallDeal = 10000;
//
// Run on the amount
//
function runSenarioBlending() {
try {
var maxAmount = 0;
for (var i=0; i < amountBigDeals; i++) {
var profit = runDealsBlending(i+3);
SpreadsheetApp.flush();
if (profit > maxAmount) {
maxAmount = profit;
}
}
var outStr = "The max amount: $" + maxAmount;
Browser.msgBox(outStr);
Logger.log(outStr);
} catch(err) {
Logger.log("Error: runSenarioBlending(): " + err);
}
//return maxAmount;
}
//
// We do X Big Deals and than the rest of Y Small Deals
//
function runDealsBlending(curLine) {
var winsBig = 0;
var lossBig = 0;
var winSmall = 0;
var lossSmall = 0;
var curTrial = 0;
var moneyWeMade = 0;
// Try the X big deals
for (var b=0; b < curLine-2; b++) {
if (winBigDeal()) {
// We got the Big deal!
moneyWeMade += bigDeal;
winsBig++;
}
else {
lossBig++;
}
}
// Try the rest of the deals on small ones
for (var s=0; s < amountSmallDeals && ((b*2)+s) < amountOfDealsExecution; s++) {
if (winSmallDeal()) {
// We got the Small deal!
moneyWeMade += smallDeal;
winSmall++;
}
else {
lossSmall++;
}
}
// update our spreadsheet
var curSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Deals");
curSheet.getRange("a"+curLine).setValue(winsBig);
curSheet.getRange("c"+curLine).setValue(winSmall);
curSheet.getRange("b"+curLine).setValue(lossBig);
curSheet.getRange("d"+curLine).setValue(lossSmall);
curSheet.getRange("e"+curLine).setValue(moneyWeMade);
return moneyWeMade;
};
//
// Determines if we win in a given trial when we aim for BIG deal
//
function winBigDeal() {
if (Math.random() < bigDealPerc) {
// We won big deal
return true;
}
return false;
};
//
// Determines if we win in a given trials when we aim for small deal
//
function winSmallDeal() {
if (Math.random() < smallDealPerc) {
// We won small deal
return true;
}
return false;
};
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [];
// When the user selects "addMenuExample" menu, and clicks "Menu Entry 1", the function function1 is executed.
menuEntries.push({name: "Run One Deal Simulation", functionName: "runDealSim"});
menuEntries.push({name: "Run Many Simulations", functionName: "runSenario"});
menuEntries.push({name: "Run Many Control Simulations", functionName: "runSenarioBlending"});
menuEntries.push({name: "Find Max Profit", functionName: "findMaxAmount"});
ss.addMenu("GDL Sim Example", menuEntries);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//
// For the next GDL IL
//
//////////////////////////////////////////////////////////////////////////////////////////////////
var trials = 25; //Number of trials to run for simulation
var senarios = 10; // num of senarios...
// Main function to find the max return from many simulations
function findMaxAmount() {
for (var i=0; i < senarios; i++) {
var amount = runSenarioBlending();
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Deals").getRange("e"+(40+i)).setValue(amount);
Logger.log(i + ") Amount: " + amount + " \n " );
}
}
function runSenario() {
for (var i=0; i < senarios; i++)
runDealSim(i+3);
}
//
function runDealSim(curLine) {
var winsBig = 0;
var lossBig = 0;
var winSmall = 0;
var lossSmall = 0;
var curTrial = 0;
var moneyWeMade = 0;
while (curTrial < trials) {
if (winBigDeal()) {
moneyWeMade += bigDeal;
winsBig++;
}
else {
lossBig++;
}
if (winSmallDeal()) {
moneyWeMade += smallDeal;
winSmall++;
}
else {
lossSmall++;
}
//Keep track of the number of trials
curTrial++;
}
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Deals").getRange("a"+curLine).setValue(winsBig);
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Deals").getRange("c"+curLine).setValue(winSmall);
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Deals").getRange("b"+curLine).setValue(lossBig);
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Deals").getRange("d"+curLine).setValue(lossSmall);
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Deals").getRange("e"+curLine).setValue(moneyWeMade);
};
////////////////////////
// init
//var trails = 10;
var cube = new Array(6);
function cleanCube() {
for(var j=0; j<6; j++) {
cube[j]=0;
}
}
//
function findProp() {
for (var i=0; i < trails; i++) {
runSim();
var luckyNum = cube[3];
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Prop").getRange("b"+(i+2)).setValue(luckyNum);
}
}
//
function runSim() {
cleanCube();
for (var i=0; i < trails; i++) {
var face = Math.floor((Math.random()*6)+1); // Give us a rand between 1-6
cube[face-1]++;
}
printResults();
}
function printResults() {
for(var j=2; j<8; j++) {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sim").getRange("c"+j).setValue(cube[j-2]);
}
Logger.log("The cube results: " + cube);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment