フィルターのクリア

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 Center および File ExchangeLanguage Fundamentals についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by