1. Simulating Coin Toss Experiments
There are many ways to model stochastic experiments. The following two programs simulate the toss of a fair coin N times, and calculate the experimental probability of getting heads (p_heads) or tails (p_heads). Both programs provide the same results, but they differ in the way the models are coded.
• The first model is programmed in Python using "for loops".
• The second model makes use of the arrays, and it is computationally very efficient.
MODEL 1
import numpy as np def coin(N):
heads, tails = 0, 0 for k in range(0,N): coin=randint(0,2) if coin==1:
heads=heads+1 else:
tails=tails+1
#
p_heads=heads/N p_tails=tails/N
print('probability of heads = ', p_heads) print('probability of tails = ', p_tails)
MODEL 2 – MORE EFFICIENT CODE
import numpy as np def coin2(N):
coin=randint(0,2,N) heads=sum(coin) tails=N-heads
#
p_heads=heads/N p_tails=tails/N
print('probability of heads = ', p_heads) print('probability of tails = ', p_tails)
0.2. Roll of Two Fair Dice; Probability Mass Function (PMF)
This experiment models the roll of a pair of dice for N times. The sum each roll is recorded, and stored in vector "s". The probability of each possible outcome is calculated and plotted in a "Probability Mass Function" (PMF) plot
To create the plots, the simulation has been run for N=100000 times.
SUM OF THE ROLLS OF TWO FAIR DICE
import numpy as np import matplotlib
import matplotlib.pyplot as plt
# def sum2dice(N):
d1=randint(1,7,N) d2=randint(1,7,N) s=d1+d2
b=range(1,15) ; sb=size(b) h1, bin_edges = histogram(s,bins=b) b1=bin_edges[0:sb-1] close('all')
#
fig1=plt.figure(1) plt.stem(b1,h1)
plt.title('Stem plot - Sum of two dice') plt.xlabel('Sum of two dice') plt.ylabel('Number of occurrences') fig1.savefig('1 EE381 Proj Stoch Exper-1.jpg')
#
fig2=plt.figure(2) p1=h1/N plt.stem(b1,p1)
plt.title('Stem plot - Sum of two dice: Probability mass function') plt.xlabel('Sum of two dice')
plt.ylabel('Probability')
fig2.savefig('1 EE381 Proj Stoch Exper-2.jpg')
0.3. Generating an unfair three-sided die
This example models the roll of a 3-sided die, with non-uniform probabilities. The die has three sides [1,2,3] with probabilities: [p p p1, 2, 3] = [0.3, 0.6, 0.1].
The experiment simulates the roll of the die for N=10,000 times, and the outcome of the N rolls is plotted as a stem plot. The stem plot verifies that the three sides of the die follow the required probabilities.
The following code will simulate a single roll of the three-sided die. The variable “d” represents the number after the roll.
import numpy as np
# n=3
p=array([0.3, 0.6, 0.1]) cs=cumsum(p) cp=append(0,cs) r=rand() for k in range(0,n): if rcp[k] and r<=cp[k+1]:
d=k+1
The following code will simulate the rolling of the three-sided die for N=10,000 times and will plot the outcome as a stem plot.
import numpy as np import matplotlib
import matplotlib.pyplot as plt # def ThreeSidedDie(p):
N=10000 s=zeros((N,1)) n=3
# p=array([0.3, 0.6, 0.1]) cs=cumsum(p) cp=append(0,cs) # for j in range(0,N):
r=rand() for k in range(0,n): if rcp[k] and r<=cp[k+1]:
d=k+1 s[j]=d
#
# Plotting b=range(1,n+2) sb=size(b)
h1, bin_edges=histogram(s, bins=b) b1=bin_edges[0:sb-1] close('all') prob=h1/N
plt.stem(b1,prob)
# Graph labels
plt.title('PMF for an unfair 3-sided die') plt.xlabel('Number on the face of the die') plt.ylabel('Probability') plt.xticks(b1)
1. Function for a n-sided die
Write a function that simulates a single roll of a n-sided die. The inputs and outputs of the function are: Inputs:
• The probabilities for each side, given as a vector p p p= [ 1, 2,Lpn]
Outputs:
• The number on the face of the die after a single roll, i.e. one number from the set of integers {1,2,Ln}
Note: The sum p p1 + +2 Lpn must be equal to 1.0, otherwise the probability values are incorrect.
Save the function as: nSidedDie(p)
Test the function using the probability vector p p p= [ 1, 2,Lpn] which has been given to you. To create a random number with a single roll of the die you must use the following command: r=nSidedDie(p)
To validate your function, roll the die for N=10,000 times and plot the outcome as a stem plot.
SUBMIT a report that follows the guidelines as described in the syllabus.
Ø The section on RESULTS must include The PMF in the form of a stem plot Ø The code must be provided in the appendix
2. Number of rolls needed to get a "7" with two dice
Consider the following experiment:
o You roll a pair of fair dice and calculate the sum of the faces. You are interested in the number of rolls it takes until you get a sum of "7". The first time you get a "7" the experiment is considered a "success". You record the number of rolls and you stop the experiment.
o You repeat the experiment N=100,000 times. Each time you keep track of the number of rolls it takes to have "success".