# The theory of happiness. The law of the zebra and another queue

I continue to acquaint Habr's readers with chapters from his book “The Theory of Happiness” with the subtitle “Mathematical foundations of the laws of meanness”. This is not yet a popular science book published, very informally telling how mathematics allows us to look at the world and life of people with a new degree of awareness. It is for those who are interested in science and for those who are interested in life. And since our life is complex and, by and large, unpredictable, the emphasis in the book is mainly on probability theory and mathematical statistics. Here theorems are not proved and the fundamentals of science are not given; this is by no means a textbook, but what is called recreational science. But it is this almost playful approach that allows us to develop intuition, brighten up with vivid examples lectures for students and, finally, explain to nonmathematics and our children,

Published chapters:

We will talk about fatum, earthquakes, queues, and wonderful processes: the Poisson stream, random walk, and a little about Markov chains.

### Zebra law

They say that life is like a zebra: either a white stripe, or a black one ... And it also happens that another is added to one trouble, and so difficult in life, and then the cat has begun to give birth! That thick, it is empty! One to one! But the saddest thing is that when it’s good and the light band in life has come, bad thoughts creep in: oh, you wouldn’t jinx it, oh, would you have to pay for your happiness ... Familiar feeling? On this, one of the laws of merphology is formulated - the second law of Chisholm: " When things are going well, something has to happen in the very near future ." But since Francis Chisholm, in his original workIt does not give a detailed analysis or proof of this law, we will try to find out for ourselves whether there is any pattern behind this, or so it only seems to us. And if this is a fad of mathematics, is it possible to determine the characteristic duration or frequency of the strips on the body of our zebra, and on what does it depend?

Events happen every now and then. Sometimes they are not at all related to each other, sometimes they form chains of cause-effect relationships. Arguments about these connections, chains, and the predestination of life's journey can take us very far, and we will talk about them later. In the meantime, let's try, as always, to get away with the least amount of basic data for analyzing our law. Consider the sequence of events unrelated to each other, and see what can be obtained from it.

Events that are not related to each other and occur in time are randomly described using a well-known Poisson stream . It corresponds to many random phenomena from earthquakes to the appearance of buyers in the store. Poisson flow of events is characterized by the intensity or density of the flow - a parameter that determines the expected number of events per unit of time. For example, when measuring time in days, the value of the parameterwill correspond to a chain of random events, on average, occurring once a week. This does not mean that events will occur with a frequency of once a week. There can be no allotted frequency for the sequence of events at all. It is best to imagine a Poisson stream with intensity once a week like this: there are 52 weeks in a year, which means there should be about 52 events a year (on average, over many years). If we choose 52 random evenly distributed dates in a year, then they can be considered as moments of the occurrence of completely independent Poisson events.

An example of building a Poisson flow with intensity 1/7 (time is measured in days). On the interval of 365 days, 52 events that were unrelated to each other were randomly scattered.

At the same time, any kind of periodicity in these events is not discussed, when they wish, then they will happen. But even in this confusion, statistics can show us certain patterns. For example, the distribution of the duration of the periods between the events shown in the previous figure will not be uniform.

The density of the distribution of the duration of the intervals between 52 events, randomly scattered over a segment of 365 days.

The distribution of the durations of the intervals tends to exponential, it is shown as a solid line. This distribution has a maximum (mode) at zero, and the mean value is just 7 days. Moreover, the standard deviation will also be 7 days. Equality of standard deviation and mean value is a characteristic property of the exponential distribution. As you can see, these characteristics do not guarantee at all that one week will pass between events, on average - yes, but more often - less, besides, quite long intervals can be observed. Finally, the median shows that half of all intervals will have a duration not exceeding 5 days. The intensity and frequency is not the same, this is a very important note!

For justice, we suppose that good and bad events occur equally likely, but bright and significant events occur much less frequently than minor and minor ones. Let it be a normal life in which the emotional coloring of events is subject to a normal (Gaussian) distribution. This is how a year of synthetic fate may look like a series of random, completely independent life's vicissitudes:

A series of events of different emotional coloring, forming a Poisson stream with an intensity of 2/7 (2 events in 7 days).

While no bands are observed, there is some noise instead. Each event passes without a trace, leaving nothing in memory or in the mood. It does not happen, let's endow our model hero with a memory, for a start, an ideal one. Let each event be forever bumped into his memory and mood, respectively, either improving or worsening it. Here is a picture we can get by observing the fate of our hero for ten years.

Events, merging in memory, form the emotional coloring of "synthetic life."

Well, well, we already see some kind of alternation of mood, but the picture came out not very happy. Our hero after a series of mood swings fell into a deep depression. It's a pity. Let's try some more destinies. All of them experience a series of light and dark stripes, but for a long time they get bogged down either in hopeless longing, or in transcendent happiness. This happens, of course, but this is clearly not normal.

A few examples of "synthetic destinies" of people with an ideal memory.

## Relax, dude!

We described our model fates as a very remarkable process, it is called a one-dimensional random walk and has a number of unusual properties, among which is self-similarity , that is, the absence of any characteristic time scale. In addition, having received at its disposal for an unlimited time, a random walk can take you unlimitedly far away, and moreover, it will certainly take you to any predetermined distance about the initial value! So, no matter how good your business is, if they are subject to random wandering, they will surely slide to zero and go lower, it's just a matter of time! This famous and instructive law of life received the name of the player’s curse and its essence can be expressed by a simple maxim:
The optimal strategy in gambling is to own a casino, otherwise you will lose.
We will not dwell on this, painfully known result, but we will meet this property of a one-dimensional random walk.

It seems that the ideal emotional memory is not very good. Our heroes do not forget anything and carefully keep everything in their memory, even the most ancient events! Grief from a broken toy in childhood or joy from a kiss in adolescence affects their mood in old age. Moreover, all subsequent kisses and toys are of the same importance to them. It is necessary to save these poor fellows. Emotions subside with time, grief is dulled, joy, alas, too. Forgetting, in many respects, is like cooling, diffusing or slowing down movement in a viscous fluid, therefore it is reasonable to model it in a similar way. These processes are called relaxation processes.. We will also give our poor fellows the ability to relax!

Relaxing system returns to equilibrium, and, the faster, the greater the deviation from equilibrium. This property can be modeled by a geometric progression, or an exponential law . We introduce into our model a new parameter - the speed of forgetting.. It can be expressed through time (in the readings of our model), for which the level of emotion will decrease quite strongly. For example forthe emotional trace of the event will decrease by an order of two months. And now life has become a good "striped"!

The limitation of memory leads to the fact that a series of events and their traces in memory merge to form a series of emotionally colored bands.

By changing the “degree of forgetfulness,” we can get more or less emotionally balanced experimental subjects. It seems we found the source of zebra-like! This is, firstly, random walks, prone to spreading in all directions, and, secondly, healing forgetfulness, which returns the mood to normal. The result is a wave-like mood meandering.

Let's study the properties of the “synthetic” everyday bands we received. We construct a histogram showing the distribution of their durations for a long life (or for many ordinary ones) with parameters.

Distribution of the durations of periods of happiness and grief for a large number of synthetic destinies. The vertical line indicates an average value of 33.

The first thing that catches your eye is that the distribution maximum (mode) is near zero, which means that most often the periods of happiness and unhappiness are very short, however, periods of more than a year are also found. On average, the duration of the periods is 33 days, with a standard deviation of 36 days. This distribution is close to exponential (in fact, it is well described by a more general gamma distributionwith parameters that bring it closer to exponential). In turn, the exponential distribution of the durations of bands in life means that mood changes can be viewed as a Poisson flow, that is, as a chain of independent random events that do not have a selected frequency, but occur with a certain known intensity. For example, in the example we have considered, dark and light stripes are replaced with intensity once every 33 days, but at the same time, there are short periods most of all in life: half of them are no longer than ten days.

In the absence of "memory" (for), the distribution ceases to be exponentially decreasing and becomes a power law.

The distribution of the meander duration for a random walk has the character of a power distribution.

Statisticians say that such distributions have a heavy tail, making it very likely very large deviations from the mean, we observed them in the form of long “dives” in a different mood. The distribution obtained by us has one, unusual and strange property - neither the mean value (expectation), nor the standard deviation, nor the median are defined for it. The fact is that all these characteristics are calculated based on the area under the density distribution curve, and it is infinite. In this regard, you can hear that the average value in this case is infinite, but it is not. See what happens when you try to calculate the average value of the meanders of random walk:

An attempt to calculate the average value for a sequence of durations of periods between mood swings in the absence of memory. Emerging extremal values ​​from the heavy tail of the distribution lead to the fact that the mean value does not converge to any value.

Huge jumps originating from the heavy tail continually knock down the mean value and the sequence of averages do not converge to any limit. The value of the mean is not infinite at all, it’s just that the integral does not converge to any number or any particular value. It is in the impossibility to calculate the average value of the duration of the meanders that the self-similarity property of a random walk is reflected, namely the absence of any proper time scale.

We simulated adaptability to everyday troubles with the help of relaxation, or attenuation of emotional outbursts. It is possible to interpret this process in a different way, as a person's adaptability to life circumstances. When processing noisy signals or sequences, the moving average method is often used for smoothing and extracting the useful signal, considering at each moment not the signal itself, but the average value of the signal for a certain period of time. Thus it is possible to get rid of the noise and get an idea of ​​the long-term trends of the signal. Applying such averaging to everyday turmoil, we can simulate human adaptability. And during wars, people fall in love and find a reason for joy, just as the lives of rich idlers are not without a cloud. Shift rate from which the mood deviates in one direction or another. Considering the difference between the sequence of emotions and the smoothed background line, we get the same picture, bands, which gave us the previous model, with the same statistical characteristics. This is not surprising, because conceptually they practically do not differ, describing a system with relaxation.

Meandering and changing moods can be obtained by simulating a person's adaptability to circumstances using a moving average.

### Connected by one chain

In the considered models, we obtained a Poisson mood change flow, generating events by a Poisson flow. This can be seen some fraud - the Poisson random process was “sewn” into the model. How universal is our result? Is it possible to get it somehow differently?

Everyday experience is a poorly formalized piece, and it can be adjusted to fit various mathematical tools, allowing not only simplifying assumptions, but also speculation. In science, this approach is unacceptable, but on our journey through the methods of the theory of random processes, we can afford to play with them to get to know better.

Observing the dynamics of mood and world perception, it can be noted that it is common for a person to “stick” in a certain mood. If things are going well in general, then bad news can be received with optimism. And, on the contrary, a melancholic mood, once swallowing a person, can ruin even good news. From a mathematical point of view, this means that the probability of remaining in the current mood is more likely to change it. This behavior can be described using a random process called the Markov chain.. In general, a Markov chain can be represented as a fixed set of states with transitions between them, moreover, transitions from state to state have different but known probabilities. It is convenient to represent such chains in the form of weighted graphs, for example, an elementary symmetric Markov chain describing the dynamics of mood can be represented as follows:

Markov chain with two states ("joyful" and "sad"). Arrows indicate transitions and probabilities of these transitions. In our symmetric case, the probability of remaining in the existing mood exceeds the probability of its change, but does not depend on the mood itself.

Our chain is capable of generating sequences of states and, of course, stripes of everyday zebra will appear in it.

Most interestingly, find out what distribution will obey the duration of these bands. For our more than simple model, the answer can be obtained exactly — this is the geometric distribution describing the probability of observing a given number of trials of a random experiment before the first “success”.

The histogram for the durations of periods of the same mood in the sequence generated by the symmetric Markov chain and the probability function of the geometric distribution with a parameter equal to the transition probability between states. The sequence has a duration of ten years.

The geometric distribution is a discrete analogue of the exponential distribution, in the sense that it is subject to rounded values ​​of an exponentially distributed random variable. There is a relationship between the geometric distribution parameter and the intensity of the corresponding exponential distribution. Thus, we again get the Poisson mood shift flow, and for the Markov chain we described, its intensity is equal to.

If we break the symmetry of the chain, then we will be able to describe the “optimist” or “pessimist”, more readily “stuck” in one or another mood. The distribution of the durations of the bands at the same time deviate from the geometric, but at the same time, still, most of the bands will be short. and any selected periodicity will not be observed.

The histogram for the durations of the periods of constant mood in the sequence generated by the asymmetric Markov chain. The sequence has a duration of ten years.

Markov chains are a powerful tool for analyzing random processes in which a certain algorithm or scenario is hidden. They give us a peculiar look at the processes that are commonly considered cyclical. For example, the well-known maxim: “the history of mankind walks in a circle” is often interpreted as the fact that there are certain cycles or even periodicities in history. It is necessary to hear, for example, that the beginning of the century promises turmoil and war. Risking not getting into my own topic, I will take the liberty to assume that in fact it makes sense to speak not of literal cycles, but of more or less stable scenarios — regular chains that can be described by the Markov chain. Among Markov chains there is a class of cyclic chains, which, in fact, are capable of creating repetitive sequences. However, there is no real deterministic periodicity in their behavior. Occurring randomly in different historical periods and in different contexts, such cycles are similar to each other, and can create a sense of historical “déjà-vu”. It is useful to study and describe them, but you should not expect a strict calendar plan.

On this we close the theme of the zebra. What conclusions can be drawn from our frivolous research? A series of light and dark stripes in life is not an illusion, they really are. But there are no special patterns in them. Most often they are short, but they are also protracted. It all depends on the lightness of character and the ability to let go of the past. Moreover, if events in life happen rarely, then life will become a gray series of memories disappearing in the past. So it is in our interest to memorize what has been lived, and it is in our power to ensure that life does not become a random walk. We can make the events of good ones become bigger and take place more often, even if they are insignificant. Ski walk, sincere smile of a passerby, ticket to the concert, a cup of hot chocolate on a cold day, All this will help to create a positive trend and extend the light strip in life. True, following the trend, the average value will be pulled, so that the inevitable sad events will surely change the mood. But do not blame your happiness. This is not a retribution for him, and not the evil eye. This property of relaxing systems is the tendency to oscillations under stochastic external influence.

### About waiting for a bus or an earthquake

The difference between the frequency (period) and the intensity of the flow of events is important enough to understand, listening to the news or reading the results of scientific research. For example, today, seismologists, alas, cannot predict a specific earthquake: its time, place, and power. On the other hand, long-term seismic forecast methods have been developed for a particular region, but their results are formulated in the language of probability theory, and what to do with them is not always obvious.

For example, for Avachinsky Bay, on the banks of which Petropavlovsk-Kamchatsky is located in 2018, this forecast is given: “The total probability of earthquakes with a magnitude of more than 7.7, which may be 7-9 points in Petropavlovsk-Kamchatsky, may reach 52.3% over the next five years.” What does this mean? Tomorrow shake? And when? And where? Alas, we are not yet able to answer such direct questions. The exact interpretation of this message is as follows: the intensity of the seismic flow is now such that in the next 500 years approximately 52 earthquakes will occur (assuming the flow does not change). Moreover, a month later, the forecast may change. Intensity is similar, in some sense, to instantaneous speed of movement: to measure that you are moving at a speed of 60 km / h it is not necessary to travel all 60 km for an hour. And, most importantly, the forecast given by scientists does not mean that ten years pass between earthquakes, as it can be assumed, dividing 500 years into 52 events. Thus, if for ten years there have been no strong earthquakes, this does not mean that it will happen not today or tomorrow. It will happen, of course, but how long it will have to wait is unknown.

Look at how the level of seismic activity of the Kamchatka region changes at different time scales (the image is taken from the site of the Seismic Activity Monitor of the Kamchatka Branch of the Unified Geophysical Service of the Russian Academy of Sciences)

The low level of activity is replaced by an increased one, the activity “breathes”, but not periodically, but similarly to the same random walk with relaxation.

But earthquakes, nevertheless, unpleasant phenomena and let them not have happened longer. There are things that you expect with great impatience, for example, a bus. Coming to a stop, we, of course, want to instantly take the desired bus or tram route, but, most likely, this is not possible. Then, if there is a clear timetable in this place, we look at it, then at the clock, and then plunge into a book or phone. But often, in the middle of the route, instead of the schedule, the interval of traffic is indicated, for example, 15 minutes. This means that we are already far from the bus station, from which the buses leave on time, and some error accumulates, which makes the arrival of the bus a random one. Well, 15, so 15, wait. And here we must keep in mind that on average you will have to wait just 15 minutes, regardless of when you come.intervals of 15 minutes, the average waiting time would amount to half of the period - 7.5 minutes, but with an intensity so will not work! In the absence of additional conditions, the traffic flow is modeled by a Poisson flow, which means that the waiting time for a bus will obey an exponential law with the same intensity. And the expectation for an exponential distributed quantity with intensity equally This is how our conclusion comes out. And what a shame - how much time you have already spent at the bus stop does not affect the likelihood that the bus is about to come. This works such a property of exponential distribution, as the lack of memory associated with the independence of Poisson events.

Summarize. Coming to a bus stop, you need to make a clear decision: wait, or go on foot, and think about the topic: wait a while or go on foot — only to doom yourself to a meeting with the law of meanness. For when you, having waited for 17 minutes, spat, and went on foot, you are very likely to be overtaken by the long-awaited bus, or even two. Well, and in order to wait for the bus turned into a tedious and hopeless competition with fate, let us remember the curse of the cyclist from the previous chapter, effectively extending the observed latency.

Interestingly, the Lorenz curve for the exponential distribution is the same for any intensities. Thus, for all Poisson processes, the statement is true: half of the total observation time falls on 20% of cases when this next event is delayed. The Gini coefficient for the exponential distribution is exactly 1/2.

### Alien queue

I think about the laws of meanness, standing at the airport in line for passenger check-in and baggage check-in. The line is long, people are different and visible with all their bags, children or cells. I hear grunts from behind: “As usual, our turn is slowing down. Look, look, that mustache in a cap stood on a par with us and now there where ... That's the law of meanness! It’s always my turn to be slow! ”This law is called Ettore’s observation:
“The nearby queue always moves faster.”

There is a whole area of ​​control theory and random process theory that deals with queue dynamics. This is important for the design of shops and waiting rooms, optimal management of the operating room in the bank, the gate to the toll highway and document flow. And the starting point for modeling the queue is the same Poisson flow, because it requires a minimum of additional assumptions. Thus, the movements of the one in the queue will have the appearance of a monotonously growing stepped line, with identical steps occurring at random intervals. The accumulation of data in this way is called the Poisson process .

This is how two long lines can move:

Moves two adjacent queues, as the Poisson processes with equal intensity.

In turn, the difference between two identical Poisson processes, namely, it is observed by a person bored in the tail, is a kind of random walk. And if so, then we are ready to draw some conclusions. First: the distance between people who simultaneously get into the same queues will either increase or decrease, and the characteristic meanders will form with a constantly changing duration. The second conclusion: due to the self-similarity of the random walk, both for short queues and for long ones, the meanders will have a duration that is commensurate with standing in line, which means they will be noticeable, and the meanders are a cause for discontent. The third conclusion: it is not known in advance which queue will pass faster, because a random walk equiprobably goes both up and down. And finally, the fourth conclusion: the queues move independently,Skellam distribution ). It turns out, or guessed with a quick queue or not - no mean things from the villain of fate!

But the laws of meanness would not be called laws if they did not claim to be universal. If we are not lucky to be in a lagging queue, then we will spend more time in it and we will have more opportunities to complain about our fate! And now, attention, good news: in any selected time interval, those who are lucky enough to get into a fast queue are more than lucky ones, because a quick queue can be missed by more people! But, alas, this does not comfort the one who is stuck in the queue.

The presented text has not yet been published, which means that it can change. I expect that the comments and observations of Habr's readers will help him change for the better.