calculate probability in MATLAB
조회 수: 24 (최근 30일)
이전 댓글 표시
THIS IS A MULTINOMIAL PROBABILITY DISTRIBUTION PROBLEM!
say we are given 4 outcome below along with the probabilities
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1706056/image.png)
Calculate the probability such that
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1706061/image.png)
my attempted code is
% Define the probabilities for each outcome
HH = 0.013;
HL = 0.267;
LH = 0.031;
LL = 0.689;
% Initialize the total probability
totalProbability = 0;
% Loop through possible counts for HL and LH
for nHL = 0:20
for nLH = 0:2
nHH = 0;
nLL = 100 - nHH - nHL - nLH
probability = mnpdf([nHH nHL nLH nLL], [HH HL LH LL])
totalProbability = totalProbability + probability;
end
end
% Display the result
disp(['The total probability is: ', num2str(totalProbability)])
However the code gives the value 0.0056902 but the real answer is 0.00132705 so clearly something is wrong with my code.
댓글 수: 3
Torsten
2024년 5월 29일
편집: Torsten
2024년 5월 30일
The argument in MATHEMATICA's CDF - function on the web page for the example given is wrong.
What is calculated on the web page is P(n1=0,n2<=20,n3<=2,n4<=78) which equals P(n1=0,n2=20,n3=2,n4=78) (because 0 + 20 + 2 + 78 is the only combination such that the sum equals 100):
format long
HH = 0.013;
HL = 0.267;
LH = 0.031;
LL = 0.689;
p = [HH HL LH LL];
x = [0 20 2 78];
mnpdf(x,p)
But what is needed is P(n1=0,n2<=20,n3<=2,n4<=100).
I have no access to MATHEMATICA, but my guess is that the correct command should be
<< Needs["MultivariateStatistics`"]
<< multinomial = MultinomialDistribution[100, {0.013, 0.267, 0.031, 0.689}]
<< CDF[multinomial, {0, 20, 2, 100}]
답변 (1개)
Nipun
2024년 6월 3일
편집: Nipun
2024년 6월 3일
Hi Nafisa,
I understand that you are working on a multinomial probability distribution problem and need help with your MATLAB code. Based on the shared information, I recommend using the following looping and assignment of frequencies.
% Define the probabilities for each outcome
HH = 0.013;
HL = 0.267;
LH = 0.031;
LL = 0.689;
% Initialize the total probability
totalProbability = 0;
% Loop through possible counts for HL and LH
for nHL = 0:20
for nLH = 0:2
nHH = 0;
nLL = 100 - nHH - nHL - nLH;
% Check if nLL is within valid bounds
if nLL >= 0
probability = mnpdf([nHH nHL nLH nLL], [HH HL LH LL]);
totalProbability = totalProbability + probability;
end
end
end
% Display the result
disp(['The total probability is: ', num2str(totalProbability)])
This code ensures that the frequency nLL is non-negative and correctly sums up the probabilities for all valid combinations of nHL and nLH.
Hope this helps.
Regards,
Nipun
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!