Skip to content

Commit 978ece1

Browse files
Merge pull request #81 from QuantEcon/issue49
removed Dierckx: replaced Spline1D with CubicSpline from DataInterpolations
2 parents bec9738 + 9538167 commit 978ece1

File tree

3 files changed

+27
-38
lines changed

3 files changed

+27
-38
lines changed

lectures/Manifest.toml

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,12 @@ git-tree-sha1 = "55be907a531471de062f321147006c04b8c1e75f"
352352
uuid = "1313f7d8-7da2-5740-9ea0-a2ca25f37964"
353353
version = "0.8.0"
354354

355+
[[DataInterpolations]]
356+
deps = ["ChainRulesCore", "LinearAlgebra", "Optim", "RecipesBase", "RecursiveArrayTools", "Reexport"]
357+
git-tree-sha1 = "7d235e13a5de66ffc0cb85446bcd3edb0556f992"
358+
uuid = "82cc6244-b520-54b8-b5a6-8a565e85f1d0"
359+
version = "3.6.0"
360+
355361
[[DataStructures]]
356362
deps = ["Compat", "InteractiveUtils", "OrderedCollections"]
357363
git-tree-sha1 = "4437b64df1e0adccc3e5d1adbc3ac741095e4677"
@@ -377,18 +383,6 @@ uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"
377383
deps = ["Mmap"]
378384
uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab"
379385

380-
[[Dierckx]]
381-
deps = ["Dierckx_jll"]
382-
git-tree-sha1 = "5fefbe52e9a6e55b8f87cb89352d469bd3a3a090"
383-
uuid = "39dd38d3-220a-591b-8e3c-4c3a8c710a94"
384-
version = "0.5.1"
385-
386-
[[Dierckx_jll]]
387-
deps = ["CompilerSupportLibraries_jll", "Libdl", "Pkg"]
388-
git-tree-sha1 = "a580560f526f6fc6973e8bad2b036514a4e3b013"
389-
uuid = "cd4c43a9-7502-52ba-aa6d-59fb2a88580b"
390-
version = "0.0.1+0"
391-
392386
[[DiffEqBase]]
393387
deps = ["ArrayInterface", "ChainRulesCore", "DataStructures", "DocStringExtensions", "FastBroadcast", "FunctionWrappers", "IterativeSolvers", "LabelledArrays", "LinearAlgebra", "Logging", "MuladdMacro", "NonlinearSolve", "Parameters", "Printf", "RecursiveArrayTools", "RecursiveFactorization", "Reexport", "Requires", "SciMLBase", "Setfield", "SparseArrays", "StaticArrays", "Statistics", "SuiteSparse", "ZygoteRules"]
394388
git-tree-sha1 = "9d312bb0b7c8ace440a71c64330cf1bea0ade0c8"

lectures/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
1414
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
1515
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
1616
DataFramesMeta = "1313f7d8-7da2-5740-9ea0-a2ca25f37964"
17-
Dierckx = "39dd38d3-220a-591b-8e3c-4c3a8c710a94"
17+
DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0"
1818
DiffEqJump = "c894b116-72e5-5b58-be3c-e6d8d4ac2b12"
1919
DiffEqOperators = "9fdde737-9c7f-55bf-ade8-46b3f136cc48"
2020
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"

lectures/dynamic_programming_squared/amss.md

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ function get_policies_time1(T::BellmanEquation,
649649
init[i] = lb[i]
650650
end
651651
end
652-
(minf, minx, ret) = optimize(opt, init)
652+
(minf, minx, ret) = NLopt.optimize(opt, init)
653653
T.z0[i_x, s] = vcat(minx[1], minx[1] + G[s], minx[2:end])
654654
return vcat(-minf, T.z0[i_x, s])
655655
end
@@ -687,7 +687,7 @@ function get_policies_time0(T::BellmanEquation,
687687
init[i] = lb[i]
688688
end
689689
end
690-
(minf, minx, ret) = optimize(opt, init)
690+
(minf, minx, ret) = NLopt.optimize(opt, init)
691691
return vcat(-minf, vcat(minx[1], minx[1]+G[s0], minx[2:end]))
692692
end
693693
```
@@ -961,8 +961,8 @@ assets, returning any excess revenues to the household as nonnegative lump sum t
961961
The recursive formulation is implemented as follows
962962

963963
```{code-cell} julia
964-
using Dierckx
965964
965+
using DataInterpolations
966966
967967
mutable struct BellmanEquation_Recursive{TP <: Model, TI <: Integer, TR <: Real}
968968
model::TP
@@ -1036,19 +1036,15 @@ function solve_time1_bellman(model::Model{TR}, μgrid::AbstractArray) where {TR
10361036
xprimes = repeat(x, 1, S)
10371037
xgrid[s_, :] = x
10381038
for sprime = 1:S
1039-
splc = Spline1D(x[end:-1:1], c[:, sprime][end:-1:1], k=3)
1040-
spln = Spline1D(x[end:-1:1], n[:, sprime][end:-1:1], k=3)
1041-
splx = Spline1D(x[end:-1:1], xprimes[:, sprime][end:-1:1], k=3)
1039+
splc = CubicSpline(c[:, sprime][end:-1:1], x[end:-1:1])
1040+
spln = CubicSpline(n[:, sprime][end:-1:1], x[end:-1:1])
1041+
splx = CubicSpline(xprimes[:, sprime][end:-1:1], x[end:-1:1])
10421042
cf[s_, sprime] = y -> splc(y)
10431043
nf[s_, sprime] = y -> spln(y)
10441044
xprimef[s_, sprime] = y -> splx(y)
1045-
# cf[s_, sprime] = LinInterp(x[end:-1:1], c[:, sprime][end:-1:1])
1046-
# nf[s_, sprime] = LinInterp(x[end:-1:1], n[:, sprime][end:-1:1])
1047-
# xprimef[s_, sprime] = LinInterp(x[end:-1:1], xprimes[:, sprime][end:-1:1])
10481045
end
1049-
splV = Spline1D(x[end:-1:1], V[end:-1:1], k=3)
1046+
splV = CubicSpline(V[end:-1:1], x[end:-1:1])
10501047
Vf[s_] = y -> splV(y)
1051-
# Vf[s_] = LinInterp(x[end:-1:1], V[end:-1:1])
10521048
end
10531049
10541050
policies = [cf, nf, xprimef]
@@ -1094,14 +1090,13 @@ function fit_policy_function(T::BellmanEquation_Recursive,
10941090
for (i_x, x) in enumerate(xgrid)
10951091
PFvec[:, i_x] = PF(i_x, x, s_)
10961092
end
1097-
splV = Spline1D(xgrid, PFvec[1,:], k=3)
1093+
splV = CubicSpline(PFvec[1,:], xgrid)
10981094
Vf[s_] = y -> splV(y)
1099-
# Vf[s_] = LinInterp(xgrid, PFvec[1, :])
11001095
for sprime=1:S
1101-
splc = Spline1D(xgrid, PFvec[1 + sprime, :], k=3)
1102-
spln = Spline1D(xgrid, PFvec[1 + S + sprime, :], k=3)
1103-
splxprime = Spline1D(xgrid, PFvec[1 + 2S + sprime, :], k=3)
1104-
splTT = Spline1D(xgrid, PFvec[1 + 3S + sprime, :], k=3)
1096+
splc = CubicSpline(PFvec[1 + sprime, :], xgrid)
1097+
spln = CubicSpline(PFvec[1 + S + sprime, :], xgrid)
1098+
splxprime = CubicSpline(PFvec[1 + 2S + sprime, :], xgrid)
1099+
splTT = CubicSpline(PFvec[1 + 3S + sprime, :], xgrid)
11051100
cf[s_, sprime] = y -> splc(y)
11061101
nf[s_, sprime] = y -> spln(y)
11071102
xprimef[s_, sprime] = y -> splxprime(y)
@@ -1281,7 +1276,7 @@ function get_policies_time1(T::BellmanEquation_Recursive,
12811276
ftol_rel!(opt, 1e-8)
12821277
ftol_abs!(opt, 1e-8)
12831278
1284-
(minf, minx, ret) = optimize(opt, init)
1279+
(minf, minx, ret) = NLopt.optimize(opt, init)
12851280
12861281
if ret != :SUCCESS && ret != :ROUNDOFF_LIMITED && ret != :MAXEVAL_REACHED &&
12871282
ret != :FTOL_REACHED && ret != :MAXTIME_REACHED
@@ -1356,7 +1351,7 @@ function get_policies_time0(T::BellmanEquation_Recursive,
13561351
maxeval!(opt, 100000000)
13571352
maxtime!(opt, 30)
13581353
1359-
(minf, minx, ret) = optimize(opt, init)
1354+
(minf, minx, ret) = NLopt.optimize(opt, init)
13601355
13611356
if ret != :SUCCESS && ret != :ROUNDOFF_LIMITED && ret != :MAXEVAL_REACHED &&
13621357
ret != :FTOL_REACHED
@@ -1681,12 +1676,12 @@ p
16811676
tags: [remove-cell]
16821677
---
16831678
@testset begin
1684-
#test sim_seq_long_plot[50, 3] ≈ 0.3951985593686047
1685-
#test sim_bel_long_plot[50, 3] ≈ 0.05684753244006188 atol = 1e-2
1686-
#test sim_seq_long_plot[100, 4] ≈ 0.340233842670859
1687-
#test sim_bel_long_plot[100, 4] ≈ 0.2093423366870517 atol = 1e-3
1688-
#test sim_seq_long_plot[200, 2] ≈ 0.5839693539786998
1689-
#test sim_bel_long_plot[200, 2] ≈ 0.6324036099550768 atol = 1e-3
1679+
@test sim_seq_long_plot[50, 3] ≈ 0.3951985593686047
1680+
@test sim_bel_long_plot[50, 3] ≈ 0.05684753244006188 atol = 1e-2
1681+
@test sim_seq_long_plot[100, 4] ≈ 0.340233842670859
1682+
@test sim_bel_long_plot[100, 4] ≈ 0.2093423366870517 atol = 1e-3
1683+
@test sim_seq_long_plot[200, 2] ≈ 0.5839693539786998
1684+
@test sim_bel_long_plot[200, 2] ≈ 0.6324036099550768 atol = 1e-3
16901685
end
16911686
```
16921687

0 commit comments

Comments
 (0)