Prerequisite · Calculus Foundations

Integration and Probability

Colab Notebook · ~40 min
Google Colab Notebook
Integration and Probability
Python · ~40 min
Open in Colab
Lab Objectives
1
Implement the trapezoidal rule and verify that integration error decreases as O(1/n²) with the number of subintervals.
2
Confirm the Fundamental Theorem numerically: show that ∫_a^b f(x)dx ≈ F(b) − F(a) for at least three different functions.
3
Integrate the standard normal PDF and verify the 68–95–99.7 rule by computing P(|X| ≤ kσ) for k = 1, 2, 3.
4
Implement a Monte Carlo estimator for π and plot the convergence of the estimate as a function of sample count.
5
Numerically compute D_KL(p‖q) and D_KL(q‖p) for two Gaussians and confirm that KL divergence is non-negative and asymmetric.

Lab 3: Integration and Probability

Integration connects calculus to probability: probability densities are defined by integrals, expected values are integrals, entropy is an integral. This lab makes those connections concrete — you will compute integrals numerically and analytically, verify the Fundamental Theorem, and recover the key probabilistic quantities you will use throughout the rest of the platform.

What You'll Build

  • A trapezoidal integrator from scratch and a convergence plot showing error vs number of trapezoids, confirming the O(1/n2)O(1/n^2) rate
  • A Fundamental Theorem verifier: compare abf(x)dx\int_a^b f(x)\,dx computed numerically vs F(b)F(a)F(b) - F(a) from the analytic antiderivative, for several ff and interval pairs
  • A Gaussian normalization check: numerically integrate the standard normal PDF over [,][-\infty, \infty] (using a wide finite range) and confirm the result is 1 — then verify the 68–95–99.7 rule by integrating over [1σ,1σ][-1\sigma, 1\sigma], [2σ,2σ][-2\sigma, 2\sigma], [3σ,3σ][-3\sigma, 3\sigma]
  • A Monte Carlo integrator: estimate π\pi by sampling uniform points in the unit square and checking whether they fall in the unit circle (area =π/4= \pi/4), then plot how the estimate improves with sample count
  • A KL divergence calculator: numerically integrate DKL(pq)=p(x)ln(p(x)/q(x))dxD_{\text{KL}}(p \| q) = \int p(x) \ln(p(x)/q(x))\,dx for two Gaussians with different means, confirming non-negativity and asymmetry (DKL(pq)DKL(qp)D_{\text{KL}}(p\|q) \neq D_{\text{KL}}(q\|p))

Key Concepts Practiced

By the end you will understand that the probabilistic quantities from the probability-foundations module — PDFs, expected values, KL divergence — are all just integrals, and that numerical integration via Monte Carlo sampling is why those quantities are tractable in high-dimensional ML systems.