Skip to content

Commit 2e3bb7f

Browse files
INTRODUCTION_TO_DYNAMICS/Tools_Techniques (#270)
* First_go_(the dictionaries_in_graphs_are formated) * Apply formatting to Markdown files * First_go_issue_250 * Apply formatting to Markdown files * changed_myst * ran_julia_formatter * changed_format_file * ran_for_zeta_changes * added_to_myst_file_some_formatting * ran_formatting * removed_'' * empty_push * Apply formatting to Markdown files * added_epislon_to_unicode * add_lambda_and_rho * missed_changes --------- Co-authored-by: GitHub Action <[email protected]>
1 parent 1abba6d commit 2e3bb7f

File tree

15 files changed

+744
-597
lines changed

15 files changed

+744
-597
lines changed

format_myst.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,15 @@ function format_myst(input_file_path, output_file_path, extra_replacements = fal
4949
if ignore_errors
5050
return m # i.e., don't change anything
5151
else
52-
throw(e)
52+
throw(e)
5353
end
5454
end
5555
end
5656

5757
# Additional replacements are optional. This may be useful when replacing variable names to make it easier to type in ascii
5858
replacements = Dict("α" => "alpha", "β" => "beta", "γ" => "gamma", "" => "<=",
59-
"" => ">=", "Σ" => "Sigma", "σ" => "sigma","μ"=>"mu")
59+
"" => ">=", "Σ" => "Sigma", "σ" => "sigma","μ"=>"mu","ϕ"=>"phi","ψ"=>"psi","ϵ"=>"epsilon",
60+
"δ"=>"delta","θ" => "theta","ζ"=>"zeta","" => "X_bar","" => "p_bar","" => "x_hat","λ"=>"lambda","ρ"=>"rho")
6061

6162
# Replace the code blocks in the content and handle exceptions
6263
try

lectures/introduction_dynamics/ar1_processes.md

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ c = 0.5
149149
150150
# initial conditions mu_0, v_0
151151
mu = -3.0
152-
v = 0.6
152+
v = 0.6
153153
```
154154

155155
Here's the sequence of distributions:
@@ -165,7 +165,7 @@ for t in 1:sim_length
165165
mu = a * mu + b
166166
v = a^2 * v + c^2
167167
dist = Normal(mu, sqrt(v))
168-
plot!(plt, x_grid, pdf.(dist, x_grid), label=L"\psi_{%$t}", linealpha=0.7)
168+
plot!(plt, x_grid, pdf.(dist, x_grid), label = L"\psi_{%$t}", linealpha = 0.7)
169169
end
170170
plt
171171
```
@@ -177,15 +177,15 @@ Notice that, in the figure above, the sequence $\{ \psi_t \}$ seems to be conver
177177
This is even clearer if we project forward further into the future:
178178

179179
```{code-cell} julia
180-
function plot_density_seq(mu_0=-3.0, v_0=0.6; sim_length=60)
180+
function plot_density_seq(mu_0 = -3.0, v_0 = 0.6; sim_length = 60)
181181
mu = mu_0
182182
v = v_0
183183
plt = plot()
184184
for t in 1:sim_length
185185
mu = a * mu + b
186186
v = a^2 * v + c^2
187187
dist = Normal(mu, sqrt(v))
188-
plot!(plt, x_grid, pdf.(dist, x_grid), label=nothing, linealpha=0.5)
188+
plot!(plt, x_grid, pdf.(dist, x_grid), label = nothing, linealpha = 0.5)
189189
end
190190
return plt
191191
end
@@ -236,7 +236,7 @@ plt = plot_density_seq(3.0)
236236
mu_star = b / (1 - a)
237237
std_star = sqrt(c^2 / (1 - a^2)) # square root of v_star
238238
psi_star = Normal(mu_star, std_star)
239-
plot!(plt, x_grid, psi_star, color = :black, label=L"\psi^*", linewidth=2)
239+
plot!(plt, x_grid, psi_star, color = :black, label = L"\psi^*", linewidth = 2)
240240
plt
241241
```
242242

@@ -447,7 +447,7 @@ c = 0.5
447447
mu_star = b / (1 - a)
448448
std_star = sqrt(c^2 / (1 - a^2)) # square root of v_star
449449
450-
function sample_moments_ar1(k, m=100_000, mu_0=0.0, sigma_0=1.0; seed=1234)
450+
function sample_moments_ar1(k, m = 100_000, mu_0 = 0.0, sigma_0 = 1.0; seed = 1234)
451451
Random.seed!(seed)
452452
sample_sum = 0.0
453453
x = mu_0 + sigma_0 * randn()
@@ -480,8 +480,8 @@ for (k_idx, k) in enumerate(k_vals)
480480
end
481481
482482
plt = plot()
483-
plot!(plt, k_vals, true_moments, label="true moments")
484-
plot!(plt, k_vals, sample_moments, label="sample moments")
483+
plot!(plt, k_vals, true_moments, label = "true moments")
484+
plot!(plt, k_vals, sample_moments, label = "sample moments")
485485
plt
486486
```
487487

@@ -498,13 +498,13 @@ end
498498
```
499499

500500
```{code-cell} julia
501-
function plot_kde(ϕ, n, plt, idx; x_min=-0.2, x_max=1.2)
502-
x_data = rand(ϕ, n)
501+
function plot_kde(phi, n, plt, idx; x_min = -0.2, x_max = 1.2)
502+
x_data = rand(phi, n)
503503
x_grid = range(-0.2, 1.2, length = 100)
504504
c = std(x_data)
505-
h = 1.06 * c * n^(-1/5)
506-
plot!(plt[idx], x_grid, f.(x_grid, Ref(x_data), h), label="estimate")
507-
plot!(plt[idx], x_grid, pdf.(ϕ, x_grid), label="true density")
505+
h = 1.06 * c * n^(-1 / 5)
506+
plot!(plt[idx], x_grid, f.(x_grid, Ref(x_data), h), label = "estimate")
507+
plot!(plt[idx], x_grid, pdf.(phi, x_grid), label = "true density")
508508
return plt
509509
end
510510
```
@@ -513,8 +513,8 @@ end
513513
n = 500
514514
parameter_pairs = [(2, 2), (2, 5), (0.5, 0.5)]
515515
plt = plot(layout = (3, 1))
516-
for (idx, (α, β)) in enumerate(parameter_pairs)
517-
plot_kde(Beta(α, β), n, plt, idx)
516+
for (idx, (alpha, beta)) in enumerate(parameter_pairs)
517+
plot_kde(Beta(alpha, beta), n, plt, idx)
518518
end
519519
plt
520520
```
@@ -530,14 +530,14 @@ Here is our solution:
530530
a = 0.9
531531
b = 0.0
532532
c = 0.1
533-
μ = -3
533+
mu = -3
534534
s = 0.2
535535
536-
μ_next = a * μ + b
536+
mu_next = a * mu + b
537537
s_next = sqrt(a^2 * s^2 + c^2)
538538
539-
ψ = Normal(μ, s)
540-
ψ_next = Normal(μ_next, s_next)
539+
psi = Normal(mu, s)
540+
psi_next = Normal(mu_next, s_next)
541541
```
542542

543543
```{code-cell} julia
@@ -550,17 +550,18 @@ end
550550

551551
```{code-cell} julia
552552
n = 2000
553-
x_draws = rand(ψ, n)
553+
x_draws = rand(psi, n)
554554
x_draws_next = a * x_draws .+ b + c * randn(n)
555555
c = std(x_draws_next)
556-
h = 1.06 * c * n^(-1/5)
556+
h = 1.06 * c * n^(-1 / 5)
557557
558-
x_grid = range(μ - 1, μ + 1, length = 100)
558+
x_grid = range(mu - 1, mu + 1, length = 100)
559559
plt = plot()
560560
561-
plot!(plt, x_grid, pdf.(ψ, x_grid), label=L"$\psi_t$")
562-
plot!(plt, x_grid, pdf.(ψ_next, x_grid), label=L"$\psi_{t+1}$")
563-
plot!(plt, x_grid, f.(x_grid, Ref(x_draws_next), h), label=L"estimate of $\psi_{t+1}$")
561+
plot!(plt, x_grid, pdf.(psi, x_grid), label = L"$\psi_t$")
562+
plot!(plt, x_grid, pdf.(psi_next, x_grid), label = L"$\psi_{t+1}$")
563+
plot!(plt, x_grid, f.(x_grid, Ref(x_draws_next), h),
564+
label = L"estimate of $\psi_{t+1}$")
564565
565566
plt
566567
```

lectures/introduction_dynamics/finite_markov.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ function mc_sample_path(P; init = 1, sample_size = 1000)
256256
X[1] = init # set the initial state
257257
258258
for t in 2:sample_size
259-
dist = dists[X[t-1]] # get discrete RV from last state's transition distribution
259+
dist = dists[X[t - 1]] # get discrete RV from last state's transition distribution
260260
X[t] = rand(dist) # draw new value
261261
end
262262
return X
@@ -291,7 +291,7 @@ Random.seed!(42); # for result reproducibility
291291
```{code-cell} julia
292292
P = [0.4 0.6; 0.2 0.8]
293293
X = mc_sample_path(P, sample_size = 100_000); # note 100_000 = 100000
294-
μ_1 = count(X .== 1)/length(X) # .== broadcasts test for equality. Could use mean(X .== 1)
294+
mu_1 = count(X .== 1) / length(X) # .== broadcasts test for equality. Could use mean(X .== 1)
295295
```
296296

297297
```{code-cell} julia
@@ -321,7 +321,7 @@ Random.seed!(42); # For reproducibility
321321
P = [0.4 0.6; 0.2 0.8];
322322
mc = MarkovChain(P)
323323
X = simulate(mc, 100_000);
324-
μ_2 = count(X .== 1)/length(X) # or mean(x -> x == 1, X)
324+
mu_2 = count(X .== 1) / length(X) # or mean(x -> x == 1, X)
325325
```
326326

327327
```{code-cell} julia
@@ -331,7 +331,7 @@ tags: [remove-cell]
331331
@testset "QE Sample Path Test" begin
332332
@test P ≈ [0.4 0.6; 0.2 0.8] # Make sure the primitive doesn't change.
333333
@test X[1:5] == [2, 2, 2, 2, 2]
334-
#test μ_1μ_2 atol = 1e-2
334+
#test mu_1mu_2 atol = 1e-2
335335
end
336336
```
337337

@@ -704,9 +704,9 @@ As seen in {eq}`fin_mc_fr`, we can shift probabilities forward one unit of time
704704
Some distributions are invariant under this updating process --- for example,
705705

706706
```{code-cell} julia
707-
P = [.4 .6; .2 .8];
708-
ψ = [0.25, 0.75];
709-
ψ' * P
707+
P = [0.4 0.6; 0.2 0.8];
708+
psi = [0.25, 0.75];
709+
psi' * P
710710
```
711711

712712
Such distributions are called **stationary**, or **invariant**.
@@ -801,7 +801,7 @@ Hence we need to impose the restriction that the solution must be a probability
801801
A suitable algorithm is implemented in [QuantEcon.jl](http://quantecon.org/quantecon-jl) --- the next code block illustrates
802802

803803
```{code-cell} julia
804-
P = [.4 .6; .2 .8];
804+
P = [0.4 0.6; 0.2 0.8];
805805
mc = MarkovChain(P);
806806
stationary_distributions(mc)
807807
```
@@ -824,7 +824,7 @@ P = [0.971 0.029 0.000
824824
0.145 0.778 0.077
825825
0.000 0.508 0.492] # stochastic matrix
826826
827-
ψ = [0.0 0.2 0.8] # initial distribution
827+
psi = [0.0 0.2 0.8] # initial distribution
828828
829829
t = 20 # path length
830830
x_vals = zeros(t)
@@ -833,18 +833,18 @@ z_vals = similar(x_vals)
833833
colors = [repeat([:red], 20); :black] # for plotting
834834
835835
for i in 1:t
836-
x_vals[i] = ψ[1]
837-
y_vals[i] = ψ[2]
838-
z_vals[i] = ψ[3]
839-
ψ = ψ * P # update distribution
836+
x_vals[i] = psi[1]
837+
y_vals[i] = psi[2]
838+
z_vals[i] = psi[3]
839+
psi = psi * P # update distribution
840840
end
841841
842842
mc = MarkovChain(P)
843-
ψ_star = stationary_distributions(mc)[1]
844-
x_star, y_star, z_star = ψ_star # unpack the stationary dist
843+
psi_star = stationary_distributions(mc)[1]
844+
x_star, y_star, z_star = psi_star # unpack the stationary dist
845845
plt = scatter([x_vals; x_star], [y_vals; y_star], [z_vals; z_star], color = colors,
846846
gridalpha = 0.5, legend = :none)
847-
plot!(plt, camera = (45,45))
847+
plot!(plt, camera = (45, 45))
848848
```
849849

850850
```{code-cell} julia
@@ -1234,20 +1234,20 @@ Random.seed!(42); # For reproducibility
12341234
```
12351235

12361236
```{code-cell} julia
1237-
α = 0.1 # probability of getting hired
1238-
β = 0.1 # probability of getting fired
1237+
alpha = 0.1 # probability of getting hired
1238+
beta = 0.1 # probability of getting fired
12391239
N = 10_000
1240-
= β / (α + β) # steady-state probabilities
1241-
P = [1 - α α
1242-
β 1 - β] # stochastic matrix
1240+
p_bar = beta / (alpha + beta) # steady-state probabilities
1241+
P = [1-alpha alpha
1242+
beta 1-beta] # stochastic matrix
12431243
mc = MarkovChain(P)
12441244
labels = ["start unemployed", "start employed"]
12451245
y_vals = Array{Vector}(undef, 2) # sample paths holder
12461246
12471247
for x0 in 1:2
12481248
X = simulate_indices(mc, N; init = x0) # generate the sample path
1249-
= cumsum(X .== 1) ./ (1:N) # compute state fraction. ./ required for precedence
1250-
y_vals[x0] = .- # plot divergence from steady state
1249+
X_bar = cumsum(X .== 1) ./ (1:N) # compute state fraction. ./ required for precedence
1250+
y_vals[x0] = X_bar .- p_bar # plot divergence from steady state
12511251
end
12521252
12531253
plot(y_vals, color = [:blue :green], fillrange = 0, fillalpha = 0.1,

0 commit comments

Comments
 (0)