N=10000; U=randn(1,N); pas=0.1;h=hist(X,[0,1]); h(1)/N

boites=[-5:pas:5]; h=hist(U,boites); bar(boites,h/(N*pas)); hold on;
plot(boites,exp(-boites.^2/2)/sqrt(2*pi))

N=10000; p=0.3; X=rand(1,N)<p; h=hist(X,[0,1]); h(1)/N

p=[0.3 0.2 0.1 0.1 0.3] F=cumsum(p); U=rand; X=sum(F<U)

n=length(p); N=10000; U=rand(1,N); Z=ones(n,1)*rand(1,N)>F'*ones(1,N); X=sum(Z);

a=0.3; N=1000; M=10; b=rand(N,M)<a; H=-a*log2(a)-(1-a)*log2(1-a); logproba=-(log2(a)

S=textread('texte.m','%c','whitespace','');
W=double(S)

A=(W>=65&W<=90); B=(W>=97&W<=122); Y0=A.*(W-64-27)+B.*(W-96-27)+27;

Z=(Y0+96).*(Y0<=26)+32*(Y0==27); char(Z)

Y=[ ]; for k=1:length(Y0)-1
  if((Y0(k)~=27)|(Y0(k+1)~=27)) Y=[Y Y0(k)];
  end
end

Z=(Y+96).*(Y<=26)+32*(Y==27); char(Z)

ordre0=ones(1,27)/27; ordre1=zeros(1,27); ordre2=zeros(27,27); markov2=zeros(27,27);
Fcum2=zeros(27,27); for k=1:27
  ordre1(k)=sum(Y==k)/length(Y);
  for l=1:27
    ordre2(k,l)=sum((Y(1:length(Y)-1)==k & Y(2:length(Y))==l))/(length(Y)-1);
  end
  Fcum2(k,:)=cumsum(ordre2(k,:))/sum(ordre2(k,:));
  markov2(k,:)=ordre2(k,:)/sum(ordre2(k,:));
end

Fcum1=cumsum(ordre1); bar([1:27],ordre1);

Nbsim=100; R0=ceil(27*rand(1,Nbsim)); R=R0; char((R+96).*(R<=26)+32*(R==27))

R1=sum(((ones(27,1)*rand(1,Nbsim))>(Fcum1'*ones(1,Nbsim))))+1; R=R1;
char((R+96).*(R<=26)+32*(R==27))

bar([1:27],(histc(R1,[1:27]))/Nbsim) hold on; plot([1:27],ordre1,'*')

Nbsim=1000; R2=zeros(1,Nbsim); R2(1)=sum(Fcum1<rand(1))+1; for n=2:Nbsim
  R2(n)=sum(Fcum2(R2(n-1),:)<rand(1))+1;
end R=R2; char((R+96).*(R<=26)+32*(R==27))



%====== bigrammes de mots

% extraction des mots

j=1; M=zeros(1000,20); l=zeros(1000); YY=[27 Y 27]; i=1; for k=1:length(YY)
  if (YY(k)==27)
    l(j)=i-1;
    j=j+1;i=1;
  else
    M(j,i)=YY(k);
    i=i+1;
  end
end for k=1:j
  char((M(k,:)+96).*(M(k,:)<=26)+32*(M(k,:)==27))
end j

% ordre 1

Nbsim=30; S=[ ]; for k=1:Nbsim
  q=2+floor(rand(1)*(j-2));
  S=[S M(q,[1:l(q)]) 27];
end R=S; char((R+96).*(R<=26)+32*(R==27))

% ordre 2

Nbsim=100; q=2+floor(rand(1)*(j-2)); P=M(q,[1:l(q)]); S=[P 27]; for k=1:Nbsim
  m=0;
q
  for i=2:j-1
    if (l(i)==length(P))
      if (M(i,[1:l(i)])==P)
        m=m+1;
      end
    end
  end
  m
  m=floor((rand(1)*m))+1;
  for i=2:j-1
    if (l(i)==length(P))
      if (M(i,[1:l(i)])==P)
        m=m-1;
        if (m==0)
          q=i+1;
          if (q==j) q=2; end
        end
      end
    end
  end
  P=M(q,[1:l(q)]);
  S=[S P 27];
end R=S; char((R+96).*(R<=26)+32*(R==27))
