function cltexp(option)
%CLTEXP  Interactive Central Limit Theorem with exponentialdDistribution

%Peter Dunn
%28 May 1998

if nargin == 0,

   option=0;
   figure('Name','Interactive Central Limit Theorem',...
          'Resize','off');

   %SET UP THE CANVAS

   uicontrol('Style','Frame','Units','normalized',...
             'Position',[0.02 0.02 1-2*0.02 0.2-0.02]);

   %parameter buttons
   pbborder = 0.02;
   pbwidth = ( 1 - (2 * 0.02) - 6*pbborder ) / 4;

   %Exponential Mean
   uicontrol('Style','text','String','Exp. Mean, beta',...
             'Units','normalized',...
             'Position',[0.02+pbborder 0.14 pbwidth 0.05]);
   uicontrol('Style','Slider',  'Max',10.0,  'Min',0.01,  ...
             'Callback','cltexp(1);','value',2.0,...
             'tag','hcltemean', 'Units','normalized',  ...
             'Position',[0.02+pbborder 0.04 pbwidth 0.05]);
   uicontrol('Style','Edit','String','2.0','Units','normalized',...
             'Position',[0.02+pbborder 0.09 pbwidth 0.05],...
             'tag','hcltemeanedit','Callback','cltexp(2);');

   %Sample Size
   uicontrol('Style','text','String','Sample Size, n','Units','normalized',...
             'Position',[0.02+2*pbborder+pbwidth 0.14 pbwidth 0.05]);
   uicontrol('Style','Slider',  'Max',200,  'Min',1,  ...
             'Callback','cltexp(1);','value',10,...
             'tag','hcltssize', 'Units','normalized',  ...
             'Position',[0.02+2*pbborder+pbwidth 0.04 pbwidth 0.05]);
   uicontrol('Style','Edit','String','10','Units','normalized',...
             'Position',[0.02+2*pbborder+pbwidth 0.09 pbwidth 0.05],...
             'tag','hcltssizeedit','Callback','cltexp(2);');

   %Sampling Distribution Parameters
   uicontrol('Style','text','String','Sampling Distn:',...
             'Units','normalized',...
             'Position',[0.02+3*pbborder+2*pbwidth 0.14 pbwidth 0.05],...
             'HorizontalAlignment','left');
   uicontrol('Style','text','String','Mean: 2.0','Units','normalized',...
             'Position',[0.02+3*pbborder+2*pbwidth 0.09 pbwidth 0.05],...
             'tag','hcltmeantext','HorizontalAlignment','left');
   uicontrol('Style','text','String','Var: 0.4','Units','normalized',...
             'Position',[0.02+3*pbborder+2*pbwidth 0.04 pbwidth 0.05],...
             'tag','hcltvartext','HorizontalAlignment','left');

   %Style
   uicontrol('Style','Popup','String','pdf|cdf',...
             'Callback','cltexp(5);','Units','normalized',...
             'tag','hclttypedist',...
             'Position',[0.02+4*pbborder+3*pbwidth 0.12 pbwidth 0.07]);

   %Close
   uicontrol('Style','Pushbutton','String','Close',...
             'Callback','close(gcf);','Units','normalized',...
             'Position',[0.02+4*pbborder+3*pbwidth 0.04 pbwidth 0.07]);

   %axes('Position',[0.10 0.27 0.85 0.70],'tag','haxes');

   cltexp(5);

end;


if (option==1), %Parameters slid

   ssize = round( get(findobj('tag','hcltssize'),'value') );
   set( findobj('tag','hcltssizeedit'),  ...
        'string', ssize );

   emean =  get(findobj('tag','hcltemean'),'value');
   set( findobj('tag','hcltemeanedit'),  ...
        'string', emean );

   set( findobj('tag','hcltmeantext'), ...
        'String',['Mean: ',num2str(emean)]);
   set( findobj('tag','hcltvartext'), ...
        'String',['Var: ',num2str(emean^2/ssize)]);

   cltexp(5);

elseif (option==2), %parameters editted

   emean = str2num( get(findobj('tag','hcltemeanedit'),'string') );
   if (emean < 0.01 )
      set( findobj('tag','hcltemeanedit'),'string', '0.01');
      emean = 0.01;
   elseif (emean > 10.0)
      set( findobj('tag','hcltmeanedit'),'string', '10.0');
      emean = 10.0;
   end
   set( findobj('tag','hcltemean'), 'value', emean );

   ssize = round( str2num( get(findobj('tag','hcltssizeedit'),'string') ) );
   if (ssize < 1 )
      ssize = 1;
   elseif (ssize > 200)
      ssize = 200;
   end
   set( findobj('tag','hcltssize'), 'value', ssize );
   set( findobj('tag','hcltssizeedit'),'string', ssize);

   set( findobj('tag','hcltmeantext'), ...
        'String',['Mean: ', num2str(emean) ]);
   set( findobj('tag','hcltvartext'), ...
        'String',['Var: ',num2str(emean^2/ssize)]);

   cltexp(5);

elseif (option==5), %plot

   delete(gca);  %delete current plot
   axes('Position',[0.10 0.27 0.85 0.70],'tag','haxes');

   emean = get(findobj('tag','hcltemean'),'value');
   ssize =  round(get(findobj('tag','hcltssize'),'value'));
   mn = emean;
   std = sqrt( emean^2/ssize );

   %distribution limits
   lower = max( 0.0001, mn - 4 * std);
   upper = mn + 4 * std;
   xg = linspace(lower,  upper, 100);

   if get(findobj('tag','hclttypedist'), 'value') == 1, %pdf

      %gamma
      ldenom = gammaln(ssize) + ssize*log(emean/ssize);
      ep = (ssize-1)*log(xg) + (-ssize*xg/emean);
      yg = exp( ep - ldenom );

      %normal
      ep = -0.5*( (xg-mn)/std ).^2;
      yn = 1/(std*sqrt(2*pi)) * exp( ep );

      %plot
      plot( xg, yg, 'b-',  xg, yn, 'r--');

      axis( [lower upper 0 max([yg, yn])]);

   else %cdf

      %plot gamma
      yg = gammainc( xg*ssize/emean, ssize );

      %plot normal
      yn =  ( erf( (xg - mn)/(sqrt(2)*std) ) + 1 ) / 2;
      plot( xg, yg, 'b-', xg, yn, 'r--');

      axis( [lower upper 0 1] );

   end
   
   set(gca, 'HandleVisibility','Callback');
   set(gcf, 'HandleVisibility','Callback');

end


return;
