Adding PMF of a binomial to a rng coin flip graph

2 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Torsten
Torsten il 8 Mar 2022
Modificato: Torsten il 8 Mar 2022
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

Più risposte (0)

Categorie

Scopri di più su Lighting, Transparency, and Shading in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by