Differentiation and Integration: (Lectures On Numerical Analysis For Economists II)
Differentiation and Integration: (Lectures On Numerical Analysis For Economists II)
1. Equation solving.
5. Machine learning.
1
Algorithms
• Differentiation:
1. Forward/backward/central differentiation.
3. Symbolic differentiation.
4. Automatic differentiation.
• Integration:
1. Quadrature methods.
2. Monte Carlo.
f (x + ε) − f (x)
f 0 (x) = lim
ε→0 ε
f (x + h) − f (x)
f 0 (x) ≈
h
• Which h to pick?
√
h = max(|x|, 1)
where is the machine precision (I am skipping proof, standard result).
4
Point of Diminishing Return
To
tal
Log Error
Er
ro
r
Ro
rror un
tio nE do
nca ff E
Tru rro
r
f (x) − f (x − h)
f 0 (x) ≈
h
• Thus, in practice, one uses forward or backward differences depending on whether we care more
about left or right derivative (kinks, finite difference solvers for ODEs, ...).
6
Centered difference
f (x + h) − f (x − h)
f 0 (x) ≈
2h
8
Higher-order and cross-derivatives
• For second- and higher-order derivatives, and often, for cross-derivatives, the error can become
substantial.
2. Symbolic derivatives.
3. Automatic differentiation.
9
Richardson’s extrapolation
• We will skip the derivation (check any standard numerical analysis textbook).
• Result:
−f (x + 2h) + 8f (x + h) − 8f (x − h) + f (x − 2h)
f 0 (x) = + O(h4 ),
12h
a fourth-order approximation.
• Adjust h accordingly.
10
Complex step differentiation I
11
Complex step differentiation II
2
f 00 (x) = ∗ (f (x) − Real (f (x + ih))
h2
• This algorithm depends on the ability of your programming language to deal efficient and accurately
with complex arithmetic.
12
Symbolic differentiation
1. C++: GiNaC.
2. Python: SymPy.
3. Julia: SymPy.jl.
4. R: Racayas.
• Disadvantages:
1. Performance penalty.
2. Limitations in abstractions.
13
Automatic differentiation
Definition
Set of techniques designed to numerically evaluate the derivative of a function while minimizing the
amount of arithmetic operations.
• Automatic differentiation divides the function to derivate into small parts and then applies the chain
rule to solve for the derivative.
• Example:
xy 2 + log x
2
= w1 (w2 ) + log w1
= w3 + w4
= w5
• We get:
w50 = w30 + w40
2 w10
= w10 (w2 ) + 2w1 w20 +
w1
2 1
= (w2 ) +
w1
1
= y2 +
x
• How do you implement it in the computer? 15
Quadrature Integration
Overview
Quadrature
Method of solving an integral numerically by exploiting the definition of the integral.
• There are several quadrature methods, each evaluating the integral at different points and using the
evaluations differently.
1. Newton-Coates.
2. Gaussian.
3. Clenshaw-Curtis.
• Some methods are more general, but slower, whereas others can be more restrictive and complicated,
but have faster running time.
16
Newton-Cotes: overview
17
18
Midpoint rule
• We can define irregularly-spaced step sizes if additional information about f (·) is available. 19
20
Trapezoid rule
• The trapezoid (close) rule uses a linear approximation of f along with the values of f at the
endpoints:
Z b
f (a) + f (b) (b − a)3 00
f (x)dx = (b − a) − f (ξ)
a 2 12
where ξ ∈ [a, b].
• We can define the composite trapezoid rule as we did with the composite midpoint rule:
Z b n−1
b − a f (a) X b−a f (a)
f (x)dx ≈ + f a+ j +
a n 2 2 2
j=1
21
22
Simpson rule
• The Simpson rule uses a piecewise-quadratic approximation of f along with the values of f at the
endpoints and the midpoint.
Z b
(b − a)5 (4)
b−a b+a
f (x)dx = [f (a) + 4f + f (b)] − f (ξ)
a 6 2 2880
where ξ ∈ [a, b].
(b−a)
• Analogous composite rule with n ≥ 2 intervals, h = n and xj = a + jh, is defined as
h h4 (b − a) (4)
Sn (f ) = [f0 + 4f1 + 2f2 + 4f3 + ... + 4fn−1 + fn ] − f (ξ)
3 180
where ξ ∈ [a, b]
• Other rules: Simpson’s 3/8 rule and Boole’s rule.
• For any fixed non-negative weighting function w (x) Gaussian quadrature creates approximation of
the form: Z b Xn
f (x)w (x)dx ≈ ωi f (xi )
a i=1
• Often, computing the nodes and weights is not required, as the more useful Gaussian quadrature
nodes and weights can be found in tables.
25
Chebyshev quadrature
2i−1
for some ξ ∈ [−1, 1] where the quadrature nodes are xi = cos 2n π with i = 1, ..., n.
• Constant weight π
n for each node and quadrature nodes that are easy to compute.
26
Hermite quadrature
• Useful for economics due to the common use of normally distributed random variables, especially in
macro and finance.
27
Interpolatory rules
• Interpolatory quadrature rules involves using derivatives of f (x) to approximate the integral
Rb
a
f (x)dx.
Z b Xn X m
f (x)w (x)dx ≈ ωij f (j) (xi )
a i=i j=1
where once again the xi are nodes and the ωi are weights.
28
Newton-Cotes vs. Gaussian
• In Newton-Cotes formulas, the xi points are chosen arbitrarily, ususally uniformly spaced on [a, b]
whereas in the Gaussian formulas, both the nodes and weights are chosen efficiently.
• Efficiency is measured using the exact integration for finite-dimensional collection of functions.
• While Gaussian may have a time advantage over Newton-Cotes, this comes at the cost of having to
perform complex calculations to find the weights and nodes.
29
Multidimensional quadrature
• One approach to deal with multidimensional integrals is to directly extend the one-dimensional
methods via product rules.
• However:
• There is no guarantee of a solution and if there is a solution, then there will be multiple and it is
possible that they will have negative weights.
• The main practice used to extend to higher dimensions is Monte Carlo integration.
30
Monte Carlo Integration
A bit of historical background and intuition
1. Probability of getting a total of six points when rolling two (fair) dices.
31
Overview
• This method can handle problems of far greater complexity and size than most other methods.
• As well, Monte Carlo methods can deliver accurate results using moderate number of points (which
are randomly selected).
• It is based on the law of large numbers and the central limit theorem.
• Monte Carlo produces a random approximation, which puts structure on the error term.
32
Crude method
R1
• A crude Monte Carlo estimate of E [f (X )] = 0
f (x)dx is calculated by generating N draws from
U[0, 1],{xi }N
i=1 and takes the form
N
ˆ 1 X
If = f (xi )
N
i=1
• Although this estimator is unbiased, it is not commonly used, because of its large variance.
• There are variety of simple techniques that can reduce the variance, but retain its unbiasedness.
33
Randomness in computation
• Matlab uses highly non-linear iterative algorithms that “look like” random.
• Other (standard and nonstandard) distributions come from manipulations of the uniform.
2. Christian Robert and George Casella, Monte Carlo Statistical Methods, 2nd ed, Springer-Verlag, 2004.
35
Stratefied sampling
• This sampling method exploits the fact that there will be subintervals with lower variance.
• Suppose that we divide [0, 1] into [0, α] and [α, 1], then if we have N points in each interval we can
form the estimate
αX 1−α X
Iˆf = f (x1i ) + f (x2i )
N N
i i
36
37
Importance sampling
• The idea of the method is that we sample more intensively where f is large, which is where f is
R
making the greatest contribution to f (x)dx.
R1
• If p(x) > 0, and 0 p(x)dx = 1, then p(x) is a density and
Z 1 Z 1
f (x)
If = f (x)dx = p(x)dx
0 0 p(x)
• Therefore, if xi is drawn with density p(x), then the following is an unbiased estimator of I
n
1 X f (xi )
Iˆf =
N p(xi )
i=1
38
39
Comparison: 1 dimension
Z 1
Calculate e x dx
0
Approximation Error
N Midpoint Trapezoid Simpson’s Monte Carlo
10 -0.00071574 0.00143166 0.00000006 0.09523842
100 -0.00000716 0.00001432 0.00000000 0.01416057
1000 -0.00000007 0.00000014 -0.00000000 -0.00515829
5000 -0.00000000 0.00000001 0.00000000 0.00359500
Approximation Error
N Midpoint Monte Carlo
10 -0.00245918 0.33897914
100 -0.00002460 0.03021147
1000 -0.00000025 -0.05486922
5000 -0.00000001 0.01183325
42
Quasi Monte Carlo Integration
General idea
• Low-discrepancy sequence: a sequence with the property that for all values of N, its subsequence
x1 , ..., xN has a low discrepancy with respect to interval [a, b]:
# |{x1 , ..., xN } ∩ [c, d]| d − c
DN = sup −
a≤c≤d≤b N b −a
• Intuition.
43
More resources
• Check:
1. http://mikejuniperhill.blogspot.com/2014/03/using-c-nag-random-numbers-in-excel.html
2. https://www.rdocumentation.org/packages/randtoolbox/versions/1.17/topics/quasiRNG
44
Sobol vs. pseudorandom
45