Debian Codegraph @

src/dr/math/UnivariateMinimum.java

raw symbols SCIP
1 /*
2 * UnivariateMinimum.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.math;
27
28
29 /**
30 * minimization of a real-valued function of one variable
31 * without using derivatives.
32 *
33 * <p>algorithm: Brent's golden section method
34 * (Richard P. Brent. 1973. Algorithms for finding zeros and extrema
35 * of functions without calculating derivatives. Prentice-Hall.)
36 *
37 * @version $Id: UnivariateMinimum.java,v 1.3 2005/05/24 20:26:01 rambaut Exp $
38 *
39 * @author Korbinian Strimmer
40 */
41 public class UnivariateMinimum
42 {
43 //
44 // Public stuff
45 //
46
47 /** last minimum */
48 public double minx;
49
50 /** function value at minimum */
51 public double fminx;
52
53 /** curvature at minimum */
54 public double f2minx;
55
56 /** total number of function evaluations neccessary */
57 public int numFun;
58
59 /**
60 * maximum number of function evaluations
61 * (default 0 indicates no limit on calls)
62 */
63 public int maxFun = 0;
64
65 /**
66 * Find minimum
67 * (first estimate given)
68 *
69 * @param x first estimate
70 * @param f function
71 *
72 * @return position of minimum
73 */
74 public double findMinimum(double x, UnivariateFunction f)
75 {
76 double tol = MachineAccuracy.EPSILON;
77
78 return optimize(x, f, tol);
79 }
80
81 /**
82 * Find minimum
83 * (first estimate given, desired number of fractional digits specified)
84 *
85 * @param x first estimate
86 * @param f function
87 * @param fracDigits desired fractional digits
88 *
89 * @return position of minimum
90 */
91 public double findMinimum(double x, UnivariateFunction f, int fracDigits)
92 {
93 double tol = Math.pow(10, -1-fracDigits);
94
95 final double optx = optimize(x, f, tol);
96
97 //return trim(optx, fracDigits);
98 return optx;
99 }
100
101 /**
102 * Find minimum
103 * (no first estimate given)
104 *
105 * @param f function
106 *
107 * @return position of minimum
108 */
109 public double findMinimum(UnivariateFunction f)
110 {
111 double tol = MachineAccuracy.EPSILON;
112
113 return optimize(f, tol);
114 }
115
116 /**
117 * Find minimum
118 * (no first estimate given, desired number of fractional digits specified)
119 *
120 * @param f function
121 * @param fracDigits desired fractional digits
122 *
123 * @return position of minimum
124 */
125 public double findMinimum(UnivariateFunction f, int fracDigits)
126 {
127 double tol = Math.pow(10, -1-fracDigits);
128
129 final double optx = optimize(f, tol);
130
131 //return trim(optx, fracDigits);
132 return optx;
133 }
134
135 /**
136 * The actual optimization routine (Brent's golden section method)
137 *
138 * @param f univariate function
139 * @param tol absolute tolerance of each parameter
140 * @param lowerBound the lower bound of input
141 * @param upperBound the upper bound of input
142 *
143 * @return position of minimum
144 */
145 public double optimize(UnivariateFunction f, double tol, double lowerBound, double upperBound)
146 {
147 numFun = 2;
148 return minin(lowerBound, upperBound, f.evaluate(lowerBound), f.evaluate(upperBound), f, tol);
149 }
150 /**
151 * The actual optimization routine (Brent's golden section method)
152 *
153 * @param f univariate function
154 * @param tol absolute tolerance of each parameter
155 *
156 * @return position of minimum
157 */
158 public double optimize(UnivariateFunction f, double tol)
159 {
160 return optimize(f,tol,f.getLowerBound(), f.getUpperBound());
161 }
162
163 /**
164 * The actual optimization routine (Brent's golden section method)
165 *
166 * @param x initial guess
167 * @param f univariate function
168 * @param tol absolute tolerance of each parameter
169 * @param lowerBound the lower bound of input
170 * @param upperBound the upper bound of input
171 *
172 * @return position of minimum
173 */
174 public double optimize(double x, UnivariateFunction f, double tol, double lowerBound, double upperBound)
175 {
176 double[] range = bracketize(lowerBound, x, upperBound, f);
177
178 return minin(range[0], range[1], range[2], range[3], f, tol);
179 }
180 /**
181 * The actual optimization routine (Brent's golden section method)
182 *
183 * @param x initial guess
184 * @param f univariate function
185 * @param tol absolute tolerance of each parameter
186 * @note bounded by the given bounds of the function f
187 *
188 * @return position of minimum
189 */
190 public double optimize(double x, UnivariateFunction f, double tol)
191 {
192 return optimize(x,f,tol,f.getLowerBound(),f.getUpperBound());
193 }
194 //
195 // Private stuff
196 //
197
198 private static final double C = (3.0- Math.sqrt(5.0))/2.0; // = 0.38197
199 private static final double GOLD = (Math.sqrt(5.0) + 1.0)/2.0; // = 1.61803
200 private static final double delta = 0.01; // Determines second trial point
201
202 // trim x to have a specified number of fractional digits
203 /*private double trim(double x, int fracDigits)
204 {
205 double m = Math.pow(10, fracDigits);
206
207 return Math.round(x*m)/m;
208 }*/
209
210 private double constrain(double x, boolean toMax, double min, double max)
211 {
212 if (toMax)
213 {
214 if (x > max)
215 {
216 return max;
217 }
218 else
219 {
220 return x;
221 }
222 }
223 else
224 {
225 if (x < min)
226 {
227 return min;
228 }
229 else
230 {
231 return x;
232 }
233 }
234 }
235
236 private double[] bracketize(double min, double a, double max, UnivariateFunction f)
237 {
238 if (min > max)
239 {
240 throw new IllegalArgumentException("Argument min (" + min +
241 ") larger than argument max (" + max + ")");
242 }
243
244 if (a < min)
245 {
246 a = min;
247 }
248 else if (a > max)
249 {
250 a = max;
251 }
252
253
254 if (a < min || a > max)
255 {
256 throw new IllegalArgumentException("Starting point not in given range ("
257 + min + ", " + a + ", " + max + ")");
258 }
259
260
261 // Get second point
262 double b;
263 if (a - min < max - a)
264 {
265 b = a + delta*(max - a);
266 }
267 else
268 {
269 b = a - delta*(a - min);
270 }
271
272 numFun = 0;
273
274 double fa = f.evaluate(a); numFun++;
275 double fb = f.evaluate(b); numFun++;
276
277 double tmp;
278 if (fb > fa)
279 {
280 tmp = a; a = b; b = tmp;
281 tmp = fa; fa = fb; fb = tmp;
282 }
283
284 // From here on we always have fa >= fb
285 // Our aims is to determine a new point c with fc >= fb
286
287 // Direction of search (towards min or towards max)
288 boolean searchToMax;
289 double ulim;
290 if (b > a)
291 {
292 searchToMax = true;
293 ulim = max;
294 }
295 else
296 {
297 searchToMax = false;
298 ulim = min;
299 }
300
301 // First guess: default magnification
302 double c = b + GOLD * (b - a);
303 c = constrain(c, searchToMax, min, max);
304 double fc = f.evaluate(c); numFun++;
305
306 while (fb > fc)
307 {
308 // Compute u as minimum of a parabola through a, b, c
309 double r = (b - a) * (fb - fc);
310 double q = (b - c) * (fb - fa);
311 if (q == r)
312 {
313 q += MachineAccuracy.EPSILON;
314 }
315 double u = b - ((b - c) * q - (b - a) * r) / 2.0 / (q - r);
316 u = constrain(u, searchToMax, min, max);
317 double fu = 0; // Don't evaluate now
318
319 boolean magnify = false;
320
321 // Check out all possibilities
322
323 // u is between b and c
324 if ((b - u) * (u - c) > 0)
325 {
326 fu = f.evaluate(u); numFun++;
327
328 // minimum between b and c
329 if (fu < fc)
330 {
331 a = b; b = u;
332 fa = fb; fb = fu;
333
334 break;
335 }
336 // minimum between a and u
337 else if (fu > fb)
338 {
339 c = u;
340 fc = fu;
341
342 break;
343 }
344
345 magnify = true;
346 }
347 // u is between c and limit
348 else if ((c - u) * (u - ulim) > 0)
349 {
350 fu = f.evaluate(u); numFun++;
351
352 // u is not a minimum
353 if (fu < fc)
354 {
355 b = c; c = u;
356 fb = fc; fc = fu;
357
358 magnify = true;
359 }
360 }
361 // u equals limit
362 else if (u == ulim)
363 {
364 fu = f.evaluate(u); numFun++;
365 }
366 // All other cases
367 else
368 {
369 magnify = true;
370 }
371
372 if (magnify)
373 {
374 // Next guess: default magnification
375 u = c + GOLD * (c - b);
376 u = constrain(u, searchToMax, min, max);
377 fu = f.evaluate(u); numFun++;
378 }
379
380 a = b; b = c; c = u;
381 fa = fb; fb = fc; fc = fu;
382 }
383
384 // Once we are here be have a minimum in [a, c]
385 double[] result = new double[4];
386 result[0] = a;
387 result[1] = c;
388 result[2] = fa;
389 result[3] = fc;
390 return result;
391 }
392
393
394 private double minin(double a, double b, double fa , double fb, UnivariateFunction f, double tol)
395 {
396 double z, d = 0, e, m, p, q, r, u, v, w, fu, fv, fw, fz, tmp;
397
398 if (tol <= 0)
399 {
400 throw new IllegalArgumentException("Nonpositive absolute tolerance tol");
401 }
402
403 if (a == b)
404 {
405 minx = a;
406 fminx = fa;
407
408 f2minx = NumericalDerivative.secondDerivative(f, minx);
409
410 return minx;
411
412 //throw new IllegalArgumentException("Borders of range not distinct");
413 }
414
415 if (b < a)
416 {
417 tmp = a; a = b; b = tmp;
418 tmp = fa; fa = fb; fb = tmp;
419 }
420
421 w = a; fw = fa;
422 z = b; fz = fb;
423 if (fz > fw) // Exchange z and w
424 {
425 v = z; z = w; w = v;
426 v = fz; fz = fw; fw = v;
427 }
428 v = w;
429 fv = fw;
430 e = 0.0;
431 while (maxFun == 0 || numFun <= maxFun)
432 {
433 m = (a + b)*0.5;
434 double tol_act = MachineAccuracy.SQRT_EPSILON + tol; // Absolute tolerance
435 //double tol_act = MachineAccuracy.SQRT_EPSILON*Math.abs(z) + tol/3; // Actual tolerance
436 double tol_act2 = 2.0*tol_act;
437 if (Math.abs(z-m) <= tol_act2-(b - a)*0.5)
438 {
439 break;
440 }
441 p = q = r = 0.0;
442 if (Math.abs(e) > tol_act)
443 {
444 r = (z-w)*(fz-fv);
445 q = (z-v)*(fz-fw);
446 p = (z-v)*q-(z-w)*r;
447 q = (q-r)*2.0;
448 if (q > 0.0)
449 {
450 p = -p;
451 }
452 else
453 {
454 q = -q;
455 }
456 r = e;
457 e = d;
458 }
459 if (Math.abs(p) < Math.abs(q*r*0.5) && p > (a-z)*q && p < (b-z)*q)
460 {
461 d = p/q;
462 u = z+d;
463 if (u-(a) < tol_act2 || (b)-u < tol_act2)
464 {
465 d = ((z < m) ? tol_act : -tol_act);
466 }
467 }
468 else
469 {
470 e = ((z < m) ? b : a) - z;
471 d = C*e;
472 }
473 u = z + ((Math.abs(d) >= tol_act) ? d : ((d > 0.0) ? tol_act : -tol_act));
474 fu = f.evaluate(u); numFun++;
475 if (fu <= fz)
476 {
477 if (u < z)
478 {
479 b = z;
480 }
481 else
482 {
483 a = z;
484 }
485 v = w;
486 fv = fw;
487 w = z;
488 fw = fz;
489 z = u;
490 fz = fu;
491 }
492 else
493 {
494 if (u < z)
495 {
496 a = u;
497 }
498 else
499 {
500 b = u;
501 }
502 if (fu <= fw)
503 {
504 v = w; fv = fw;
505 w = u; fw = fu;
506 }
507 else if (fu <= fv || v == w)
508 {
509 v = u;
510 fv = fu;
511 }
512 }
513 }
514 minx = z;
515 fminx = fz;
516
517 f2minx = NumericalDerivative.secondDerivative(f, minx);
518
519 return z;
520 }
521 }