Regression and Fitting, Part 2: Polynomial Regression#
A polynomial regression fits \(y\) as a polynomial in \(x\). It is linear in the coefficients, so everything in Regression and Fitting, Part 1: Linear Regression applies, and it is the workhorse for fitting a smooth shape: a volatility smile, a payoff profile, and the continuation value in the least-squares Monte Carlo of Advanced Methods, Part 1: Least-Squares Monte Carlo. The choices are the degree and the basis. Too low a degree leaves a systematic error, too high fits the noise, and between the data the polynomial can do almost anything. This page sets out the method, shows how to choose the degree by cross-validation, shows what the choice of the basis does to the numerics, and tests a regression for a continuation value against the exact answer. It is the second of four pages on regression and fitting.
The model#
The fit is
with a basis \(\phi_k\) of degree up to \(d\), and the coefficients \(c\) are found by least squares on the design matrix \(A_{ik} = \phi_k(x_i)\). In exact arithmetic the choice of basis does not change the fitted function. In floating point it matters a great deal. The monomials \(1, x, x^2, \dots\) are nearly collinear on an interval, and the condition number of the design matrix grows exponentially with \(d\). Orthogonal polynomials, such as the Chebyshev polynomials \(T_k(u) = \cos(k \arccos u)\) on \(u \in [-1, 1]\), with \(x\) mapped to \(u\), are far better conditioned, and the Legendre, Laguerre and Hermite polynomials do the same for other weights and domains.
Choosing the degree#
The in-sample error always falls as the degree rises, and cannot be used to choose it. Two things happen as the degree grows. The bias, the systematic difference between the true function and the best polynomial of that degree, falls. The variance, the sensitivity of the fit to the noise in this sample, rises. The error on new data is smallest in between.
Leave-one-out cross-validation measures it. Each point is left out in turn, the polynomial is fitted to the rest, and the error at the left-out point is recorded. For least squares it needs no refitting. With the residual \(e_i\) and the diagonal element \(h_{ii}\) of the hat matrix \(H = A (A^\top A)^{-1} A^\top\), the leave-one-out error is
The degree with the smallest value is a good choice. Information criteria, such as the Akaike and Bayesian criteria, penalise the number of coefficients in a similar way.
Extrapolation#
A polynomial that fits well between the data does not predict well outside them. Outside the range the highest power dominates and the value grows without limit. The same is true of the cubic continuations of Interpolation, Part 2: Extrapolation.
Regression for a continuation value#
In a Monte Carlo for an option with early exercise, the value of holding the option at an exercise date is the conditional expectation of the later cash flows given the state, \(E[\,V_{t'} \mid S_t = s\,]\). It is unknown, and it is estimated by regressing the discounted later cash flows of the paths on a polynomial in \(S_t\). The cash flows around their conditional mean are noisy, so the R squared of this regression is low even when the fit is excellent. The test of the fit is the difference from the conditional expectation, which is known for the benchmark put in closed form.
Worked example#
The example generates a smile from a known SVI total variance, adds 0.2 volatility points of noise to 15 quotes, and fits Chebyshev polynomials of degree 1 to 12. For each degree it reports the in-sample error, the leave-one-out error, the error against the true smile and the extrapolated volatility at log-moneyness \(\pm 0.6\). It then compares the condition number of the monomial and the Chebyshev design matrices, and finally regresses the discounted payoff of the benchmark put at 0.5 years on a polynomial in the stock price, for 1,000, 10,000 and 100,000 paths.
Python code
"""Polynomial regression: choice of degree for a volatility smile, conditioning of the basis, and a regression for the continuation value."""
import numpy as np
from numpy.polynomial import chebyshev as C
from common import K, R, S0, SIGMA, bs_price
def svi_vol(k, a=0.015, b=0.12, rho=-0.4, m=0.0, s=0.15):
"""The true smile: a one-year implied volatility from the SVI total variance."""
return np.sqrt(a + b * (rho * (k - m) + np.sqrt((k - m) ** 2 + s**2)))
def design(x, degree, lo, hi, basis="chebyshev"):
u = (2 * x - (lo + hi)) / (hi - lo)
return C.chebvander(u, degree) if basis == "chebyshev" else np.vander(x, degree + 1, increasing=True)
if __name__ == "__main__":
rng = np.random.default_rng(5)
strikes = np.linspace(-0.3, 0.3, 15)
quotes = svi_vol(strikes) + rng.normal(0, 0.002, strikes.size)
fine = np.linspace(-0.3, 0.3, 601)
print("1. A smile of 15 quotes with 0.2 volatility points of noise, fitted by polynomials in log-moneyness")
print(" Volatilities are in percent. The true vol at k = +/-0.6 is "
f"{svi_vol(-0.6) * 100:.2f} and {svi_vol(0.6) * 100:.2f}")
print(f" {'Degree':>6} {'fit RMSE':>9} {'leave-one-out':>14} {'error vs truth':>15} {'vol at k=-0.6':>14} {'vol at k=+0.6':>14}")
for degree in (1, 2, 3, 4, 6, 8, 10, 12):
a = design(strikes, degree, -0.3, 0.3)
coef, *_ = np.linalg.lstsq(a, quotes, rcond=None)
resid = quotes - a @ coef
hat = np.einsum("ij,ji->i", a, np.linalg.pinv(a))
loo = np.sqrt(np.mean((resid / (1 - hat)) ** 2)) if degree < 14 else np.nan
truth = np.sqrt(np.mean((design(fine, degree, -0.3, 0.3) @ coef - svi_vol(fine)) ** 2))
wing = [(design(np.array([k]), degree, -0.3, 0.3) @ coef)[0] * 100 for k in (-0.6, 0.6)]
print(f" {degree:6d} {np.sqrt(np.mean(resid**2)) * 100:9.3f} {loo * 100:14.3f} {truth * 100:15.3f} {wing[0]:14.2f} {wing[1]:14.2f}")
print("\n2. Condition number of the design matrix on the 15 strikes: monomials against Chebyshev polynomials")
print(f" {'Degree':>6} {'monomials 1, k, k^2, ...':>26} {'Chebyshev':>12}")
for degree in (2, 4, 6, 8, 10, 12):
print(f" {degree:6d} {np.linalg.cond(design(strikes, degree, -0.3, 0.3, 'monomial')):26.2e} {np.linalg.cond(design(strikes, degree, -0.3, 0.3)):12.2e}")
print("\n3. Continuation value of a put at t = 0.5: regress the discounted payoff on a polynomial in the stock price")
print(" RMSE is the root-mean-square difference between the fitted and the exact (Black-Scholes) value, in price units")
print(f" {'Paths':>8} {'Degree':>6} {'R squared':>10} {'RMSE':>8}")
for n in (1_000, 10_000, 100_000):
z = np.random.default_rng(9).standard_normal((2, n))
s_half = S0 * np.exp((R - 0.5 * SIGMA**2) * 0.5 + SIGMA * np.sqrt(0.5) * z[0])
s_end = s_half * np.exp((R - 0.5 * SIGMA**2) * 0.5 + SIGMA * np.sqrt(0.5) * z[1])
target = np.exp(-R * 0.5) * np.maximum(K - s_end, 0)
exact = bs_price(s=s_half, t=0.5, call=False)
lo, hi = s_half.min(), s_half.max()
for degree in (1, 2, 3, 5, 8):
a = design(s_half, degree, lo, hi)
coef, *_ = np.linalg.lstsq(a, target, rcond=None)
fit = a @ coef
r2 = 1 - np.sum((target - fit) ** 2) / np.sum((target - target.mean()) ** 2)
print(f" {n:8d} {degree:6d} {r2:10.3f} {np.sqrt(np.mean((fit - exact) ** 2)):8.4f}")
The output is:
1. A smile of 15 quotes with 0.2 volatility points of noise, fitted by polynomials in log-moneyness
Volatilities are in percent. The true vol at k = +/-0.6 is 34.35 and 24.58
Degree fit RMSE leave-one-out error vs truth vol at k=-0.6 vol at k=+0.6
1 1.725 2.067 1.629 26.87 14.05
2 0.340 0.452 0.274 44.96 32.14
3 0.292 0.494 0.243 38.53 38.56
4 0.186 0.321 0.077 9.09 9.12
6 0.171 0.992 0.074 143.71 124.76
8 0.044 0.482 0.204 -1992.83 -3836.67
10 0.023 1.560 0.200 19434.43 19888.52
12 0.021 30.024 0.214 -49000.41 -104631.89
2. Condition number of the design matrix on the 15 strikes: monomials against Chebyshev polynomials
Degree monomials 1, k, k^2, ... Chebyshev
2 3.29e+01 1.70e+00
4 1.32e+03 1.83e+00
6 5.91e+04 1.85e+00
8 3.06e+06 2.26e+00
10 2.00e+08 4.12e+00
12 1.90e+10 1.65e+01
3. Continuation value of a put at t = 0.5: regress the discounted payoff on a polynomial in the stock price
RMSE is the root-mean-square difference between the fitted and the exact (Black-Scholes) value, in price units
Paths Degree R squared RMSE
1000 1 0.359 2.7970
1000 2 0.459 0.7317
1000 3 0.465 0.6062
1000 5 0.465 0.5587
1000 8 0.467 0.6663
10000 1 0.334 2.7806
10000 2 0.412 0.7595
10000 3 0.417 0.2803
10000 5 0.418 0.0877
10000 8 0.418 0.0690
100000 1 0.337 2.8070
100000 2 0.420 0.7112
100000 3 0.425 0.2984
100000 5 0.426 0.0889
100000 8 0.426 0.0612
Reading the output:
The in-sample error cannot choose the degree. It falls at every step, from 1.73 volatility points at degree 1 to 0.021 at degree 12, below the 0.2 of noise, so the high-degree fits are fitting the noise. The leave-one-out error is smallest at degree 4, 0.32, and rises to 0.99 at degree 6 and 30.0 at degree 12. The error against the true smile agrees: 0.077 at degree 4, and about 0.2 from degree 8, the size of the noise.
Too low a degree leaves a systematic error. A line misses the smile by 1.63 volatility points and a quadratic by 0.27, an error that more data would not remove.
Outside the data the polynomial is useless. The true volatilities at log-moneyness -0.6 and +0.6 are 34.35 and 24.58. Degree 4, the best inside the data, gives 9.1 at both ends, degree 6 gives 144 and 125, and degree 8 gives -1,993 and -3,837. Fit the smile with a polynomial and use it only between the strikes that were fitted, and extrapolate with the rules of Interpolation, Part 2: Extrapolation.
The basis decides whether the numerics work. The monomial design matrix has a condition number of 3.1e6 at degree 8 and 1.9e10 at degree 12, against 2.3 and 16.5 for the Chebyshev basis. The fitted functions are the same in exact arithmetic. With monomials, 1e10 means that ten digits of the coefficients are lost.
The R squared of a continuation regression is a poor guide. It is 0.46 at degree 3 and 0.47 at degree 8 for 1,000 paths, and 0.42 to 0.43 for the larger samples, so it hardly changes after degree 2. The error against the exact value falls from 2.8 at degree 1, to 0.30 at degree 3, to 0.09 at degree 5 and 0.06 at degree 8, for 100,000 paths. The payoff is noisy around its conditional mean, and most of the variance is noise that no regression can explain.
More terms need more paths. With 1,000 paths the error is lowest at degree 5, 0.559, and rises to 0.666 at degree 8. With 10,000 and 100,000 paths it keeps falling, 0.069 and 0.061. A low degree is biased, and does not improve with the number of paths, 2.8 at every size for degree 1.
A valuation control view#
Ask how the degree was chosen: a degree that is picked by the in-sample fit is too high. Look for cross-validation or an out-of-sample test, and for the sensitivity of the price to the degree.
Never trust a polynomial outside the data: check that the fitted range covers the range where the model is used, and that the tail behaviour is controlled by something other than the polynomial.
Use a well-conditioned basis: orthogonal polynomials, with the variable scaled to the interval. A monomial fit of high degree can fail with no warning.
Test a continuation regression against a benchmark: the price of the option with the regression-based exercise rule is a lower bound for the true price, and a change in price with the degree and with the number of paths shows how converged it is. See Advanced Methods, Part 1: Least-Squares Monte Carlo.
Do not read the R squared as a pass mark: a low R squared is expected in a regression on noisy cash flows, and a high one is not a proof that the shape is right.
Next, Regression and Fitting, Part 3: Non-linear Regression covers models that are not linear in their parameters, where the fit needs an iterative method and can end at the wrong answer.