draft/prob/prob.m
brancharpeges
changeset 0 1397c2bfefa2
equal deleted inserted replaced
-1:000000000000 0:1397c2bfefa2
       
     1 # Nombre de résultats calculés
       
     2 # de -N à +N
       
     3 N = 20;
       
     4 # Taille du dé
       
     5 M = 10;
       
     6 # Nombre de résultats affichés
       
     7 # de -R à +R
       
     8 R = 20;
       
     9 # Nombre de dés
       
    10 J = 2;
       
    11 # Nombre de résultats affichés
       
    12 # de -R à +R
       
    13 R = 20;
       
    14 # Prob de 1 à N
       
    15 P1N = 1:N;
       
    16 # Prob de -N à N
       
    17 PDN = 0:2*N;
       
    18 
       
    19 NSG = 0:2*N;
       
    20 
       
    21 function y = P1_ferme(x,M,J)
       
    22   if ((x < 1) || (x > M))
       
    23     y = 0;
       
    24   else
       
    25     y = 1/M;
       
    26   end;
       
    27 end;
       
    28 
       
    29 function y = P1_limite(x,M,J)
       
    30   if (x < 1)
       
    31     y = 0;
       
    32   elseif (x < M)
       
    33     y = 1/M;
       
    34   elseif (x < J*M)
       
    35     y = P1_limite(x-M,M,J)/M;
       
    36   elseif (x == J*M)
       
    37     y = (1/M)^J;
       
    38   else
       
    39     y = 0;	
       
    40   end;
       
    41 end;
       
    42 
       
    43 function y = P1_ouvert(x,M,J)
       
    44   if (x < 1)
       
    45     y = 0;
       
    46   elseif (x < M)
       
    47     y = 1/M;
       
    48   else
       
    49     y = P1_ouvert(x-M,M)/M;
       
    50   end;
       
    51 end;
       
    52 
       
    53 function y = P1(x,M,J)
       
    54   y = P1_limite(x,M,J);
       
    55 end;
       
    56 
       
    57 function y = diff(x,P1N,N)
       
    58   if (x < 0)
       
    59     c = -x;
       
    60   else
       
    61     c = x;
       
    62   end
       
    63 
       
    64   if (c >= N)
       
    65     y = 0;
       
    66     return;
       
    67   end
       
    68 
       
    69   y = 0;
       
    70   for i = (c+1):N
       
    71     y = y + P1N(i) * P1N(i-c);
       
    72   end;
       
    73 end;
       
    74 
       
    75 function y = prob_real(N, M, J)
       
    76 # usage: y = prob_real(N, M, J)
       
    77 # Return a -N:N vector containing
       
    78 # the compute value of probability
       
    79 # for J M-side dices.
       
    80   for i = 1:N
       
    81     p1(i) = P1(i,M,J);
       
    82   end;
       
    83   for i = -N:N
       
    84     y(i+N+1) = diff(i,p1,N);
       
    85   end;
       
    86 end;
       
    87 
       
    88 function y = prob_theo(N)
       
    89 # usage: y = prob_theo(N)
       
    90 # Return a -N:N vector containing
       
    91 # the theorical value of probability
       
    92   for i = -N:N
       
    93     if (i < 0)
       
    94       y(i+N+1) = 0.5 * 2^(i/3);
       
    95     else
       
    96       y(i+N+1) = 1 - 0.5 * 2^(-i/3);
       
    97     end;
       
    98   end;
       
    99 end;
       
   100 
       
   101 PDN = prob_real(N,M,J);
       
   102 IPDN = cumsum(PDN);
       
   103 NSG = prob_theo(N);
       
   104 
       
   105 RANGE=-R:R;
       
   106 IRANGE=N-R+1:N+R+1;
       
   107 
       
   108 # Affichage des résultats
       
   109 IPDN(IRANGE)
       
   110 
       
   111 title("courbe de probabilité");
       
   112 plot(RANGE, IPDN(IRANGE), "-.", RANGE, NSG(IRANGE), "-");
       
   113 r = scanf("%s","C");
       
   114 title("erreur absolue");
       
   115 plot(RANGE, abs(IPDN(IRANGE)-NSG(IRANGE))*100);
       
   116 r = scanf("%s","C");
       
   117 title("erreur relative");
       
   118 plot(RANGE, abs(IPDN(IRANGE)-NSG(IRANGE))./NSG(IRANGE)*100);
       
   119 r = scanf("%s","C");
       
   120 
       
   121 function y = diff_sum(P,R)
       
   122   Psize = size(P,1)
       
   123   Rsize = size(R,1)
       
   124   for i = 1:Rsize-1
       
   125     off = R(i+1)-1
       
   126     y(i) = sum(P(R(i):off))
       
   127   end;
       
   128   y(Rsize) = sum(P(R(Rsize):Psize))
       
   129 end;
       
   130 
       
   131 X = PDN(N:-1:1);
       
   132 X = X / sum(X);
       
   133 S = [1;5;10;15]
       
   134 diff_sum(X,S)
       
   135