spm_dcm_HMM.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. function [HMM,csd] = spm_dcm_HMM(GCM,N,b)
  2. % PEB Inversion of a DCM under a hidden Markov model of state transitions
  3. % FORMAT [HMM,CSD] = spm_dcm_HMM(DCM,N,b)
  4. % FORMAT [HMM] = spm_dcm_HMM(CSD,b)
  5. % -------------------------------------------------------------------------
  6. % DCM{p} - DCMs for p sessons: DCM.b encodes state-dependent connections
  7. % N - number of windows within which to evaluate states
  8. % b{s} - Cell array state transition priors (Dirichlet parameters)
  9. %
  10. % returns HMM(s):
  11. % HMM(s).X - posterior expectation of hidden states
  12. % HMM(s).qB - posterior expectation of HMM parameters
  13. % HMM(s).qb - and Dirichlet concentration parameters
  14. % HMM(s).qP - posterior expectation of PEB parameters
  15. % HMM(s).qC - posterior covariances of PEB parameters
  16. % HMM(s).iP - indices of DCM parameters
  17. % HMM(s).Ep - posterior expectation of DCM parameters
  18. % HMM(s).Cp - posterior covariances of DCM parameters
  19. % HMM(s).L - free energy components
  20. % HMM(s).F - total free energy (model evidence)
  21. % s - index of HMM structure (prior model of state transitions)
  22. %
  23. % CSD{N,P} - inverted DCM of each window; with window functions in CSD{n}.W
  24. %__________________________________________________________________________
  25. %
  26. % This routine characterises a single timeseries in terms of latent or
  27. % hidden brain states manifest in terms of state dependent connectivity.
  28. % It first inverts the complex cross spectral density of the observed
  29. % timeseries and then estimates epoch specific fluctuations in state
  30. % dependent connectivity in a subset of connections (specified in the
  31. % logical field DCM.b). The ensuing sequence of posterior densities are
  32. % then subject to Bayesian model reduction to provide evidence for
  33. % sequences of state transitions under a hidden Markov model. Effectively,
  34. % this involves supplying the evidence that the brain is in a particular
  35. % connectivity state at each epoch – using the reduced free energy - to a
  36. % variational message passing scheme based upon a Markov decision process.
  37. % The higher (discrete state space for hidden Markov model) level that
  38. % returns the Bayesian model average for iterative optimisation of the
  39. % state dependent connection (PEB) parameters, and the epoch specific
  40. % connectivity (DCM) parameters. The products of this inversion are
  41. % posteriors at the DCM (epoch specific), PEB, (state specific)and HMM
  42. % (transition) level. These posterior densities fully characterise a
  43. % given time series in terms of discrete state transitions, where each
  44. % brain state is associated with a location in (connectivity) parameter
  45. % space; in other words, a discrete characterisation of dynamic or
  46. % fluctuating effective connectivity.
  47. %__________________________________________________________________________
  48. % Copyright (C) 2015-2016 Wellcome Trust Centre for Neuroimaging
  49. % Karl Friston
  50. % $Id: spm_dcm_HMM.m 7279 2018-03-10 21:22:44Z karl $
  51. % get windowed cross spectra if necessary
  52. %==========================================================================
  53. if nargin == 3
  54. csd = spm_dcm_window(GCM,N);
  55. else
  56. csd = GCM; % cross spectra of windows
  57. b = N; % state transition priors
  58. end
  59. %% inversion of hierarchical (empirical) Bayesian HMM model
  60. %==========================================================================
  61. [N,P] = size(csd); % number of windows and sessions
  62. a = spm_find_pC(csd{1},{'A'}); % indices of state-dependent parameters
  63. for k = 1:numel(b)
  64. % initialise this model
  65. % ---------------------------------------------------------------------
  66. clear F L M O MDP
  67. b0 = b{k}; % prior contraints on transitions
  68. s = size(b0,1); % number of hidden states
  69. D = sparse(1,1,1,s,1); % intial state
  70. % initialise succession of hidden states (assume an orbit)
  71. % ---------------------------------------------------------------------
  72. X = kron(ones(1,N),eye(s,s));
  73. X = spm_softmax(X(1:s,1:N));
  74. X = kron(ones(1,P),X);
  75. % initialise priors on hidden Markov model
  76. % ---------------------------------------------------------------------
  77. MDP.A = {eye(s,s)}; % likelihood of outcomes given hidden states
  78. MDP.D = {full(D)}; % initial states
  79. MDP.b = {full(b0)}; % transitions among states
  80. % prepare
  81. % ---------------------------------------------------------------------
  82. for i = 1:16
  83. % update state-dependent parameters; using PEB
  84. %==================================================================
  85. M.X = X'; % expected hidden states
  86. [PEB,CSD] = spm_dcm_peb(csd(:),M,{'A'}); % expected connectivity
  87. % evaluate the likelihood of each state (at each epoch)
  88. %==================================================================
  89. for t = 1:numel(CSD)
  90. qE = spm_vec(CSD{t}.Ep);
  91. qP = spm_inv(CSD{t}.Cp);
  92. for o = 1:s
  93. rE = PEB.Ep(:,o) - qE(a);
  94. F(o,t) = -rE'*qP(a,a)*rE/2;
  95. end
  96. end
  97. % update expected hidden states, using MDP (HMM)
  98. %==================================================================
  99. MDP.O{1} = spm_softmax(F);
  100. mdp = spm_MDP_VB_X(MDP);
  101. % update transition probabilities and inital states
  102. %==================================================================
  103. MDP.B = mdp.b;
  104. X = mdp.X{1};
  105. % record free energy terms
  106. % -----------------------------------------------------------------
  107. L(1,i) = mdp.F(end); % HMM: complexity from hidden states
  108. L(2,i) = mdp.Fb; % HMM: complexity from HMM parameters
  109. L(3,i) = PEB.F; % PEB: complexity from PEB parameters
  110. % test for convergence
  111. % -----------------------------------------------------------------
  112. if i > 4 && norm(L(:,i) - L(:,i - 1)) < 1/128
  113. break
  114. end
  115. end
  116. % store posteriors for this hidden Markov model
  117. % ---------------------------------------------------------------------
  118. for t = 1:numel(CSD)
  119. Ep{t} = CSD{t}.Ep; % epoch specific (PEB) expectation
  120. Cp{t} = CSD{t}.Cp; % epoch specific (PEB) covariances
  121. end
  122. HMM(k).X = X; % posterior expectation of hidden states
  123. HMM(k).qB = mdp.B{1}; % posterior expectation of HMM parameters
  124. HMM(k).qb = mdp.b{1}; % and Dirichlet concentration parameters
  125. HMM(k).qP = PEB.Ep; % posterior expectation of PEB parameters
  126. HMM(k).qC = PEB.Cp; % posterior covariances of PEB parameters
  127. HMM(k).iP = PEB.Pind; % indices of DCM parameters
  128. HMM(k).Ep = Ep; % posterior expectation of DCM parameters
  129. HMM(k).Cp = Cp; % posterior covariances of DCM parameters
  130. HMM(k).L = L; % free energy components
  131. HMM(k).F = sum(L(:,end)); % total free energy (model evidence)
  132. end
  133. if nargout, return, end
  134. % report analysis
  135. %==========================================================================
  136. % get figure
  137. %--------------------------------------------------------------------------
  138. spm_figure('Getwin','HMM'); clf
  139. spm_dcm_HMM_plot(HMM)
  140. return
  141. function [csd] = spm_dcm_window(GCM,N)
  142. % Auxiliary function: cross spectra of windowed segments
  143. % FORMAT [HMM,CSD] = spm_dcm_HMM(DCM,N)
  144. % -------------------------------------------------------------------------
  145. % check DCM is a cell array and number of sessions: e.g., participants (P)
  146. %--------------------------------------------------------------------------
  147. if isstruct(GCM), GCM = {GCM}; end
  148. P = numel(GCM);
  149. % nonlinear system identification (DCM for CSD)
  150. %==========================================================================
  151. % First, invert the entire timeseries to estimate DCM parameters that
  152. % do not change over time. These parameters will be fixed using precise
  153. % shrinkage priors to estimate fluctuating (state-dependent) parameters
  154. % using windowed data to evaluate complex cross spectra
  155. %--------------------------------------------------------------------------
  156. % invert sessions and take parameter average
  157. %--------------------------------------------------------------------------
  158. for p =1:P
  159. CSD{p} = spm_dcm_fmri_csd(GCM{p});
  160. end
  161. CSD = spm_dcm_bpa(CSD(:));
  162. % for each session (e.g., subject)
  163. %--------------------------------------------------------------------------
  164. for p = 1:P
  165. % preliminaries: extract state-dependent parameter specification (b)
  166. %----------------------------------------------------------------------
  167. DCM = GCM{p};
  168. n = size(DCM.b,1);
  169. B = DCM.b;
  170. DCM.b = zeros(n,n,0);
  171. % (overlapping) Hanning windows (epochs) W
  172. %----------------------------------------------------------------------
  173. nS = size(DCM.Y.y,1); % number of scans
  174. nw = nS/N; % window length
  175. iw = (1:N)*nw - nw/2; % indices of window centres
  176. jw = -nw:nw;
  177. hw = spm_hanning(length(jw));
  178. W = zeros(nS,N);
  179. for i = 1:N
  180. k = iw(i) + jw;
  181. j = find(k > 0 & k < nS);
  182. W(k(j),i) = hw(j);
  183. end
  184. % use posterior expectations as precise priors that relax hyperpriors
  185. %----------------------------------------------------------------------
  186. pC = spm_zeros(CSD.Ep); % infinitely precise shrinkage priors
  187. pC.A = B/16; % state dependent parameters
  188. DCM.M.pE = CSD.Ep; % prior expectation of parameters
  189. DCM.M.pC = diag(spm_vec(pC)); % prior covariances of parameters
  190. DCM.M.hE = 0; % expected log degrees of freedom
  191. DCM.M.hC = 1/64; % intermediate covariance
  192. % invert each window
  193. %----------------------------------------------------------------------
  194. DCM.options.order = 6;
  195. Y.y = DCM.Y.y;
  196. for t = 1:N
  197. w = diag(W(:,t)); % get windowing matrix
  198. w = w(W(:,t) > 0,:); % and truncate timeseries
  199. DCM.Y.y = w*Y.y; % windowed timeseries
  200. csd{t,p} = spm_dcm_fmri_csd(DCM); % invert window
  201. csd{t,p}.W = sum(w)'; % store window
  202. end
  203. end