# Simulating a demand curve

Recently, I've been watching the new microeconomics lectures at Khan Academy, which got me thinking about creating an economics simulation, something I've thought about many times. Specifically, I was wondering about how to come up with a reasonable demand curve. They always seem to be drawn as a straight line but with no justification other than simplicity.

### Utility curve

I started with the assumption that in a population, the utility of a given product, say an apple, is normally distributed. I expect the curve is less symmetrical in reality, but it's good enough for now. Below is a possible example, showing the distribution of utility values for a population of 1000 people.

For this curve, the mean utility is \$10 and the variance is 16. So pretty much no one thinks thinks an apple is worth \$25, ~100 think it is worth \$10, ~45 people think it is worth \$5, and ~8 people think it has negative utility (maybe they are allergic to apples).

### Generating a demand curve

We can use the utility curve to find how many people would by an apple at a certain price (all those whose utility for the apply is equal to or greater than that price), thus generating a demand curve. This requires integrating the utility curve from each price to infinity (which is done programmatically using the error function: math.erf()).

The demand curve is therefore a cumulative distribution function, except with the axes swapped because of the strange way economists like to draw these graphs. It shows that if we set the price at \$0 we can sell nearly 1000 apples; if we set the price at \$10 we can sell 500; and if we set the price at \$20 we will only sell a couple.

### Income curve

We can then calculate the income for any given price by multiplying the price by the quantity demanded at that price.

I was initially surprised that the maximum income is obtained with a price of \$8 and not the mean utility of \$10. I think the reason is that the utility curve has an inflection point at \$8, changing from an increasing gradient to an decreasing gradient. This presumably corresponds to the point in the demand curve where the elasticity equals 1. Looking at the utility graph, you can see that if you are currently charging \$10 and slightly decreas your price, a large section of the population will now find you apples a good deal.

### Low variance utility goods

If we consider a product with the same mean utility, but a lower variance in its utility (e.g. 1 instead of 16), then most people will consider it to have a utility of \$10 or close to \$10.

This unsurprisingly results in a high price elasticity of demand around \$10 as any deviation from \$10 causes a rapid change in demand. At prices much lower or greater than \$10 there is a low elasiticity of demand: if everyone thinks apples are worth \$10 and you lower your prices from \$25 to \$20, it won't make much difference to the quantity demanded. Similarly, if you raise prices from \$1 to \$5, most people will still consider it a bargain apple.

The income graph looks quite strange. In this case, the optimum price is ~\$8.5; above this price, the quantity demanded starts to fall rapidly. Compared to the previous situation, you have the potential to make more money (since fewer people believe apples to be worth less than \$8), but you have a lot less margin for error in your pricing.

### High variance utility good

Finally, we can consider a product with a high variance in its utility to people (256 instead of 16): many people consider it worthless, but a significant number consider it to be worth >\$25.

This results in a demand curve that is almost straight over the price range \$0 - \$25, and with a much larger gradient, representing a lower price elasticity of demand.

The result of this is that the optimum price is ~\$15.5, as you can focus on the proportion of the population that considers this product to have a high utility. You can't make as money as with the low variance product, but you can actually make slightly more than for the apples.

### Maximum income

Below is a plot of the maximum achievable income for different standard deviations (square root of the variance) of the utility. With very low variance (or standard deviation) nearly everyone will consider the utility of the product to be \$10 so you can get nearly \$10 000 by pricing the product at just under \$10. With less variance, you have to lower your price to ensure more people will buy your product. I'm not sure what the minimum at a standard deviation of around 8 represents.