# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + tags=["remove_cell"]
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import sympy
import numpy as np
import warnings
warnings.filterwarnings('ignore')
plt.style.use("seaborn-muted")
solve = lambda x,y: sympy.solve(x-y)[0] if len(sympy.solve(x-y))==1 else "Not Single Solution"
# -

# # Market Equilibrium and Government Intervention

# ## Market Equilibrium
#     
# Over the past two weeks, we have individually introduced supply and demand, represented as functions of quantity supplied or demanded, respectively. Now let's bring them together.
#
# The intersection of both curves is called market equilibrium: the condition when quantity supplied equals quantity demanded at a given price. This is the "market-clearing" condition: $Q_s = Q_d$
#
# This means that for any given equation of S and D, we can find both the equilibrium quantity and price. As demand and supply functions solve for price, we simply equate the two functions together. To demonstrate this, we will use SymPy.
#
# For simplicity's sake, we have defined `solve(x, y)` that returns the value of the input variable that results in equivalent values for the equations `x` and `y` that were passed in.

# +
Q = sympy.Symbol('Q')
P_demand = -0.04 * Q + 20
P_supply = 0.02 * Q + 14

Q_star = solve(P_demand, P_supply)
Q_star
# -

# `Q_star`, the quantity of goods produced and sold at equilibrium, is equal to 100. To get the equilibrium price, we can substitute `!_star` into either `P_demand` or `P_supply`:

P_star = P_demand.subs(Q, Q_star)
P_star

# This gives us that `P_star` is equal to 16.

# ### Movements away from equilibrium
#
# What happens to market equilibrium when either supply or demand shifts due to an exogenous shock?
#
# Let's assume that consumers now prefer Green Tea as their hot beverage of choice moreso than before. We have an outward shift of the demand curve - quantity demanded is greater at every price. The market is no longer in equilibrium.

# +
# [INSERT GRAPH: DEMAND SHIFT RIGHT, SHORTAGE AT ORIGINAL EQ PRICE]
# -

# At the same price level (the former equilibrium price), there is a shortage of Green Tea. The amount demanded by consumers exceeds that supplied by producers: $Q_d > Q_s$. As a result, buyers would bid up the price of Green Tea. At the same time, producers are incentivized to produce more, causing $Q_s$ to increase along the supply curve. This market-clearing force (sometimes referred to as the "invisible hand") pushes $Q_s$ and $Q_d$ to converge to a new equilibrium level of quantity and price.

# ## Theoretical Government intervention
#
# Now that we have discussed cases of market equilibrium with just demand and supply, also known as free market cases, we will examine what happens when the government intervenes. In all of these cases, the market is pushed from equilibrium to a state of disequilibrium. This causes the price to change and, as a result, the number of participants in the market. 

# ### Taxation and its effects on supply and demand
#
# The primary method that governments use to intervene in markets is taxation. This occurs through percentage or fixed taxes that increase the net price that consumers eventually have to pay for a good or service. Taxes can be levied on either the producer or the consumer.

# +
# [INSERT GRAPH: SUPPLY CURVE SHIFT LEFT]
# -

# If it is levied on producers, this decreases the quantity of goods they can supply at each price as the tax is effectively acting as an additional cost of production. This shifts the supply curve leftward.

# +
# [INSERT GRAPH: DEMAND CURVE SHIFT LEFT]
# -

# If the tax is levied on consumers, this increases the price per unit they must pay, thereby reducing quantity demanded at every price. This shifts the demand curve leftward.

# +
# [INSERT GRAPH: DWL TRIANGLE + TAX WEDGE]
# -

# The resulting equilibrium - both price and quantity - is the same in both cases. However, the prices paid by producers and consumers are different. Let us denote the equilibrium quantity to be $Q^*$. The price that producers pay $P_p$ occurs where $Q^*$ intersects with the supply curve. At the same time, the price that consumers pay $P_c$ occurs where $Q^*$ intersects  the demand curve.
#
# You will notice that the vertical distance between $P_p$ and $P_c$ will be the value of the tax. That is to say, $P_c = P_p + \text{tax}$. We call the vertical distance between $P_p$ and $P_c$ at quantity $Q^*$ the tax wedge.

# ### Incidence
#     
# Determining who bears the greater burden, or incidence, of the tax depends on the elasticity of the good. An inelastic good would mean that it is more of a neccesity than an elastic one. This allows the producer to push more of the tax onto consumers, who are willing to purchase as much of the good even as prices increase. The incidence or burden of paying the tax will thus fall on consumers moreso than producers.
#
# With elastic goods, incidences are distributed in the opposite way. As elastic goods are less of a necessity, consumers will substitute away from them when prices increase. Thus, when the tax is levied on an elastic good and its price increases, consumers will be more willing to switch to other goods, thereby leaving the producer to bear more of the tax-paying burden.
#
# One can calculate the burden share, or the proportion of the tax paid by consumers or producers:
#
# Consumer's burden share: $\dfrac{\text{Increase in unit price after the tax paid by consumers} + \text{Increase in price paid per unit by consumers to producers}}{\text{Tax per unit}}$
#
# Producer's burden share: $\dfrac{\text{Increase in unit price after the tax paid by producers} - \text{Increase in price paid per unit by consumers to producers}}{\text{Tax per unit}}$
#
# Graphically, the total tax burden is the rectangle formed by the tax wedge and the horizontal distance between 0 and $Q^*$: $Q^* \cdot \text{tax}$ This is also how you calculate the revenue from the tax earned by the government.

# ### Deadweight Loss
#
# Naturally, the introduction of the tax disrupts the economy and pushes it away from equilibrium. For consumers, the higher price they must pay essentially "prices" out some individuals - they are now unwilling to pay more for the good. This leads them to leave the market that they previously participated in. At the same time, for producers, the introduction of the tax increases production costs and cuts into their revenues. Some of the businesses that were willing to produce at moderately high costs now find themselves unable to make a profit with the introduction of the tax. They too leave the market. There are market actors who are no longer able to purchase or sell the good.
#
# We call this loss of transactions: deadweight welfare loss. It is represented by the triangle with a vertex at the original market equilibrium and a base at the tax wedge. The area of the deadweight loss triangle, also known as Harberger's triangle, is the size of the welfare loss - the total value of transactions lost as a result of the tax.
#
# Another way to think about deadweight loss is the change (decrease) in total surplus. Consumer and producer surplus decrease significantly, but this is slightly offset by the revenue earned by the government from the tax. 
#
# We can calculate the size of Harberger's triangle using the following formula: $\dfrac{1}{2} \cdot \dfrac{\epsilon_s \cdot \epsilon_d}{\epsilon_s - \epsilon_d} \cdot \dfrac{Q^*}{P_p} (\text{tax})^2$ where $\epsilon_s$ is the price elasticity of supply and  $\epsilon_d$ is the price elasticity of demand.

# ### Salience
#
# We noted in our discussion about taxes that the equilibrium quantity and price is the same regardless of whether the tax is levied on producers or consumers. This is the traditional theory's assumption: that individuals, whether they be producers or consumers, are fully aware of the taxes they pay. They decide how much to produce or consume with this in mind.
#
# We call the visibility at which taxes are displayed their salience. As an example, the final price of a food item in a restaurant is not inclusive of sales tax. Traditional economic theory would say that this difference between advertized or poster price and the actual price paid by a consumer has no bearing on the quantity they demand. That is to say taxes are fully salient. However, recent research has suggested that this is not the case. 
#
# A number of recent studies, including by Chetty, Looney and Kroft in 2009, found that posting prices that included sales tax actually reduces demand for those goods. Individuals are not fully informed or rational, implying that tax salience does matter.

# ### Subsidy
#
# Another form of government intervention is a subsidy. It involves either a monetary benefit given by the government or a reduction in taxes granted to individual businesses or whole industries. They intend to lower production costs, and thus increase the quantity supplied of goods and services at equilibrium.

# +
# [INSERT GRAPH: SUPPLY CURVE SHIFT RIGHT]
# -

# We represent this visually as a rightward shift in the supply curve. As costs are lower, producers are now willing to supply more goods and services at every price. The demand curve remains unchanged as a subsidy goes directly to producers. The resulting equilibrium has a lower price $P^*$ and higher quantity $Q^*$. It is assumed that the lower production costs would be passed onto consumers through lower market prices. $P^*$ is what consumers pay, but producers receive $P_P = P^* + \text{subsidy}$. This is depicted visually by the price along the new supply curve at quantity $Q^*$.
#
# Consumer surplus increases as more individuals are able to purchase the good than before. Similarly, producer surplus has increased as the subsidy takes care of part (if not all) of their costs. Overall market surplus has increased.
#
# The welfare gain is depicted in a similar way to that of a tax: a triangle with a vertex at the original market equilibrium and a base along $Q^*$. The cost of the subsidy to the government is $\text{per-unit subsidy} \cdot Q^*$.

# ### Price controls
#
# The last type of government intervention is far more forceful than taxes and subsidies. Instead of changing the per-unit cost of a good paid by consumers or producers, price controls directly fix the market price. It does not change the amount different consumers are willing and able to pay or how much producers are willing and able to produce. 
#
# When price controls are introduced to a market, the equilibrium price and quantity no longer exist. Consumer and producers are forced to sell or buy goods at the prescribed price.

# +
# [INSERT GRAPH: PRICE FLOOR]
# -

# Let's take a price floor as the first example. This is when the government imposes a minimum price for which a good can be sold. For this to be effective, the price floor must be set above the equilibrium price, otherwise the market would just settle at equilibrium. This results in a surplus, as producers are willing to sell far more goods than consumers are willing to purchase. This results in a surplus as $Q_s > Q_d$. Normally, without government intervention, the invisible hand would push prices down until $Q_s = Q_d$. However, with the price floor, the market remains in a state of disequilibrium.
#
# Minimum wages act as a price floor, keeping wages above a certain level. At this higher rate, more workers are willing to work than there is demand for, creating unemployment. **For extra commentary, should I talk about Card & Auerbach's study, or would that be extraneous?**
#
#
# # Umar: bolded above
#

# +
# [INSERT GRAPH: PRICE CEILING]
# -

# The other control is a price ceiling. This is when the government imposes a maximum price for which a good can be sold. This results in a shortage as consumers are to purchase more goods than producer are willing to sell: $Q_d > Q_s$.
#
# Rent control in Berkeley acts as a price ceiling, keeping rents from rising above a certain level.

# ## Optional: World Trade vs. Autarky
#
# Throughout the class so far, we have assumed that the economy is operating by itself, in isolation. Economists use the word "autarky" to represent the scenario when a country is economically self-sufficient. The key consequence of this is that the country's economy is unaffected by global events, allowing us to conclude that any changes to equilibrium price or quantity are purely a result of shifts in domestic demand or supply.
#
# We will now assume that the country is no longer in autarky, and is thus subject to world prices, demand and supply.

# +
# [INSERT GRAPH: AUTARKY]
# -

# We now label our demand and supply curves as domestic demand and supply, respectively. Their intersection represents where the market would be if we were in autarky. However, as we are now open to world trade, the price where the market operates is now determined by the world price, which may be totally different from the equilibrium price. 
#
# Where the world price line intersects with domestic demand represents the total quantity demanded within the market. Where it intersects with the domestic supply curve is the total quantity supplied.
#
# Let's first imagine a scenario where world price is lower than domestic price. From our graph, we find that $Q_s < Q_d$. This is a shortage. Domestic producers are not willing to fully provide all goods demanded by consumers in the economy. The difference between what is produced domestically and the total quantity demanded will be fulfilled by international producers. Thus, $Q_d - Q_s =$ imports. As prices are lower than what some consumers were willing to pay, there is a significant consumer surplus. Unfortunately for producers, as world prices are lower than what they would have been able to charge, they lose producer surplus. There is a net gain in the change in consumer surplus is greater than the loss in producer surplus. This is represented by the triangle between the curves and above the world price line.
#
# Now, let's imagine that world price is greater than domestic price. In this case, $Q_s > Q_d$. We now have a surplus where the quantity demanded by consumers is far less than what domestic producers are willing to provide at this higher price. Thus, domestic producers will willingly sell goods to domestic customers and sell the rest on the global market. $Q_s - Q_d =$ exports. As prices are higher than what some consumers were willing to pay, there is a significant decrease in consumer surplus. Producers however rejoice as world prices are higher than what they would have been able to charge. They gain producer surplus. There is a net gain in total surplus as the increase in producer surplus is greater than the loss in consumer surplus. This is represented by the triangle between the curves and above the world price line.

#  

StarcoderdataJupyterScriptsDedupFiltered