필터 지우기
필터 지우기

Adding PMF of a binomial to a rng coin flip graph

조회 수: 1 (최근 30일)
Kylenino Espinas
Kylenino Espinas 2022년 3월 7일
편집: Torsten 2022년 3월 8일
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