calculate probability in MATLAB
8 views (last 30 days)
Show older comments
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 Comments
Torsten
on 29 May 2024
Edited: Torsten
on 30 May 2024
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}]
Answers (1)
Nipun
on 3 Jun 2024
Edited: Nipun
on 3 Jun 2024
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 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!