% _____  Binomial Tree :   Prob. Distribution    based on   " Symbolic " - Formula
% __________________________________________________________
clear all; close all ; clf;
disp(['°°°°°°°°°°°° S T A R T °°°°°°°°° ',date,' °°°°°°°°°°°°°°°°°°°°°'])

syms p q;

dd =   p+q      ;           ff = (p+q)^2;          gg = (p+q)^3;  hh = (p+q)^4;                    

pretty(dd);                         pretty(expand(ff));
pretty(expand(gg));          pretty(expand(hh));

fprintf('\n')

p = 0.07 ;                                                  %  equal probab. for up-  Movements
q = 1-p;                                                     %  equal probab. for down-  Movements
Cred = 270;                                             % > Credit-Volumen je Schuldnerklasse
threshold = 0.99;                                    % > V@R = 99%
VaR_99 = 0;
Probsum = 0;

m = 3 ;                                                        % > Anzahl Credit - Klassen
Prob(1) = q^m ;

for k = 1:m
    Prob(k+1) = p*(m-k+1) * Prob(k) / (q*(k));
    fprintf('\t  \t Probability =  %8.4f\n ',  Prob(k))
    Probsum = Probsum + Prob(k);
    pause(0.2)
    if Probsum < threshold
       VaR_99 = VaR_99 + Cred;  
    end   
end
Prob;
 cumProb = cumsum(Prob)
fprintf('\n')
fprintf('\t  \t Probsum =  %4.2f\n ', Probsum)
fprintf('\t  \t V@R__99 =  %4.2f\n ',  VaR_99)

 figure(1)
 subplot(111)
      HB = stem3(Prob, 'r', 'filled');
      set(HB, 'markersize', 10, 'linewidth', 3);
%____
Y = binoinv([ 0.05 0.95], 162, 0.5)              %  alternat. Zahlen-Bsp.  >>  Quantil - Berechng.

 disp([' ***************** FINISH ****************** ',date,' **************'])
