Data fitting and optimization for machine learning · Lecture 24

Polynomial, Exponential and Trigonometric Fitting

Curved trends with the same least-squares machinery: polynomial regression, straightening growth and power laws with logarithms, fitting periodic data, and choosing a model that follows the trend without memorising the noise.

Md Mahbub E Noor · Assistant Professor
CSE-3107 Numerical Methods · B.Sc. 3rd Year 1st Semester
Department of CSE, University of Barishal

Why this lecture

The problem: how fast do the bacteria double?

A lab counts bacteria every hour. Each hour the count is multiplied by about 1.5. When does the culture double, and how many cells will there be after 8 hours?

  • A straight line misses the curve: residuals +74,−5,−55,−76,−36,+96+74, -5, -55, -76, -36, +96, and it predicts only 1063 cells at 8 h
  • The model is N=aebtN = ae^{bt}: bb is inside the exponential, not linear in the unknowns
  • Least squares from Lecture 23 needs a model linear in its coefficients
  • Idea: take logarithms, and the curve becomes a straight line
time tt (h)count NN
0100
1150
2230
3340
4510
5770
8?
Md Mahbub E Noor
Lecture 24: Polynomial, exponential and trigonometric fits
University of Barishal
2 / 23

Today's goals

  • Fit a polynomial of degree mm by least squares (normal equations of size m+1m + 1)
  • Linearise y=aebxy = ae^{bx} and y=axby = ax^{b} with logarithms and fit them
  • Fit a trigonometric model to periodic data
  • Recognise overfitting and choose the degree by test error
  • Know what linearisation changes, and when a direct nonlinear fit is needed
Last time

Least squares for y=a+bxy = a + bx; matrix form ATAc=ATyA^{T}A\mathbf{c} = A^{T}\mathbf{y}; conditioning (Lecture 23).


Today

Same normal equations with new columns: x2,x3,…x^2, x^3, \dots, cos⁡ωx\cos\omega x, sin⁡ωx\sin\omega x; and logarithms for growth laws.

Data fitting and optimization for machine learning · you are here
L23 · Least Squares and Linear Regression→L24 · Polynomial, Exponential and Trigonometric Fitting→L25 · Numerical Optimization for Machine Learning
Builds on: L23
Md Mahbub E Noor
Lecture 24: Polynomial, exponential and trigonometric fits
University of Barishal
3 / 23
Start here

The idea in one picture

count Ntime tln Ntime ta curve: N = a·eᵇᵗtake lna straight line!ln N = ln a + b·tslope = b, intercept = ln a
Bacteria counts: they grow faster and faster.The model is an exponential curve. A line fit (Lecture 23) cannot handle it directly.Trick: take the logarithm of every count.Plot ln N against t …… the dots now lie on a straight line.Fit that line with last lecture's formulas, then read b and a from its slope and intercept.
In plain words
  • Many laws are curves: growth, decay, powers, cycles
  • A logarithm can turn a curve into a straight line
  • Then last lecture's line fit does the work
New words today
linearise
transform the data so the model becomes a line
polynomial fit
least squares with x², x³, … added
overfitting
following the noise instead of the trend
test error
error on data not used for the fit
Md Mahbub E Noor
Lecture 24: Polynomial, exponential and trigonometric fits
University of Barishal
4 / 23

Polynomial least squares: more columns, same method

Model of degree m, and its least-squares criteriony^=a0+a1x+a2x2+⋯+amxm,S=∑i(yi−y^i)2\hat y = a_0 + a_1x + a_2x^2 + \cdots + a_mx^m, \qquad S = \sum_{i}\big(y_i - \hat y_i\big)^2
Parabola (m = 2): set ∂S/∂a₀ = ∂S/∂a₁ = ∂S/∂a₂ = 0[n∑x∑x2∑x∑x2∑x3∑x2∑x3∑x4][a0a1a2]=[∑y∑xy∑x2y]\begin{bmatrix} n & \sum x & \sum x^2 \\ \sum x & \sum x^2 & \sum x^3 \\ \sum x^2 & \sum x^3 & \sum x^4 \end{bmatrix} \begin{bmatrix} a_0 \\ a_1 \\ a_2 \end{bmatrix} = \begin{bmatrix} \sum y \\ \sum xy \\ \sum x^2y \end{bmatrix}
  • Still linear in the unknowns a0,…,ama_0, \dots, a_m: the curve is nonlinear, the problem is not
  • Entry (j,k)(j, k) is ∑xj+k\sum x^{j+k}: this is ATAA^{T}A with columns 1,x,x21, x, x^2
  • Need n>m+1n > m + 1 points (with n=m+1n = m + 1 it is interpolation)
  • Solve by Gaussian elimination or Cholesky (Lectures 12–13)
Md Mahbub E Noor
Lecture 24: Polynomial, exponential and trigonometric fits
University of Barishal
5 / 23
The method, step by step

Polynomial fitting, step by step

  1. Choose the degree mm (plot the data first; start low)
  2. Tabulate the powers: ∑xk\sum x^k for k=0,…,2mk = 0, \dots, 2m and ∑xky\sum x^ky for k=0,…,mk = 0, \dots, m
  3. Fill the (m+1)×(m+1)(m + 1) \times (m + 1) normal equations: entry (j,k)=∑xj+k(j, k) = \sum x^{j+k}
  4. Solve for a0,…,ama_0, \dots, a_m (Gauss with pivoting)
  5. Compute residuals, SS, R2R^2; check the pattern; for new xx evaluate by Horner

For large mm use QR on centred, scaled xx instead (see the limitations).

Md Mahbub E Noor
Lecture 24: Polynomial, exponential and trigonometric fits
University of Barishal
6 / 23

Problem 1: fit a parabola y=a0+a1x+a2x2y = a_0 + a_1x + a_2x^2

Data: xx = 1, 2, 3, 4, 5, 6;   y\;y = 2.1, 3.9, 8.2, 15.8, 26.1, 37.7. A straight line leaves the residual pattern +,−,−,−,−,++, -, -, -, -, +.
sumvaluesumvalue
nn6∑y\sum y93.8
∑x\sum x21∑xy\sum xy454.4
∑x2\sum x^291∑x2y\sum x^2y2354
∑x3\sum x^3441
∑x4\sum x^42275
Normal equations: 6a0+21a1+91a2=93.86a_0 + 21a_1 + 91a_2 = 93.8,   21a0+91a1+441a2=454.4\;21a_0 + 91a_1 + 441a_2 = 454.4,   91a0+441a1+2275a2=2354\;91a_0 + 441a_1 + 2275a_2 = 2354. Solving: a0=2.580a_0 = 2.580, a1=−1.9193a_1 = -1.9193, a2=1.3036a_2 = 1.3036.
Md Mahbub E Noor
Lecture 24: Polynomial, exponential and trigonometric fits
University of Barishal
7 / 23

Explore: choose the model and the degree

  • Raise the degree: SS always falls, but does the curve still look sensible?
  • Bacteria preset: compare "polynomial" with "exponential"
  • Pendulum: the power law T=aLbT = aL^{b} gives b≈0.5b \approx 0.5
  • Temperature: one sine wave per day
Md Mahbub E Noor
Lecture 24: Polynomial, exponential and trigonometric fits
University of Barishal
8 / 23

Linearisation: straighten the curve, then fit a line

Exponential: plot ln y against xy=aebx  ⟹  ln⁡y⏟Y=ln⁡a⏟A+b xy = ae^{bx} \;\Longrightarrow\; \underbrace{\ln y}_{Y} = \underbrace{\ln a}_{A} + b\,x
Power law: plot ln y against ln xy=axb  ⟹  ln⁡y⏟Y=ln⁡a⏟A+b ln⁡x⏟Xy = ax^{b} \;\Longrightarrow\; \underbrace{\ln y}_{Y} = \underbrace{\ln a}_{A} + b\,\underbrace{\ln x}_{X}
Saturation (Michaelis–Menten): plot 1/y against 1/xy=axb+x  ⟹  1y=1a+ba⋅1xy = \frac{ax}{b + x} \;\Longrightarrow\; \frac1y = \frac1a + \frac ba\cdot\frac1x
  • Fit Y=A+bXY = A + bX with Lecture 23's formulas, then a=eAa = e^{A}
  • Test: if the transformed points lie on a line, the model is right
  • Needs y>0y > 0 (and x>0x > 0 for the power law)
  • Caution: this minimises errors in ln⁡y\ln y, not in yy, so large yy get less weight
Md Mahbub E Noor
Lecture 24: Polynomial, exponential and trigonometric fits
University of Barishal
9 / 23

Problem 2: fit N=aebtN = ae^{bt} to the bacteria counts

1
Y=ln⁡NY = \ln N: 4.6052, 5.0106, 5.4381, 5.8289, 6.2344, 6.6464
2
∑t=15\sum t = 15, ∑t2=55\sum t^2 = 55, ∑Y=33.76363\sum Y = 33.76363, ∑tY=91.54323\sum tY = 91.54323, n=6n = 6
3
b=6(91.54323)−15(33.76363)6(55)−152=42.80493105=0.407666b = \dfrac{6(91.54323) - 15(33.76363)}{6(55) - 15^2} = \dfrac{42.80493}{105} = 0.407666
4
A=33.76363−15(0.407666)6=4.608108A = \dfrac{33.76363 - 15(0.407666)}{6} = 4.608108,   a=eA=100.29\;a = e^{A} = 100.29
5
Doubling time ln⁡2/b=1.70\ln 2/b = 1.70 h;   N(8)=100.29 e0.407666×8≈2616\;N(8) = 100.29\,e^{0.407666 \times 8} \approx 2616
N=100.3 e0.4077tN = 100.3\,e^{0.4077t}: the count grows 50.3% per hour (eb=1.503e^{b} = 1.503) and doubles every 1.70 hours.
Md Mahbub E Noor
Lecture 24: Polynomial, exponential and trigonometric fits
University of Barishal
10 / 23

Problem 3: a power law for the pendulum, T=aLbT = aL^{b}

Lengths LL (m) = 0.25, 0.5, 1, 1.5, 2; periods TT (s) = 1.00, 1.42, 2.01, 2.46, 2.84. Find aa and bb.
1
X=ln⁡LX = \ln L: −1.3863,−0.6931,0,0.4055,0.6931-1.3863, -0.6931, 0, 0.4055, 0.6931
2
Y=ln⁡TY = \ln T: 0,0.3507,0.6981,0.9002,1.04380, 0.3507, 0.6981, 0.9002, 1.0438
3
Line fit of YY on XX: b=0.5018b = 0.5018,   A=0.6970\;A = 0.6970
4
a=e0.6970=2.0077a = e^{0.6970} = 2.0077
T=2.008 L0.502T = 2.008\,L^{0.502}. Theory: T=2πgL1/2=2.006 L0.5T = \dfrac{2\pi}{\sqrt g}L^{1/2} = 2.006\,L^{0.5}: the fit recovers the physics, and g=(2π/a)2=9.79g = (2\pi/a)^2 = 9.79 m/s².
Md Mahbub E Noor
Lecture 24: Polynomial, exponential and trigonometric fits
University of Barishal
11 / 23

Periodic data: a trigonometric fit

Known period P, ω = 2π/P; still linear in a₀, a₁, b₁y^=a0+a1cos⁡ωx+b1sin⁡ωx\hat y = a_0 + a_1\cos\omega x + b_1\sin\omega x
n points equally spaced over one period: the normal equations decouplea0=1n∑yi,a1=2n∑yicos⁡ωxi,b1=2n∑yisin⁡ωxia_0 = \frac1n\sum y_i, \quad a_1 = \frac2n\sum y_i\cos\omega x_i, \quad b_1 = \frac2n\sum y_i\sin\omega x_i
Amplitude and time of the peakC=a12+b12,y^=a0+Ccos⁡(ω(x−xpeak))C = \sqrt{a_1^2 + b_1^2}, \qquad \hat y = a_0 + C\cos\big(\omega(x - x_{\text{peak}})\big)
  • Temperature every 3 h over a day (P=24P = 24): a0=27.44a_0 = 27.44, a1=−4.12a_1 = -4.12, b1=−3.64b_1 = -3.64
  • Amplitude C=5.50C = 5.50 °C; peak at ωx=atan2(b1,a1)\omega x = \text{atan2}(b_1, a_1): about 14.8 h (2:45 pm)
  • More harmonics (cos⁡2ωx\cos 2\omega x, …) give the Fourier series: the FFT does this fast
  • Tides, seasons, electricity demand, heart rhythms
Md Mahbub E Noor
Lecture 24: Polynomial, exponential and trigonometric fits
University of Barishal
12 / 23
Choosing the degree

Overfitting: a perfect fit to the training data, a bad model

  • Ten noisy samples of sin⁡2πx\sin 2\pi x; "test error" compares the fit with the true curve
  • Degree 1: underfits (train 0.62, test 0.46)
  • Degree 3–5: test error lowest (about 0.12)
  • Degree 9: passes through every point (train 0) but test error 0.58: it memorised the noise
  • Choose the degree by error on data not used for fitting
Md Mahbub E Noor
Lecture 24: Polynomial, exponential and trigonometric fits
University of Barishal
13 / 23
Classical vs numerical

Same growth data, two approaches

Logarithms

Classical (linearise by hand)

  • Take ln⁡N\ln N, fit a line with the sums formulas
  • a=100.29a = 100.29, b=0.407666b = 0.407666
  • Works only for models that can be straightened
vs
Algorithm

Numerical (minimise SS directly)

  • Minimise ∑(Ni−aebti)2\sum (N_i - ae^{bt_i})^2 by Gauss–Newton
  • a=100.44a = 100.44, b=0.407153b = 0.407153 in 3 iterations
  • Any model, errors measured in the original units
When the classical approach cannot work

Logistic growth N=K1+e−r(t−t0)N = \dfrac{K}{1 + e^{-r(t - t_0)}} with unknown capacity KK

No logarithm turns this into a straight line in KK, rr and t0t_0 together. The only way is to minimise S(K,r,t0)S(K, r, t_0) iteratively: Gauss–Newton or gradient descent (Lecture 25). This is how epidemic and market-adoption curves are fitted.

Md Mahbub E Noor
Lecture 24: Polynomial, exponential and trigonometric fits
University of Barishal
14 / 23

Polynomial and exponential fits in code

  • Builds the (m+1)×(m+1)(m+1) \times (m+1) normal equations from power sums
  • Gaussian elimination with pivoting
  • Prints coefficients, SS and R2R^2
  • Lab: the exponential fit through logarithms
Md Mahbub E Noor
Lecture 24: Polynomial, exponential and trigonometric fits
University of Barishal
15 / 23
When curve fitting misleads

Limitations shown failing

problemwhat happensremedy
High degree, normal equationscond(ATAA^{T}A) for 10 points in [0,1][0, 1]: degree 3: 10410^{4}, degree 5: 10710^{7}, degree 9: 2×10142\times10^{14}centre and scale xx; QR; orthogonal polynomials
Overfittingdegree 9 through 10 noisy points: train error 0, test error 0.58choose the degree by validation; regularise (ridge)
Extrapolating a polynomialLecture 23 data, degree 5: 40.4 min at 12 km, 32.9 at 13 kmuse a model with physical meaning; do not extrapolate far
Log transformneeds y>0y > 0; a count of 0 gives ln⁡0=−∞\ln 0 = -\inftyadd a small constant, or fit directly
Log transform weightsminimises relative error: small yy dominate, large yy fit worseweights wi=yi2w_i = y_i^2, or Gauss–Newton (L25)
Md Mahbub E Noor
Lecture 24: Polynomial, exponential and trigonometric fits
University of Barishal
16 / 23
Practice

Practice problem

Problem 4"Fit a second-degree parabola y=a+bx+cx2y = a + bx + cx^2 to the data xx: 0, 1, 2, 3, 4 and yy: 1, 1.8, 1.3, 2.5, 6.3 by the method of least squares."
How to write a complete answer
  • Sums: ∑x=10\sum x = 10, ∑x2=30\sum x^2 = 30, ∑x3=100\sum x^3 = 100, ∑x4=354\sum x^4 = 354, ∑y=12.9\sum y = 12.9, ∑xy=37.1\sum xy = 37.1, ∑x2y=130.3\sum x^2y = 130.3
  • Normal equations: 5a+10b+30c=12.95a + 10b + 30c = 12.9;   10a+30b+100c=37.1\;10a + 30b + 100c = 37.1;   30a+100b+354c=130.3\;30a + 100b + 354c = 130.3
  • Eliminate: c=0.55c = 0.55,   b=−1.07\;b = -1.07,   a=1.42\;a = 1.42
  • Answer y=1.42−1.07x+0.55x2y = 1.42 - 1.07x + 0.55x^2; check one residual, e.g. at x=4x = 4: 1.42−4.28+8.8=5.941.42 - 4.28 + 8.8 = 5.94 vs 6.3
Md Mahbub E Noor
Lecture 24: Polynomial, exponential and trigonometric fits
University of Barishal
17 / 23
When to use it

Polynomial, exponential and trigonometric fitting: method profile

Best case
  • Smooth trends with noise; low degree (1–3)
  • Growth and decay (exponential) or scaling laws (power) seen as straight lines on log axes
  • Periodic data with a known period
Worst case
  • High-degree polynomials: ill-conditioned, wiggly, extrapolate badly
  • Log transforms with zeros or very noisy small values
  • Models that cannot be made linear in the parameters
Best suited for
  • Growth of populations, investments, epidemics (early phase)
  • Physics and engineering laws (T∝L1/2T \propto L^{1/2}, Kepler T∝a3/2T \propto a^{3/2})
  • Seasonal and daily cycles
Avoid when
  • Choosing the degree by R2R^2 on the same data
  • Predicting far beyond the data with polynomials
  • Fitting ln⁡y\ln y when errors in yy matter (use a direct fit)

Rule of thumb: pick the model from the physics or from the shape on log axes, keep the degree low, and check it on data not used for fitting.

Md Mahbub E Noor
Lecture 24: Polynomial, exponential and trigonometric fits
University of Barishal
18 / 23

Comparative analysis: models for curved data

ModelLinear in unknowns?How to fitSize of systemTypical use
a+bxa + bxyessums formulas (L23)2 × 2trends
a0+a1x+⋯+amxma_0 + a_1x + \dots + a_mx^myesnormal equations / QR(m+1)(m+1) squaresmooth curves, low mm
aebxae^{bx}no, but linearisableline fit to (x,ln⁡y)(x, \ln y)2 × 2growth, decay, cooling
axbax^{b}no, but linearisableline fit to (ln⁡x,ln⁡y)(\ln x, \ln y)2 × 2scaling laws
a0+a1cos⁡ωx+b1sin⁡ωxa_0 + a_1\cos\omega x + b_1\sin\omega xyes (ω known)decoupled formulas3 diagonalcycles, seasons
K1+e−r(t−t0)\dfrac{K}{1 + e^{-r(t - t_0)}}noGauss–Newton (L25)iterativesaturation, adoption

"Linear least squares" means linear in the coefficients, not a straight-line curve.

Md Mahbub E Noor
Lecture 24: Polynomial, exponential and trigonometric fits
University of Barishal
19 / 23
Roadmap

Limitations, and where we fix them

Every fit so far was a single linear solve. The last lecture replaces the linear solve with an iteration that can minimise anything.

What it does well (keep this)
  • Any model linear in its coefficients: one linear solve
  • Logarithms straighten growth and power laws
  • Trigonometric terms for cycles
  • Validation error picks the degree
LimitationHow it is overcomeWhere
Linearisation fits ln⁡y\ln y, not yy
→
Fit the original model directly: Gauss–Newton
Lecture 25 →
Models that cannot be linearised (logistic, neural networks)
→
Minimise SS by gradient descent
Lecture 25 →
Choosing a regularisation λ and a degree
→
Minimise validation error: a 1-D optimisation (golden section)
Lecture 25 →
Huge data sets: forming ATAA^{T}A is too costly
→
Stochastic gradient descent, one batch at a time
Lecture 25 →
Md Mahbub E Noor
Lecture 24: Polynomial, exponential and trigonometric fits
University of Barishal
20 / 23
Applications

Where this method runs in the real world

Click a card to fit real-world laws.

Md Mahbub E Noor
Lecture 24: Polynomial, exponential and trigonometric fits
University of Barishal
21 / 23

Checkpoint

Checkpoint · think, then discuss with your neighbourTo fit y=aebxy = ae^{bx} by linearisation, which pairs do you put into the straight-line formulas?
A(xi,ln⁡yi)(x_i, \ln y_i)
B(ln⁡xi,ln⁡yi)(\ln x_i, \ln y_i)
C(ln⁡xi,yi)(\ln x_i, y_i)
D(xi,yi)(x_i, y_i), then take logs of aa and bb
A. ln⁡y=ln⁡a+bx\ln y = \ln a + bx is a line in xx and ln⁡y\ln y; the slope is bb and a=eintercepta = e^{\text{intercept}}. Option B is for the power law y=axby = ax^{b}.
Md Mahbub E Noor
Lecture 24: Polynomial, exponential and trigonometric fits
University of Barishal
22 / 23

Summary

Key points
  • Polynomial of degree mm: (m+1)(m+1) normal equations with entries ∑xj+k\sum x^{j+k}; keep mm low
  • y=aebxy = ae^{bx}: line through (x,ln⁡y)(x, \ln y);   y=axb\;y = ax^{b}: line through (ln⁡x,ln⁡y)(\ln x, \ln y)
  • Periodic: a0+a1cos⁡ωx+b1sin⁡ωxa_0 + a_1\cos\omega x + b_1\sin\omega x; equally spaced data decouple
  • Choose the model by validation error, not by training error
Opening problem: solved

N=100.3 e0.4077tN = 100.3\,e^{0.4077t}: the bacteria double every 1.70 hours, and after 8 hours there will be about 2600 cells (direct nonlinear fit: 2609).

Homework
  1. Fit y=a+bx+cx2y = a + bx + cx^2 to xx: 1, 2, 3, 4, 5, 6 and yy: 2.1, 3.9, 8.2, 15.8, 26.1, 37.7 and verify Problem 1.
  2. Fit y=aebxy = ae^{bx} to xx: 0, 1, 2, 3 and yy: 2.0, 3.3, 5.4, 9.0.
  3. Fit y=axby = ax^{b} to xx: 1, 2, 4, 8 and yy: 3.0, 4.2, 6.1, 8.4.
  4. With the overfitting widget, record train and test error for degrees 0–9 and explain the shape of both curves.
Next lecture

Lecture 25: numerical optimisation for machine learning. Golden-section search, gradient descent, Newton and Gauss–Newton: minimising any error function.

Md Mahbub E Noor
Lecture 24: Polynomial, exponential and trigonometric fits
University of Barishal
23 / 23