Skip to content

Commit 6d3b093

Browse files
annabellasdjlperla
andauthored
Multi agent models update (#276)
* Updated formatting and code changes for the multi_agent models * Julia 1.10 support --------- Co-authored-by: Jesse Perla <[email protected]>
1 parent bf0ae39 commit 6d3b093

File tree

17 files changed

+860
-913
lines changed

17 files changed

+860
-913
lines changed

.JuliaFormatter.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
style = "sciml"
2-
margin = 84
2+
margin = 80
33
yas_style_nesting = true
44
annotate_untyped_fields_with_any = false

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,14 @@ julia format_myst.jl lectures/getting_started_julia/getting_started.md
5858
```
5959
6060
As a helper, you can call a shell script to do it for an entire folder
61+
6162
```bash
6263
bash format_all_directory.sh lectures/dynamic_programming
6364
```
65+
6466
or to also do the unicode substitutions
67+
68+
```bash
6569
bash format_all_directory.sh lectures/dynamic_programming true
6670
```
6771

lectures/Manifest.toml

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,9 @@ version = "1.9.1"
417417

418418
[[deps.DiffEqBase]]
419419
deps = ["ArrayInterface", "DataStructures", "DocStringExtensions", "EnumX", "EnzymeCore", "FastBroadcast", "ForwardDiff", "FunctionWrappers", "FunctionWrappersWrappers", "LinearAlgebra", "Logging", "Markdown", "MuladdMacro", "Parameters", "PreallocationTools", "PrecompileTools", "Printf", "RecursiveArrayTools", "Reexport", "SciMLBase", "SciMLOperators", "Setfield", "SparseArrays", "Static", "StaticArraysCore", "Statistics", "Tricks", "TruncatedStacktraces"]
420-
git-tree-sha1 = "53159da9fef2dd92815499e18f93b699ed54f397"
420+
git-tree-sha1 = "044648af911974c3928058c1f8c83f159dece274"
421421
uuid = "2b5f629d-d688-5b77-993f-72d75c75574e"
422-
version = "6.145.5"
422+
version = "6.145.6"
423423

424424
[deps.DiffEqBase.extensions]
425425
DiffEqBaseChainRulesCoreExt = "ChainRulesCore"
@@ -624,9 +624,9 @@ version = "2.0.0"
624624

625625
[[deps.FileIO]]
626626
deps = ["Pkg", "Requires", "UUIDs"]
627-
git-tree-sha1 = "299dc33549f68299137e51e6d49a13b5b1da9673"
627+
git-tree-sha1 = "c5c28c245101bd59154f649e19b038d15901b5dc"
628628
uuid = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
629-
version = "1.16.1"
629+
version = "1.16.2"
630630

631631
[[deps.FilePathsBase]]
632632
deps = ["Compat", "Dates", "Mmap", "Printf", "Test", "UUIDs"]
@@ -1265,9 +1265,9 @@ version = "1.0.3"
12651265

12661266
[[deps.LoopVectorization]]
12671267
deps = ["ArrayInterface", "CPUSummary", "CloseOpenIntervals", "DocStringExtensions", "HostCPUFeatures", "IfElse", "LayoutPointers", "LinearAlgebra", "OffsetArrays", "PolyesterWeave", "PrecompileTools", "SIMDTypes", "SLEEFPirates", "Static", "StaticArrayInterface", "ThreadingUtilities", "UnPack", "VectorizationBase"]
1268-
git-tree-sha1 = "0f5648fbae0d015e3abe5867bca2b362f67a5894"
1268+
git-tree-sha1 = "fc712dd664097440f19a91a704299cca02134ca0"
12691269
uuid = "bdcacae8-1622-11e9-2a5c-532679323890"
1270-
version = "0.12.166"
1270+
version = "0.12.168"
12711271
weakdeps = ["ChainRulesCore", "ForwardDiff", "SpecialFunctions"]
12721272

12731273
[deps.LoopVectorization.extensions]
@@ -1313,12 +1313,6 @@ git-tree-sha1 = "d2a140e551c9ec9028483e3c7d1244f417567ac0"
13131313
uuid = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
13141314
version = "1.24.0"
13151315

1316-
[[deps.MathProgBase]]
1317-
deps = ["LinearAlgebra", "SparseArrays"]
1318-
git-tree-sha1 = "9abbe463a1e9fc507f12a69e7f29346c2cdc472c"
1319-
uuid = "fdba3010-5040-5b88-9595-932c9decdf73"
1320-
version = "0.7.8"
1321-
13221316
[[deps.MatrixFactorizations]]
13231317
deps = ["ArrayLayouts", "LinearAlgebra", "Printf", "Random"]
13241318
git-tree-sha1 = "78f6e33434939b0ac9ba1df81e6d005ee85a7396"
@@ -1396,10 +1390,14 @@ uuid = "d41bc354-129a-5804-8e4c-c37616107c6c"
13961390
version = "7.8.3"
13971391

13981392
[[deps.NLopt]]
1399-
deps = ["MathOptInterface", "MathProgBase", "NLopt_jll"]
1400-
git-tree-sha1 = "5a7e32c569200a8a03c3d55d286254b0321cd262"
1393+
deps = ["NLopt_jll"]
1394+
git-tree-sha1 = "19d2a1c8a3c5b5a459f54a10e54de630c4a05701"
14011395
uuid = "76087f3c-5699-56af-9a33-bf431cd00edd"
1402-
version = "0.6.5"
1396+
version = "1.0.0"
1397+
weakdeps = ["MathOptInterface"]
1398+
1399+
[deps.NLopt.extensions]
1400+
NLoptMathOptInterfaceExt = ["MathOptInterface"]
14031401

14041402
[[deps.NLopt_jll]]
14051403
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
@@ -1663,9 +1661,9 @@ version = "0.2.4"
16631661

16641662
[[deps.PreallocationTools]]
16651663
deps = ["Adapt", "ArrayInterface", "ForwardDiff"]
1666-
git-tree-sha1 = "13078a8afc9f88ede295c17c3c673eb52c05b0b4"
1664+
git-tree-sha1 = "64bb68f76f789f5fe5930a80af310f19cdafeaed"
16671665
uuid = "d236fae5-4411-538c-8e31-a6e3d9e00b46"
1668-
version = "0.4.16"
1666+
version = "0.4.17"
16691667

16701668
[deps.PreallocationTools.extensions]
16711669
PreallocationToolsReverseDiffExt = "ReverseDiff"
@@ -1731,9 +1729,9 @@ version = "2.9.1"
17311729

17321730
[[deps.QuantEcon]]
17331731
deps = ["DSP", "DataStructures", "Distributions", "FFTW", "Graphs", "LinearAlgebra", "Markdown", "NLopt", "Optim", "Pkg", "Primes", "Random", "SparseArrays", "SpecialFunctions", "Statistics", "StatsBase", "Test"]
1734-
git-tree-sha1 = "4f989614763519b089f4127063c1795d7a412753"
1732+
git-tree-sha1 = "034293b29fdbcae73aeb7ca0b2755e693f04701b"
17351733
uuid = "fcd29c91-0bd7-5a09-975d-7ac3f643a60c"
1736-
version = "0.16.5"
1734+
version = "0.16.6"
17371735

17381736
[[deps.Query]]
17391737
deps = ["DataValues", "IterableTables", "MacroTools", "QueryOperators", "Statistics"]
@@ -2095,9 +2093,9 @@ weakdeps = ["OffsetArrays", "StaticArrays"]
20952093

20962094
[[deps.StaticArrays]]
20972095
deps = ["LinearAlgebra", "PrecompileTools", "Random", "StaticArraysCore"]
2098-
git-tree-sha1 = "42d5373c10272d14ef49cc68ffc22df3b93c549a"
2096+
git-tree-sha1 = "4e17a790909b17f7bf1496e3aec138cf01b60b3b"
20992097
uuid = "90137ffa-7385-5640-81b9-e52037218182"
2100-
version = "1.8.2"
2098+
version = "1.9.0"
21012099
weakdeps = ["ChainRulesCore", "Statistics"]
21022100

21032101
[deps.StaticArrays.extensions]

lectures/continuous_time/covid_sde.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,4 +605,3 @@ In this case, there are significant differences between the early and late death
605605
This bleak simulation has assumed that no individuals has long-term immunity and that there will be no medical advancements on that time horizon - both of which are unlikely to be true.
606606

607607
Nevertheless, it suggests that the timing of lifting lockdown has a more profound impact after 18 months if we allow stochastic shocks imperfect immunity.
608-

lectures/continuous_time/seir_model.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,4 +517,3 @@ Despite its richness, the model above is fully deterministic. The policy $\bar{
517517
One way that randomness can lead to aggregate fluctuations is the granularity that comes through the discreteness of individuals. This topic, the connection between SDEs and the Langevin equations typically used in the approximation of chemical reactions in well-mixed media is explored in further lectures on continuous time Markov chains.
518518

519519
Instead, in the {doc}`next lecture <covid_sde>`, we will concentrate on randomness that comes from aggregate changes in behavior or policy.
520-

lectures/multi_agent_models/aiyagari.md

Lines changed: 66 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ The object also includes a default set of parameters that we'll adopt unless oth
195195

196196
```{code-cell} julia
197197
using LinearAlgebra, Statistics
198+
using LaTeXStrings, Plots, QuantEcon
198199
```
199200

200201
```{code-cell} julia
@@ -205,30 +206,27 @@ using Test, Random
205206
```
206207

207208
```{code-cell} julia
208-
using LaTeXStrings, Parameters, Plots, QuantEcon
209-
210-
```
211-
212-
```{code-cell} julia
213-
Household = @with_kw (r = 0.01,
214-
w = 1.0,
215-
σ = 1.0,
216-
β = 0.96,
217-
z_chain = MarkovChain([0.9 0.1; 0.1 0.9], [0.1; 1.0]),
218-
a_min = 1e-10,
219-
a_max = 18.0,
220-
a_size = 200,
221-
a_vals = range(a_min, a_max, length = a_size),
222-
z_size = length(z_chain.state_values),
223-
n = a_size * z_size,
224-
s_vals = gridmake(a_vals, z_chain.state_values),
225-
s_i_vals = gridmake(1:a_size, 1:z_size),
226-
u = σ == 1 ? x -> log(x) : x -> (x^(1 - σ) - 1) / (1 - σ),
227-
R = setup_R!(fill(-Inf, n, a_size), a_vals, s_vals, r, w, u),
228-
# -Inf is the utility of dying (0 consumption)
229-
Q = setup_Q!(zeros(n, a_size, n), s_i_vals, z_chain))
230-
231-
function setup_Q!(Q, s_i_vals, z_chain)
209+
function Household(; r = 0.01,
210+
w = 1.0,
211+
sigma = 1.0,
212+
beta = 0.96,
213+
z_chain = MarkovChain([0.9 0.1; 0.1 0.9], [0.1; 1.0]),
214+
a_min = 1e-10,
215+
a_max = 18.0,
216+
a_size = 200,
217+
a_vals = range(a_min, a_max, length = a_size),
218+
# -Inf is the utility of dying (0 consumption)
219+
u = sigma == 1 ? x -> log(x) :
220+
x -> (x^(1 - sigma) - 1) / (1 - sigma))
221+
222+
# Create grids
223+
z_size = length(z_chain.state_values)
224+
n = a_size * z_size
225+
s_vals = gridmake(a_vals, z_chain.state_values)
226+
s_i_vals = gridmake(1:a_size, 1:z_size)
227+
228+
# Fill in the Q and R
229+
Q = zeros(n, a_size, n)
232230
for next_s_i in 1:size(Q, 3)
233231
for a_i in 1:size(Q, 2)
234232
for s_i in 1:size(Q, 1)
@@ -241,10 +239,8 @@ function setup_Q!(Q, s_i_vals, z_chain)
241239
end
242240
end
243241
end
244-
return Q
245-
end
246242
247-
function setup_R!(R, a_vals, s_vals, r, w, u)
243+
R = fill(-Inf, n, a_size)
248244
for new_a_i in 1:size(R, 2)
249245
a_new = a_vals[new_a_i]
250246
for s_i in 1:size(R, 1)
@@ -256,7 +252,8 @@ function setup_R!(R, a_vals, s_vals, r, w, u)
256252
end
257253
end
258254
end
259-
return R
255+
return (; r, w, sigma, beta, z_chain, a_min, a_max, a_size, a_vals, z_size,
256+
n, s_vals, s_i_vals, u, R, Q)
260257
end
261258
```
262259

@@ -271,16 +268,16 @@ Random.seed!(42);
271268

272269
```{code-cell} julia
273270
# Create an instance of Household
274-
am = Household(a_max = 20.0, r = 0.03, w = 0.956)
271+
am = Household(; a_max = 20.0, r = 0.03, w = 0.956)
275272
276273
# Use the instance to build a discrete dynamic program
277-
am_ddp = DiscreteDP(am.R, am.Q, am.β)
274+
am_ddp = DiscreteDP(am.R, am.Q, am.beta)
278275
279276
# Solve using policy function iteration
280277
results = solve(am_ddp, PFI)
281278
282279
# Simplify names
283-
(;z_size, a_size, n, a_vals) = am
280+
(; z_size, a_size, n, a_vals) = am
284281
z_vals = am.z_chain.state_values
285282
286283
# Get all optimal actions across the set of
@@ -298,10 +295,10 @@ plot!(xlabel = "current assets", ylabel = "next period assets", grid = false)
298295
tags: [remove-cell]
299296
---
300297
@testset begin
301-
#test a_vals[4] ≈ 0.3015075377869347
302-
#test a_star[4] ≈ 0.2010050252246231
303-
#test results.v[4] ≈ -27.48291672016239
304-
#test z_vals ≈ [0.1, 1.0]
298+
@test a_vals[4] ≈ 0.3015075377869347
299+
@test a_star[4] ≈ 0.2010050252246231
300+
@test results.v[4] ≈ -27.48291672016239
301+
@test z_vals ≈ [0.1, 1.0]
305302
end
306303
```
307304

@@ -323,29 +320,16 @@ Random.seed!(42);
323320
```
324321

325322
```{code-cell} julia
326-
# Firms' parameters
327-
const A = 1
328-
const N = 1
329-
const α = 0.33
330-
const β = 0.96
331-
const δ = 0.05
332-
333-
function r_to_w(r)
334-
return A * (1 - α) * (A * α / (r + δ)) ^ (α / (1 - α))
335-
end
336323
337-
function rd(K)
338-
return A * α * (N / K) ^ (1 - α) - δ
339-
end
340-
341-
function prices_to_capital_stock(am, r)
324+
# Calculate supply of capital for a given r
325+
function prices_to_capital_stock(r; beta, A, N, alpha, delta, a_max)
326+
# Create an instance of Household given the parameters
342327
343-
# Set up problem
344-
w = r_to_w(r)
345-
(;a_vals, s_vals, u) = am
346-
setup_R!(am.R, a_vals, s_vals, r, w, u)
328+
# Calculate the equilibrium wages
329+
w = A * (1 - alpha) * (A * alpha / (r + delta))^(alpha / (1 - alpha))
330+
am = Household(; beta, a_max, w, r)
347331
348-
aiyagari_ddp = DiscreteDP(am.R, am.Q, am.β)
332+
aiyagari_ddp = DiscreteDP(am.R, am.Q, am.beta)
349333
350334
# Compute the optimal policy
351335
results = solve(aiyagari_ddp, PFI)
@@ -354,33 +338,39 @@ function prices_to_capital_stock(am, r)
354338
stationary_probs = stationary_distributions(results.mc)[:, 1][1]
355339
356340
# Return K
357-
return dot(am.s_vals[:, 1], stationary_probs)
341+
K = dot(am.s_vals[:, 1], stationary_probs)
342+
343+
# Return capital
344+
return K
358345
end
359346
360-
# Create an instance of Household
361-
am = Household(β = β, a_max = 20.0)
347+
# Inverse Demand for capital
348+
function r_inverse_demand(K; A, N, alpha, delta)
349+
return A * alpha * (N / K)^(1 - alpha) - delta
350+
end
362351
363352
# Create a grid of r values at which to compute demand and supply of capital
364353
r_vals = range(0.005, 0.04, length = 20)
365354
366-
# Compute supply of capital
367-
k_vals = prices_to_capital_stock.(Ref(am), r_vals)
355+
# Firms' parameters
356+
A = 1
357+
N = 1
358+
alpha = 0.33
359+
beta = 0.96
360+
delta = 0.05
361+
a_max = 20.0
368362
369-
# Plot against demand for capital by firms
370-
demand = rd.(k_vals)
371-
labels = ["demand for capital" "supply of capital"]
372-
plot(k_vals, [demand r_vals], label = labels, lw = 2, alpha = 0.6)
373-
plot!(xlabel = "capital", ylabel = "interest rate", xlim = (2, 14), ylim = (0.0, 0.1))
374-
```
363+
prices_to_capital_stock(r_vals[1]; A, N, alpha, beta, delta, a_max)
375364
376-
```{code-cell} julia
377-
---
378-
tags: [remove-cell]
379-
---
380-
@testset begin
381-
#test k_vals[4] ≈ 3.920775511050653
382-
#test demand[4] ≈ 0.08211578674946372
383-
#test r_vals[4] ≈ 0.010526315789473684
384-
end
385-
```
365+
# Compute supply of capital
366+
k_vals = prices_to_capital_stock.(r_vals; A, N, alpha, beta, delta, a_max)
367+
368+
r_inverse_demand_vals = r_inverse_demand.(k_vals; A, N, alpha, delta)
386369
370+
# Plot against demand for capital by firms
371+
labels = ["demand for capital" "supply of capital"]
372+
plot(k_vals, [r_inverse_demand_vals r_vals], label = labels, lw = 2,
373+
alpha = 0.6)
374+
plot!(xlabel = "capital", ylabel = "interest rate", xlim = (2, 14),
375+
ylim = (0.0, 0.1))
376+
```

0 commit comments

Comments
 (0)