|
|
Re: the math in classify.m
Posted:
Aug 9, 2005 12:06 PM
|
|
Thanks for the quick answer! I fully understand the code now.
> With a flat prior, I think the third output POSTERIOR is what you > are asking for.
Yes, I need the posterior p(class|x). Unfortunately my version of classify.m (Matlab 6.5) doesn't have the option nor the code to return it. To fully calculate p(k|x) = p(x|k)*p(k)/p(x) I replaced the for-loop with:
===================================================== % MVN relative log posterior density, by group, for each sample Sigma = R'*R; Sigma_det = det(2*pi*Sigma); help1 = 1/sqrt( Sigma_det ); norm = 0; for k = 1:ngroups, A = (sample - repmat(gmeans(k,:), mm, 1)) / R; help2 = exp(-.5*sum(A.*A, 2)); pdf = help1 * help2; % p(x|k) norm = norm + prior(k)*pdf; % p(x) D(:,k) = prior(k) .* pdf; end D = D ./ (repmat(norm, 1, ngroups)); % p(k|x) ====================================================
If I haven't overlooked something important, this should be fine, right?
|
|