kmodel1pv.m 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. function [sres, xest, pest, resp, wp]=kmodel1pv(par,stimrep)
  2. % input par : current parameter of the model, here only q/r
  3. % input stimrep : stimulus stimrep(:,1) and response stimrep(:,2)
  4. % output sres : residual of simulation and response
  5. % simulation=resp-sres
  6. % par(1) : ratio q/r for kalman filter
  7. % two-stage model published in
  8. % Petzschner FH, Glasauer S. Iterative bayesian estimation as an explanation
  9. % for range and regression effects: a study on human path integration.
  10. % J Neurosci. 2011 Nov 23;31(47):17220-9.
  11. %
  12. % S.Glasauer 2011/2021
  13. r=1;
  14. q=par(1)*r;
  15. p=r;
  16. a=10;
  17. off=1;
  18. dist=stimrep(:,1);
  19. pest=dist*0;
  20. xest=dist*0;
  21. resp=dist;
  22. wp=dist*0;
  23. for i=1:length(dist) % walk each distance in randomized order
  24. d=dist(i);
  25. % this is the core estimation routine
  26. % measurement
  27. z=log(a*d+off); % transform distance to log
  28. if i==1 % initial mean prior equals measurement d
  29. x=z;
  30. end
  31. % kalman filter for the mean of the prior
  32. km=(p+q)/(p+q+r); % kalman gain
  33. p=km*r; % new variance
  34. pest(i)=p;
  35. %x=(1-km)*x+km*z; % prior
  36. x=x+km*(z-x);
  37. xest(i)=(exp(x)-off)/a;
  38. % final fusion
  39. w1=1/r/(1/p+1/r); % weight of measurement
  40. xe=(1-w1)*x+w1*z; % estimate
  41. wp(i)=1-w1; % weight of prior
  42. % backtransform
  43. resp(i)=(exp(xe)-off)/a; % estimate backtransform
  44. end
  45. sres=stimrep(:,2)-resp;
  46. % analytical steady state solutions
  47. % k_final=0.5*q/r*(sqrt(1+4*r/q)-1);
  48. % final prior weight: w_prior=(1-k_final)/(1+k_final);
  49. % remove NaNs ... this treatment of NaNs should lead to better estimation
  50. % than just setting the residuals to zero, since here the missing values
  51. % are really treated as missing
  52. sres=sres(isfinite(sres));
  53. end