[Solved] EE381 Lab 1 -Stochastic_Experiments

$25

File Name: EE381_Lab_1__Stochastic_Experiments.zip
File Size: 329.7 KB

SKU: [Solved] EE381 Lab 1 -Stochastic_Experiments Category: Tag:
5/5 - (1 vote)

As mentioned in class, 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/Nprint(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/Nprint(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 matplotlibimport matplotlib.pyplot as plt# def sum2dice(N):d1=randint(1,7,N) d2=randint(1,7,N) s=d1+d2b=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=3p=array([0.3, 0.6, 0.1]) cs=cumsum(p) cp=append(0,cs) r=rand() for k in range(0,n): if r>cp[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 matplotlibimport 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 r>cp[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/Nplt.stem(b1,prob)# Graph labelsplt.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:

  • 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.
  • You repeat the experiment N=100,000 times. Each time you keep track of the number of rolls it takes to have success.

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

3. Getting 50 heads when tossing 100 coins

Consider the following experiment:

  • You toss 100 fair coins and record the number of heads. This is considered a single experiment. If you get exactly 50 heads, the experiment is considered a success.
  • You repeat the experiment N=100,000 times. After the N experiments are completed count the total successes, and calculate the probability of success, i.e. the probability of getting exactly 50 heads.

SUBMIT a report that follows the guidelines as described in the syllabus.

  • The section on RESULTS must include the calculated answer. Use the table below for your answer. Note: You will need to replicate the table in your Word file, in order to provide the answer in your report. Points will be taken off if you do not use the table.
  • The code must be provided in the appendix
Probability of 50 heads in tossing 100 fair coins
Ans. p = 0.0798

4. The Password Hacking Problem

Your computer system uses a 4-letter password for login. For our purposes the password is restricted to lower case letters of the alphabet only. It is easy to calculate that the total number of passwords which can be produced is n= 264.

  • A hacker creates a list of random 4-letter words, as candidates for matching the password. Note that it is possible that some of the words may be duplicates. The number that you must use has been given to you.
  • You are given your own 4-letter password and you are going to check if the hackers list contains at least one word that matches your password. This process of checking is considered one experiment. If a word in the list matches your password, the experiment is considered a success. Repeat the experiment for N=1000 times and find the probability that at least one of the words in the hackers list will match your password.
  • The hacker creates a longer list of k* random 4-letter words. The numbers k and have been given to you. Repeat the previous experiment for

N=1000 times and find the probability that at least one of the words in the hackers list will match your password.

  • Repeat the previous experiment for N=1000times to find the approximate number (m) of words that must be contained in the hackers list so that the probability of at least one word matching the password is

p= 0.5. You should do this by trial and error: assume a value for (m) and

calculate the corresponding probability as you did in the previous part. The answer will be value of (m) that makes this probability approximately equal to p= 0.5.

SUBMIT a report that follows the guidelines as described in the syllabus.

  • The section on RESULTS must include the calculated answer. Use the table below for your answer. Note: You will need to replicate the table in your Word file, in order to provide the answer in your report. Points will be taken off if you do not use the table.
  • The code must be provided in the appendix
Hacker creates wordsProb. that at least one of the words matches the password p = 0.1965
Hacker creates k* wordsProb. that at least one of the words matches the password p = 0.8879
p= 0.5Approximate number of words in the list m = 317,000

5. References

[1] Introduction to Probability, by H. Pishro-Nik. Available online at:

https://www.probabilitycourse.com

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] EE381 Lab 1 -Stochastic_Experiments[Solved] EE381 Lab 1 -Stochastic_Experiments
$25