 
%----------------------- lifetimecells.m ---------------------
close all force
clear all
disp('Lifetime of Cells')
% Cells in human body have a vast spread of life spans. One cell may last a day;
% another a lifetime.  Red blood cells (RBCs) have a lifespan of several 
% months and cannot replicate, which is the price RBCs pay for being 
% specialized cells. The lifetime of a red blood cell can be modeled by 
% an exponential distribution with density
%      f(t) = 1/beta  e^(-t/beta), 
% where  beta = 4  (in units of months). For simplicity, assume that 
% when a particular cell dies it is instantly replaced by a newborn 
% cell of the same type. For example, a replacement cell could be
% defined as any new cell born approximately at the time when the 
% original cell died.
% 
% (a) Find the expected lifetime of a single red blood cell.
% Find the probability that the cell's life exceeds 150
% days.   Hint: Days have to be expressed in units of beta.
%------------------------sol (a)--------------------------
%Expected life: beta = 4 months
%150 days = 5 months
1-expcdf(5,4) %(Cells(a))
%ans = 0.2865
%%
% (b) A single cell and its replacements are monitored over the 
% period of 1 year. How many deaths/replacements
% are observed on average?
% What is the probability that the number of deaths/replacements exceeds 5.
%   Hint:
% Utilize a link between Exponential and Poisson distributions. 
% In simple terms, if life-times are exponential with parameter 
% beta, then the number of deaths/replacements in the time interval [0, t] 
% is Poisson with parameter  lambda= t/beta. Time units for 
% t  and beta have to be the same.
%----------------sol (b)-----------------
%1 year= 12 months; Poisson(12/4)
%Observed on average: expectation of Poisson(3) = 3.
%Or rationalize:
%12/average life time = 3, but this is informal
1-poisscdf(5, 3) %(Cells(b))
%ans = 0.0839 
%%
% (c) Suppose that a single cell and two of its replacements are monitored. 
% What is the distribution of their total lifetime? Find the probability 
% that their total lifetime exceeds 1 year.
%  Hint: Consult Gamma
% distribution. If  n  random variables are exponential with parameter
%  beta  then their sum is Gamma with
% parameters  r = n and  beta. 
%--------------------- sol (c) ------------------------
1 - gamcdf(12, 3, 4) %(Cells(c))
%ans = 0.4232
%%
% (d)  A particular cell is observed  t = 2.2 months after its birth 
% and is found to still be alive.  What is the probability that the total
% lifetime of this cell will exceed 7.2 months?
% -------------------- sol (d) ------------------------
%(Cells(d)): By memoryless property
% P(X>=7.2|X>=2.2)=P(X>=7.2-2.2)=P(X>=5)
% which is equal to (Cells(a)), 0.2865
%