%
%
Programming Assignment: Probability
%%
%
Instructions
% ————
%
% This file contains code that helps you get started on the
% probability assignment.
%
% For this part of the exercise, you will need to change some
% parts of the code below for various experiments.
%
% Initialization
rng(“default”);
%% ================ Part 1: Toss a coin ===================
% Instructions: Define a random variable called “coin” that represents the coin,
% i.e., it can take two possible values, 0 (tail)
% and 1 (head), with the probability of taking each
% value being 0.5.
% Then, complete the following steps.
% Step 1: toss a coin once, and show the value of the
% coin
% Step 2: toss a coin for 10 times, count the
% number of heads. Store the number of
% heads in variable “c” and print its value
% ============================================================
fprintf(‘===== Part 1: Toss a coin ===== \n’);
% ====================== YOUR CODE HERE ======================
% Step 1: toss a coin, and show the value of the coin
fprintf(‘Toss a coin …\n’);
fprintf(‘The value of the coin: \n %s\n’, coin);
% Step 2: toss a coin for 10 times and count the number of heads
% Store the number of heads in variable “c” and print its value
fprintf(‘Toss a coin for 10 times …\n’);
fprintf(‘The number of heads: \n %d\n’, c);
% ============================================================
fprintf(‘Program paused. Press enter to continue.\n’);
pause;
%% ================ Part 2: Roll a die ================
% Instructions: Define a random variable “die” that represents the die,
% i.e., it can take six possible values, 0 – 6, with
% the probability of taking each value being 1/6.
% Then, complete the following steps.
% Step 1: roll a die once
, and show its value
% Step 2: roll a die for 10 times and make a histogram
% showing the distribution (Hint: can use
% function “hist()” to plot the histogram)
% Step 3: roll a die for 10000 times, make a histogram,
% and then plot an empirical cdf (Hint: can use
% function “stairs()” to plot the cdf)
% ============================================================
fprintf(‘===== Part 2: Roll a die ===== \n’);
% ====================== YOUR CODE HERE ======================
% Step 1: roll a die once
fprintf(‘Roll a die …\n’);
fprintf(‘The value of the die: \n %s\n’, die);
% Step 2: roll a die 10 times and make a histogram
fprintf(‘Roll a die 10 times …\n’);
fprintf(‘Showing the histogram of the die…\n’);
% Step 3: roll a die 10000 times, make a histogram, and plot the cdf
fprintf(‘Roll a die 10000 times …\n’);
fprintf(‘Showing the empiral cdf of the die…\n’);
% ============================================================fprintf(‘Program paused. Press enter to continue.\n’);pause;
%% ================ Part 3: Plot a normal distribution ================
% Instructions: Generate 10000 random samples from the normal distribution
% with mean = 1 and variance = 4 (Hint: use function “rand()”).
% Define the variable name as “ND”, e.g.: ND = ….
% Then complete the following:
% Step 1: Make a histogram to show the distribution.
% Step 2: Fit a probability density function (normal distribution)
% to the data and plot the pdf superimposed over a histogram
% of the data (Hint: use function “fitdist()”)
% ============================================================
fprintf(‘===== Part 3: Plot a normal distribution ===== \n’);
% ====================== YOUR CODE HERE ======================% Step 1: Make a histogram to show the distribution.
fprintf(‘Showing the histogram of the data …\n’);
% Step 2: Fit a probability density function to the data and plot the pdf
% superimposed over a histogram of the data (Hint: use function “fitdist()”)
fprintf(‘Showing the pdf created by fitting a normal distribution to the data …\n’);
% ============================================================