Skip to content

Commit 4c78965

Browse files
FarhadShahryarpoorFarhad Shahryarpoor
andauthored
lae cleanup done. (#369)
* lae cleanup done. * Small edits. * Comment implemented. * small edit. --------- Co-authored-by: Farhad Shahryarpoor <[email protected]>
1 parent 8051348 commit 4c78965

File tree

1 file changed

+19
-25
lines changed

1 file changed

+19
-25
lines changed

lectures/tools_and_techniques/stationary_densities.md

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ Markov process.
6868
---
6969
tags: [hide-output]
7070
---
71-
using LinearAlgebra, Statistics
72-
using KernelDensity, Distributions, LaTeXStrings, Plots, QuantEcon, Random
71+
using LinearAlgebra, Statistics, Distributions, LaTeXStrings, Plots, StatsPlots,
72+
Random
7373
```
7474

7575
(statd_density_case)=
@@ -432,17 +432,7 @@ In fact much stronger convergence results are true (see, for example, [this pape
432432
```
433433

434434
### Implementation
435-
436-
A function which calls an `LAE` type for estimating densities by this technique can be found in [lae.jl](https://github.com/QuantEcon/QuantEcon.jl/blob/master/src/lae.jl).
437-
438-
This function returns the right-hand side of {eq}`statd_lae1` using
439-
440-
* an object of type `LAE` that stores the stochastic kernel and the observations
441-
* the value $y$ as its second argument
442-
443-
The function is vectorized, in the sense that if `psi` is such an instance and `y` is an array, then the call `psi(y)` acts elementwise.
444-
445-
(This is the reason that we reshaped `X` and `y` inside the type --- to make vectorization work)
435+
We'll implement a function `lae_est(p, X, ygrid)` that returns the right-hand side of {eq}`statd_lae1`, averaging the kernel $p$ over simulated draws `X` on a grid `ygrid` (see [here](https://github.com/QuantEcon/QuantEcon.jl/blob/master/src/lae.jl) for the original implementation)
446436

447437
### Example
448438

@@ -456,8 +446,6 @@ using Test
456446
```
457447

458448
```{code-cell} julia
459-
using Distributions, StatsPlots, Plots, QuantEcon, Random
460-
461449
Random.seed!(42) # For deterministic results.
462450
463451
s = 0.2
@@ -477,6 +465,13 @@ function p(x, y)
477465
return pdf.(phi, pdf_arg) ./ d
478466
end
479467
468+
function lae_est(p, X, ygrid)
469+
Xmat = reshape(X, :, 1)
470+
Ymat = reshape(ygrid, 1, :)
471+
psi_vals = mean(p.(Xmat, Ymat), dims = 1)
472+
return dropdims(psi_vals, dims = 1)
473+
end
474+
480475
n = 10000 # Number of observations at each date t
481476
T = 30 # Compute density of k_t at 1,...,T+1
482477
@@ -490,16 +485,16 @@ for t in 1:(T - 1)
490485
k[:, t + 1] = s * A[:, t] .* k[:, t] .^ alpha + (1 - delta) .* k[:, t]
491486
end
492487
493-
# Generate T instances of LAE using this data, one for each date t
494-
laes = [LAE(p, k[:, t]) for t in T:-1:1]
488+
# Store draws for each date t
489+
laes = [k[:, t] for t in T:-1:1]
495490
496491
# Plot
497492
ygrid = range(0.01, 4, length = 200)
498493
laes_plot = []
499494
colors = []
500495
for i in 1:T
501-
psi = laes[i]
502-
push!(laes_plot, lae_est(psi, ygrid))
496+
lae = laes[i]
497+
push!(laes_plot, lae_est(p, lae, ygrid))
503498
push!(colors, RGBA(0, 0, 0, 1 - (i - 1) / T))
504499
end
505500
plot(ygrid, laes_plot, color = reshape(colors, 1, length(colors)), lw = 2,
@@ -943,7 +938,7 @@ for t in 1:(n - 1)
943938
X[t + 1] = theta * abs(X[t]) + d * Z[t]
944939
end
945940
946-
psi_est(a) = lae_est(LAE(p_TAR, X), a)
941+
psi_est(a) = lae_est(p_TAR, X, a)
947942
k_est = kde(X)
948943
949944
ys = range(-3, 3, length = 200)
@@ -1012,12 +1007,12 @@ for i in 1:4
10121007
k[:, t + 1] = s * A[:, t] .* k[:, t] .^ alpha + (1 - delta) .* k[:, t]
10131008
end
10141009
1015-
# Generate T instances of LAE using this data, one for each date t
1016-
laes = [LAE(p_growth, k[:, t]) for t in T:-1:1]
1010+
# Store draws for each date t
1011+
laes = [k[:, t] for t in T:-1:1]
10171012
ind = i
10181013
for j in 1:T
1019-
psi = laes[j]
1020-
laes_plot[:, ind] = lae_est(psi, ygrid)
1014+
lae = laes[j]
1015+
laes_plot[:, ind] = lae_est(p_growth, lae, ygrid)
10211016
ind = ind + 4
10221017
push!(colors, RGBA(0, 0, 0, 1 - (j - 1) / T))
10231018
end
@@ -1107,4 +1102,3 @@ By the definition of $V$, we have $F_V(v) = \mathbb P \{ a + b U \leq v \} = \ma
11071102
In other words, $F_V(v) = F_U ( (v - a)/b )$.
11081103

11091104
Differentiating with respect to $v$ yields {eq}`statd_dv`.
1110-

0 commit comments

Comments
 (0)