//////////////////// Exerice 1
clear;
n=3;T=1; deltat=T/n;S0=140; sigma=0.2;
up=exp(sigma*sqrt(deltat));down=1/up;
function s=S(i,j); // on doit avoir 0<=j<=i<=n
  s=S0*up.^j.*down.^(i-j);
endfunction;
// Exercice 2.1
deltaJ=[1,1,0,0,0,1,0,0,1,1];
J=cumsum(deltaJ);
n=10; deltat=T/n;
up=exp(sigma*sqrt(deltat));down=1/up;
ttraj=zeros(1,n+1);
ttraj(1)=S(0,0);
for i=1:n // ii=i+1
  ttraj(i+1)=S(i,J(i));
end;
scf(1);
plot2d(0:10,ttraj,5);
// Exercice 2.2
deltaJ=[0,1,1,1,0,1,0,0,1,1];
J=cumsum(deltaJ);
ttraj=zeros(1,n);
ttraj(1)=S(0,0);
for i=1:n
  ttraj(i+1)=S(i,J(i));
end;
scf(1);
plot2d(0:10,ttraj,4)
//////////////////// Exercice 3
x=rand(1,1000);
scf(2);
histplot(10,x)
y=grand(1,40,"bin",1,1/4);
scf(3);
histplot(2,y)
//////////////////// Exercice 4
M=20;n=40;up=exp(sigma*sqrt(deltat));down=1/up;p=1/2;

MdeltaJ=grand(M,n,"bin",1,p);
MJ=cumsum(MdeltaJ,"c");
Mttraj=zeros(M,n+1);
for m=1:M
  Mttraj(m,1)=S0;
  for i=1:n
    Mttraj(m,i+1)=S(i,MJ(m,i));
  end;
end;
scf(4);
for m=1:M
  plot2d(0:n,Mttraj(m,1:n+1));
end;
//////////////////// Exercice 5
for m=1:M
    MdeltaJ(m,1:10)=deltaJ;
end;
MJ=cumsum(MdeltaJ,"c");
Mttraj=zeros(M,n+1);
for m=1:M
  Mttraj(m,1)=S0;
  for i=1:n
      Mttraj(m,i+1)=S(i,MJ(m,i));
  end;
end;
scf(5);
for m=1:M
  plot2d(0:n,Mttraj(m,1:n+1));
end;
