functionfind_root_newton(demand, demand_grad) p =.3# initial guess deltap =1e10# initialize stepsizewhileabs(deltap) >1e-4 deltap =demand(p)/demand_grad(p) p += deltapprintln("Intermediate guess of p = $(round(p,digits=3)).")endprintln("The solution is p = $(round(p,digits=3)).")return pend;
3.10 Newton code
Code
# Solve for pricefind_root_newton(demand, demand_grad)
Intermediate guess of p = 0.068.
Intermediate guess of p = 0.115.
Intermediate guess of p = 0.147.
Intermediate guess of p = 0.154.
Intermediate guess of p = 0.154.
Intermediate guess of p = 0.154.
The solution is p = 0.154.
0.15419764093200633
3.11 Example 3
Consider a two period ag commodity market model
Period 1: Farmer makes acreage decisions for planting
Period 2: Per-acre yield realizes, equilibrium crop price clears the market
. . .
The farmer’s policy function is:
\[a(E[p]) = \frac{1}{2} + \frac{1}{2}E[p]\]
3.12 Example 3
Yield \(\hat{y}\) is stochastic and only realized after planting period. It follows \(\hat{y} \sim \mathcal{N}(1, 0.1)\)
Crop production is
\[q = a\hat{y}\]
. . .
Demand is given by
\[p(q) = 3-2q\]
. . .
Q: How many acres get planted?
3.13 Example 3
Q: How many acres get planted?
A: We plug in the stochastic quantity and take expectations
\(p(\hat{y}) = 3-2a\hat{y}\)
. . .
\(a = \frac{1}{2} + \frac{1}{2}(3-2aE[\hat{y}])\)
. . .
Rearrange and solve:
\(a^* = 1\)
3.14 Example 3
Now suppose the government implements a price floor such that \(p \geq 1\).
We now have that
\[p(\hat{y}) = \max(1,3-2a\hat{y})\]
Q: How many acres get planted with a price floor?
3.15 Example 3
Q: How many acres get planted with a price floor?
This is analytically intractable
. . .
The max operator is non-linear so we can’t pass the expectation through
We can solve this using Monte Carlo integration and function iteration
Code
usingStatistics# Function iteration method to find a rootfunctionfind_root_fi(mn, variance) y =randn(1000)*sqrt(variance) .+ mn # draws of the random variable a =1. # initial guess differ =100. # initialize error exp_price =1. # initialize expected pricewhile differ >1e-4 a_old = a # save old acreage p =max.(1, 3.-2.*a.*y) # compute price at all distribution points exp_price =mean(p) # compute expected price a =1/2+1/2*exp_price # get new acreage planted given new price differ=abs(a - a_old) # change in acreage plantedprintln("Intermediate acreage guess: $(round(a,digits=3))")endreturn a, exp_priceend;
3.17 Function iteration
Code
acreage, expected_price =find_root_fi(1, 0.1);println("The optimal number of acres to plant is $(round(acreage, digits =3)).\nThe expected price is $(round(expected_price, digits =3)).")
Intermediate acreage guess: 1.132
Intermediate acreage guess: 1.093
Intermediate acreage guess: 1.103
Intermediate acreage guess: 1.1
Intermediate acreage guess: 1.101
Intermediate acreage guess: 1.101
Intermediate acreage guess: 1.101
The optimal number of acres to plant is 1.101.
The expected price is 1.202.
3.18 Don’t leave just yet!
Things to do before next class
Read Gentzkow and Shapiro (2014) Code and Data for the Social Sciences
Special attention to Chapters 2–4, 6, 7
Watch the 5-minute video What is version control?
Links for both are on Canvas
Create a GitHub account with your Illinois email (or add it to your existing account) and send me an email with your username