Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic_Programming #272

Merged
merged 26 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
43460d8
fixed_with_kw
Jonah-Heyl Aug 29, 2023
5ee0a24
formating
Jonah-Heyl Aug 29, 2023
a8f757f
formatting_changes
Jonah-Heyl Aug 30, 2023
cccf20c
the_tuples_where_refferenced_using_named_args
Jonah-Heyl Aug 30, 2023
2b7460f
Merge cccf20c21a92be72494dd0dd4a457aa9d4bc7377 into 2e3bb7f867adf9c3a…
Jonah-Heyl Aug 30, 2023
37fe695
Apply formatting to Markdown files
actions-user Aug 30, 2023
c997e23
small_mistakes_forget_after_lb
Jonah-Heyl Aug 30, 2023
dfd5698
Merge c997e23e82697a3e7a5220dfa4929342e5e94b9f into 2e3bb7f867adf9c3a…
Jonah-Heyl Aug 30, 2023
bdaa0fd
Apply formatting to Markdown files
actions-user Aug 30, 2023
40a29e7
fixes
Jonah-Heyl Sep 1, 2023
dbc5c76
mistakes_in_local_build
Jonah-Heyl Sep 1, 2023
d0860e6
Merge dbc5c76f58a94b004b530fde8942e9e49ef195f8 into 2e3bb7f867adf9c3a…
Jonah-Heyl Sep 1, 2023
8cb431e
Apply formatting to Markdown files
actions-user Sep 1, 2023
30942ae
formatter_did'nt_pick_up_Xi
Jonah-Heyl Sep 1, 2023
b38a570
Merge 30942ae978f4ed04adeaa69785d0e3e4e30151dd into 2e3bb7f867adf9c3a…
Jonah-Heyl Sep 1, 2023
ccf998c
mcm_fix
Jonah-Heyl Sep 2, 2023
ddfef21
Merge ccf998c48e036838e314f76daa877c565dd16245 into 2e3bb7f867adf9c3a…
Jonah-Heyl Sep 2, 2023
aeb503d
Apply formatting to Markdown files
actions-user Sep 2, 2023
a082cba
43_fix
Jonah-Heyl Sep 2, 2023
040758d
Merge a082cba1a1475c2b98d2db871321a6677f77f32c into 2e3bb7f867adf9c3a…
Jonah-Heyl Sep 2, 2023
5da3f6a
Apply formatting to Markdown files
actions-user Sep 2, 2023
b6bee2c
Made changes based on previous comments
jlperla Sep 6, 2023
9cfa98f
Ran formatter
jlperla Sep 6, 2023
0735580
Merge 9cfa98f3f77d34e16c039b00d8b085ee22d84017 into 2e3bb7f867adf9c3a…
Jonah-Heyl Sep 6, 2023
f083a65
Try to drop the scrollbars
jlperla Sep 7, 2023
4c9862c
Turn off formatter for now
jlperla Sep 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ Julia code blocks in the myst `.md` files can be formatted using a script in thi
julia format_myst.jl lectures/getting_started_julia/getting_started.md
```

As a helper, you can call a shell script to do it for an entire folder
```bash
bash format_all_directory.sh lectures/dynamic_programming
```
or to also do the unicode substitutions
```bash
bash format_all_directory.sh lectures/dynamic_programming true
```

Alternatively, the formatter will run automatically when a pull-request is made


Expand Down
22 changes: 22 additions & 0 deletions format_all_directory.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

# Check if at least one argument is given
if [ "$#" -lt 1 ]; then
echo "Usage: $0 path/to/md/files/ [use_replacements]"
exit 1
fi

# Directory containing markdown files, ensuring it has a trailing slash
dir_path="${1%/}/"

# Optional use_replacements flag, default to "false" if not provided
use_replacements=${2:-false}

# Loop over all .md files in the given directory
for file_path in ${dir_path}*.md; do
# Only process regular files
if [ -f "$file_path" ]; then
# Call the Julia script with the current .md file and the use_replacements flag
julia format_myst.jl "$file_path" $use_replacements
fi
done
3 changes: 2 additions & 1 deletion format_myst.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ function format_myst(input_file_path, output_file_path, extra_replacements = fal
# Additional replacements are optional. This may be useful when replacing variable names to make it easier to type in ascii
replacements = Dict("α" => "alpha", "β" => "beta", "γ" => "gamma", "≤" => "<=",
"≥" => ">=", "Σ" => "Sigma", "σ" => "sigma","μ"=>"mu","ϕ"=>"phi","ψ"=>"psi","ϵ"=>"epsilon",
"δ"=>"delta","θ" => "theta","ζ"=>"zeta","X̄" => "X_bar","p̄" => "p_bar","x̂" => "x_hat","λ"=>"lambda","ρ"=>"rho")
"δ"=>"delta","θ" => "theta","ζ"=>"zeta","X̄" => "X_bar","p̄" => "p_bar","x̂" => "x_hat","λ"=>"lambda",
"ρ"=>"rho","u′" => "u_prime" , "f′"=>"f_prime"," ∂u∂c"=>"dudc","Π"=>"Pi","π"=>"pi"," ξ"=>"Xi","c̄"=>"c_bar","w̄"=>"w_bar")

# Replace the code blocks in the content and handle exceptions
try
Expand Down
92 changes: 43 additions & 49 deletions lectures/dynamic_programming/career.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ $$

Interpretation:

* draw $q$ from a β distribution with shape parameters $(a, b)$
* draw $q$ from a $\beta$ distribution with shape parameters $(a, b)$
* run $n$ independent binary trials, each with success probability $q$
* $p(k \,|\, n, a, b)$ is the probability of $k$ successes in these $n$ trials

Expand All @@ -156,7 +156,6 @@ using Test
```{code-cell} julia
using LaTeXStrings, Plots, QuantEcon, Distributions


n = 50
a_vals = [0.5, 1, 100]
b_vals = [0.5, 1, 100]
Expand All @@ -182,41 +181,37 @@ Implementation:
The code for solving the DP problem described above is found below:

```{code-cell} julia
function CareerWorkerProblem(;β = 0.95,
function CareerWorkerProblem(; beta = 0.95,
B = 5.0,
N = 50,
F_a = 1.0,
F_b = 1.0,
G_a = 1.0,
G_b = 1.0)
θ = range(0, B, length = N)
ϵ = copy(θ)
dist_F = BetaBinomial(N-1, F_a, F_b)
dist_G = BetaBinomial(N-1, G_a, G_b)
theta = range(0, B, length = N)
epsilon = copy(theta)
dist_F = BetaBinomial(N - 1, F_a, F_b)
dist_G = BetaBinomial(N - 1, G_a, G_b)
F_probs = pdf.(dist_F, support(dist_F))
G_probs = pdf.(dist_G, support(dist_G))
F_mean = sum(θ .* F_probs)
G_mean = sum(ϵ .* G_probs)
return (β = β, N = N, B = B, θ = θ, ϵ = ϵ,
F_probs = F_probs, G_probs = G_probs,
F_mean = F_mean, G_mean = G_mean)
F_mean = sum(theta .* F_probs)
G_mean = sum(epsilon .* G_probs)
return (; beta, N, B, theta, epsilon, F_probs, G_probs, F_mean, G_mean)
end

function update_bellman!(cp, v, out; ret_policy = false)

# new life. This is a function of the distribution parameters and is
# always constant. No need to recompute it in the loop
v3 = (cp.G_mean + cp.F_mean .+ cp.β .*
cp.F_probs' * v * cp.G_probs)[1] # do not need 1 element array
v3 = (cp.G_mean + cp.F_mean .+ cp.beta .* cp.F_probs' * v * cp.G_probs)[1] # do not need 1 element array

for j in 1:cp.N
for i in 1:cp.N
for j in 1:(cp.N)
for i in 1:(cp.N)
# stay put
v1 = cp.θ[i] + cp.ϵ[j] + cp.β * v[i, j]
v1 = cp.theta[i] + cp.epsilon[j] + cp.beta * v[i, j]

# new job
v2 = (cp.θ[i] .+ cp.G_mean .+ cp.β .*
v[i, :]' * cp.G_probs)[1] # do not need a single element array
v2 = (cp.theta[i] .+ cp.G_mean .+ cp.beta .* v[i, :]' * cp.G_probs)[1] # do not need a single element array

if ret_policy
if v1 > max(v2, v3)
Expand All @@ -234,7 +229,6 @@ function update_bellman!(cp, v, out; ret_policy = false)
end
end


function update_bellman(cp, v; ret_policy = false)
out = similar(v)
update_bellman!(cp, v, out, ret_policy = ret_policy)
Expand Down Expand Up @@ -274,8 +268,9 @@ v_init = fill(100.0, wp.N, wp.N)
func(x) = update_bellman(wp, x)
v = compute_fixed_point(func, v_init, max_iter = 500, verbose = false)

plot(linetype = :surface, wp.θ, wp.ϵ, transpose(v), xlabel=L"\theta", ylabel=L"\epsilon",
seriescolor=:plasma, gridalpha = 1)
plot(linetype = :surface, wp.theta, wp.epsilon, transpose(v), xlabel = L"\theta",
ylabel = L"\epsilon",
seriescolor = :plasma, gridalpha = 1)
```

The optimal policy can be represented as follows (see {ref}`Exercise 3 <career_ex3>` for code).
Expand Down Expand Up @@ -373,31 +368,31 @@ G = DiscreteRV(wp.G_probs)

function gen_path(T = 20)
i = j = 1
θ_ind = Int[]
ϵ_ind = Int[]
theta_ind = Int[]
epsilon_ind = Int[]

for t=1:T
for t in 1:T
# do nothing if stay put
if optimal_policy[i, j] == 2 # new job
j = rand(G)[1]
elseif optimal_policy[i, j] == 3 # new life
i, j = rand(F)[1], rand(G)[1]
end
push!(θ_ind, i)
push!(ϵ_ind, j)
push!(theta_ind, i)
push!(epsilon_ind, j)
end
return wp.θ[θ_ind], wp.ϵ[ϵ_ind]
return wp.theta[theta_ind], wp.epsilon[epsilon_ind]
end

plot_array = Any[]
for i in 1:2
θ_path, ϵ_path = gen_path()
plt = plot(ϵ_path, label=L"\epsilon")
plot!(plt, θ_path, label=L"\theta")
plot!(plt, legend=:bottomright)
theta_path, epsilon_path = gen_path()
plt = plot(epsilon_path, label = L"\epsilon")
plot!(plt, theta_path, label = L"\theta")
plot!(plt, legend = :bottomright)
push!(plot_array, plt)
end
plot(plot_array..., layout = (2,1))
plot(plot_array..., layout = (2, 1))
```

```{code-cell} julia
Expand Down Expand Up @@ -430,7 +425,6 @@ function gen_first_passage_time(optimal_policy)
end
end


M = 25000
samples = zeros(M)
for i in 1:M
Expand All @@ -448,12 +442,12 @@ tags: [remove-cell]
end
```

To compute the median with $\beta=0.99$ instead of the default value $\beta=0.95$, replace `wp=CareerWorkerProblem()` with `wp=CareerWorkerProblem(β=0.99)`.
To compute the median with $\beta=0.99$ instead of the default value $\beta=0.95$, replace `wp=CareerWorkerProblem()` with `wp=CareerWorkerProblem(beta=0.99)`.

The medians are subject to randomness, but should be about 7 and 14 respectively. Not surprisingly, more patient workers will wait longer to settle down to their final job.

```{code-cell} julia
wp2 = CareerWorkerProblem(β=0.99)
wp2 = CareerWorkerProblem(beta = 0.99)

v2, optimal_policy2 = solve_wp(wp2)

Expand Down Expand Up @@ -485,30 +479,30 @@ lvls = [0.5, 1.5, 2.5, 3.5]
x_grid = range(0, 5, length = 50)
y_grid = range(0, 5, length = 50)

contour(x_grid, y_grid, optimal_policy', fill=true, levels=lvls,color = :Blues,
fillalpha=1, cbar = false)
contour!(xlabel=L"\theta", ylabel=L"\epsilon")
annotate!([(1.8,2.5, text("new life", 14, :white, :center))])
annotate!([(4.5,2.5, text("new job", 14, :center))])
annotate!([(4.0,4.5, text("stay put", 14, :center))])
contour(x_grid, y_grid, optimal_policy', fill = true, levels = lvls, color = :Blues,
fillalpha = 1, cbar = false)
contour!(xlabel = L"\theta", ylabel = L"\epsilon")
annotate!([(1.8, 2.5, text("new life", 14, :white, :center))])
annotate!([(4.5, 2.5, text("new job", 14, :center))])
annotate!([(4.0, 4.5, text("stay put", 14, :center))])
```

Now, we need only swap out for the new parameters

```{code-cell} julia
wp = CareerWorkerProblem(G_a=100.0, G_b=100.0); # use new params
wp = CareerWorkerProblem(G_a = 100.0, G_b = 100.0); # use new params
v, optimal_policy = solve_wp(wp)

lvls = [0.5, 1.5, 2.5, 3.5]
x_grid = range(0, 5, length = 50)
y_grid = range(0, 5, length = 50)

contour(x_grid, y_grid, optimal_policy', fill=true, levels=lvls,color = :Blues,
fillalpha=1, cbar = false)
contour!(xlabel=L"\theta", ylabel=L"\epsilon")
annotate!([(1.8,2.5, text("new life", 14, :white, :center))])
annotate!([(4.5,2.5, text("new job", 14, :center))])
annotate!([(4.0,4.5, text("stay put", 14, :center))])
contour(x_grid, y_grid, optimal_policy', fill = true, levels = lvls, color = :Blues,
fillalpha = 1, cbar = false)
contour!(xlabel = L"\theta", ylabel = L"\epsilon")
annotate!([(1.8, 2.5, text("new life", 14, :white, :center))])
annotate!([(4.5, 2.5, text("new job", 14, :center))])
annotate!([(4.0, 4.5, text("stay put", 14, :center))])
```

You will see that the region for which the worker
Expand Down
Loading
Loading