% this is a MATLAB script to compute whether to 
% use your employer's contribution towards SUSORP or FRS
% Naturally, I assume no liability but only bask in
% your praise if you find this tool useful.
%
% (1) type "matlab" on your favorite computer (that has Matlab installed)
% (2) edit the assumptions section of this program 
% (3) run it by typing in Matlab: "frs_susorp"
%  
clear;
clf;

% --- assumption (your predictions and data)
%-------------------------------------------------------------
   base = 100;  % current yearly salary in thousand $$
   myage = 45;  % current age
   yearstoPayAvg = 60-myage;  % If you want 5 years of DROP
      % (otherwise FRS does not look so hot) and your highest paid years are
      % towards the end of your career then the age 60 income determines
      % your FRS pay level
   retirePercent = .43;  % depends on how many years you have worked and how
      % many year you still hope  to work before age 62
   option3 = (100-15)/100; % get "15" or similar from FRS -- 
      % for example 15% because you insure your spouse, 0% if not
   dropPercent = 1.06;
      % best guestimate
   buy_in = 140;  % get this from FRS: how much will they charge you?
   rr= 2;  % Your estimate of yearly pay raise
   raise = rr/100.0;
   ypast67 = 3; % how many years you amy want to work under SUSORP past
        % age 67
   loyield = 5; hiyield = 7; % yield estimate of SUSORP/market
   taxrate = 0.7;  % 1-your average tax bracket (here 30% pretty high)

%-------------------------------------------------------------

   years = yearstoPayAvg + 7;  
   uf = .1042;  
   maxry= 33;
for yy= loyield:hiyield, 
      yield = 1.0+yy/100;

      %%%% FRS_age67 = age67annual + drop
      avgFinalPay = base*(1+raise)^yearstoPayAvg;
      ret_pay = avgFinalPay*retirePercent;
      age62annual = ret_pay*option3;
      age67annual = age62annual*dropPercent^5;
      drop = age62annual* sum(dropPercent.^[1:5]);

      %%%%OPR_age67
      buyin67 = buy_in*yield^years;

      y1 = years-1;
      ufcontrib = 0;
      for k=0:y1,
         ufcontrib = ufcontrib +  ...
            (base*(1+raise)^k*uf)* yield^(years-k);
      end;
      pay67 = base*(1+raise)^20;

      opr = buyin67 + ufcontrib;

      %earned6770 = avgFinalPay*sum(yield.^([1:3]))*.7
         % earned and uf 10%  if working from 67 to 70
      % --- into the future  ry=retirement years
      nopr = opr; % age 67 principal
      dfrs = drop; % age 67 principal
      pay = age67annual; % anual pay out
      %earned = earned6770;
      earned = 0;
      % 15 =82 equal at y=5% and r=3%
      for ry=1:maxry, % stop work at 67
          nopr = (nopr-pay)*yield;
          dfrs = dfrs*yield;  % no reduction since pension is paid
          pay = pay*1.03;  % 3% cost of living adjustment
          if ry > ypast67,
             earned = earned*yield;
          else
             earned = earned*yield+...
                 pay67*(1+raise)^(ry)*taxrate;
             if ry==ypast67, earned, end;
          end;
          accnopr(ry) = nopr;
          accdfrs(ry) = dfrs;
          accearn(ry) = earned;
      end;
      %earned = earned6770 * yield^maxry;
      yrs = 67+[1:maxry];
      colr = [0,1,0]; 
      if yy==loyield, colr = [1,0,0]; end;
      if yy==hiyield, colr = [0,0,1]; end;
         %colr = (yy-loyield)*[1,1/2,1]/(hiyield-loyield);
      plot (yrs, accdfrs,'-','Color',colr); hold on;
      plot (yrs, accnopr,'--','Color',colr);
      %plot (yrs, accearn,'-.','Color',colr);
      plot (yrs, accnopr+accearn,':','Color',colr);
      text(66,accnopr(1),sprintf('%1d',yy));
%   end;
end;
axis([65 100 0 3000]);

   tit = sprintf('base %3d raise %1.1f to %3.0f opt %1.2f drop %3.0f',...
      base,rr,avgFinalPay,option3,drop);
   short = ...
   sprintf('%3d%3.0f%1.2f%1d.eps',base,avgFinalPay,option3,ypast67);

title(tit);
[rr,drop,avgFinalPay,pay67,ypast67,option3]

% print('-depsc2',short);  % nice .eps output
