%

function lec7

clear all; close all

rng('default')

N = 2^20;

r     = 0.05;
sigma = 0.2;
T     = 1;
K     = 100;
S0    = 100;

Omega = eye(5) + 0.1*(ones(5)-eye(5));
L     = chol(Omega,'lower');

%

for pass = 1:2
  vals  = [];
  vars  = [];
  bumps = [];

  for bump = 10.^linspace(-1,1,20)
    if (pass==2)
      rng('default');          % reset random number generator
    end

    fm = basket_vals(r,sigma,L,T,S0-bump,K, N);

    if (pass==2)
      rng('default');          % reset random number generator
    end

    fp = basket_vals(r,sigma,L,T,S0+bump,K, N);

    Del = (fp-fm)/(2*bump);

    val =  sum(Del)/N;
    var = (sum(Del.^2)/N - val^2)/(N-1);

    vals  = [vals  val];
    vars  = [vars  var];
    bumps = [bumps bump];
  end

  figure(pass);
  pos=get(gcf,'pos'); pos(3:4)=pos(3:4).*[0.8 0.8]; set(gcf,'pos',pos);

  subplot(2,1,1)
  plot(bumps,vals,'-x');
  xlabel('bump'); ylabel('Delta')

  if (pass==1)
    title('different random numbers')
  else
    title('same random numbers')
  end

  subplot(2,1,2)
  plot(bumps,sqrt(vars),'-x');
  xlabel('bump'); ylabel('s.d.')

  if (pass==1)
    print('-deps2c','lec7_1.eps')
  else
    print('-deps2c','lec7_2.eps')
  end
end

end

%
% function to evaluate lots of samples of basket call
%

function F = basket_vals(r,sigma,L,T,S0,K, N)

Y = randn(5,N);
S = S0*exp((r-sigma^2/2)*T + sigma*sqrt(T)*L*Y);
F = exp(-r*T)*max(0,sum(S,1)/5-K);

end
