← Math|7 of 100
Mathematics for Data Science & AI

Numerical Methods — Computing with Math

Master numerical methods for data science: floating point arithmetic, root finding, integration, and linear system solving.

📂 Numerical Methods📖 Lesson 7 of 100🎓 Free Course

Advertisement

Numerical Methods — Computing with Math

ℹ️ Why It Matters

Computers can't do exact math with real numbers. Numerical methods bridge the gap between mathematical theory and computer implementation.


Floating Point Arithmetic

How Computers Store Numbers

IEEE 754 Standard:

ℹ️ IEEE 754 Standard

32-bit float:

  • 1 bit sign + 8 bits exponent + 23 bits mantissa

64-bit double:

  • 1 bit sign + 11 bits exponent + 52 bits mantissa

Precision Limits:

⚠️ Precision Limits

  • Float: ~7 decimal digits
  • Double: ~15 decimal digits

Example: 0.1+0.20.30.1 + 0.2 \neq 0.3 (in floating point!)

0.1+0.2=0.300000000000000040.1 + 0.2 = 0.30000000000000004

Common Numerical Issues

⚠️ Common Numerical Issues

Loss of significance: Subtracting nearly equal numbers

1.00000011.0000000=0.00000011.0000001 - 1.0000000 = 0.0000001

But with limited precision: might lose significant digits

Overflow: Number too large to represent Underflow: Number too small (close to zero) to represent Catastrophic cancellation: When subtraction leads to huge relative error


Root Finding

Bisection Method

ℹ️ Bisection Method

  1. Start with [a,b][a, b] where f(a)f(a) and f(b)f(b) have opposite signs
  2. Compute midpoint c=(a+b)/2c = (a+b)/2
  3. If f(c)f(c) has same sign as f(a)f(a), replace aa with cc Otherwise, replace bb with cc
  4. Repeat until converged

Convergence: Linear (slow but guaranteed)

Newton's Method

Newton's Method

xn+1=xnf(xn)f(xn)x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)}

Here,

  • xnx_n=Current estimate
  • f(xn)f'(x_n)=Derivative at current estimate

ℹ️ Newton's Method Properties

  • Requires: f(x)f'(x)
  • Convergence: Quadratic (very fast)
  • Can fail if f(x)0f'(x) \approx 0

Secant Method

ℹ️ Secant Method

Like Newton's but approximates the derivative:

f(xn)f(xn)f(xn1)xnxn1f'(x_n) \approx \frac{f(x_n) - f(x_{n-1})}{x_n - x_{n-1}}
  • Doesn't need the derivative
  • Superlinear convergence

Numerical Integration

Riemann Sum

Riemann Sum

abf(x)dxif(xi)×Δx\int_a^b f(x) \, dx \approx \sum_i f(x_i) \times \Delta x

Here,

  • Δx\Delta x=Width of each subinterval

ℹ️ Riemann Sum

Simple but inaccurate

Trapezoidal Rule

Trapezoidal Rule

abf(x)dxΔx2[f(a)+2if(xi)+f(b)]\int_a^b f(x) \, dx \approx \frac{\Delta x}{2} \left[f(a) + 2\sum_i f(x_i) + f(b)\right]

Here,

  • Δx\Delta x=Width of each subinterval

ℹ️ Trapezoidal Rule

Better accuracy than Riemann sum

Simpson's Rule

Simpson's Rule

abf(x)dxΔx3[f(a)+4oddf(xi)+2evenf(xi)+f(b)]\int_a^b f(x) \, dx \approx \frac{\Delta x}{3} \left[f(a) + 4\sum_{\text{odd}} f(x_i) + 2\sum_{\text{even}} f(x_i) + f(b)\right]

Here,

  • Δx\Delta x=Width of each subinterval

ℹ️ Simpson's Rule

  • Even better accuracy
  • Uses parabolic approximation

Monte Carlo Integration

Monte Carlo Integration

abf(x)dxbaNif(xi)\int_a^b f(x) \, dx \approx \frac{b-a}{N} \sum_i f(x_i)

Here,

  • xix_i=Random sample points
  • NN=Number of samples

💡 Monte Carlo Integration

Works well in high dimensions!

Used in: Ray tracing, physics simulations, Bayesian inference


Linear System Solving

Gaussian Elimination

ℹ️ Gaussian Elimination

  1. Write augmented matrix [Ab][A|b]
  2. Forward elimination: Convert to upper triangular
  3. Back substitution: Solve from bottom up

Time: O(n3)O(n^3)

LU Decomposition

LU Decomposition

A=LUA = LU

Here,

  • LL=Lower triangular matrix
  • UU=Upper triangular matrix

ℹ️ LU Decomposition

  • Solve Ly=bLy = b (forward substitution)
  • Solve Ux=yUx = y (back substitution)

Time: O(n3)O(n^3) for decomposition, O(n2)O(n^2) for each solve Efficient when solving for multiple bb vectors

Iterative Methods

Jacobi Method:

Jacobi Method

xi(k+1)=bijiaijxj(k)aiix_i^{(k+1)} = \frac{b_i - \sum_{j \neq i} a_{ij} x_j^{(k)}}{a_{ii}}

Here,

  • xi(k+1)x_i^{(k+1)}=Updated value
  • xj(k)x_j^{(k)}=Previous values

ℹ️ Jacobi Method

Simple but slow convergence

Gauss-Seidel Method:

ℹ️ Gauss-Seidel Method

Like Jacobi but uses updated values immediately. Faster convergence than Jacobi.

Conjugate Gradient:

ℹ️ Conjugate Gradient

  • For symmetric positive definite matrices
  • Much faster than Gauss-Seidel
  • Time: O(nκ)O(n\sqrt{\kappa}) where κ\kappa is the condition number

Eigenvalue Algorithms

Power Method:

ℹ️ Power Method

Find the largest eigenvalue:

  1. Start with random vector vv
  2. Repeat: vAv/Avv \leftarrow Av / \|Av\|
  3. Eigenvalue vTAv\approx v^TAv

Simple but only finds the largest eigenvalue

QR Algorithm:

ℹ️ QR Algorithm

  1. Start with A0=AA_0 = A
  2. Decompose Ak=QkRkA_k = Q_kR_k
  3. Ak+1=RkQkA_{k+1} = R_kQ_k
  4. AkA_k converges to upper triangular (eigenvalues on diagonal)

Finds all eigenvalues. Time: O(n3)O(n^3) per iteration


Optimization Algorithms

Line Search

ℹ️ Line Search

Given direction dd, find step size α\alpha:

α=argminf(x+αd)\alpha = \arg\min f(x + \alpha d)

Methods:

  • Exact line search (expensive)
  • Backtracking line search (cheap, practical)

Trust Region Methods

ℹ️ Trust Region Methods

  1. Build a model of ff near current point
  2. Find the best point within a "trust region"
  3. If model is accurate, expand trust region
  4. If not, shrink trust region

Conjugate Gradient for Optimization

Conjugate Gradient Optimization

minf(x)=12xTAxbTx\min f(x) = \frac{1}{2}x^TAx - b^Tx

Here,

  • AA=Symmetric positive definite matrix
  • bb=Target vector

ℹ️ Conjugate Gradient Algorithm

  1. Start with x0=0x_0 = 0, r0=br_0 = b
  2. p0=r0p_0 = r_0
  3. Repeat:
    • αk=rkTrk/pkTApk\alpha_k = r_k^Tr_k / p_k^TAp_k
    • xk+1=xk+αkpkx_{k+1} = x_k + \alpha_kp_k
    • rk+1=rkαkApkr_{k+1} = r_k - \alpha_kAp_k
    • βk=rk+1Trk+1/rkTrk\beta_k = r_{k+1}^Tr_{k+1} / r_k^Tr_k
    • pk+1=rk+1+βkpkp_{k+1} = r_{k+1} + \beta_kp_k

Very efficient for large sparse systems


📋Key Takeaways

  • Floating point arithmetic has precision limits. IEEE 754 doubles give ~15 decimal digits; 0.1+0.20.30.1 + 0.2 \neq 0.3 in floating point — always use tolerances when comparing real numbers.

  • Newton's Method converges quadratically. xn+1=xnf(xn)f(xn)x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)} doubles correct digits each iteration, but requires the derivative and can fail if f(x)0f'(x) \approx 0.

  • Integration methods trade accuracy for simplicity. Riemann sums are simple but inaccurate; Simpson's rule uses parabolic approximations for better精度; Monte Carlo integration abf(x)dxbaNif(xi)\int_a^b f(x) \, dx \approx \frac{b-a}{N} \sum_i f(x_i) excels in high dimensions.

  • LU Decomposition A=LUA = LU efficiently solves linear systems. Factor once in O(n3)O(n^3), then solve Ly=bLy = b and Ux=yUx = y in O(n2)O(n^2) each — ideal when solving for multiple right-hand sides.

  • The Conjugate Gradient method solves sparse systems in O(nκ)O(n\sqrt{\kappa}). For symmetric positive definite matrices, it's far more efficient than direct methods for large-scale problems common in ML.

  • The condition number κ(A)=AA1\kappa(A) = \|A\| \cdot \|A^{-1}\| predicts numerical stability. Large κ\kappa means small input changes cause large output changes — always check conditioning before solving linear systems in production.

Lesson Progress7 / 100