The following built-in MATLAB functions and commands are permitted for this assignment.
Vector/Matrix
Flow Control
Strings and Character Arrays
Other
Points will be deducted from any programs using functions outside this list.
AI for any part of this problem.function_name.m, where each function_name is listed below.pHat_marginErr_w_CL
Inputs:
|
N (1x1) integer β number of samples or trials.
|
Outputs:
|
|
Details:
|
|
βΈ 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β).
|
|
Helpers:
|
z_from_CL
|
% 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
muHat_marginErr_w_CL
Inputs:
|
s (1x1) double β estimated sample standard deviation.
N (1x1) integer β number of samples or trials.
|
Outputs:
|
|
Details:
|
|
βΈ 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.
|
|
Helpers:
|
z_from_CL
|
% 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
mc_prob_H_heads_F_flips
Inputs:
|
H (1x1) integer β desired number of heads.
F (1x1) integer β number of coin flips per trial.
seed (1x1) integer β optional rng seed for reproducibility.
|
Outputs:
|
|
Details:
|
βΈ Use randi([0 1],*,*) to create a random row vector of ones and zeros, where 1 = heads and 0 = tails.
|
Helpers:
|
pHat_marginErr_w_CL
|
% 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
mc_approx_pi
Inputs:
|
seed (1x1) integer β optional rng seed for reproducibility.
|
Outputs:
|
simEstimates (1x1) struct containing the fields:
|
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.
|
Helpers:
|
pHat_marginErr_w_CL
|
% 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
mc_prob_die_roll_sum
Inputs:
|
D (1x1) integer β number of dice rolled per trial.
seed (1x1) integer β optional rng seed for reproducibility.
|
Outputs:
|
|
Details:
|
βΈ Use randi to generate a random 1x6 roll.
|
Helpers:
|
pHat_marginErr_w_CL
|
% 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
mc_sum_until_S
Inputs:
|
target_sum (1x1) double β threshold for the running sum of draws.
seed (1x1) integer β optional rng seed for reproducibility.
|
Outputs:
|
|
Details:
|
βΈ Use rand() to generate the random numbers between 0 and 1.
|
Helpers:
|
muHat_marginErr_w_CL
|
% 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
% 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
mc_prob_same_bday
Inputs:
|
P (1x1) integer β number of people in the group.
seed (1x1) integer β optional rng seed for reproducibility.
|
Outputs:
|
|
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.
|
|
Helpers:
|
pHat_marginErr_w_CL
|
% 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
mc_prob_dice_poker_rolls
get_face_counts, sort_by_rank
is_5Kind, is_4Kind, is_straight, ... ,is_1pair
Inputs:
|
seed (1x1) integer β random number generator seed.
|
Outputs:
|
simEstimates (1x1) struct containing the following fields:
where each confidene interval is formatted as β\(pΜ%\) Β± \(ME%\)β.
|
Details:
|
|
βΈ 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.
|
|
Helpers:
|
% 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%"