Classic Market Emulator

image

Every time I read something about the economy, I constantly miss the moment between “yes, of course, this is all obvious” and “why?” how did you get it? ”

This is usually due to the fact that examples that can be cited from real life are either elementary or so complex that they no longer help in understanding.

But you can make some kind of simulation to illustrate ideas? Not really from real life. Throw away the insignificant, leave the important and see what happens?

Under the cut, I will tell you how I tried to write an emulator of the simplest market competition and what happened in the end. There will be many charts.

What do we do?


Consider, for starters, the most classic, everything as grandfather Adam Smith loves: we have buyers and manufacturers of goods that compete with each other.

The classical theory claims that in the absence of other factors (!) Competition between sellers leads to cost optimization and the establishment of an equilibrium price.

Moreover, it is argued that this same equilibrium price will be significantly lower than if the seller would be one (monopoly) or several (oligopoly).

It sounds sensible, logical, interesting. In practice, however, we do not always observe this. Too many additional factors.

I’ll try to formulate the main reasons why “pure” competition is almost never encountered in real life:

  • , ( ), , ,
  • , ( , ), “ ”
  • ( , , , )
  • ( , , )
  • in addition to the participants in the sale process, there are a huge number of external factors (states, with taxes and duties, geographical concentration, random factors like crop failures, disasters or spontaneous human decisions)

There is a desire to try to take into account all these factors, except the last.

We formalize the problem


The main characters:

  • consumers (buyers, citizens, citizens)
  • factories (workshops, manufacturers, traders, factories)

Consumers regularly earn money and have a need for a product.
In our model, there will be only one product. It satisfies at once the entire pyramid of needs of all consumers.

All factories produce the product, the differences are only in quality and price. Factories themselves enter the market and sell their product to citizens, without intermediaries.

The goal of consumers is to purchase the best product for them, spending a minimum of money.
The goal of factories is to maximize their capital.

In addition to the fact that we have only one product, it still has an objective and transparent numerical characteristic of its quality. Both factories and consumers value this quality the same. These are the properties of a product that appears along with its production.

Factories have some production capabilities: cost (costs, below which the product will not be produced) and maximum quality (quality value, above which the product will not work).

After the sales round, factories invest part of their capital modernization. Because scientific and technological progress is a complicated thing, the algorithm works this way: every factory that still has money spends a random amount of money to randomly change its production capabilities (it can get worse).

When a factory runs out of money, it no longer produces anything.

The full market cycle is as follows:

  1. Factories, if they can, produce products and market them.
  2. Consumers come and choose the best option for themselves (from available for them)
  3. Deals are made
  4. Factories upgrade if they can

And so - a sufficient number of iterations.

The theory says that in such conditions, factories will compete fiercely with each other, which will lead to lower prices and / or quality of products.

Let's see what happens in practice.

Start of implementation


As a development tool, I chose python Jupyter Notebook - an environment that is as friendly as possible to work with data, fast tables and graphs in a hurry.

The emulation process itself didn’t seem so scary, but to summarize the results, I wanted to use the rich pandas tools.

The code is available here.

I don’t think that the main implementation will interest anyone: three classes (factory, consumer and product), functions for working with instances of this class, functions of the “market”, everything is in a loop, everything is logged, the results are written to a file, then pandas takes over.

First adjustments


Quickly enough, it became clear that inequality between people and between factories can and should be introduced into the model.

I note right away, just in case, that the names of the distributions are conditional below, they are enclosed in quotation marks and are simply a compressed metaphor. The goals were not to make fun of someone’s political convictions or, moreover, to demonstrate their own.

I tried several models of the distribution of indicators:

  • “Communism” - each object has a random number, the probability distribution is uniform
  • “Developed socialism” is the same thing, the probability distribution is normal (for most, the average salary)
  • “Modern society” - the “tailed” probability distribution (millionaires at a greater distance from the average salary than the poor), random.gammavariate function

If we take consumers as an example, the type of distribution of their salaries directly affects the form of so-called “Demand curve” (dependence of the proportion of people willing to buy a product, depending on its price):
image
Demand curve for the three distributions of the population’s income: “communism” (equal), “developed socialism” (normalize) and “modern society” (gamma) .

As you can see, in the case of an even distribution, the demand curve turns into a broken demand (the probability of buying 1 when the price of the goods is less than the minimum wage, then decreases linearly).

In the case of a uniform distribution of income, the demand curve becomes like a sigmoid.
In the case of a distribution of income similar to the present, the sigmoid becomes asymmetric. And it becomes more like demand curves from economics textbooks:

image

Does this mean that the very logic of the demand curve implies income inequality? No, there are a million factors. But we can roughly generalize, saying that the unevenness in the “strength of demand” (someone is willing to overpay for a cheburek, someone is ready to wait an hour in line, and someone is not) is distributed approximately the same as the real incomes of the population.

By analogy, we can construct a supply curve.

The supply curve in this case is how many plants at what prices are ready to produce a product of a given quality.

If we take 50 plants, the curve is obtained with a large number of faults, and with a large number it tends to a straight line.

image
Supply curve for k = 50 plants

image
Supply curve for k = 1000 plants

To obtain a curve from a textbook, a) many sellers are required, b) a uniform distribution at cost.

This is not obvious, interesting, and requires additional interpretation.

So, the first victory.
We have some pale similarities of supply and demand curves from textbooks.

We are laying inequalities and moving on.

In most cases, for distributions, I laid down gamma distributions (which are “tailed”):

  • incomes of citizens (see the chart below, average salary 25, minimum 5, maximum 160)
  • start-up capital of factories (see graph, the logic is the same, the spread is less)
  • production capabilities of factories (quality limit from 2 to 30, prime cost - from 1 to 5)
  • the chances of a successful modernization (but they are distributed normally)


image
( , — , )

image
( , — , )


If we did not have any changes, long-term modeling would not make much sense. Modernization means that factories will try to compete with each other. Those who have big sales (starting capitals, good conditions) will not be able to rest on their laurels for a long time, competitors will quickly approach in price and quality of manufactured goods.

However, it quickly became clear that factories needed “memory." It often happened that a successful factory began a cycle of negative modernization and lost everything.
Therefore, I had to add this logic to the algorithm: if there were sales before the modernization, and after it disappeared, the next round of modernization will be in the format of “roll back everything”.

Parameters of the first serious emulation


Starting conditions:

  • number of inhabitants, N = 2000
  • number of plants, k = 50
  • , R=30
  • -

The result is written in two files: a log of citizens and a log of factories.

First log data:

image
where
id is the id of the purchased product (it should have been called normally)
citizen_id is the unique number of the citizen,
period is the number of the emulation round
money is his salary in this round,
quality and price are the quality and price of the product he bought in this round (-1 if I didn’t buy anything)

Second log

image
where
id is the id of the factory,
period is the number of the emulation round,
capital is the capital of the factory in this round,
pur is the number of sales in this round,
price is the sale price
cost, quality is the cost and quality respectively
lag - data from the last round

It seems time to watch the results.

See the results


What result do you want to see first? Probably how many plants went bankrupt, failing to put on the market an interesting offer.

The result is this.

  • They completely went bankrupt (went to minus) - one plant. Sad but true.
  • No money for modernization - 45 plants. It is almost ruined. But if suddenly a demand for their goods suddenly appears, they will theoretically be able to get out.
  • There are capital, no sales - 3 plants. Once, everything was fine with them, but then their competitors removed them.
  • There are capital, there are sales - 2 plants. One sells expensive products of maximum quality, the other - a very cheap acceptable quality. Cartel conspiracy, divided the market, that's all.

At least one sale in 30 rounds had 11 factories.

The dynamics are a little more complicated. There is a lot of data.

With those factories in which sales did not work, as in happy families - everything is about the same. The difference is only in the speed of fall.

image
The dynamics of changes in the capitalization of those factories for which there were no sales: everyone started from different starting conditions, spent different amounts on modernization, but there is only one result: everyone fell “to zero” or floundering around him.

It’s more interesting, but also more difficult with happier “families” - who still had sales.

image

We have a champion who is doing well (id = 11) - there are sales, capital is growing. But in order to find your audience, it took 7 moves.
The only competitor with sales (id = 0) was able to reach only the third place in terms of capital. Second place (id = 20) from the 10th round rests on its laurels and slowly loses money. The rest lose money faster.

It is worth noting that, according to the initial conditions, our leaders held a different position. The second place in capitals “in the end” (id = 0) was the absolute leader in initial capital, the “winner in the consumer goods market” (id = 20) was in the thick of the leaders, but the “champion” occupied only 11th place in initial accumulations.

It seems still interesting to see the dynamics of sales on their own.

image
Dynamics of the number of sales by factory.
Horizontal: round number. Vertical - the number of consumers (out of 2000) who made purchases (different factories have different shades).


What you should pay attention to?

Firstly, on the first move, not all residents could afford to buy at least something (obviously, these are people with minimal incomes). Quickly enough, this number dropped to zero, then dipped again, and then 100% supply was reached.

Secondly, some kind of competition was long enough. On the first move, 6 factories had sales, on the fourth - 7, on the fifteenth - 4. For a long time there were 3 sellers (one had quite microscopic sales) and only by move 27 was it finally “removed from the market”.

It seems worth a little get carried away by the “survivor's mistake”?

Let's look at our champions in more detail?

Id = 11 (top-1 in sales and capital) “from distribution” had an acceptable quality, but high price (price = 37, with a median salary of 27.5, that is, most could not afford to buy these products). Got 8 percent of the market. Made a generally acceptable modernization - increased quality and raised the price. Got zero sales. Rolled back. Got almost 30% of the market (obviously, the failures of competitors helped here). Made a perfect upgrade - increased quality and lowered the price. Got zero sales, alas, at that moment offers came out even better. In order to get sales it took a series of 4 upgrades “at least not back”. By the ninth move, the offer already looked great - a product with a quality of 43 for 21 (users noted this, giving it two-thirds of the market). But a sharp price increase - and again zero sales. Then three systematic trends: quality is better,price is lower, sales are higher.

image
Dynamics of changes in the main parameters of the factory supply id = 11 (top-1 in sales and capital).

The peak of the “career” is 21 moves, 99% of the market, but it is worth raising the price a little - and the competitors are right there.

The only survivor in sales, except for the leader, had low prices and low quality from the very beginning. Sales were irregular, the main vector of modernization was on lowering prices. He received stability only on the 25th move, when with a quality of 31 he was able to sell goods for 3. Since then he has firmly taken his niche and has been successfully fighting for the “lower middle class”.
The peak of the “career” is the last move, 41% of the market.

I wonder what would happen if the simulation lasted a couple more dozen moves?

image
The dynamics of changes in the main parameters of the factory supply id = 0 (top-3 in terms of capital, has sales by the end of the simulation).

The current top-2 in terms of capital (id = 20), which has not had sales for the last 10 rounds, was the leader in start-up capital (therefore, it could not lose everything in 30 moves), it produced moderately poor moderately cheap goods. Successful modernization of quality allowed us to get good sales and keep them long enough. The deterioration in quality did not affect income for a long time, because the price was down. It all ended on move 20. A good upgrade did not save. Probably, it’s a shame to have a stable 10% of the market, at the same time improve quality and price and lose sales (id = 0 came out with an even more effective upgrade).

No further action helped either return the old audience or find a new one. However, the factory is separated from complete ruin by 8 to 20 rounds. Enough time to finally get lucky.

image
Dynamics of changes in the main parameters of the factory’s supply id = 20 (top-2 in terms of capital, has not had sales for the last 10 rounds).

It seems that some pattern is being traced. Factories that offer better products at low prices are booming. The rest are dying.

Let's see how the market has changed on the part of the consumer in 30 moves.

image
Value for money at the initial stage. There are 50 product offers on the market.
Horizontally - the price of the product, vertically - its quality.
Blue indicates “user choice”, i.e. those products that had sales in this round.


Here you can clearly see how factories are in unequal conditions. Someone may not do anything and have good performance. Factories, however, “from the lower right corner” will need significantly more actions in order to find their customers.

How will the picture change by the end of the simulation?

image
Value for money by the end of the simulation. Blue indicates “user selection”.

If at the beginning the majority bought goods of quality 26 for 18 (at the same time there were those who bought goods of quality 30 for 42 and goods of quality 15 for 7), then in the end the majority buys goods with quality 57 for 24 (and part - goods of quality 28 for 1.16 ) There is a change!

And how did it look in dynamics?

image
The dynamics of changes in the price of goods sold.
Horizontally the round number, vertically - the selling price, each factory is marked in a separate color, the larger the radius of the circle, the greater the sales volume.


Interestingly, in this simulation the “premium” price segment (because of which there wasn’t much point in watching the average price or the average quality) disappeared as such.
This was due to the fact that the quality of the “mass market” was equal to the quality of “premium products” rather quickly.

image
Dynamics of changes in the quality of goods sold
Horizontal number of the round, vertical - product quality, each factory is marked with a different color, the larger the radius of the circle, the greater the volume of sales.


Those. from the point of view of the user it became better. Theory triumphs.

Now let's see how the factories began to live. The fact that the majority did not work is understandable. I wonder what are the chances to make a good action (lower prices, improve quality) and get an improvement in sales.

So, modernization No. 1: price reduction.

According to the condition, the chance to go in the right direction is 25% (the same amount - in the opposite direction, in other cases, the modernization will be on another parameter).

If you did everything right, the chances of improving sales will be 14% (43 out of 310 outcomes) and another 5% for what will become worse (but also 3% of the chances of improving sales with rising prices).
Almost always, this is due to a poor starting position. But several times this was due to the fact that competitors did not stand still.

Modernization No. 2: quality improvement.

The chances to go in the right direction are the same.

The chances of improvement in the case of correct actions are the same 14%, but the chance of deterioration is higher - 9% (and a 7% chance of improvement when moving in the wrong direction).

This does not seem to be a mathematical effect. Perhaps just a game of small numbers.

There is one more question.

We have "survived" a few factories, most went bankrupt. Is it inevitable?

It’s worth a couple more simulations, but it seems so.

Only the matter is not in the market, economic laws and statistics. The point is the selection algorithm.

We now have a user’s choice - a clear function of his salary, price and product quality. Accordingly, a product that is worse than the ideal one millionth in the current model does not have a single chance - all 100% of the market will go to the ideal product.

In real life, random factors influence a user's choice. They could be laid.

That's all for now.

Conclusions briefly


Non-economic conclusions:

  • Playing with such models is fun
  • Data can be twisted in very different ways.
  • Well, when everything is pledged

Economic findings:

  • It seems that in the classical economic model the inequalities of starting conditions are inherent
  • Curves of supply and demand, similar to training, can be built, but they will be curves (rather than broken) with a very large number of both buyers and sellers
  • In general, the main points of the theory (competition among sellers leads to better conditions for buyers) are correct
  • Starting conditions strongly affect the final result, but do not predetermine it
  • ( “ ” “-” )
  • ,


  • ( )
  • ,
  • - ( )
  • “ ”,

All Articles