spm_ADEM_diff.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. function [u,dg,df] = spm_ADEM_diff(M,u)
  2. % Evaluate an active model given innovations z{i} and w{i}
  3. % FORMAT [u dg df] = spm_ADEM_diff(M,u);
  4. %
  5. % M - generative model
  6. %
  7. % u.a - active states
  8. % u.v - causal states - updated
  9. % u.x - hidden states - updated
  10. % u.z - innovation (causal state)
  11. % u.w - innovation (hidden states)
  12. %
  13. % dg.dv, ... components of the Jacobian in generalised coordinates
  14. %
  15. % The system is evaluated at the prior expectation of the parameters.
  16. %__________________________________________________________________________
  17. % Copyright (C) 2008-2014 Wellcome Trust Centre for Neuroimaging
  18. % Karl Friston
  19. % $Id: spm_ADEM_diff.m 5962 2014-04-17 12:47:43Z spm $
  20. % number of states and parameters
  21. %--------------------------------------------------------------------------
  22. nl = size(M,2); % number of levels
  23. % order parameters (n = 1 for static models)
  24. %==========================================================================
  25. n = M(1).E.n + 1; % order of embedding
  26. % initialise arrays for hierarchical form
  27. %--------------------------------------------------------------------------
  28. dfdvi = cell(nl,nl);
  29. dfdxi = cell(nl,nl);
  30. dfdai = cell(nl,nl);
  31. dgdvi = cell(nl,nl);
  32. dgdxi = cell(nl,nl);
  33. dgdai = cell(nl,nl);
  34. for i = 1:nl
  35. dgdvi{i,i} = sparse(M(i).l,M(i).l);
  36. dgdxi{i,i} = sparse(M(i).l,M(i).n);
  37. dgdai{i,i} = sparse(M(i).l,M(i).k);
  38. dfdvi{i,i} = sparse(M(i).n,M(i).l);
  39. dfdxi{i,i} = sparse(M(i).n,M(i).n);
  40. dfdai{i,i} = sparse(M(i).n,M(i).k);
  41. end
  42. % partition states {x,v,z,w} into distinct vector arrays v{i}, ...
  43. %--------------------------------------------------------------------------
  44. vi = spm_unvec(u.v{1},{M.v});
  45. xi = spm_unvec(u.x{1},{M.x});
  46. ai = spm_unvec(u.a{1},{M.a});
  47. zi = spm_unvec(u.z{1},{M.v});
  48. % Derivatives for Jacobian
  49. %==========================================================================
  50. vi{nl} = zi{nl};
  51. for i = (nl - 1):-1:1
  52. % evaluate
  53. %----------------------------------------------------------------------
  54. [dgdx,g] = spm_diff(M(i).g,xi{i},vi{i + 1},ai{i + 1},M(i).pE,1);
  55. [dfdx,f] = spm_diff(M(i).f,xi{i},vi{i + 1},ai{i + 1},M(i).pE,1);
  56. dgdv = spm_diff(M(i).g,xi{i},vi{i + 1},ai{i + 1},M(i).pE,2);
  57. dfdv = spm_diff(M(i).f,xi{i},vi{i + 1},ai{i + 1},M(i).pE,2);
  58. dgda = spm_diff(M(i).g,xi{i},vi{i + 1},ai{i + 1},M(i).pE,3);
  59. dfda = spm_diff(M(i).f,xi{i},vi{i + 1},ai{i + 1},M(i).pE,3);
  60. % g(x,v) & f(x,v)
  61. %----------------------------------------------------------------------
  62. gi{i} = g;
  63. fi{i} = f;
  64. vi{i} = spm_vec(gi{i}) + spm_vec(zi{i});
  65. % and partial derivatives
  66. %----------------------------------------------------------------------
  67. dgdxi{i, i} = dgdx;
  68. dgdvi{i,i + 1} = dgdv;
  69. dgdai{i,i + 1} = dgda;
  70. dfdxi{i, i} = dfdx;
  71. dfdvi{i,i + 1} = dfdv;
  72. dfdai{i,i + 1} = dfda;
  73. end
  74. % concatenate hierarchical arrays
  75. %--------------------------------------------------------------------------
  76. dg.da = spm_cat(dgdai);
  77. dg.dv = spm_cat(dgdvi);
  78. dg.dx = spm_cat(dgdxi);
  79. df.da = spm_cat(dfdai);
  80. df.dv = spm_cat(dfdvi);
  81. df.dx = spm_cat(dfdxi);
  82. % update generalised coordinates
  83. %--------------------------------------------------------------------------
  84. u.v{1} = spm_vec(vi);
  85. u.x{2} = spm_vec(fi) + u.w{1};
  86. for i = 2:(n - 1)
  87. u.v{i} = dg.dv*u.v{i} + dg.dx*u.x{i} + dg.da*u.a{i} + u.z{i};
  88. u.x{i + 1} = df.dv*u.v{i} + df.dx*u.x{i} + df.da*u.a{i} + u.w{i};
  89. end
  90. u.v{n} = dg.dv*u.v{n} + dg.dx*u.x{n} + dg.da*u.a{n} + u.z{n};