Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 37 ยท Projects and Practice

Project: Build a Market Simulator

Build a small simulator in Python that finds equilibrium, then adds taxes, price controls, an externality and monopoly to see each effect side by side.

In this project you will build a small model market in Python and then change one thing at a time: a tax, a price ceiling, an external cost, a monopolist. Writing the model forces you to state every assumption, and comparing scenarios is exactly how economists work.

This is a stand-in: a hand-written model with straight-line curves, not data from a real market. Use it to understand the logic, not to forecast prices.

Step 1: Linear demand and supply

We represent a market by four numbers: demand Q = a - bยทP and supply Q = c + dยทP. The equilibrium is where they are equal:

P* = (a - c) / (b + d), Q* = a - bยทP*

Surpluses are triangles: consumer surplus ยฝ ร— (maximum price โˆ’ P*) ร— Q* where the maximum price is a / b, and producer surplus ยฝ ร— (P* โˆ’ minimum price) ร— Q* where the minimum price is -c / d (the price at which quantity supplied is zero; with c = -20 and d = 2 it is 10).

Python Step 1: the market class

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For this market the equilibrium price is 30 and the quantity is 40. Consumer surplus is ยฝ ร— (50 - 30) ร— 40 = 400 and producer surplus is ยฝ ร— (30 - 10) ร— 40 = 400.

Step 2: Add policies

Now extend the market with scenarios. We reuse the formulas from earlier lessons:

  • Tax t on sellers: seller price = buyer price - t.
  • Price ceiling or floor: the quantity traded is the smaller of demand and supply.
  • External cost e per unit: social cost = private cost + e.
  • Monopoly: price from demand at the quantity where MR = MC, assuming constant MC.
Python Step 2: policy scenarios

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Run it and compare every result with your hand calculations from earlier lessons. For the demand curve here (Q = 100 - 2P, so P = 50 - 0.5Q), the monopolist's quantity is (50 - 20) / 1 = 30, and the price is 50 - 15 = 35.

Step 3: Your own scenarios

Extend the model and write short answers to each question.

  1. Elasticity and tax incidence. Make demand steeper (b = 0.5 with a = 70) and tax the sellers $8. Who bears more of the tax? Explain using elasticity.
  2. Shift demand. Increase a from 100 to 130. How do price and quantity change? Which shifts of the curve are these?
  3. Welfare of a ceiling. Compute the total surplus with and without a ceiling at $25. Hint: only 30 units are traded, and the lost trades are the deadweight loss.
  4. Pigouvian tax. For an external cost of 10, find the tax that makes the market produce the efficient quantity, and the deadweight loss without it.
  5. Free entry. Write a loop that adds one firm at a time (each with the cost function from the monopolistic competition lesson) until profit is not positive.
Python Starter for question 3: surplus with a ceiling

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The starter measures total surplus as the area between the willingness-to-pay curve and the cost curve, ignoring who gets it. A ceiling also changes who gets the surplus; to study that you need to know which buyers get the scarce units.

Reflection

After building the simulator, notice how many results follow from the same two ideas: the equilibrium where demand equals supply, and the comparison of marginal benefit with marginal cost. Real markets add many details, such as many goods, time, uncertainty and information problems. The tools you have learned are a good foundation to add them.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In the simulator, a per-unit tax paid by sellers shifts which curve?

    Sellers now need a price that is higher by the tax to receive the same net price, so the supply curve shifts up.
  2. 2. With demand Q = 100 - 2P and supply Q = 20 + 2P, what is the equilibrium price?

    100 - 2P = 20 + 2P gives 4P = 80, so P = 20.
  3. 3. When a binding price ceiling is set, the quantity traded is

    The short side of the market limits trade.
  4. 4. Why is the simulator called a stand-in?

    It uses assumed linear curves to illustrate the logic, not measured data.

Summary

  • A market model needs demand, supply and the equilibrium condition.
  • Policies (taxes, controls, externality fixes, monopoly) are small changes to the same model.
  • Coding the model makes every assumption explicit and lets you compare scenarios quickly.

Next: Back to the Microeconomics course