7. General Purpose Packages#
Contents
7.1. Overview#
Julia has both a large number of useful, well written libraries and many incomplete poorly maintained proofs of concept.
A major advantage of Julia libraries is that, because Julia itself is sufficiently fast, there is less need to mix in low level languages like C and Fortran.
As a result, most Julia libraries are written exclusively in Julia.
Not only does this make the libraries more portable, it makes them much easier to dive into, read, learn from and modify.
In this lecture we introduce a few of the Julia libraries that we’ve found particularly useful for quantitative work in economics.
Also see data and statistical packages and optimization, solver, and related packages for more domain specific packages.
using LinearAlgebra, Statistics
using QuadGK, FastGaussQuadrature, Distributions, Expectations
using Interpolations, Plots, ProgressMeter
7.2. Numerical Integration#
Many applications require directly calculating a numerical derivative and calculating expectations.
7.2.1. Adaptive Quadrature#
A high accuracy solution for calculating numerical integrals is QuadGK.
using QuadGK
@show value, tol = quadgk(cos, -2π, 2π);
(value, tol) = quadgk(cos, -2π, 2π) = (-1.8403051655192592e-17, 1.0683776742236108e-23)
This is an adaptive Gauss-Kronrod integration technique that’s relatively accurate for smooth functions.
However, its adaptive implementation makes it slow and not well suited to inner loops.
7.2.2. Gaussian Quadrature#
Alternatively, many integrals can be done efficiently with (non-adaptive) Gaussian quadrature.
For example, using FastGaussQuadrature.jl
using FastGaussQuadrature
x, w = gausslegendre(100_000); # i.e. find 100,000 nodes
# integrates f(x) = x^2 from -1 to 1
f(x) = x^2
@show w ⋅ f.(x); # calculate integral
w ⋅ f.(x) = 0.6666666666666665
The only problem with the FastGaussQuadrature
package is that you will need to deal with affine transformations to the non-default domains yourself.
7.2.3. Expectations#
If the calculations of the numerical integral is simply for calculating mathematical expectations of a particular distribution, then Expectations.jl provides a convenient interface.
Under the hood, it is finding the appropriate Gaussian quadrature scheme for the distribution using FastGaussQuadrature
.
using Distributions, Expectations
dist = Normal()
E = expectation(dist)
f(x) = x
@show E(f) #i.e. identity
# Or using as a linear operator
f(x) = x^2
x = nodes(E)
w = weights(E)
E * f.(x) == f.(x) ⋅ w
E(f) = -5.028942338420413e-18
true
7.3. Interpolation#
In economics we often wish to interpolate discrete data (i.e., build continuous functions that join discrete sequences of points).
The package we usually turn to for this purpose is Interpolations.jl.
There are a variety of options, but we will only demonstrate the convenient notations.
7.3.1. Univariate with a Regular Grid#
Let’s start with the univariate case.
We begin by creating some data points, using a sine function
using Interpolations
using Plots
x = -7:7 # x points, coase grid
y = sin.(x) # corresponding y points
xf = -7:0.1:7 # fine grid
plot(xf, sin.(xf), label = "sin function")
scatter!(x, y, label = "sampled data", markersize = 4)
To implement linear and cubic spline interpolation
li = LinearInterpolation(x, y)
li_spline = CubicSplineInterpolation(x, y)
@show li(0.3) # evaluate at a single point
scatter(x, y, label = "sampled data", markersize = 4)
plot!(xf, li.(xf), label = "linear")
plot!(xf, li_spline.(xf), label = "spline")
li(0.3) = 0.25244129544236954
7.3.2. Univariate with Irregular Grid#
In the above, the LinearInterpolation
function uses a specialized function
for regular grids since x
is a Range
type.
For an arbitrary, irregular grid
x = log.(range(1, exp(4), length = 10)) .+ 1 # uneven grid
y = log.(x) # corresponding y points
interp = LinearInterpolation(x, y)
xf = log.(range(1, exp(4), length = 100)) .+ 1 # finer grid
plot(xf, interp.(xf), label = "linear")
scatter!(x, y, label = "sampled data", markersize = 4, size = (800, 400))
At this point, Interpolations.jl
does not have support for cubic splines with irregular grids, but there are plenty of other packages that do (e.g. Dierckx.jl and GridInterpolations.jl).
7.3.3. Multivariate Interpolation#
Interpolating a regular multivariate function uses the same function
f(x, y) = log(x + y)
xs = 1:0.2:5
ys = 2:0.1:5
A = [f(x, y) for x in xs, y in ys]
# linear interpolation
interp_linear = LinearInterpolation((xs, ys), A)
@show interp_linear(3, 2) # exactly log(3 + 2)
@show interp_linear(3.1, 2.1) # approximately log(3.1 + 2.1)
# cubic spline interpolation
interp_cubic = CubicSplineInterpolation((xs, ys), A)
@show interp_cubic(3, 2) # exactly log(3 + 2)
@show interp_cubic(3.1, 2.1) # approximately log(3.1 + 2.1);
interp_linear(3, 2) = 1.6094379124341003
interp_linear(3.1, 2.1) = 1.6484736801441782
interp_cubic(3, 2) = 1.6094379124341
interp_cubic(3.1, 2.1) = 1.6486586594237707
1.6486586594237707
See Interpolations.jl documentation for more details on options and settings.
7.4. Linear Algebra#
7.4.1. Standard Library#
The standard library contains many useful routines for linear algebra, in
addition to standard functions such as det()
, inv()
, factorize()
, etc.
Routines are available for
Cholesky factorization
LU decomposition
Singular value decomposition,
Schur factorization, etc.
See here for further details.
7.5. General Tools#
7.5.1. ProgressMeter.jl#
For long-running operations, you can use the ProgressMeter.jl package.
To use the package, you simply put a macro in front of for
loops, etc.
From the documentation
using ProgressMeter
@showprogress 1 "Computing..." for i in 1:50
sleep(0.1) # some computation....
end
Computing... 4%|█▌ | ETA: 0:00:32
Computing... 40%|███████████████▋ | ETA: 0:00:05
Computing... 66%|█████████████████████████▊ | ETA: 0:00:03
Computing... 86%|█████████████████████████████████▌ | ETA: 0:00:01
Computing... 100%|███████████████████████████████████████| Time: 0:00:06