Skip to main content

Section Homework 5

Before you begin ...

Permitted MATLAB Functions & Commands for Homework 5.

The following built-in MATLAB functions and commands are permitted for this assignment.
Vector/Matrix
Operations:
round β€’ mod β€’ floor β€’ ceil
Size/Dimensions:
length β€’ numel β€’ size β€’ height β€’ width
Creation:
zeros β€’ ones β€’ true β€’ false
Stats:
sum β€’ max β€’ min β€’ mean β€’ std
Logical:
all β€’ any
Flow Control
Conditional:
if β€’ switch-case β€’ try-catch
Loops:
for β€’ while β€’ continue β€’ break β€’ return
Strings and Character Arrays
Operations:
join β€’ replace
Conversion:
num2str β€’ str2num β€’ str2double β€’ string β€’ char
Other
Printing Text:
fprintf β€’ disp β€’ display β€’ input β€’ assert
Random Generators:
rand β€’ randi β€’ rng
Special Variables:
nargin
Type Detection:
isnan β€’ isfield β€’ isempty
Points will be deducted from any programs using functions outside this list.

Summary.

The focus of this assignment is on approximating different quantities using the concept of Monte Carlo Simulation.

Subsection pHat_marginErr_w_CL

This helper function computes the margin of error for a sample proportion estimate \(\hat{p}\) given the number of trials \(N\) and a desired confidence level.
Inputs:
pHat (1x1) double β€” estimated sample proportion (\(\hat{p}\)).
N (1x1) integer β€” number of samples or trials.
CL (1x1) double β€” desired confidence level. Default: 0.95.
Outputs:
ME (1x1) double β€” CL margin of error for sample proportion, \(\hat{p}\text{.}\)
Details:
β–Έ The critical value, z, is obtained from z_from_CL(CL).
β–Έ The margin of error formula is \(\text{ME} = z \sqrt{\frac{\hat{p}(1 - \hat{p})}{N}}\text{.}\)
β–Έ This calculation should be used for Monte Carlo simulations involving binary outcomes (β€œhit” vs β€œmiss”, β€œyes” vs β€œno”).
β–Έ Special nargin input handling:
  • If CL is not provided, set CL = 0.95.
Helpers:
z_from_CL
Examples:
% Example 1: Margin of error for pHat = 0.6, N = 1000
ME = pHat_marginErr_w_CL(0.6, 1000)
% Returns ME = 0.0304
% Example 2: 99% confidence level
ME = pHat_marginErr_w_CL(0.25, 500, 0.99)
% Returns ME = 0.0499

Subsection muHat_marginErr_w_CL

This helper function computes the margin of error for a sample mean estimate \(\hat{\mu}\) based on the sample’s standard deviation and a desired confidence level.
Inputs:
s (1x1) double β€” estimated sample standard deviation.
N (1x1) integer β€” number of samples or trials.
CL (1x1) double β€” desired confidence level. Default: 0.95.
Outputs:
ME (1x1) double β€” CL margin of error for sample mean, \(\hat{\mu}\text{.}\)
Details:
β–Έ The critical value, z, is obtained from z_from_CL(CL).
β–Έ The margin of error formula is \(\text{ME} = z \frac{s}{\sqrt{N}}\text{.}\)
β–Έ This calculation should be used for continuous-valued Monte Carlo experiments where each trial produces a numeric measurement.
β–Έ Special nargin input handling:
  • If CL is not provided, set CL = 0.95.
Helpers:
z_from_CL
Examples:
% Example 1: Margin of error for s = 0.5, N = 100
ME = muHat_marginErr_w_CL(0.5, 100)
% Returns ME = 0.0980
% Example 2: 99% confidence level
ME = muHat_marginErr_w_CL(0.1, 1000, 0.99)
% Returns ME = 0.0081

Subsection mc_prob_H_heads_F_flips

This function uses Monte Carlo simulation to estimate the probability of obtaining exactly \(H\) heads when flipping a fair coin \(F\) times.
Inputs:
H (1x1) integer β€” desired number of heads.
F (1x1) integer β€” number of coin flips per trial.
N (1x1) integer β€” number of experiment trials. Default: 1e5
seed (1x1) integer β€” optional rng seed for reproducibility.
Outputs:
simEstimates (1x1) struct containing the fields:
  • N (1x1) integer β€” number of experiment trials.
  • prob (1x1) double β€” estimated probability of getting exactly \(H\) heads in \(F\) flips.
  • ME_CL95 (1x1) double β€” confidence interval margin of error.
Details:
β–Έ Use randi([0 1],*,*) to create a random row vector of ones and zeros, where 1 = heads and 0 = tails.
β–Έ Special nargin input handling:
  • If seed is provided, set the random number generator to this seed using the command: rng(seed,'twister')
  • If only two inputs, H & F, are provided, set N=1e5.
Helpers:
pHat_marginErr_w_CL
Examples:
% Example 1: Estimate P(5 heads in 5 flips)
simEstimates = mc_prob_H_heads_F_flips(5, 5, 5000)
% simEstimates = 
% 
%   struct with fields:
% 
%           N: 5000
%        prob: 0.0324
%     ME_CL95: 0.0049
% Example 2: P(3 heads in 2 flips) = 0 (Impossible)
simEstimates = mc_prob_H_heads_F_flips(3, 2, 100)
% simEstimates = 
% 
%   struct with fields:
% 
%           N: 100
%        prob: 0
%     ME_CL95: 0
% Example 3: P(10 heads in 13 flips) with seed=33
simEstimates = mc_prob_H_heads_F_flips(10, 13, 1000, 33)
% simEstimates = 
% 
%   struct with fields:
% 
%           N: 1000
%        prob: 0.0410
%     ME_CL95: 0.0123

Subsection mc_approx_pi

This function uses Monte Carlo simulation to estimate the value of \(\pi\text{.}\)
Inputs:
N (1x1) integer β€” number of experiment trials. Default: 1e5
seed (1x1) integer β€” optional rng seed for reproducibility.
Outputs:
simEstimates (1x1) struct containing the fields:
  • approxPi (1x1) double β€” Monte Carlo estimate of \(\pi\)
  • N (1x1) integer β€” number of experiment trials.
  • pHat (1x1) double β€” the estimated quantity used to approximate approxPi.
  • ME_CL95 (1x1) double β€” confidence interval margin of error.
Details:
β–Έ To code this, slightly alter the in-class demonstration where we approximated the area of a circle of radius r. Think about the connection of \(\pi\) to this demo.
β–Έ Handle the special inputs options as follows:
  • If seed is provided, set the random number generator to this seed using the command: rng(seed,'twister')
  • If no inputs are provided, set N=1e5 (i.e., \(10^5\)).
β–Έ Use rand() to approximate the random \(x\) and \(y\) values.
Helpers:
pHat_marginErr_w_CL
Examples:
% Example 1: N=10^5 with seed
results = mc_approx_pi(5000, 10)
% results = 
% 
%   struct with fields:
% 
%     approxPi: 3.1680
%            N: 5000
%         pHat: 0.7920
%      ME_CL95: 0.0113
% Example 2: No inputs, using the default N and no seed
approx_pi = mc_approx_pi()
% results = 
% 
%   struct with fields:
% 
%     approxPi: 3.1422
%            N: 100000
%         pHat: 0.7855
%      ME_CL95: 0.0025
Note: since no seed was used in Example 2 your values will be different.

Subsection mc_prob_die_roll_sum

This function uses Monte Carlo simulation to estimate the probability that the sum of \(D\) fair six-sided dice equals a specified target value.
Inputs:
D (1x1) integer β€” number of dice rolled per trial.
tar_sum (1x1) integer β€” target total (desired sum of the D dice).
N (1x1) integer β€” number of experiment trials. Default: 1e5
seed (1x1) integer β€” optional rng seed for reproducibility.
Outputs:
simEstimates (1x1) struct containing the fields:
  • N (1x1) integer β€” number of experiment trials.
  • prob (1x1) double β€” \(\hat{p}\) Estimate of \(\mathbb{P}(\text{sum} = \text{tar_sum})\text{.}\)
  • ME_CL95 (1x1) double β€” 95% CI margin of error for prob.
Details:
β–Έ Use randi to generate a random 1x6 roll.
β–Έ Special nargin input handling:
  • If seed is provided, set the random number generator using rng(seed,'twister').
  • If only D and tar_sum are provided (no N), set N = 1e5.
Helpers:
pHat_marginErr_w_CL
Examples:
% Example 1: Estimate P(sum = 7) for two dice
simEstimates = mc_prob_die_roll_sum(2, 7, 10000)
% simEstimates = 
% 
%   struct with fields:
% 
%           N: 10000
%        prob: 0.1658
%     ME_CL95: 0.0073
% Example 2: Reproducible estimate with fixed seed
simEstimates = mc_prob_die_roll_sum(3, 10, 2e6, 42)
% simEstimates = 
% 
%   struct with fields:
% 
%           N: 2000000
%        prob: 0.1254
%     ME_CL95: 4.5891e-04

Subsection mc_sum_until_S

This function using Monte Carlo simulation to estimate the expected number of uniformly distributed random samples (between 0 & 1) required for their cumulative sum to exceed a target value.
Inputs:
target_sum (1x1) double β€” threshold for the running sum of draws.
N (1x1) integer β€” number of experiment trials. Default: 1e5
seed (1x1) integer β€” optional rng seed for reproducibility.
Outputs:
simEstimates (1x1) struct with fields:
  • N (1x1) integer β€” number of trials run.
  • avgSamples (1x1) double β€” sample mean of how many random numbers are needed to exceed target_sum.
  • ME_CL95 (1x1) double β€” 95%-margin of error for avgSamples.
Details:
β–Έ Use rand() to generate the random numbers between 0 and 1.
β–Έ Special nargin input handling:
  • If seed is provided, initialize the RNG with rng(seed,'twister').
  • If only target_sum is provided, set N = 1e5.
Helpers:
muHat_marginErr_w_CL
Examples:
% Example 1: Expected draws to reach sum β‰₯ 1
simEstimates = mc_sum_until_S(1, 100000)
% simEstimates = 
% 
%   struct with fields:
% 
%              N: 100000
%     avgSamples: 2.7126
%        ME_CL95: 0.0054
πŸ€”πŸ’­ What does this average πŸ‘† appear to be close to?
% Example 2: Reproducible run with fixed seed
simEstimates = mc_sum_until_S(3.0, 200000, 31415)
% simEstimates = 
% 
%   struct with fields:
% 
%              N: 200000
%     avgSamples: 6.6642
%        ME_CL95: 0.0065

Subsection mc_prob_same_bday

This function uses Monte Carlo simulation to estimate the probability that, in a group of \(P\) people, at least two share the same birthday.
Inputs:
P (1x1) integer β€” number of people in the group.
N (1x1) integer β€” number of experiment trials. Default: 1e5
seed (1x1) integer β€” optional rng seed for reproducibility.
Outputs:
simEstimates (1x1) struct containing the fields:
  • N (1x1) integer β€” number of experiment trials.
  • prob (1x1) double β€” estimated probability that at least two of the P people share a birthday.
  • ME_CL95 (1x1) double β€” 95% CI margin of error for prob.
Details:
β–Έ Each trial simulates a group of P people where each person has random birthday (integer from 1 to 365, assuming no leap years).
β–Έ Use randi to generate the random birthdays.
β–Έ Special nargin input handling:
  • If seed is provided, initialize the random number generator with rng(seed,'twister').
  • If only P is provided, set N = 1e5.
Helpers:
pHat_marginErr_w_CL
Examples:
% Example 1: Estimate probability for 23 people (the classic case)
simEstimates = mc_prob_same_bday(23, 100000)
% simEstimates = 
% 
%   struct with fields:
% 
%           N: 100000
%        prob: 0.5081
%     ME_CL95: 0.0031
% Example 2: Reproducible simulation with fixed seed
simEstimates = mc_prob_same_bday(30, 500000, 123)
% simEstimates = 
% 
%   struct with fields:
% 
%           N: 500000
%        prob: 0.7075
%     ME_CL95: 0.0013

Subsection mc_prob_dice_poker_rolls

For this problem, you will need the following dice poker helper functions you wrote in Homework 3 and 4:
  • get_face_counts, sort_by_rank
  • is_5Kind, is_4Kind, is_straight, ... ,is_1pair
Make sure the programs, below, can see them in you working folder.
This function uses Monte Carlo simulation to estimate the probability of the eight possible 5-dice poker hand ranks.The estimates are summarized as confidence interval strings for each hand rank.
Inputs:
N (1x1) integer β€” number of simulated 5-dice rolls. Default: 1e5.
seed (1x1) integer β€” random number generator seed.
Outputs:
simEstimates (1x1) struct containing the following fields:
  • fiveKind (1x1) string β€” CI for a five-of-a-kind.
  • fourKind (1x1) string β€” CI for a four-of-a-kind.
  • straight (1x1) string β€” CI for a straight.
  • fullhouse (1x1) string β€” CI for a full-house.
  • threeKind (1x1) string β€” CI for a three-of-a-kind.
  • twoPair (1x1) string β€” CI for a two-pair.
  • onePair (1x1) string β€” CI for a one-pair.
  • singles (1x1) string β€” CI for a singles.
where each confidene interval is formatted as β€œ\(pΜ‚%\) Β± \(ME%\)”.
Details:
β–Έ Use randi to generate each roll. Don’t call roll_dice.
β–Έ Hard code the confidence levels to 95% and use your pHat_marginErr_w_CL to find the margin of error for each estimate.
β–Έ Results are formatted as percentages with one decimal place for pHat and two decimals for ME. Use round to set the decimals.
β–Έ Handle the special inputs options as follows:
  • If seed is provided, set the rng to this seed as usual.
  • If no inputs are provided, set N=1e5.
Helpers:
get_face_counts, sort_by_rank, get_rank, pHat_marginErr_w_CL
Examples:
% Example 1: Run a default simulation
simEstimates = mc_prob_dice_poker_rolls()
% Returns: 
% 
%   struct with fields:
% 
%      fiveKind: "0.1% Β± 0.02%"
%      fourKind: "1.9% Β± 0.08%"
%      straight: "3.8% Β± 0.12%"
%     fullhouse: "3% Β± 0.11%"
%     threeKind: "15.7% Β± 0.23%"
%       twoPair: "23.1% Β± 0.26%"
%       onePair: "46.1% Β± 0.31%"
%       singles: "6.1% Β± 0.15%"
% Example 2: Reproducible results with fixed seed
simEstimates = mc_prob_dice_poker_rolls(500, 123)
% Returns:
% 
%   struct with fields:
% 
%      fiveKind: "0.4% Β± 0.55%"
%      fourKind: "1.6% Β± 1.1%"
%      straight: "4% Β± 1.72%"
%     fullhouse: "2.6% Β± 1.39%"
%     threeKind: "16.8% Β± 3.28%"
%       twoPair: "22.8% Β± 3.68%"
%       onePair: "43.8% Β± 4.35%"
%       singles: "8% Β± 2.38%"