calculate probability in MATLAB
15 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
THIS IS A MULTINOMIAL PROBABILITY DISTRIBUTION PROBLEM!
say we are given 4 outcome below along with the probabilities
Calculate the probability such that
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 commentaires
Torsten
le 29 Mai 2024
Modifié(e) : Torsten
le 30 Mai 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}]
Réponses (1)
Nipun
le 3 Juin 2024
Modifié(e) : Nipun
le 3 Juin 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 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!