This is the final project of Intro to Probability (Part 1)
import random
import pandas as pd
Imagine that we will play a role-playing game. First, we need to know how throw a dice.
Try to do alone:
def dice():
return random.randint(1,6)
dice()
Suppose that you attack your enemy, to do this you will throw a certain number of dice. The attack will be the sum of your throw. Define a function "attack" to calculate this amount depending on the number of dice.
def attack(number_of_dice):
return sum([dice() for _ in range(number_of_dice)])
attack(3)
Now that we know how to attack, we begin a battle.
We call "enemy_armour" to the number that represent the power of the armour of your enemy and "number_of_dice" to the amount of dice you throw when you attack.
Define a function "battle" that depends on this two values ("enemy_armour" and "number_of_dice") and returns true if you win the battle (if there is a tie, you lose) and false otherwise.
def battle(number_of_dice,enemy_armour):
'''Returns True if you beat the enemy and False otherwise'''
return attack(number_of_dice)>enemy_armour
battle(4,18)
Let's try some specific battles. Suppose that the enemy has an armour with power 8 and you attack with 2 dice. How is the battle? Define a function "battle_1" to model this battle.
def battle_1():
return battle(2,8)
Now, what if the enemy has an armour with power 12 and you attack with 3 dice? Define a function "battle_2" to model this one.
def battle_2():
return battle(3,12)
From the two previous battles, which one will be more likely for you to win? Let's try to calculate the empirical probability of winning each.
To do so, let's define a function "empiric_prob" that receives an experiment (in this case we want to use battle_1 and battle_2 as our experiments) and a number of trials "n" (with a default value of 10000), and returns the empirical probability of winning that experiment.
def empiric_prob(experiment,n=10000):
return sum([experiment() for _ in range(n)])/n
empiric_prob(battle_1)
empiric_prob(battle_2)
So, according to the empirical probability we are more likely to win battle_1. But it is a close call, could it be that the number of trials is influencing the results?
To be sure, let's compare the empirical probability with the theoretical:
We begin with battle_1, of 2 dice and 8 in the enemy armour. Then we need that the sum of the dice is greater than 8.
Let's calculate how many of all the 36 possible outcomes are grater than 8.
amount = 0
for x in range(1,7):
for y in range(1,7):
if x+y>8:
amount=amount+1
print(amount)
Then we have $\frac{10}{36}=\frac{5}{18}$, which is:
5/18
If we compare this amount with the empirical probability, the difference is quite low.
abs(empiric_prob(battle_1)-5/18)
It's close to "empiric_prob(battle_1)", but it could be better. What could we change to make the empirical probability a better approximation?
abs(empiric_prob(battle_1,100000)- 5/18)
We can do a table to compare the results with different values for n:
times = [10000, 100000, 1000000, 10000000]
experiments = []
for t in times:
experiments.append(empiric_prob(battle_1,t))
dif = []
for i in range(len(times)):
dif.append(abs(experiments[i]-5/18))
data = {'Times':['10000', '100000', '1000000', '10000000'], 'Empirical Probability': experiments,
'Difference': dif}
df = pd.DataFrame(data)
df
Try to do the same comparison with battle_2
amount = 0
for x in range(1,7):
for y in range(1,7):
for z in range(1,7):
if x+y+z>12:
amount=amount+1
times = [10000, 100000, 1000000, 10000000]
experiments = []
for t in times:
experiments.append(empiric_prob(battle_2,t))
dif = []
for i in range(len(times)):
dif.append(abs(experiments[i]-amount/6**3))
data = {'Times':['10000', '100000', '1000000', '10000000'], 'Empirical Probability': experiments,
'Difference': dif}
df = pd.DataFrame(data)
df