spm_dirichlet_exceedance.m 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. function xp = spm_dirichlet_exceedance(alpha,Nsamp)
  2. % Compute exceedance probabilities for a Dirichlet distribution
  3. % FORMAT xp = spm_dirichlet_exceedance(alpha,Nsamp)
  4. %
  5. % Input:
  6. % alpha - Dirichlet parameters
  7. % Nsamp - number of samples used to compute xp [default = 1e6]
  8. %
  9. % Output:
  10. % xp - exceedance probability
  11. %__________________________________________________________________________
  12. %
  13. % This function computes exceedance probabilities, i.e. for any given model
  14. % k1, the probability that it is more likely than any other model k2.
  15. % More formally, for k1=1..Nk and for all k2~=k1, it returns p(x_k1>x_k2)
  16. % given that p(x)=dirichlet(alpha).
  17. %
  18. % Refs:
  19. % Stephan KE, Penny WD, Daunizeau J, Moran RJ, Friston KJ
  20. % Bayesian Model Selection for Group Studies. NeuroImage (in press)
  21. %__________________________________________________________________________
  22. % Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
  23. % Will Penny & Klaas Enno Stephan
  24. % $Id: spm_dirichlet_exceedance.m 3118 2009-05-12 17:37:32Z guillaume $
  25. if nargin < 2
  26. Nsamp = 1e6;
  27. end
  28. Nk = length(alpha);
  29. % Perform sampling in blocks
  30. %--------------------------------------------------------------------------
  31. blk = ceil(Nsamp*Nk*8 / 2^28);
  32. blk = floor(Nsamp/blk * ones(1,blk));
  33. blk(end) = Nsamp - sum(blk(1:end-1));
  34. xp = zeros(1,Nk);
  35. for i=1:length(blk)
  36. % Sample from univariate gamma densities then normalise
  37. % (see Dirichlet entry in Wikipedia or Ferguson (1973) Ann. Stat. 1,
  38. % 209-230)
  39. %----------------------------------------------------------------------
  40. r = zeros(blk(i),Nk);
  41. for k = 1:Nk
  42. r(:,k) = spm_gamrnd(alpha(k),1,blk(i),1);
  43. end
  44. sr = sum(r,2);
  45. for k = 1:Nk
  46. r(:,k) = r(:,k)./sr;
  47. end
  48. % Exceedance probabilities:
  49. % For any given model k1, compute the probability that it is more
  50. % likely than any other model k2~=k1
  51. %----------------------------------------------------------------------
  52. [y, j] = max(r,[],2);
  53. xp = xp + histc(j, 1:Nk)';
  54. end
  55. xp = xp / Nsamp;