inhomopoissrnd.m 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. function y = inhomopoissrnd(intens, varargin)
  2. % y = inhomopoissrnd(intens,T,binsize)
  3. % Inputs:
  4. % ===========
  5. % intens an intesity function. Should take an input (or vector of
  6. % inputs) and return lambda (in events/second) of the process at that time.
  7. % 'pre' [ 0] The time at which to start the interval of simulation
  8. % 'post' [ 1] The time at which to end the interval of simulation
  9. % 'binsize' [0.01] The binsize to sample at (only really important for
  10. % non-monotonic processes, where the estimate of the max rate
  11. % is influences by resolution.
  12. % 'offset' If you are using this function to generate multiple
  13. % "trials", then pass list of offsets. The simulation will be run
  14. % numel(offset) times, each time the offset will be added to
  15. % the events.
  16. % 'noise' a function that takes a size input and will be added to the
  17. % intens function.
  18. % 'refract_dur' [0] if >0 will shift events so that there are no events
  19. % closer than this value (in seconds)
  20. %
  21. % intens should be in units of events / second
  22. %
  23. % lambda = @(x)10 + 5*sin(x/5);
  24. % post = 100;
  25. % events = stats.inhomopoissrnd(lambda, 'post',post);
  26. % hist(events, post)
  27. % set(gca,'NextPlot','add');
  28. % plot(0:0.1:post, lambda(0:0.1:post),'r-','LineWidth',2)
  29. %
  30. % stats.inhomopoissrnd() % a demo. see code for more details.
  31. if nargin==0
  32. %%
  33. fprintf(1,'A demo of stat.inhomopoissrnd.m\nSee code for details.\n')
  34. clf;
  35. lambda = @(x)utils.constrain_var(50*x/10,20,50);
  36. % Simulate a ramp from 20Hz to 50Hz over 10 seconds
  37. post = 20;
  38. offset = [0:40:2222];
  39. noise = @(x)randn(x)*5;
  40. events = stats.inhomopoissrnd(lambda, 'pre',-5,'post',post,'offset',offset,'noise',noise,'refract_dur',0.001);
  41. krn = normpdf(-3:0.001:3, 0,1);
  42. kbs = 0.001;
  43. draw.exampleraster(offset(:), events,'pre',1,'post',20,'errorbars',0);
  44. return
  45. end
  46. inpd = @utils.inputordefault;
  47. [pre, args] = inpd('pre', 0,varargin);
  48. [post, args] = inpd('post', 1,args);
  49. [binsize, args] = inpd('binsize',0.01,args);
  50. [offset, args] = inpd('offset',0,args);
  51. [noise, args] = inpd('noise',@zeros,args);
  52. [refract_dur, args] = inpd('refract_dur',0,args); % Highly discouraged
  53. if ~isempty(args)
  54. fprintf(2,'Unused arguments in inhomopoissrnd:')
  55. disp(args)
  56. end
  57. if any(offset(2:end) < (post-pre))
  58. warning('STATS:overlapping_intervals','Some offsets smaller than simulation duration. Event count may be inaccurate in those intervals');
  59. end
  60. timex = pre:binsize:post; %
  61. T = post-pre;
  62. maxlambda = max(intens(timex)); % generate homogeneouos poisson process
  63. max_events = ceil(1.5*T*maxlambda);
  64. u = rand(max_events, numel(offset));
  65. y = cumsum(-(1/maxlambda)*log(u) + refract_dur+rand(size(u))*refract_dur*2)+pre; %points of homogeneous pp
  66. %y = y(y<T); n=length(y); % select those points less than T
  67. y(y>=post)=NaN;
  68. m = intens(y) + noise(size(y)); % evaluates intensity function
  69. y(rand(size(y))>m/maxlambda) = NaN; % filter out some points
  70. y = bsxfun(@plus,y,reshape(offset,1,numel(offset)));
  71. y = sort(y(:));
  72. y(isnan(y))=[];