remez.qbk 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. [section:remez The Remez Method]
  2. The [@http://en.wikipedia.org/wiki/Remez_algorithm Remez algorithm]
  3. is a methodology for locating the minimax rational approximation
  4. to a function. This short article gives a brief overview of the method, but
  5. it should not be regarded as a thorough theoretical treatment, for that you
  6. should consult your favorite textbook.
  7. Imagine that you want to approximate some function /f(x)/ by way of a rational
  8. function /R(x)/, where /R(x)/ may be either a polynomial /P(x)/ or a ratio of two
  9. polynomials /P(x)/Q(x)/ (a rational function). Initially we'll concentrate on the
  10. polynomial case, as it's by far the easier to deal with, later we'll extend
  11. to the full rational function case.
  12. We want to find the "best" rational approximation, where
  13. "best" is defined to be the approximation that has the least deviation
  14. from /f(x)/. We can measure the deviation by way of an error function:
  15. [expression E[sub abs](x) = f(x) - R(x)]
  16. which is expressed in terms of absolute error, but we can equally use
  17. relative error:
  18. [expression E[sub rel](x) = (f(x) - R(x)) / |f(x)|]
  19. And indeed in general we can scale the error function in any way we want, it
  20. makes no difference to the maths, although the two forms above cover almost
  21. every practical case that you're likely to encounter.
  22. The minimax rational function /R(x)/ is then defined to be the function that
  23. yields the smallest maximal value of the error function. Chebyshev showed
  24. that there is a unique minimax solution for /R(x)/ that has the following
  25. properties:
  26. * If /R(x)/ is a polynomial of degree /N/, then there are /N+2/ unknowns:
  27. the /N+1/ coefficients of the polynomial, and maximal value of the error
  28. function.
  29. * The error function has /N+1/ roots, and /N+2/ extrema (minima and maxima).
  30. * The extrema alternate in sign, and all have the same magnitude.
  31. That means that if we know the location of the extrema of the error function
  32. then we can write /N+2/ simultaneous equations:
  33. [expression R(x[sub i]) + (-1)[super i]E = f(x[sub i])]
  34. where /E/ is the maximal error term, and ['x[sub i]] are the abscissa values of the
  35. /N+2/ extrema of the error function. It is then trivial to solve the simultaneous
  36. equations to obtain the polynomial coefficients and the error term.
  37. ['Unfortunately we don't know where the extrema of the error function are located!]
  38. [h4 The Remez Method]
  39. The Remez method is an iterative technique which, given a broad range of
  40. assumptions, will converge on the extrema of the error function, and therefore
  41. the minimax solution.
  42. In the following discussion we'll use a concrete example to illustrate
  43. the Remez method: an approximation to the function e[super x] over
  44. the range \[-1, 1\].
  45. Before we can begin the Remez method, we must obtain an initial value
  46. for the location of the extrema of the error function. We could "guess"
  47. these, but a much closer first approximation can be obtained by first
  48. constructing an interpolated polynomial approximation to /f(x)/.
  49. In order to obtain the /N+1/ coefficients of the interpolated polynomial
  50. we need N+1 points /(x[sub 0][hellip]x[sub N]): with our interpolated form
  51. passing through each of those points
  52. that yields /N+1/ simultaneous equations:
  53. [expression f(x[sub i]) = P(x[sub i]) = c[sub 0] + c[sub 1]x[sub i] [hellip] + c[sub N]x[sub i][super N]]
  54. Which can be solved for the coefficients ['c[sub 0] [hellip]c[sub N]] in /P(x)/.
  55. Obviously this is not a minimax solution, indeed our only guarantee is that /f(x)/ and
  56. /P(x)/ touch at /N+1/ locations, away from those points the error may be arbitrarily
  57. large. However, we would clearly like this initial approximation to be as close to
  58. /f(x)/ as possible, and it turns out that using the zeros of an orthogonal polynomial
  59. as the initial interpolation points is a good choice. In our example we'll use the
  60. zeros of a Chebyshev polynomial as these are particularly easy to calculate,
  61. interpolating for a polynomial of degree 4, and measuring /relative error/
  62. we get the following error function:
  63. [$../graphs/remez-2.png]
  64. Which has a peak relative error of 1.2x10[super -3].
  65. While this is a pretty good approximation already, judging by the
  66. shape of the error function we can clearly do better. Before starting
  67. on the Remez method propper, we have one more step to perform: locate
  68. all the extrema of the error function, and store
  69. these locations as our initial ['Chebyshev control points].
  70. [note
  71. In the simple case of a polynomial approximation, by interpolating through
  72. the roots of a Chebyshev polynomial we have in fact created a ['Chebyshev
  73. approximation] to the function: in terms of /absolute error/
  74. this is the best a priori choice for the interpolated form we can
  75. achieve, and typically is very close to the minimax solution.
  76. However, if we want to optimise for /relative error/, or if the approximation
  77. is a rational function, then the initial Chebyshev solution can be quite far
  78. from the ideal minimax solution.
  79. A more technical discussion of the theory involved can be found in this
  80. [@http://math.fullerton.edu/mathews/n2003/ChebyshevPolyMod.html online course].]
  81. [h4 Remez Step 1]
  82. The first step in the Remez method, given our current set of
  83. /N+2/ Chebyshev control points ['x[sub i]], is to solve the /N+2/ simultaneous
  84. equations:
  85. [expression P(x[sub i]) + (-1)[super i]E = f(x[sub i])]
  86. To obtain the error term /E/, and the coefficients of the polynomial /P(x)/.
  87. This gives us a new approximation to /f(x)/ that has the same error /E/ at
  88. each of the control points, and whose error function ['alternates in sign]
  89. at the control points. This is still not necessarily the minimax
  90. solution though: since the control points may not be at the extrema of the error
  91. function. After this first step here's what our approximation's error
  92. function looks like:
  93. [$../graphs/remez-3.png]
  94. Clearly this is still not the minimax solution since the control points
  95. are not located at the extrema, but the maximum relative error has now
  96. dropped to 5.6x10[super -4].
  97. [h4 Remez Step 2]
  98. The second step is to locate the extrema of the new approximation, which we do
  99. in two stages: first, since the error function changes sign at each
  100. control point, we must have N+1 roots of the error function located between
  101. each pair of N+2 control points. Once these roots are found by standard root finding
  102. techniques, we know that N extrema are bracketed between each pair of
  103. roots, plus two more between the endpoints of the range and the first and last roots.
  104. The N+2 extrema can then be found using standard function minimisation techniques.
  105. We now have a choice: multi-point exchange, or single point exchange.
  106. In single point exchange, we move the control point nearest to the largest extrema to
  107. the absissa value of the extrema.
  108. In multi-point exchange we swap all the current control points, for the locations
  109. of the extrema.
  110. In our example we perform multi-point exchange.
  111. [h4 Iteration]
  112. The Remez method then performs steps 1 and 2 above iteratively until the control
  113. points are located at the extrema of the error function: this is then
  114. the minimax solution.
  115. For our current example, two more iterations converges on a minimax
  116. solution with a peak relative error of
  117. 5x10[super -4] and an error function that looks like:
  118. [$../graphs/remez-4.png]
  119. [h4 Rational Approximations]
  120. If we wish to extend the Remez method to a rational approximation of the form
  121. [expression f(x) = R(x) = P(x) / Q(x)]
  122. where /P(x)/ and /Q(x)/ are polynomials, then we proceed as before, except that now
  123. we have /N+M+2/ unknowns if /P(x)/ is of order /N/ and /Q(x)/ is of order /M/ This assumes
  124. that /Q(x)/ is normalised so that its leading coefficient is 1, giving
  125. /N+M+1/ polynomial coefficients in total, plus the error term /E/.
  126. The simultaneous equations to be solved are now:
  127. [expression P(x[sub i]) / Q(x[sub i]) + (-1)[super i]E = f(x[sub i])]
  128. Evaluated at the /N+M+2/ control points ['x[sub i]].
  129. Unfortunately these equations are non-linear in the error term /E/: we can only
  130. solve them if we know /E/, and yet /E/ is one of the unknowns!
  131. The method usually adopted to solve these equations is an iterative one: we guess the
  132. value of /E/, solve the equations to obtain a new value for /E/ (as well as the polynomial
  133. coefficients), then use the new value of /E/ as the next guess. The method is
  134. repeated until /E/ converges on a stable value.
  135. These complications extend the running time required for the development
  136. of rational approximations quite considerably. It is often desirable
  137. to obtain a rational rather than polynomial approximation none the less:
  138. rational approximations will often match more difficult to approximate
  139. functions, to greater accuracy, and with greater efficiency, than their
  140. polynomial alternatives. For example, if we takes our previous example
  141. of an approximation to e[super x], we obtained 5x10[super -4] accuracy
  142. with an order 4 polynomial. If we move two of the unknowns into the denominator
  143. to give a pair of order 2 polynomials, and re-minimise, then the peak relative error drops
  144. to 8.7x10[super -5]. That's a 5 fold increase in accuracy, for the same number
  145. of terms overall.
  146. [h4:remez_practical Practical Considerations]
  147. Most treatises on approximation theory stop at this point. However, from
  148. a practical point of view, most of the work involves finding the right
  149. approximating form, and then persuading the Remez method to converge
  150. on a solution.
  151. So far we have used a direct approximation:
  152. [expression f(x) = R(x)]
  153. But this will converge to a useful approximation only if /f(x)/ is smooth. In
  154. addition round-off errors when evaluating the rational form mean that this
  155. will never get closer than within a few epsilon of machine precision.
  156. Therefore this form of direct approximation is often reserved for situations
  157. where we want efficiency, rather than accuracy.
  158. The first step in improving the situation is generally to split /f(x)/ into
  159. a dominant part that we can compute accurately by another method, and a
  160. slowly changing remainder which can be approximated by a rational approximation.
  161. We might be tempted to write:
  162. [expression f(x) = g(x) + R(x)]
  163. where /g(x)/ is the dominant part of /f(x)/, but if ['f(x)/g(x)] is approximately
  164. constant over the interval of interest then:
  165. [expression f(x) = g(x)(c + R(x))]
  166. Will yield a much better solution: here /c/ is a constant that is the approximate
  167. value of ['f(x)/g(x)] and /R(x)/ is typically tiny compared to /c/. In this situation
  168. if /R(x)/ is optimised for absolute error, then as long as its error is small compared
  169. to the constant /c/, that error will effectively get wiped out when /R(x)/ is added to
  170. /c/.
  171. The difficult part is obviously finding the right /g(x)/ to extract from your
  172. function: often the asymptotic behaviour of the function will give a clue, so
  173. for example the function __erfc becomes proportional to
  174. ['e[super -x[super 2]]\/x] as /x/ becomes large. Therefore using:
  175. [expression erfc(z) = (C + R(x)) e[super -x[super 2]]/x]
  176. as the approximating form seems like an obvious thing to try, and does indeed
  177. yield a useful approximation.
  178. However, the difficulty then becomes one of converging the minimax solution.
  179. Unfortunately, it is known that for some functions the Remez method can lead
  180. to divergent behaviour, even when the initial starting approximation is quite good.
  181. Furthermore, it is not uncommon for the solution obtained in the first Remez step
  182. above to be a bad one: the equations to be solved are generally "stiff", often
  183. very close to being singular, and assuming a solution is found at all, round-off
  184. errors and a rapidly changing error function, can lead to a situation where the
  185. error function does not in fact change sign at each control point as required.
  186. If this occurs, it is fatal to the Remez method. It is also possible to
  187. obtain solutions that are perfectly valid mathematically, but which are
  188. quite useless computationally: either because there is an unavoidable amount
  189. of roundoff error in the computation of the rational function, or because
  190. the denominator has one or more roots over the interval of the approximation.
  191. In the latter case while the approximation may have the correct limiting value at
  192. the roots, the approximation is nonetheless useless.
  193. Assuming that the approximation does not have any fatal errors, and that the only
  194. issue is converging adequately on the minimax solution, the aim is to
  195. get as close as possible to the minimax solution before beginning the Remez method.
  196. Using the zeros of a Chebyshev polynomial for the initial interpolation is a
  197. good start, but may not be ideal when dealing with relative errors and\/or
  198. rational (rather than polynomial) approximations. One approach is to skew
  199. the initial interpolation points to one end: for example if we raise the
  200. roots of the Chebyshev polynomial to a positive power greater than 1
  201. then the roots will be skewed towards the middle of the \[-1,1\] interval,
  202. while a positive power less than one
  203. will skew them towards either end. More usefully, if we initially rescale the
  204. points over \[0,1\] and then raise to a positive power, we can skew them to the left
  205. or right. Returning to our example of e[super x] over \[-1,1\], the initial
  206. interpolated form was some way from the minimax solution:
  207. [$../graphs/remez-2.png]
  208. However, if we first skew the interpolation points to the left (rescale them
  209. to \[0, 1\], raise to the power 1.3, and then rescale back to \[-1,1\]) we
  210. reduce the error from 1.3x10[super -3] to 6x10[super -4]:
  211. [$../graphs/remez-5.png]
  212. It's clearly still not ideal, but it is only a few percent away from
  213. our desired minimax solution (5x10[super -4]).
  214. [h4 Remez Method Checklist]
  215. The following lists some of the things to check if the Remez method goes wrong,
  216. it is by no means an exhaustive list, but is provided in the hopes that it will
  217. prove useful.
  218. * Is the function smooth enough? Can it be better separated into
  219. a rapidly changing part, and an asymptotic part?
  220. * Does the function being approximated have any "blips" in it? Check
  221. for problems as the function changes computation method, or
  222. if a root, or an infinity has been divided out. The telltale
  223. sign is if there is a narrow region where the Remez method will
  224. not converge.
  225. * Check you have enough accuracy in your calculations: remember that
  226. the Remez method works on the difference between the approximation
  227. and the function being approximated: so you must have more digits of
  228. precision available than the precision of the approximation
  229. being constructed. So for example at double precision, you
  230. shouldn't expect to be able to get better than a float precision
  231. approximation.
  232. * Try skewing the initial interpolated approximation to minimise the
  233. error before you begin the Remez steps.
  234. * If the approximation won't converge or is ill-conditioned from one starting
  235. location, try starting from a different location.
  236. * If a rational function won't converge, one can minimise a polynomial
  237. (which presents no problems), then rotate one term from the numerator to
  238. the denominator and minimise again. In theory one can continue moving
  239. terms one at a time from numerator to denominator, and then re-minimising,
  240. retaining the last set of control points at each stage.
  241. * Try using a smaller interval. It may also be possible to optimise over
  242. one (small) interval, rescale the control points over a larger interval,
  243. and then re-minimise.
  244. * Keep absissa values small: use a change of variable to keep the abscissa
  245. over, say \[0, b\], for some smallish value /b/.
  246. [h4 References]
  247. The original references for the Remez Method and its extension
  248. to rational functions are unfortunately in Russian:
  249. Remez, E.Ya., ['Fundamentals of numerical methods for Chebyshev approximations],
  250. "Naukova Dumka", Kiev, 1969.
  251. Remez, E.Ya., Gavrilyuk, V.T., ['Computer development of certain approaches
  252. to the approximate construction of solutions of Chebyshev problems
  253. nonlinearly depending on parameters], Ukr. Mat. Zh. 12 (1960), 324-338.
  254. Gavrilyuk, V.T., ['Generalization of the first polynomial algorithm of
  255. E.Ya.Remez for the problem of constructing rational-fractional
  256. Chebyshev approximations], Ukr. Mat. Zh. 16 (1961), 575-585.
  257. Some English language sources include:
  258. Fraser, W., Hart, J.F., ['On the computation of rational approximations
  259. to continuous functions], Comm. of the ACM 5 (1962), 401-403, 414.
  260. Ralston, A., ['Rational Chebyshev approximation by Remes' algorithms],
  261. Numer.Math. 7 (1965), no. 4, 322-330.
  262. A. Ralston, ['Rational Chebyshev approximation, Mathematical
  263. Methods for Digital Computers v. 2] (Ralston A., Wilf H., eds.),
  264. Wiley, New York, 1967, pp. 264-284.
  265. Hart, J.F. e.a., ['Computer approximations], Wiley, New York a.o., 1968.
  266. Cody, W.J., Fraser, W., Hart, J.F., ['Rational Chebyshev approximation
  267. using linear equations], Numer.Math. 12 (1968), 242-251.
  268. Cody, W.J., ['A survey of practical rational and polynomial
  269. approximation of functions], SIAM Review 12 (1970), no. 3, 400-423.
  270. Barrar, R.B., Loeb, H.J., ['On the Remez algorithm for non-linear
  271. families], Numer.Math. 15 (1970), 382-391.
  272. Dunham, Ch.B., ['Convergence of the Fraser-Hart algorithm for rational
  273. Chebyshev approximation], Math. Comp. 29 (1975), no. 132, 1078-1082.
  274. G. L. Litvinov, ['Approximate construction of rational
  275. approximations and the effect of error autocorrection],
  276. Russian Journal of Mathematical Physics, vol.1, No. 3, 1994.
  277. [endsect] [/section:remez The Remez Method]
  278. [/
  279. Copyright 2006 John Maddock and Paul A. Bristow.
  280. Distributed under the Boost Software License, Version 1.0.
  281. (See accompanying file LICENSE_1_0.txt or copy at
  282. http://www.boost.org/LICENSE_1_0.txt).
  283. ]