Adding PMF of a binomial to a rng coin flip graph

5 次查看(过去 30 天)
P = 23/36;
n = 10; %amount of flips done 1 million times
arr = zeros(1,100); %array to store heads in n flips
j = 1;
while j <= 100 %Repeating the 10 coin flips 100 times
heads = 0; %Reset heads counter
for i = 1:n %The 10 coin flips
U = rand(); %Rng
if U < 0.6388888888888 %Head/tail counter, biased coin toss probability
heads = heads + 1; %Amount of heads /10 tosses total counter
end
end
arr(j) = heads; %accessing point in array and adding one tick
j = j + 1; %ticker for while loop
end
hold on
x = 0:10
y = binornd(x,10,P); %generates red circles as the PMF of the coin toss
plot(x,y,'r.')
histogram(arr) %graphing
xlabel(sprintf('# of heads in %d coin flips',n))
hold off
So I have a code that is able to run a randomly generated coin flip 10 times repeated 100 times. However I would like red circles of PMF of binomial(10, P). I tried using different binomial functions but is there a specific way I would go about it?

采纳的回答

Torsten
Torsten 2022-3-8
编辑:Torsten 2022-3-8
P = 23/36;
n = 10; %amount of flips done 1 million times
arr = zeros(1,100000); %array to store heads in n flips
j = 1;
while j <= 100000 %Repeating the 10 coin flips 100 times
heads = 0; %Reset heads counter
for i = 1:n %The 10 coin flips
U = rand(); %Rng
if U < 0.6388888888888 %Head/tail counter, biased coin toss probability
heads = heads + 1; %Amount of heads /10 tosses total counter
end
end
arr(j) = heads; %accessing point in array and adding one tick
j = j + 1; %ticker for while loop
end
hold on
x = 0:10
y = binopdf(x,10,P); %generates red circles as the PMF of the coin toss
plot(x,y,'r.','MarkerSize',15)
histogram(arr,x,'Normalization','probability')
xlabel(sprintf('# of heads in %d coin flips',n))
hold off

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Language Fundamentals 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by