how to calculate conditional probability

63 views (last 30 days)
Hi! I have a cell array (values, attached) that contains in the first column a series
of words and in the second, the categories in which the words are included.
I want to calculate the conditional probability: a measure of the probability of an
event given that another event has occurred.
In this example I want to know: Prob(sunny|n); Prob(sunny|y); Prob(rain|n); Prob(rain|y); Prob(overcast|n); Prob(overcast|y)
Can you help me, please?

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 19 Jul 2017
Edited: Andrei Bobrov on 19 Jul 2017
[a,~,c] = unique(values,'stable');
d = reshape(c,[],2);
dm = min(d(:,2));
ny = accumarray(d(:,2) - dm + 1,1);
[a1,~,c1] = unique(d,'rows');
P = accumarray(c1,1)./ny(a1(:,2) - dm + 1);
with use table:
T1 = table(values(:,1),values(:,2),'v',{'weather','par'});
[g,name] = findgroups(T1);
[g1,name1] = findgroups(T1.par);
[~,ii] = ismember(name.par,name1);
ny = accumarray(g1,1);
P = [name,table(accumarray(g,1)./ny(ii),'v',{'probability'})];

More Answers (0)

Community Treasure Hunt

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

Start Hunting!