examples


In [1]:
ygrid0 = linspace(-4, 4, 10)
agrid0 = linspace(0.0.^0.4, 100.0.^0.4, 25).^(1/0.4);

In [2]:
using BasisMatrices

# 1st method
y_basis = Basis(ChebParams(length(ygrid0), minimum(ygrid0), maximum(ygrid0)))
a_basis = Basis(SplineParams(agrid0, 0, 3))
basis = Basis(a_basis, y_basis)

# 2nd method
basis = Basis(SplineParams(agrid0, 0, 3),
              ChebParams(length(ygrid0), minimum(ygrid0), maximum(ygrid0)))


Out[2]:
2 dimensional Basis on the hypercube formed by [0.0,-4.0] × [100.00000000000003,4.0].
Basis families are BasisMatrices.Spline × BasisMatrices.Cheb

In [3]:
S, (agrid, ygrid) = nodes(basis);

In [4]:
Φ = BasisMatrix(basis, Expanded(), S, 0)


Out[4]:
BasisMatrix{BasisMatrices.Expanded} of order 1x2 Array{Int64,2}:
 0  0

In [5]:
# Actual function at interpolation nodes
f(a::Vector{Float64}, y::Vector{Float64}) = a.^(1/2).*exp(y)
y = f(S[:,1], S[:, 2])

# Get coefficients
c = Φ.vals[1] \ y;

In [6]:
using QuantEcon: gridmake

ygridf = linspace(-4, 4, 100)
agridf = linspace(0.0, 100.0, 250)
Sf = gridmake(agridf, ygridf)
yf = f(Sf[:, 1], Sf[:, 2])
interp = funeval(c, basis, Sf);

In [7]:
using Plots
plotlyjs()

plt_a = plot(agridf, [interp[1:length(agridf)] yf[1:length(agridf)]], 
             label=["Interpolant", "Original"]',
             lw=2,
             xlabel="a")
plt_y = plot(ygridf, [interp[5:length(agridf):end] yf[5:length(agridf):end]], 
             label="",
             lw=2,
             xlabel="y")
plot(plt_a, plt_y, layout=(1, 2), size=(800, 350))


INFO: Recompiling stale cache file C:\Users\taishi\.julia\lib\v0.4\Plots.ji for module Plots.
INFO: Recompiling stale cache file C:\Users\taishi\.julia\lib\v0.4\FixedSizeArrays.ji for module FixedSizeArrays.
INFO: Recompiling stale cache file C:\Users\taishi\.julia\lib\v0.4\Colors.ji for module Colors.
INFO: Recompiling stale cache file C:\Users\taishi\.julia\lib\v0.4\FixedPointNumbers.ji for module FixedPointNumbers.
WARNING: New definition 
    floattype(Type{#T<:FixedPointNumbers.Fixed}) at C:\Users\taishi\.julia\v0.4\FixedPointNumbers\src\fixed.jl:16
is ambiguous with: 
    floattype(Type{FixedPointNumbers.FixedPoint{#T<:Union{Int8, UInt16, Int16, UInt8}, #f<:Any}}) at C:\Users\taishi\.julia\v0.4\FixedPointNumbers\src\FixedPointNumbers.jl:89.
To fix, define 
    floattype(Type{FixedPointNumbers.Fixed{_<:Union{Int8, Int16}, #f<:Any}})
before the new definition.
WARNING: New definition 
    floattype(Type{#T<:FixedPointNumbers.UFixed}) at C:\Users\taishi\.julia\v0.4\FixedPointNumbers\src\ufixed.jl:14
is ambiguous with: 
    floattype(Type{FixedPointNumbers.FixedPoint{#T<:Union{Int8, UInt16, Int16, UInt8}, #f<:Any}}) at C:\Users\taishi\.julia\v0.4\FixedPointNumbers\src\FixedPointNumbers.jl:89.
To fix, define 
    floattype(Type{FixedPointNumbers.UFixed{_<:Union{UInt16, UInt8}, #f<:Any}})
before the new definition.
INFO: Recompiling stale cache file C:\Users\taishi\.julia\lib\v0.4\ColorTypes.ji for module ColorTypes.
INFO: Recompiling stale cache file C:\Users\taishi\.julia\lib\v0.4\Iterators.ji for module Iterators.
INFO: Recompiling stale cache file C:\Users\taishi\.julia\lib\v0.4\Measures.ji for module Measures.
[Plots.jl] Initializing backend: plotlyjs
INFO: Precompiling module PlotlyJS...
INFO: Recompiling stale cache file C:\Users\taishi\.julia\lib\v0.4\Blink.ji for module Blink.
INFO: Recompiling stale cache file C:\Users\taishi\.julia\lib\v0.4\Requires.ji for module Requires.
INFO: Recompiling stale cache file C:\Users\taishi\.julia\lib\v0.4\MacroTools.ji for module MacroTools.
INFO: Recompiling stale cache file C:\Users\taishi\.julia\lib\v0.4\Lazy.ji for module Lazy.
INFO: Recompiling stale cache file C:\Users\taishi\.julia\lib\v0.4\Mux.ji for module Mux.
INFO: Recompiling stale cache file C:\Users\taishi\.julia\lib\v0.4\Hiccup.ji for module Hiccup.
INFO: Recompiling stale cache file C:\Users\taishi\.julia\lib\v0.4\HttpServer.ji for module HttpServer.
INFO: Recompiling stale cache file C:\Users\taishi\.julia\lib\v0.4\HttpCommon.ji for module HttpCommon.
INFO: Recompiling stale cache file C:\Users\taishi\.julia\lib\v0.4\MbedTLS.ji for module MbedTLS.
INFO: Recompiling stale cache file C:\Users\taishi\.julia\lib\v0.4\HttpParser.ji for module HttpParser.
INFO: Recompiling stale cache file C:\Users\taishi\.julia\lib\v0.4\WebSockets.ji for module WebSockets.
INFO: Recompiling stale cache file C:\Users\taishi\.julia\lib\v0.4\Codecs.ji for module Codecs.
INFO: Recompiling stale cache file C:\Users\taishi\.julia\lib\v0.4\Mustache.ji for module Mustache.
INFO: Recompiling stale cache file C:\Users\taishi\.julia\lib\v0.4\LaTeXStrings.ji for module LaTeXStrings.

Plotly javascript loaded.

To load again call

init_notebook(true)

Out[7]:

In [ ]: