Adding PMF of a binomial to a rng coin flip graph
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Kylenino Espinas
am 7 Mär. 2022
Bearbeitet: Torsten
am 8 Mär. 2022
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?
0 Kommentare
Akzeptierte Antwort
Torsten
am 8 Mär. 2022
Bearbeitet: Torsten
am 8 Mär. 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
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Language Fundamentals finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!