% DeathProcess.m simulates the 'pure death process'. Starting with
% nstart individuals, each having probability p of dieing in each
% time step, we continue until all have died. The population size
% is plotted over time and compared to the deterministic model 
% for expected population size. 

% parameters
nstart=50; p=0.02;

% initialize variables for current population size
nnow=nstart; nbarnow=nstart; t=0;

% initialize vectors storing the results over time
nt=nstart; nbar=nstart; tvals=0;

while nnow>0;
   % update the stochastic population	
	m=rand(nnow,1);
	ndie=sum(m<p);
	nnow=nnow-ndie;
	nbarnow=(1-p)*nbarnow;
   % store the results
   t=t+1;
	tvals=[tvals;t];
	nt=[nt;nnow];
	nbar=[nbar;nbarnow];
end; 
disp([t nbarnow])
plot(tvals,nt,'+')
hold on;
plot(tvals,nbar)
hold off;
xlabel('Time'); ylabel('Population size'); title('Pure Death Process');


   
	



