Could anyone help me to do this question Use Monte Carlo exp
Could anyone help me to do this question?
Use Monte Carlo experiment to determine the probability that HHT appears before HTT in a sequence of independent coin tossing with a balanced coin. Make sure you report your result too.
Thank you guys so much!
Solution
Both the events are equiprobable. Probability of them occuring is 0.125. I will be using MATLAB to answer this question.
clear all %Clears all variables from memory
clc %Clears terminal
N = 1000000; %Total number of simulations
%Algorithm is as follows. You toss a coin thrice. If you get a sequence HHT
%append its count variable by 1, if you get a sequence HTT append its count
%variable by 1. We assume that the event H occurs when a random number is
%greater than 0.5. If the random number is less than 0.5, then T occurs.
hht_count = 0;
htt_count = 0;
for(i=1:N)
trial = rand(3,1); %Generates uniform random number
%We now check for the sequences
if((trial(1) > 0.5) && (trial(2) > 0.5) && (trial(3) <= 0.5))
hht_count = hht_count + 1;
end
if((trial(1) > 0.5) && (trial(2) <= 0.5) && (trial(3) <= 0.5))
htt_count = htt_count + 1;
end
end
probability_hht = hht_count / N
probability_htt = htt_count / N
