6. 15 points) We are rolling a fair die, and keeping a running sum (that is, the
ID: 3203270 • Letter: 6
Question
6. 15 points) We are rolling a fair die, and keeping a running sum (that is, the sum of all of the results so far). Let X be the number of rolls it takes for this sum to be 100. The pmf for X is complicated; in this question, we will esti ate it through simulation using MATLAB lm. Note that X S 100. Here is a summary of what we will do Allocate an array of length 100 called px that will hold our estimates of px(k) for k 31,..., 100. (Note that the first 16 entries will always remain 0.) Set the number of experiments Q. Each experiment consists of rolling the die until the sum is 100 Loop over Q iterations. At each iteration. we set the roll count x 0 and the running sum sum 0, then generate random die rolls, incrementing sum by the result and x by one after each, until sum 100. This inner loop is easily accomplished with a while loop. A single die roll can be simulated using2 ceil (6*rand(1)) Increment the corresponding entry of px.Explanation / Answer
Code is below:
% Experimental pmf of number of rolls it takes to get a sum >= 100
%% this code is simple but slowtic
Q = 100000;
pX = zeros(100,1);
for qq = 1:Q
runningSum = 0;
x = 0;
while (runningSum < 100)
runningSum = runningSum + ceil(6*rand(1));
x = x + 1;
end
pX(x) = pX(x) + 1;
end
pX = pX/Q;
toc
%% this code is much faster, but harder to understandtic
Q = 100000;
pX = zeros(100,1);
for qq = 1:Q
cP = cumsum(ceil(6*rand(100,1)));
x = find(cP >= 100, 1);
pX(x) = pX(x) + 1;
end
pX = pX/Q;
toc stem(pX,’LineWidth’,2);
set(gca,’FontSize’,20);
xlabel(’x’,’FontSize’,20); ylabel(’p_X(x)’,’FontSize’,20);
EXECUTE THE CODE AND YOU WILL GET THE PLOT
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.