Skip to content

Commit c206d18

Browse files
committed
update sympy code and exercise
1 parent 5dc4565 commit c206d18

File tree

1 file changed

+69
-42
lines changed

1 file changed

+69
-42
lines changed

lectures/equalizing_difference.md

Lines changed: 69 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ As usual, we'll start by importing some Python modules.
5151
import numpy as np
5252
import matplotlib.pyplot as plt
5353
from collections import namedtuple
54-
from sympy import Symbol, Lambda, symbols
54+
from sympy import Symbol, Lambda, symbols, refine, \
55+
Sum, simplify, Eq, solve, Lambda,\
56+
lambdify, And
5557
```
5658

5759
## The indifference condition
@@ -276,7 +278,6 @@ plt.ylabel(r'wage gap')
276278
plt.show()
277279
```
278280
279-
280281
## An application of calculus
281282
282283
So far, we have used only linear algebra and it has been a good enough tool for us to figure out how our model works.
@@ -294,28 +295,41 @@ We'll use the Python module 'sympy' to compute partial derivatives of $\phi$ wit
294295
Define symbols
295296
296297
```{code-cell} ipython3
297-
γ_h, γ_c, w_h0, D = symbols('\gamma_h, \gamma_c, w_0^h, D', real=True)
298-
R, T = Symbol('R', real=True), Symbol('T', integer=True)
298+
R, w_h0, w_c0, γ_c, γ_h, ϕ, D, t, T = symbols(
299+
'R w^h_0 w^c_0 gamma_c gamma_h phi D t T', positive=True)
300+
301+
refine(γ_c, γ_c>1)
302+
refine(γ_h, γ_h>1)
303+
refine(R, R>1)
304+
305+
# Define the wage for college
306+
# and high school graduates at time t
307+
w_ct = w_c0 * γ_c**t
308+
w_ht = w_h0 * γ_h**t
299309
```
300310
301311
Define function $A_h$
302312
303313
```{code-cell} ipython3
304-
A_h = Lambda((γ_h, R, T), (1 - (γ_h/R)**(T+1)) / (1 - γ_h/R))
314+
h_0 = Sum(R**-t * w_ht, (t, 0, T))
315+
A_h = simplify(h_0.doit() / w_h0)
316+
A_h = simplify(A_h.args[1][0])
305317
A_h
306318
```
307319
308320
Define function $A_c$
309321
310322
```{code-cell} ipython3
311-
A_c = Lambda((γ_c, R, T), (1 - (γ_c/R)**(T-3)) / (1 - γ_c/R) * (γ_c/R)**4)
323+
c_0 = Sum(R**-t * w_ct, (t, 4, T))
324+
A_c = simplify(c_0.doit() / w_c0)
325+
A_c = simplify(A_c.args[1][0])
312326
A_c
313327
```
314328
315329
Now, define $\phi$
316330
317331
```{code-cell} ipython3
318-
ϕ = Lambda((D, γ_h, γ_c, R, T, w_h0), A_h(γ_h, R, T)/A_c(γ_c, R, T) + D/(w_h0*A_c(γ_c, R, T)))
332+
ϕ = A_h/A_c + D/(w_h0*A_c)
319333
```
320334
321335
```{code-cell} ipython3
@@ -330,34 +344,41 @@ T_value = 40
330344
γ_h_value, γ_c_value = 1.01, 1.01
331345
w_h0_value = 1
332346
D_value = 10
347+
348+
symbol_subs = {D: D_value,
349+
γ_h: γ_h_value,
350+
γ_c: γ_c_value,
351+
R: R_value,
352+
T: T_value,
353+
w_h0: w_h0_value}
354+
355+
ϕ.subs(symbol_subs)
333356
```
334357
335358
Now let's compute $\frac{\partial \phi}{\partial D}$ and then evaluate it at the default values
336359
337360
```{code-cell} ipython3
338-
ϕ_D = ϕ(D, γ_h, γ_c, R, T, w_h0).diff(D)
361+
ϕ_D = ϕ.diff(D)
339362
ϕ_D
340363
```
341364
342365
```{code-cell} ipython3
343366
# Numerical value at default parameters
344-
ϕ_D_func = Lambda((D, γ_h, γ_c, R, T, w_h0), ϕ_D)
345-
ϕ_D_func(D_value, γ_h_value, γ_c_value, R_value, T_value, w_h0_value)
367+
ϕ_D.subs(symbol_subs)
346368
```
347369
348370
Thus, as with our earlier graph, we find that raising $R$ increases the initial college wage premium $\phi$.
349371
350372
Compute $\frac{\partial \phi}{\partial T}$ and evaluate it at default parameters
351373
352374
```{code-cell} ipython3
353-
ϕ_T = ϕ(D, γ_h, γ_c, R, T, w_h0).diff(T)
375+
ϕ_T = ϕ.diff(T)
354376
ϕ_T
355377
```
356378
357379
```{code-cell} ipython3
358380
# Numerical value at default parameters
359-
ϕ_T_func = Lambda((D, γ_h, γ_c, R, T, w_h0), ϕ_T)
360-
ϕ_T_func(D_value, γ_h_value, γ_c_value, R_value, T_value, w_h0_value)
381+
ϕ_T.subs(symbol_subs)
361382
```
362383
363384
We find that raising $T$ decreases the initial college wage premium $\phi$.
@@ -367,79 +388,70 @@ This is because college graduates now have longer career lengths to "pay off" th
367388
Let's compute $\frac{\partial \phi}{\partial γ_h}$ and evaluate it at default parameters.
368389
369390
```{code-cell} ipython3
370-
ϕ_γ_h = ϕ(D, γ_h, γ_c, R, T, w_h0).diff(γ_h)
391+
ϕ_γ_h = ϕ.diff(γ_h)
371392
ϕ_γ_h
372393
```
373394
374395
```{code-cell} ipython3
375396
# Numerical value at default parameters
376-
ϕ_γ_h_func = Lambda((D, γ_h, γ_c, R, T, w_h0), ϕ_γ_h)
377-
ϕ_γ_h_func(D_value, γ_h_value, γ_c_value, R_value, T_value, w_h0_value)
397+
ϕ_γ_h.subs(symbol_subs)
378398
```
379399
380400
We find that raising $\gamma_h$ increases the initial college wage premium $\phi$, in line with our earlier graphical analysis.
381401
382402
Compute $\frac{\partial \phi}{\partial γ_c}$ and evaluate it numerically at default parameter values
383403
384404
```{code-cell} ipython3
385-
ϕ_γ_c = ϕ(D, γ_h, γ_c, R, T, w_h0).diff(γ_c)
405+
ϕ_γ_c = ϕ.diff(γ_c)
386406
ϕ_γ_c
387407
```
388408
389409
```{code-cell} ipython3
390410
# Numerical value at default parameters
391-
ϕ_γ_c_func = Lambda((D, γ_h, γ_c, R, T, w_h0), ϕ_γ_c)
392-
ϕ_γ_c_func(D_value, γ_h_value, γ_c_value, R_value, T_value, w_h0_value)
411+
ϕ_γ_c.subs(symbol_subs)
393412
```
394413
395414
We find that raising $\gamma_c$ decreases the initial college wage premium $\phi$, in line with our earlier graphical analysis.
396415
397416
Let's compute $\frac{\partial \phi}{\partial R}$ and evaluate it numerically at default parameter values
398417
399418
```{code-cell} ipython3
400-
ϕ_R = ϕ(D, γ_h, γ_c, R, T, w_h0).diff(R)
419+
ϕ_R = ϕ.diff(R)
401420
ϕ_R
402421
```
403422
404423
```{code-cell} ipython3
405424
# Numerical value at default parameters
406-
ϕ_R_func = Lambda((D, γ_h, γ_c, R, T, w_h0), ϕ_R)
407-
ϕ_R_func(D_value, γ_h_value, γ_c_value, R_value, T_value, w_h0_value)
425+
ϕ_R.subs(symbol_subs)
408426
```
409427
410428
We find that raising the gross interest rate $R$ increases the initial college wage premium $\phi$, in line with our earlier graphical analysis.
411429
412-
413430
## Exercises
414431
432+
In the following exercises, we extend our previous model to a model of entrepreneurs versus workers.
433+
415434
```{exercise-start}
416435
:label: edm_ex1
417436
```
418-
In this exercise, we add a parameter and reinterpret variables to get a model of entrepreneurs versus workers and do some computations like before.
437+
We add a parameter $\pi \in (0,1)$ representing the probability that an entrepreneur's "project" succeeds.
419438
420-
We now let $h$ be the present value of a "worker".
439+
We now let $h$ be the present value of a "worker".
421440
422441
We define the present value of an entrepreneur to be
423442
424443
$$
425444
c_0 = \pi \sum_{t=4}^T R^{-t} w_t^c
426445
$$
427446
428-
where $\pi \in (0,1) $ is the probability that an entrepreneur's "project" succeeds.
429-
430-
For our model of workers and firms, we'll interpret $D$ as the cost of becoming an entrepreneur.
447+
We interpret $D$ in the previous model as the cost of becoming an entrepreneur.
431448
432449
This cost might include costs of hiring workers, office space, and lawyers.
433450
434451
What we used to call the college, high school wage gap $\phi$ now becomes the ratio
435452
of a successful entrepreneur's earnings to a worker's earnings.
436453
437-
We'll find that as $\pi$ decreases, $\phi$ increases, indicating that the riskier it is to
438-
be an entrepreneur, the higher must be the reward for a successful project.
439-
440-
Now define `create_edm_π` and `compute_gap` following the previous *Computations* section, adopting the entrepreneur-worker interpretation of our model.
441-
442-
Given:
454+
In this exercise, update `create_edm_π` and `compute_gap` to formulate our entrepreneur-worker model with parameters below
443455
444456
```{code-cell} ipython3
445457
R=1.05, # gross rate of return
@@ -448,16 +460,22 @@ T=40, # time horizon
448460
γ_c=1.01, # college wage growth
449461
w_h0=1, # initial wage (high school)
450462
D=10, # cost for college
451-
π=0 # chance of business success
463+
π=1 # chance of business success
452464
```
453465
466+
and verify that when $\pi = 1$, the result is the same as our old model.
467+
468+
+++
469+
454470
```{exercise-end}
455471
```
456472
457473
```{solution-start} edm_ex1
458474
:class: dropdown
459475
```
460476
477+
Here is one solution
478+
461479
```{code-cell} ipython3
462480
# Define a model of entrepreneur-worker interpretation
463481
EqDiffModel = namedtuple('EqDiffModel', 'R T γ_h γ_c w_h0 D π')
@@ -468,7 +486,7 @@ def create_edm_π(R=1.05, # gross rate of return
468486
γ_c=1.01, # college wage growth
469487
w_h0=1, # initial wage (high school)
470488
D=10, # cost for college
471-
π=0 # chance of business success
489+
π=1 # chance of business success
472490
):
473491
474492
return EqDiffModel(R, T, γ_h, γ_c, w_h0, D, π)
@@ -486,6 +504,16 @@ def compute_gap(model):
486504
ϕ = A_h / A_c + D / (w_h0 * A_c)
487505
return ϕ
488506
```
507+
508+
We validate our result by checking the result with $\pi = 1$
509+
510+
```{code-cell} ipython3
511+
ex_π = create_edm_π()
512+
gap_π = compute_gap(ex_π)
513+
514+
gap_π
515+
```
516+
489517
```{solution-end}
490518
```
491519
@@ -505,9 +533,6 @@ If the probability that a new business succeeds is $0.2$, what is the initial wa
505533
ex3 = create_edm_π(π=0.2)
506534
gap3 = compute_gap(ex3)
507535
508-
gap3ex3 = create_edm_π(π=0.2)
509-
gap3 = compute_gap(ex3)
510-
511536
gap3
512537
```
513538
@@ -519,20 +544,21 @@ gap3
519544
```
520545
Now let's study how the initial wage premium for successful entrepreneurs depend on the success probability.
521546
522-
Given
547+
With $\pi \in [0.2, 1]$,
523548
524549
```{code-cell} ipython3
525550
π_arr = np.linspace(0.2, 1, 50)
526551
```
527552
528-
Plot the relationship between the wage gap and the values of $\pi$.
553+
plot the relationship between the wage gap and the values of $\pi$.
529554
530555
```{exercise-end}
531556
```
532557
533558
```{solution-start} edm_ex3
534559
:class: dropdown
535560
```
561+
536562
```{code-cell} ipython3
537563
π_arr = np.linspace(0.2, 1, 50)
538564
models = [create_edm_π(π=π) for π in π_arr]
@@ -544,7 +570,8 @@ plt.xlabel(r'$\pi$')
544570
plt.show()
545571
```
546572
547-
Does the graph make sense to you?
573+
We find that as $\pi$ decreases, $\phi$ increases, indicating that the riskier it is to
574+
be an entrepreneur, the higher must be the reward for a successful project.
548575
549576
```{solution-end}
550577
```

0 commit comments

Comments
 (0)