
%% 1-2-3 Fonction de repartition loi normale 
x=[-3:0.1:3];
Xn=randn(1000,1);
Y=sort(Xn);
stairs(Y,[1:1000]/1000)
hold on
plot(x,pnorm(x),'r')
hold off
%% 4 Lois uniformes continues
X=rand(1000,1);
XU=2*pi*X;
histo(XU,10,0,1)
XU2=2*X-1;
histo(XU2,10,0,1)
%% 5 Bernoulli(0.25)
Bernou=X<0.25;
mean(Bernou)
%% 6 Binomiale(10,0.4)
X=rand(10,1); 
Z=sum(X<0.4)
Xbino=zeros(100,1);
for k=1:100
    X=rand(10,1);
    Xbino(k) = sum(X<0.4);
end
mean(Xbino)
type rbinom
Pbino=dbinom([0:10],10,0.4)
histo(Xbino,11,1,1);
hold on
stem([0:10],Pbino,'r');
hold off
figure
Y=sort(Xbino);
stairs(Y,[1:100]/100);
hold on;
stairs([0:10],cumsum(Pbino),'r')
hold off
%% 7 Uniforme sur (1,2,..6)
D=ceil(6*rand(1000,1));
histo(D,6,1,1)
mean(D)
hold on
stem(1/6*ones(1,6),'r')
hold off
%% 8 Loi discrete sur 1,2,3
U=rand(100,1);
Xd=(U<3/5)+2*((U>3/5)&(U<4/5))+3*(U>4/5);
histo(Xd,3,1,1);
%% 9 Loi discrete generale
Xd2=discrete(1000,[0.3,0.2,0.4,0.1]);
histo(Xd2,4,1,1)
%% 10 Loi geometrique
geom=zeros(1000,1);
for k=1:1000
    j=1;
    while (rand>1/6)
        j=j+1;
    end
    geom(k)=j;
end
mean(geom)
%% 11 Inversion de la fonction de repartition : loi exponentielle
E=rexp(1000,2);
mean(E)
t=[0:0.01:4];
histo(E,20,0,1)
hold on
plot(t,2*exp(-2.*t))
hold off
figure
Y=sort(E);
stairs(Y,[1:1000]/1000);
hold on
plot(t,1-exp(-2*t),'r')
hold off
%% 12 Loi de Pareto
Pareto=rpareto(1000,2.5);
t=[1:0.01:5];
Y=sort(Pareto);
stairs(Y,[1:1000]/1000);
hold on
plot(t,1-t.^-2.5,'r')
hold off
 %% Par rejet
%%  13 Uniforme sur l'ellipse
X=4*rand(10000,1)-2;
Y=2*rand(10000,1)-1;
I=find((X.^2/4+Y.^2)<1);
Xd=X(I); Yd=Y(I);
plot(Xd,Yd,'d')
hold on
t=[0:pi/30:2*pi]; plot(2*cos(t),sin(t),'r')
hold off
%%
figure;
histo(Xd,15,0,1);
%% 14
r=rand(10000,1);
t=2*pi*rand(10000,1);
plot(2*r.*cos(t),r.*sin(t),'.')
%% 15 Loi beta 
Max=5/2/4^(1/3)
X=rand(10000,1);
Y=Max*rand(10000,1);
J=find(Y<10/3*X.*(1-X.^3));
rbeta=X(J);
histo(rbeta,10,0,1);
hold on
t=[0:0.01:1];
plot(t,10/3*t.*(1-t.^3),'r');
hold off
%% 16 Loi de Poisson
X=zeros(1000,1);
for k=1:1000
    S=-log(rand(1))/2;
    while(S<1)
        S=S-log(rand(1))/2;
        X(k)=X(k)+1;
    end;
end
mean(X)
histo(X,10,1,1)
hold on
k=[0:10];
Pois=exp(-2)*2.^k./factorial(k);
stem(k,Pois,'r');
hold off;
figure
Y=sort(X);
stairs(Y,[1:1000]/1000);
hold on
stairs(k,cumsum(Pois),'r')
hold off


