📜 ⬆️ ⬇️

Simulation of the simplest flow

In total, using two words, one can characterize such things as



and much more. All this is called the “simplest flow”.
')
It is obvious that the number of calls at the station, cars on the main line are random at each moment of time. Given that all three things from the list are quite important for the whole society and for its optimal existence, it is necessary to learn how to model this stream.

Particularly relevant is the last item in the list, as users who do not know the simple foundations of a computer, can’t get enough of them, and knowing the flow parameters you can predict their number.

The mathematical model of the simplest flow requires several properties from it.

  1. Stationarity. This means that the probability of hitting a particular number of events for a certain period of time depends only on the length of the section and does not depend on where this section is located.
  2. Lack of aftereffect. This means the following: if two time intervals do not overlap, the number of events that hit one of them does not depend on the number of events that hit the other site.
  3. Ordinary. A flow is ordinary if the probability of hitting one sufficiently small time interval of two or more events is negligibly less than its length .


Note that, depending on how the real events are divided into time intervals, it may depend on whether this stream is the simplest or not.

For example, the flow of cars on the highway per day is not the simplest, since it is not stationary: during the day there are fewer cars than in the morning and in the evening, while the same flow during rush hours can already be considered the simplest.

The point here is that the quantity of interest submits to the discrete Poisson distribution.

Algorithm


Let us set the value of the parameter a - the average number of calls to tech support, for example, and we need to generate a random variable X that obeys the distribution law Po (a)

In the Random class of C #, which I have chosen as an example, there is a Sample () method that generates a random number from 0 to 1, which is evenly distributed on this segment.

So how do we get an integer from a real number? With the help of mathematical calculations, very simple from the point of view of experts in probability theory, we can prove the following statement.

Suppose we have a certain number of independent random numbers uniformly distributed on the interval [0; 1]. Then the smallest number X, such that the product X of our random numbers is strictly less than exp (-a), obeys the Poisson distribution with the parameter a

As a result, we get the following algorithm:

class Poisson: Random { public static uint Generate(double a){ uint X = 0; double Prod = 1; double U = base.Sample(); Prod *= U; while (Prod >= Math.exp(-a)) { X++; U = base.Next(); Prod *= u; } return X; } } } 


The computational complexity of this algorithm is O (a), but here we have to repeatedly generate a random variable, when, in fact, you can do with just one thing:

  public static uint Generate(double a){ uint X = 0; double Prod = Math.Exp(-a); double Sum = Prod; double U = base.Sample(); while (U > Sum) { X++; Prod *= a/Convert.ToDouble(X); Sum += Prod; } return X; } 


The complexity of this algorithm is also O (a).

Source: https://habr.com/ru/post/204324/


All Articles