disp('Mamopixels')
  lw = 2; 
  set(0, 'DefaultAxesFontSize', 14);
  fs = 14;
  msize = 10;  

% Neighboring Pixels in Digital Mammograms.
%
% The difference D between two arbitrary neighboring pixels in a digital mammogram image is
% modeled by a Double Exponential  DE(0, lambda)  distribution.
% lambda is rate parameter and density is lambda/2 exp(-lambda |D|) and
% cdf is 1/2 exp(lambda D) if D<0 and 1 - 1/2 exp(-lambda D), D>=0.
% 
% (a)  It is known that the probability of  D  being less than -4 is 0.3.  
%       Using this info calculate lambda.
% (b) Find the probability of  D  falling between -5 and 20.
% (c) What are the mean and variance of  D?
% (d)  Plot graphs of the pdf and cdf.


dexppdf = @(x, mu, lambda) 1/2 .* exppdf(  abs(x-mu), 1./lambda) ;
dexpcdf = @(x, mu, lambda) 1/2 + sign(x-mu)/2 .* expcdf(abs(x-mu), 1./lambda);
dexpinv  = @(p,  mu, lambda) mu+sign(2*p-1) .* expinv(abs(2 * p - 1), 1./lambda);
dexprnd = @(mu,lambda,size)  mu+exprnd(1./lambda, size) - exprnd(1./lambda, size);
dexpstat = @(mu, lambda)  deal(mu,  2./lambda.^2);

% (a) 0.3=P(D<=-4)=0.5 * exp(- 4*lambda) -> lambda=-1/4*log(2*0.3)=0.1277
% To check:
dexpinv(0.3, 0, 0.1277)  %-4.0002
%(b) P( -5 < D < 20)
dexpcdf(20, 0, 0.1277) - dexpcdf(-5,0,0.1277)  %0.6971
 %(c)
[m v]=dexpstat(0, 0.1277)  %m = 0,  v=122.6445
%(d)
mu=0;  lambda=0.1277
x = mu-5/lambda:0.001:mu+5/lambda;
figure(1)
plot(x, dexppdf(x, mu, lambda),'linewidth', lw);
%print -depsc 'C:\STAT\RanVar\RanVareps\depdf.eps' 
figure(2)
plot(x, dexpcdf(x,mu, lambda),'linewidth',lw);
%print -depsc 'C:\STAT\RanVar\RanVareps\decdf.eps' 