Minimal Working Examples

Here, I sample from / fit a sparse gaussian process using results from Titsias’ and Hensman et al. (2013).

### R code

N_p = 5000  # num data for fit & pred
N_u = 15    # num pseudo data

N_IS_SMALL = (N_p <= 5000)

x_p = seq(-5, 5, length.out = N_p)
x_u = seq(-3, 3, length.out = N_u)

# K = ( Kpp Kpu )
# K = ( Kup Kuu )

    Kpp = exp(-((matrix(rep(x_p, N_p), N_p, N_p) -
               t(matrix(rep(x_p, N_p), N_p, N_p)))/4)^2)

Kpu = exp(-((matrix(rep(x_p, N_u), N_p, N_u) -
             matrix(rep(x_u, each = N_p), N_p, N_u))/4)^2)

Kuu = exp(-((matrix(rep(x_u, N_u), N_u, N_u) -
           t(matrix(rep(x_u, N_u), N_u, N_u)))/4)^2)

Kuui = solve(Kuu + diag(N_u)*1e-10)

# Prior Sample

mu_param = t(t(rnorm(N_u)))
sigma_param = diag(N_u) * 1e-2

# Exact VFE

y = sin(x_p) + rnorm(N_p, 0, 0.25)
func_pres = 10
sigma_param = solve(func_pres * Kuui %*% t(Kpu) %*% Kpu %*% Kuui + Kuui)
mu_param = func_pres * sigma_param %*% Kuui %*% t(Kpu) %*% y

mu = Kpu %*% Kuui %*% mu_param
    sigma = Kpp + Kpu %*% Kuui %*% (sigma_param - Kuu) %*% Kuui %*% t(Kpu)

plot(x_p, mu, type = "l")
    arrows(x_p, mu - 2*sqrt(diag(sigma)),
           x_p, mu + 2*sqrt(diag(sigma)),
           length = 0.01, angle = 90,
           col = "#BEBEBE20")


batch_size = 50
l = 0.1

y = sin(x_p) + rnorm(N_p, 0, 0.25)
func_pres = 10

theta_1 = solve(diag(N_u) * 1e-2) %*% t(t(rnorm(N_u)))
theta_2 = -0.5 * solve(diag(N_u) * 1e-2)

for(i in 1:10000) {
	indices = sample(1:N_p, batch_size)

	lambda = func_pres * Kuui %*% t(Kpu[indices, ]) %*% Kpu[indices, ] %*% Kuui + Kuui
	theta_2 = theta_2 - l*(0.5*lambda + theta_2)
	theta_1 = theta_1 + l*(func_pres * Kuui %*% t(Kpu[indices, ]) %*% y[indices] - theta_1)

	sigma_param = solve(-2 * theta_2)
	mu_param = sigma_param %*% theta_1


I might write this up at some point in C++.


Efficient Gaussian Process Computation

I’ll try to give examples of efficient gaussian process computation here, like the vec trick (Kronecker product trick), efficient toeliptz and circulant matrix computations, RTS smoothing and Kalman filtering using state space representations, and so on.

4 min read

Gaussian Processes in MGCV

I lay out the canonical GP interpretation of MGCV’s GAM parameters here. Prof. Wood updated the package with stationary GP smooths after a request. I’ve run through the predict.gam source code in a debugger, and mainly, the computation of predictions follows:

~1 min read


I wanted to see how easy it was to do photogrammetry (create 3d models using photos) using PyTorch3D by Facebook AI Research.

1 min read

Dead Code & Syntax Trees

This post was motivated by some R code that I came across (over a thousand lines of it) with a bunch of if-statements that were never called. I wanted an automatic way to get a minimal reproducing example of a test from this file. While reading about how to do this, I came across Dead Code Elimination, which kills unused and unreachable code and variables as an example.

~1 min read
Back to Top ↑



I used to do a fair bit of astrophotography in university - it’s harder to find good skies now living in the city. Here are some of my old pictures. I’ve kept making rookie mistakes (too much ISO, not much exposure time, using a slow lens, bad stacking, …), for that I apologize!

1 min read

Probabilistic PCA

I’ve been reading about PPCA, and this post summarizes my understanding of it. I took a lot of this from Pattern Recognition and Machine Learning by Bishop.

1 min read

Modelling with Spotify Data

The main objective of this post was just to write about my typical workflow and views rather than come up with a great model. The structure of this data is also outside my immediate domain so I thought it’d be fun to write up a small diary on making a model with it.

5 min read

Morphing with GPs

The main aim here was to morph space inside a square but such that the transformation preserves some kind of ordering of the points. I wanted to use it to generate some random graphs on a flat surface and introduce spatial deformation to make the graphs more interesting.

1 min read

SEIR Models

I had a go at a few SEIR models, this is a rough diary of the process.

3 min read

Speech Synthesis

The initial aim here was to model speech samples as realizations of a Gaussian process with some appropriate covariance function, by conditioning on the spectrogram. I fit a spectral mixture kernel to segments of audio data and concatenated the segments to obtain the full waveform. Partway into writing efficient sampling code (generating waveforms using the Gaussian process state space representation), I realized that it’s actually quite easy to obtain waveforms if you’ve already got a spectrogram.

4 min read
Back to Top ↑


Gaussian Process Middle C

First of my experiments on audio modeling using Gaussian processes. Here, I construct a GP that, when sampled, plays middle c the way a grand piano would.

1 min read
Back to Top ↑


Back to Top ↑