Purpose: short, clear code for
- fitting quadratics to data, aka quadratic regression
- iterating quad fits to a local minimum of a noisy function.
This code is for students of programming and optimization to read and try out, not for professionals.
Keywords: optimization, quadratic, parabola, python, numpy, Iteratively reweighted least squares, IRLS weighted regression, derivative-free optimization, DFO
This shows qmin
bouncing around on a parabolic hill + ripple -- no surprise:
-
lsq.py
is just a thin wrapper for np.linalg.lstsq -
quad.py
:quad_fit( X, z )
adds cross-product columns toX
, thenlstsq
quad_min( X, z )
fits a quadraticz ~ x'Ax + b x + c
and solves forxmin = - b / 2A
-- like a parabola in 1d -
qmin.py
:
Start with pointsX, y
:X_i
n-dim vectors,y_i = func( X_i )
1d .
Fit a quadraticx'Ax + b x + c
to the points by least squares, up-weighting the best ones so far
Add the new pointxmin = - b / 2A, ymin = func( xmin )
toX, y
.
Iterate these two steps. -
test-qmin.py
: a trivial 2d test to plot.
(Instead of class
, I use records (namedtuple
in python) such as
Lsqfit = namedtuple( "Lsqfit", "coef X y weight fit res rank sing" )
-- a matter of taste.)
A quadratic approximation to your function landscape
may, of course, be pretty good, or may be terrible.
The most common problem in my experience is simply not looking at the real and fitted landscapes.
Another is scaling; see the excellent scipy.optimize.least_squares on this.
A third is swamping: with 10 variables,
55 pure quadratic terms a00 x0^2 + a01 x0 x1 + ... + a99 x9^2
can swamp the 11 linear b0 x0 + ... + b9 x9 + c
.
A simple way to damp this effect is to
fit linear, fit quadratic, and average the two.
There are dozens of methods for optimizing noisy, expensive functions without gradients. Why ? For one thing, there are zillions of different problems in the universe; for another, it's easy to invent a new method that works on one or two problems, and publish it. Thus we have more papers than reproducible test cases, beyond Rosenbrock. Nonetheless, here are two starting points, each with a dozen or so algorithms:
NLopt: solid c, python interface. Of these, BOBYQA (Powell, 2009) "constructs a quadratic approximation of the objective".
A basic suggestion for running any optimizer:
- invest in test scaffolding, and in plotting: how can you see what the optimizer is doing ?
cheers
-- denis
Last change: 15 March 2017