- Mar 2, 2006
- 5,015
- 0
- 0
Prompt: Suppose you have two coins , one biased one fair, but you don't know which. Coin 1 is biased. It comes up heads with probability 3/4, while coin 2 will flip heads with probability 1/2. Suppose you pick a coin at random and flip it. Let Ci denote the event that coin i is picked (Ci = 0 coin two, Ci = 1 coin one). Let Hi denote whether it was heads or tails (Hi = 1 heads, Hi = 0 tails)
My code:
C = rand(1,50)<.5 %Generates 50 trials of picking each coin (50%)
i = 1;
while (i <= 50) %Repeat 50 times
if (C(i) == 0) %If coin two is picked, then generate one flip of fair coin
H(i) = rand(1)<.5 %
i = i + 1; %Increment vector location in C and H
else
H(i) = rand(1)>.25 %Else, generate one flip of biased coin (75%)
i = i + 1; %Increment vector location in C and H
end
end
Look good? The purpose is to be able to simulate the experiment 50 times (or more if I would like to).
edit* Comments ignore the spacing of comments, can't fix it and too lazy too. It looks fine in my original m file
My code:
C = rand(1,50)<.5 %Generates 50 trials of picking each coin (50%)
i = 1;
while (i <= 50) %Repeat 50 times
if (C(i) == 0) %If coin two is picked, then generate one flip of fair coin
H(i) = rand(1)<.5 %
i = i + 1; %Increment vector location in C and H
else
H(i) = rand(1)>.25 %Else, generate one flip of biased coin (75%)
i = i + 1; %Increment vector location in C and H
end
end
Look good? The purpose is to be able to simulate the experiment 50 times (or more if I would like to).
edit* Comments ignore the spacing of comments, can't fix it and too lazy too. It looks fine in my original m file