Lesson 18: Monte Carlo Simulation
April 13, 2016
Wed Apr 13
Review:
- Multiple Regression in Python
Presentation:
- Monte Carlo Simulation
- Resources
- Sample Code
Assignment:
- Create a simple Monte Carlo Simulation with Python
- Simulate a simple Dice Game:
- Roll 2-7, lose
- Roll 8-12, win
- Start with $100
- bet $1 per roll (win pays $1, loss takes $1 bet)
- roll 100 times and find ending balance (could be negative)
- simulate 100 outcomes (10,000 total rolls of the dice)
- Report the mean, median, and standard deviation of your final balances
- Repeat using 1,000 rolls per outcome (100,000 total rolls of the dice) and report the mean, median and standard deviation of final outcomes
[highlight color=”options: yellow, black”]Here’s some python code to help you get going (and plain text below): [/highlight]
import random
def rollDice():
roll = random.randint(1,6)
return roll
j = 0
rollcount = 0
rolls = []
while j < 100:
i = 0
j = j + 1
while i < 100:
i = i + 1
result = rollDice()
rolls.append(result)
rollcount += 1
import scipy
mr = scipy.mean(rolls)
print(“Total rolls = “)
print rollcount
print(“Mean roll = “)
print mr
2 Comments
[…] Previous | Next […]
[…] Previous | Next […]