spm_mvb_G.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. function model = spm_mvb_G(X,L,X0,G,V)
  2. % Multivariate Bayesian inversion of a linear model
  3. % FORMAT model = spm_mvb_G(X,L,X0,G,V);
  4. % X - contrast or target vector
  5. % L - pattern matrix (n x m)
  6. % X0 - confounds
  7. % G - pattern subsets (in columns of G) (m x h)
  8. % V - cell array of observation noise covariance components
  9. %
  10. % returns model:
  11. % F: log-evidence [F(0), F(1),...]
  12. % G: pattern switches
  13. % h: covariance hyperparameters (on R and cov(E))
  14. % qE: conditional expectation of pattern-weights
  15. % MAP: MAP projector (pattern-weights)
  16. % Cp: prior covariance (pattern space)
  17. %__________________________________________________________________________
  18. %
  19. % model: X = L*P + X0*Q + R
  20. % P = E;
  21. % cov(E) = h1*diag(G(:,1)) + h2*diag(G(:,2)) + ...
  22. %
  23. % See spm_mvb and:
  24. %
  25. % Bayesian decoding of brain images.
  26. % Friston K, Chu C, Mourão-Miranda J, Hulme O, Rees G, Penny W, Ashburner J.
  27. % Neuroimage. 2008 Jan 1;39(1):181-205
  28. %
  29. % Multiple sparse priors for the M/EEG inverse problem.
  30. % Friston K, Harrison L, Daunizeau J, Kiebel S, Phillips C, Trujillo-Barreto
  31. % N, Henson R, Flandin G, Mattout J.
  32. % Neuroimage. 2008 Feb 1;39(3):1104-20.
  33. %
  34. % Characterizing dynamic brain responses with fMRI: a multivariate approach.
  35. % Friston KJ, Frith CD, Frackowiak RS, Turner R.
  36. % Neuroimage. 1995 Jun;2(2):166-72.
  37. %__________________________________________________________________________
  38. % Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
  39. % Karl Friston
  40. % $Id: spm_mvb_G.m 3813 2010-04-07 19:21:49Z karl $
  41. % defaults
  42. %--------------------------------------------------------------------------
  43. Nx = size(X,1);
  44. Nk = size(G,1);
  45. Np = size(G,2);
  46. if isempty(X0), X0 = zeros(Nx,1); end
  47. try, V; catch, V = speye(Nx); end
  48. % null space of confounds
  49. %--------------------------------------------------------------------------
  50. X0 = full(X0);
  51. R = spm_svd(speye(size(X0,1)) - X0*pinv(X0));
  52. L = R'*L;
  53. X = R'*X;
  54. % random effects (and serial correlations)
  55. %--------------------------------------------------------------------------
  56. if iscell(V)
  57. Ne = length(V);
  58. for i = 1:Ne
  59. Qe{i} = R'*V{i}*R;
  60. end
  61. elseif isnumeric(V)
  62. Ne = 1;
  63. Qe = {R'*V*R};
  64. else
  65. Ne = 1;
  66. Qe = {speye(R'*R)};
  67. end
  68. % assemble empirical priors
  69. %==========================================================================
  70. Qp = {};
  71. LQpL = {};
  72. for i = 1:Np
  73. j = find(G(:,i));
  74. Qp{i} = sparse(j,j,1,Nk,Nk);
  75. LQpL{i} = L*Qp{i}*L';
  76. end
  77. % inverse solution
  78. %==========================================================================
  79. % Covariance components (with the first error Qe{1} fixed)
  80. %--------------------------------------------------------------------------
  81. if size(L,2) > 0
  82. Q = [Qe LQpL];
  83. else
  84. Q = Qe;
  85. end
  86. % ReML (with mildly informative hyperpriors) and a lower bound on error
  87. %--------------------------------------------------------------------------
  88. N = size(X,2);
  89. YY = X*X';
  90. Qe = exp(-2)*trace(YY)*Q{1}/trace(Q{1})/N;
  91. hE = -4;
  92. hC = 16;
  93. [Cy,h,P,F] = spm_reml_sc(YY,[],Q,N,hE,hC,Qe);
  94. % prior covariance: source space
  95. %--------------------------------------------------------------------------
  96. Cp = sparse(Nk,Nk);
  97. for i = 1:Np
  98. Cp = Cp + h(i + Ne)*Qp{i};
  99. end
  100. % MAP estimates of instantaneous sources
  101. %==========================================================================
  102. MAP = Cp*L'/Cy*R';
  103. qE = MAP*R*X;
  104. % assemble results (pattern weights)
  105. %==========================================================================
  106. model.F = F;
  107. model.G = G;
  108. model.h = h;
  109. model.qE = qE;
  110. model.MAP = MAP;
  111. model.Cp = Cp;