---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
Other examples of distributions (see here)

# The time between the arrival at a traffic light (or at a highway toll booth) of a car
# and the arrival of the next car,  the time between the arrival of one phone call and
# the other at the switchboard for a television broadcast, … on the one hand,  the
# number of car or the number of phone calls that arrive in a fixed time interval, …
# on the other  are example of two other laws of distribution: the negative exponential
# distribution and the Poisson distribution. Let's remember them through an example.
 
# A 1 long wall; a w wide hole in the wall; every second a zombie arrives in a
# completely random position of the wall.
                                         
# Let w be 1/10; let's simulate 8.5 hours.
# (1) The distribution (in 1 sec intervals) of the waiting times between a pass through
# the hole of a zombie and the next pass.
# (2) The distribution of the number of zombies that pass through the hole in 60 sec
 
         (1)(2)
 
zombie = function() { w <<- 1/10; TZ <<- NULL; h=8.5; sec=60*60*h
            i=1; n=0; NZ <<- NULL; u=0
            while (i <= sec) {
              t=0; j=0; while(j<1) {j=ifelse(runif(1) < w, 1,0); t=t+1; r=i/60; u=u+j
                                    if(r==floor(r)) {NZ[r] <<- u; u=0}; i=i+1}
              n=n+1; TZ[n] <<- t }  }
 
# The assignment  <<-  also applies outside of the function definition
 
# ifelse(runif(1)<w,1,0) is 1 if the zombie pass through the hole, 0 otherwise; t: time
# between 2 passes; TZ: the waiting Times; NZ: the Number of zombies that pass in 60 s 
 
BF=3; HF=2.5
zombie();  noClass=1; histogram(TZ)         # histogram of a continuous variable
c(min(NZ),max(NZ))
#  1  15
noClass=1; Histogram(NZ, 0.5,15.5, 1)         # histogram of a discrete variable
 
# If we want the histogram of the density (ie the histogram of area 1):
max( hist(TZ, right=FALSE, probability=TRUE, plot=FALSE)$density )
# 0.06664432
Plane(0,80, 0,0.07)
hist(TZ, right=FALSE, probability=TRUE, col="grey",add=TRUE)
 
      (1)   (2)
 
statistics(TZ)
#    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#    1.00    3.00    7.00   10.27   14.00   78.00 
#     The brown dots are 5^ and 95^ percentiles 
sqm(TZ)
# 9.86031
max( hist(NZ, seq(0.5,15.5,1), probability=TRUE, plot=FALSE)$density )
# 0.1666667
Plane(0,16, 0,0.17)
hist(NZ, seq(0.5,15.5,1), probability=TRUE, col="grey",add=TRUE)
statistics(NZ)
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#  1.000   4.000   6.000   5.847   7.000  15.000 
#    The brown dots are 5^ and 95^ percentiles 
#           The red dot is the mean 
sqm(NZ)
# 2.363145
 
# It can be shown that, in the hypotheses made, TZ has the density function:
# x -> w·exp(-w·x)             [negative exponential distribution with parameter w]
# and that NZ has the law of distribution:
# Pr(NZ=n) = exp(-a)·an/n!                  [Poisson distribution with parameter a]
#       (a = average number of zombies that pass through the hole in 60 s;
#     usually, the parameter of the Poisson distribution is denoted by lambda)
# In our case a=w*60=6. For the density function of the Poisson distribution I can use:
k=0:100; P = dpois(k, lambda = 6)
 
EN = function(x) w*exp(-w*x)
Plane(0,80, 0,0.07)
hist(TZ, right=FALSE, probability=TRUE, col="grey",add=TRUE)
graph(EN,0,80, "brown"); integral(EN,0,Inf)
#  1                                         OK: the integral must be 1
# the mean:
g = function(x) EN(x)*x; m = integral(g,0,Inf); m
#  10   (but I already knew that m = 10)
# the Sd (or sqm):
h = function(x) EN(x)*(x-m)^2;  S = sqrt(integral(h,0,Inf)); S
#  10
# The negative exponential distribution has both the mean and the Sd equal to w
 
      (1)   (2)
 
Plane(0,16, 0,0.17)
hist(NZ, seq(0.5,15.5,1), probability=TRUE, col="grey",add=TRUE)
polyline(k, P, "red"); POINT(k, P, "brown")
sum(P[0:40])        # I must calcuate P[0]+P[1]+P[2]+... and the sum must be 1, but
# 1     OK            it is enough P[0]+ … +P[40]: P[40]=2.7e-19, P[41]=4.1e-20, …
# the mean:
Wmean(0:100,dpois(0:100, lambda = 6))
# 6
# the variance:
WVar(0:100,dpois(0:100, lambda = 6))
# 6
# The Poisson distribution has both the mean and the variance equal to lambda

# It can be shown that, at the growth of lambda, the graph of Poisson distribution
# tendes to confuse with a Gauss curve.