Professional Documents
Culture Documents
Matlab - Optimization and Integration: Paul Schrimpf
Matlab - Optimization and Integration: Paul Schrimpf
Paul Schrimpf
min f (x)
x
Form bracket x1 < x2 < x3 such that f (x2 ) < f (x1 ) and f (x3 )
Try new x (x1 , x3 ), update bracket
||xn+1 x ||
lim =C
n ||xn x ||r
1 Choose a direction
I Nave approach: use basis or steepest descent directions
F Very inefficient in worse case
I Try new directions, keep good ones: Powells method or conjugate
gradients
I Use Newton or quasi-Newton direction
F Generally fastest method
2 Do univariate minimization along that direction, this step is called a
line search
I Exact: find the minimum along the direction
I Approximate: just find a point that is enough of an improvement
3 Choose a different direction and repeat
approximate line-search
FNeeds gradient, will compute finite difference approximation if gradient
not supplied
F Hessian approximation underdetermined by f 0 (xn ) and f 0 (xn1 ) in
dimension > 1
F Build Hessian approximation with recurrence relation:
Idea:
1 Evaluate function on a set of points
2 Use current points and function values to generate candidate new
points
3 Replace points in the set with new points that have lower function
values
4 Repeat until set collapses to a single poitn
Examples: grid search, Nelder-Mead simplex, pattern search, genetic
algorithms, simulated annealing
Pros: simple, robust
Cons: inefficient an interpolation based method will usually do
better
fminsearch() uses it
N + 1 points in N dimensions form a polyhedron, move the
polyhedron by
1 Reflect worst point across the center, expand if theres an improvement
2 Shrink, e.g. toward best point, other variations possible
Animation Another Animation
Can find global optimum (but I do not know whether this has been
formally proven)
Begin with random population of points, {xn0 }, then
1 Compute fitness of each point f (xni )
2 Select more fit points as parents
3 Produce children by mutation xni+1 = xni + , crossover
xni+1 = xni + (1 )xmi , and elites, xni+1 = xni
4 Repeat until have not improved function for many iterations
In Matlab, [x fval] = ga(@f,nvars,options)
I Requires Genetic Algorithm and Direct Search Toolbox
I Many variations and options
F Options can affect whether converge to local or global optimum
F Read the documentation and/or use optimtool
Can find global optimum, and under certain conditions, which are
difficult to check, finds the global optimum with probability 1
Algorithm:
1 Random candidate point x = xi +
2 Accept x i+1 = x if f (x) < f (xi ) or
1
F simulated annealing: with probability 1+e (f (x)f (xi ))/
F threshold: if f (x) < f (xi ) + T
3 Lower the temperature, , and threshold, T
In Matlab, [x,fval] = simulannealbnd(@objfun,x0) and
[x,fval] = threshacceptbnd(@objfun,x0)
I Requires Genetic Algorithm and Direct Search Toolbox
I Many variations and options
F Options can affect whether converge to local or global optimum
F Read the documentation and/or use optimtool
1 function data=simulateBinChoice(dataIn,parm)
2 % ... comments and error checking omitted ...
3 data.x = randn(data.N,data.nX);
4 data.y = (data.x*parm.beta + epsilon > 0);
5 end
1 % gradient of l i
2 g i = (parm.dist.pdf(xb)*ones(1,length(b))).*data.x;
3 g i(data.y,:) = g i(data.y,:);
4 % change to gradient of loglike
5 grad = sum(g i./(l i *ones(1,length(b))),1)';
1 % calculate hessian
2 h i = zeros(length(xb),length(b),length(b));
3 for i=1:length(xb)
4 h i(i,:,:) = (parm.dist.dpdf(xb(i))* ...
5 data.x(i,:)'*data.x(i,:)) ...
6 ... % make hessian of loglike
7 / l i(i);
8 end
9 h i(data.y,:,:) = h i(data.y,:,:);
10 % make hessiane of loglike
11 hess = (sum(h i,1) g i'*(g i./(l i.2*ones(1,length(b)))))
Want:
Z b
F (x) = f (x)(x)dx
a
Approximate:
n
X
F (x) f (xi )wi
i
Example: Newton-Cotes
I basis = polynomials
I (x) = 1
I Resulting rule is exact for all polynomials of degree less than or equal
to n
Not built in
Many implementations available at the Mathworks file exchange
Z b
F (x) = f (x)(x)dx
a
1 clear;
2 % some integration experiments
3
4 % Example 1: E[p(x)], xN(0,1), p(x) polynomial
5 degree = 10;
6 p = rand(1,degree+1); % make some polynomial
7 fprintf('adaptive quad, tol %.1g = %.10g\n', ...
8 1e10, quad(@(x) polyval(p,x).*normpdf(x), ...
9 30,30,1e10));
1 If you have written any code that involves optimization or integration, try modifying it to use a different method.
2 Modify the dynamic programming code from lecture 1 to allow for a continuous distribution for income. If you are
clever, will be able to evaluate E v (x 0 , y 0 ) exactly, even if Ev (x 0 , y 0 ) does not have an analytic solution.
3 We know that value function iteration converges linearly. Gauss-Newton and other quadratic approximation based
method converge at faster rates. Change the dynamic programming code from lecture 1 to use one of these methods
instead of value function iteration.
4 Change the binary choice model into a multinomial choice model. Allow for correlation between the shocks. Try to
preserve the generality of the binary model, but feel free to limit the choice of distribution if it helps.
p1 (x) 0
p0 (x) 1
!
hxpj |pj i hpj |pj i
pj+1 (x) = x x pj1 (x)
hpj |pj i hpj1 |pj1 i
Recall that the roots of the n degree polynomial will be the integration points. If you have the roots, {xj }nj=1 . Then
the weights are given by
hpn1 |pn1 i
wj =
pn1 (xj )pn0 (xj )