spm_DEM_F.m 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. function [F,p] = spm_DEM_F(DEM,ip)
  2. % Free-energy as a function of conditional parameters
  3. % [F,P1,P2] = spm_DEM_F(DEM))
  4. %
  5. % DEM - hierarchical model
  6. %
  7. % F(i) - free-energy at <q(P(ip))> = p(i)
  8. %
  9. % where p(i) is the ip-th free-parameter. This is a bound on
  10. % the log-likehood (log-evidence) conditioned on the expeceted parameters
  11. %__________________________________________________________________________
  12. % Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
  13. % Karl Friston
  14. % $Id: spm_DEM_F.m 4146 2010-12-23 21:01:39Z karl $
  15. % Find paramter ranges (using prior covariance)
  16. %--------------------------------------------------------------------------
  17. pE = spm_vec(DEM.M(1).pE);
  18. p = linspace(-6,6,16);
  19. dp = sqrt(DEM.M(1).pC(ip,ip))*p;
  20. p = dp + pE(ip);
  21. % get F
  22. %==========================================================================
  23. DEM.M(1).E.nE = 1;
  24. DEM.M(1).E.nN = 1;
  25. for i = 1:length(p)
  26. % adjust paramter (through prio expecatation)
  27. %----------------------------------------------------------------------
  28. P = pE;
  29. P(ip) = p(i);
  30. DEM.M(1).P = spm_unvec(P,DEM.M(1).pE);
  31. % comute free-energy
  32. %----------------------------------------------------------------------
  33. DEM = spm_DEM(DEM);
  34. F(i) = DEM.F(end);
  35. end
  36. % predicted F under the Laplace assumption
  37. %==========================================================================
  38. DEM.M(1).P = DEM.M(1).pE;
  39. DEM = spm_DEM(DEM);
  40. % compute free-energy
  41. %--------------------------------------------------------------------------
  42. dFdp = DEM.qP.dFdp(ip);
  43. dFdpp = DEM.qP.dFdpp(ip,ip);
  44. FP = dFdp*dp + (dFdpp*dp.^2)/2;
  45. FP = FP - max(FP);
  46. % plot
  47. %--------------------------------------------------------------------------
  48. spm_figure('GetWin','Free-energy');
  49. F = F - max(F);
  50. subplot(2,1,1)
  51. plot(p,F,p,FP,':'), hold on
  52. plot(pE(ip)*[1 1],[min(F) 0],':'), hold on
  53. xlabel('Parameter expecatation','FontSize',12)
  54. ylabel('Free-energy','FontSize',12)
  55. title('Free-energy profile','FontSize',16)
  56. axis square, box off