Capstone Project: Computer Vision & Monte Carlo Simulation
To solidify your mastery of NumPy, this comprehensive Capstone brings together every core concept you have learned—vectorization, multi-dimensional array slicing, broadcasting, linear algebra, universal functions, and random generators—across two complete end-to-end industry applications:
1
Computer Vision Pipeline: Constructing an image filter, color-to-grayscale luminosity transformation, and 2D spatial convolution kernel for image blurring and edge detection.
2
Quantitative Finance Engine: Vectorized Monte Carlo simulation modeling 10,000 geometric Brownian motion asset price paths to evaluate options pricing and Value-at-Risk (VaR).
Click to Zoom
NumPy Capstone Image Processing and Simulation
Project Part 1: Computer Vision & Spatial Image Convolution
In computer vision, an image is represented as a 3D NumPy array of shape $(H, W, C)$, where $H$ is height, $W$ is width, and $C=3$ represents the Red, Green, and Blue color channels (values in range $[0, 255]$).
1. Vectorized Grayscale Conversion (ITU-R BT.601)
Human visual perception is unequally sensitive to color wavelengths. The international standard for converting RGB to grayscale weights the channels as follows:
$$Y = 0.2989 R + 0.5870 G + 0.1140 B$$
Using matrix multiplication (@), we can convert an entire image to grayscale in a single vectorized operation:
1import numpy as np
2
3# Simulate a 100x100 synthetic RGB color test image
Image filtering (convolution) slides a small $K \times K$ kernel matrix across an image, computing the element-wise sum-product at each receptive field.
Let's implement a Gaussian Blur filter using vectorized NumPy slicing:
1# Define a 3x3 Gaussian blur kernel (weights sum to 1.0)
$\mu$: Expected annual drift rate ($8\%$ or $0.08$).
$\sigma$: Annual volatility ($25\%$ or $0.25$).
$\Delta t$: Time step ($1 / 252$ trading days per year).
$Z_t \sim \mathcal{N}(0, 1)$: Standard normal random shock.
Vectorizing 10,000 Paths Across 252 Trading Days
A naive Python loop iterating over 10,000 paths and 252 days would require 2.5 million slow loop cycles. In NumPy, we generate the entire 2D matrix at once and simulate all paths simultaneously using np.cumprod():