//1 COnvergence p.s. vers une constante

//Mn est croissante et majoree; converge p.s.

//
clf
X=rand(1,1000);
M=zeros(1,1000);
M(1)=X(1);
for i = 2:1000
    M(i)=max(M(i-1),X(i));
end
plot2d(M);
X=rand(1,1000);
N=zeros(1,1000);
N(1)=X(1);
for i = 2:1000
    N(i)=max(N(i-1),X(i));
end
plot2d([1:1000],N,2);//conv. ps. vers 1. On montre facilement conv proba vers 1

//Loi des grands nombres

// Question 2
m=mean(1:6)
// Question 3
U=rand(1,5000);
X=ceil(6*U);
S=cumsum(X);
S2=S ./[1:5000];
// Question 4
clf
plot2d(S2)
plot2d([1:5000],m*ones(1,5000),2)// Illustration de la LFGN
// Question 5
clf
plot2d((([1:5000]).^0.1).*(S2-m)) //Tend vers 0
clf
plot2d([1:5000] .*(S2-m)) //Diverge 
clf
plot2d(sqrt([1:5000]) .*(S2-m)) // Oscille (en fait, converge en loi)
// Question  6
clf
X=rand(1,5000);
S=cumsum(X);
S2=S./[1:5000];
plot2d(S2)
plot2d([1:5000],0.5*ones(1,5000),2)

// Question 8-9-10-11
clf
U=rand(1,30);
E=(U<1/2);
V=cumsum(E./2 .^[1:30]);
plot2d(V);

U=rand(1,30);
E=(U<1/2);
V=cumsum(E./2 .^[1:30]);
plot2d([1:30],V,2);
// Question 12
clf
for i=1:2000
U=rand(1,30);
E=(U<1/2);
W(i)=sum(E./2.^[1:30]);
end
plot2d(-gsort(-W),[1:2000]/2000) //conv ps. vers loi uniforme
// 13. Suite croissante bornee : converge
// Question 14
clf
U=rand(1,30);
E=(U<1/2)+(-1)*(U>1/2);
V=cumsum(E./2 .^[1:30]);
plot2d(V);
U=rand(1,30);
E=(U<1/2)+(-1)*(U>1/2);
V=cumsum(E./2 .^[1:30]);
plot2d([1:30],V,2);
clf
for i=1:2000
U=rand(1,30);
E=(U<1/2)+(-1)*(U>1/2);
W(i)=sum(E./2 .^[1:30]);
end
plot2d(-gsort(-W),[1:2000]/2000)
clf
// 15
clf
U=rand(1,30);
E=2*(U<1/2);
V=cumsum(E./3 .^[1:30]);
plot2d(V);

U=rand(1,30);
E=2*(U<1/2);
V=cumsum(E./3 .^[1:30]);
plot2d([1:30],V,2);

clf
for i=1:2000
U=rand(1,30);
E=2*(U<1/2);
W(i)=sum(E./3.^[1:30]);
end
plot2d(-gsort(-W),[1:2000]/2000) //Escalier du diable

// Polya
// 17 Voir Urne.sci
//18
clf
 R=Polya(1000,2,3);
 plot2d(R ./(5+[0:1000]));
 R=Polya(1000,2,3);
 plot2d([0:1000],R ./(5+[0:1000]),2);//conv ps vers aleatoire
 R=Polya(1000,10,2);
 plot2d(R ./(12+[0:1000]));
 R=Polya(1000,10,2);
 plot2d([0:1000],R ./(5+[0:1000]),2)
 
 //19
 X1000=zeros(1,2000) ;
 for i=1:2000
     X=Polya(1000,1,1);
       X1000(i)=X(1000);
 end
 clf
 plot2d(-gsort(-X1000),[1:2000]/2000) //loi uniforme