Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Appendix: Some Maths

KTH JupyterHub Open In Colab Binder

Linear algebra

Row Vector

A row vector is a 1-dimensional array consisting of a single row of elements. For example, a row vector with three elements can be written as:

r=[r1,r2,r3]\mathbf{r} = [r_1, r_2, r_3]

Column Vector

A column vector is a 1-dimensional array consisting of a single column of elements. It can be thought of as a matrix with one column. For instance, a column vector with three elements appears as:

c=[c1c2c3]\mathbf{c} = \begin{bmatrix} c_1 \\ c_2 \\ c_3 \end{bmatrix}

Matrix

A matrix is a rectangular array of numbers arranged in rows and columns. For example, a matrix with two rows and three columns is shown as:

A=[a11a12a13a21a22a23]\mathbf{A} = \begin{bmatrix} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \end{bmatrix}

Transpose Operator

The transpose of a matrix is obtained by swapping its rows with its columns. The transpose of matrix A\mathbf{A} is denoted AT\mathbf{A}^T. For the given matrix A\mathbf{A}, the transpose is:

AT=[a11a21a12a22a13a23]\mathbf{A}^T = \begin{bmatrix} a_{11} & a_{21} \\ a_{12} & a_{22} \\ a_{13} & a_{23} \end{bmatrix}

Multiplication between Vectors

Vector multiplication can result in either a scalar or a matrix:

aTb=a1b1+a2b2+a3b3\mathbf{a}^T \mathbf{b} = a_1b_1 + a_2b_2 + a_3b_3
abT=[a1b1a1b2a1b3a2b1a2b2a2b3a3b1a3b2a3b3]\mathbf{a} \mathbf{b}^T = \begin{bmatrix} a_1b_1 & a_1b_2 & a_1b_3 \\ a_2b_1 & a_2b_2 & a_2b_3 \\ a_3b_1 & a_3b_2 & a_3b_3 \end{bmatrix}

Matrix Multiplication

The product of an n×kn \times k matrix A\mathbf{A} and a k×pk \times p matrix B\mathbf{B} is an n×pn \times p matrix C\mathbf{C}; the number of columns of A\mathbf{A} has to equal the number of rows of B\mathbf{B}. Each element cijc_{ij} of C\mathbf{C} is computed as the dot product of the ii-th row of A\mathbf{A} and the jj-th column of B\mathbf{B}:

cij=∑kaikbkjc_{ij} = \sum_{k} a_{ik} b_{kj}

L2-norm (Euclidean norm)

The L2-norm of a vector x=[x1,…,xn]T\mathbf{x} = [x_1,\dots,x_n]^T (also called the Euclidean norm) measures its length and is defined as:

∥x∥2=∑i=1nxi2.\|\mathbf{x}\|_2 = \sqrt{\sum_{i=1}^n x_i^2}.

Projection

The projection of vector u\mathbf{u} onto vector v\mathbf{v} is given by:

projvu=uTvvTvv\text{proj}_{\mathbf{v}} \mathbf{u} = \frac{\mathbf{u}^T\mathbf{v}}{\mathbf{v}^T\mathbf{v}} \mathbf{v}

This represents the orthogonal projection of u\mathbf{u} in the direction of v\mathbf{v}.

Source
<Figure size 600x600 with 1 Axes>

Eigenvalue and Eigenvector

An eigenvalue λ\lambda and its corresponding eigenvector v≠0\mathbf{v} \neq \mathbf{0} of a square matrix A\mathbf{A} satisfy the equation:

Av=λv\mathbf{A} \mathbf{v} = \lambda \mathbf{v}

Gradient

The gradient of a multivariable function f(x)f(\mathbf{x}) is a vector of partial derivatives, which points in the direction of the steepest ascent of ff:

∇f(x)=[∂f∂x1∂f∂x2⋮∂f∂xn]\nabla f(\mathbf{x}) = \begin{bmatrix} \frac{\partial f}{\partial x_1} \\ \frac{\partial f}{\partial x_2} \\ \vdots \\ \frac{\partial f}{\partial x_n} \end{bmatrix}

Basics of Probability

Descriptive Statistics

Mean

The mean of a set of numbers is the average of all the values. For a set X={x1,x2,…,xn}X = \{x_1, x_2, \dots, x_n\}, the sample mean xˉ\bar{x} is calculated as:

xˉ=1n∑i=1nxi\bar{x} = \frac{1}{n} \sum_{i=1}^n x_i

Variance

The variance measures the spread of a set of numbers around their mean. For a sample XX with sample mean xˉ\bar{x}, the maximum likelihood estimate of the variance σ2\sigma^2 (assuming normally distributed data) is

σ^MLE2=1n∑i=1n(xi−xˉ)2.\hat{\sigma}^2_{\text{MLE}} = \frac{1}{n} \sum_{i=1}^n (x_i - \bar{x})^2 .

Because the same data are used to estimate the mean, this estimate is biased (it tends to underestimate the variance). Dividing by n−1n-1 instead (Bessel’s correction) gives the unbiased estimate

s2=1n−1∑i=1n(xi−xˉ)2.s^2 = \frac{1}{n-1} \sum_{i=1}^n (x_i - \bar{x})^2 .

Covariance

Covariance is a measure of how much two random variables change together. For paired samples x1,…,xnx_1, \dots, x_n and y1,…,yny_1, \dots, y_n of the variables XX and YY, with sample means xˉ\bar{x} and yˉ\bar{y}, the maximum likelihood estimate of the covariance is

cov^(X,Y)=1n∑i=1n(xi−xˉ)(yi−yˉ),\widehat{\text{cov}}(X, Y) = \frac{1}{n} \sum_{i=1}^n (x_i - \bar{x})(y_i - \bar{y}) ,

while the unbiased estimate uses n−1n-1:

sXY=1n−1∑i=1n(xi−xˉ)(yi−yˉ).s_{XY} = \frac{1}{n-1} \sum_{i=1}^n (x_i - \bar{x})(y_i - \bar{y}) .

Probability Distributions

Probability Density Function (PDF)

The probability density function (pdf) p(x)p(x) of a continuous random variable is a function that describes how densely the probability is concentrated around each value of the random variable; the probability of any single exact value is zero. The area under the pdf curve between two values represents the probability of the variable falling within that range. A discrete random variable is instead described by its probabilities Pr⁡(X=x)\Pr(X = x) of taking each value xx.

Cumulative Distribution Function (CDF)

The cumulative distribution function (cdf) of a random variable XX gives the probability that XX will take a value less than or equal to xx. It is defined as:

F(x)=Pr⁡(X≤x)F(x) = \Pr(X \leq x)

Uniform Distribution

The uniform distribution is a probability distribution where every outcome is equally likely over a given interval [a,b][a, b]. Its pdf is:

p(x)=1b−afor x∈[a,b],and p(x)=0 otherwisep(x) = \frac{1}{b-a} \quad \text{for } x \in [a, b], \quad \text{and } p(x) = 0 \text{ otherwise}

Normal Distribution

The normal distribution is a probability distribution that is symmetric about its mean μ\mu, with values near the mean being more frequent than values far from it. With standard deviation σ\sigma, its pdf is given by:

p(x)=1σ2πe−(x−μ)22σ2p(x) = \frac{1}{\sigma\sqrt{2\pi}} e^{-\frac{(x - \mu)^2}{2\sigma^2}}

Expectation Value

The expectation value of a random variable is the long-run average value of repetitions of the experiment it represents. For a discrete random variable XX it is given by the sum over its possible values, and for a continuous random variable with pdf p(x)p(x) by the corresponding integral:

E[X]=∑xx⋅Pr⁡(X=x)(discrete)orE[X]=∫−∞∞x⋅p(x) dx(continuous)E[X] = \sum_{x} x \cdot \Pr(X = x) \quad \text{(discrete)} \quad \text{or} \quad E[X] = \int_{-\infty}^{\infty} x \cdot p(x)\, dx \quad \text{(continuous)}

Relationships Between Random Variables

Joint Probability Distributions

A joint probability distribution describes the probability (or density) of two or more random variables taking specific values together. For two variables XX and YY,

Pr⁡(X=x,Y=y)orp(x,y)\Pr(X = x, Y = y) \quad \text{or} \quad p(x, y)

denotes their joint distribution. From it, we can obtain the marginal distributions (see below) and conditional relationships such as

Pr⁡(Y=y∣X=x)=Pr⁡(X=x,Y=y)Pr⁡(X=x),p(y∣x)=p(x,y)p(x).\Pr(Y = y \mid X = x) = \frac{\Pr(X = x, Y = y)}{\Pr(X = x)}, \quad p(y \mid x) = \frac{p(x, y)}{p(x)}.

Marginal Probability Distributions

A marginal probability distribution gives the probability or density of a subset of variables, obtained by summing or integrating out the others from a joint distribution. For example, from a joint distribution of XX and YY:

Pr⁡(X=x)=∑yPr⁡(X=x,Y=y),p(x)=∫p(x,y) dy.\Pr(X = x) = \sum_y \Pr(X = x, Y = y), \quad p(x) = \int p(x, y)\,dy.

This process is called marginalization. The resulting marginal describes the distribution of XX alone, regardless of YY. The marginal distribution of the data reappears below, as the denominator of Bayes’ theorem.

Probabilistic Inference

Likelihood

The likelihood function expresses how well different parameter values explain the observed data. For continuous data, it is written as L(θ∣x)=p(x∣θ),L(\theta \mid x) = p(x \mid \theta), and for discrete data as L(θ∣x)=Pr⁡(x∣θ).L(\theta \mid x) = \Pr(x \mid \theta).

Although the likelihood has the same mathematical form as a probability or density function, its interpretation differs: in the likelihood, the data xx are fixed and θ\theta varies. The likelihood is not a probability distribution over θ\theta; its role is to compare parameter values in light of the data.

Maximum Likelihood

The maximum likelihood estimate (MLE) is the parameter value that maximizes the likelihood function:

θ^MLE=arg⁡max⁡θL(θ∣x).\hat{\theta}_{\text{MLE}} = \arg\max_{\theta} L(\theta \mid x).

It identifies the parameter that makes the observed data most probable (or most dense) under the model. MLEs are widely used because, under general conditions, they are consistent, asymptotically efficient, and asymptotically normal. In practice, the likelihood is often maximized numerically using gradient-based or expectation–maximization algorithms.

Prior Probability

The prior probability Pr⁡(θ)\Pr(\theta) (or density p(θ)p(\theta)) represents our beliefs about parameter values before any data are observed. Priors can encode earlier experimental knowledge, theoretical limits, or subjective expectations. When little information is available, weakly informative or noninformative priors are used to prevent the prior from dominating the inference. In continuous models, the prior is a density p(θ)p(\theta) satisfying ∫p(θ) dθ=1\int p(\theta)\,d\theta = 1; in discrete models, it is a probability mass function Pr⁡(θ)\Pr(\theta) summing to one.

Posterior Probability

After observing data xx, the prior is updated using Bayes’ theorem:

p(θ∣x)=p(x∣θ) p(θ)p(x)orPr⁡(θ∣x)=Pr⁡(x∣θ) Pr⁡(θ)Pr⁡(x).p(\theta \mid x) = \frac{p(x \mid \theta)\,p(\theta)}{p(x)} \quad \text{or} \quad \Pr(\theta \mid x) = \frac{\Pr(x \mid \theta)\,\Pr(\theta)}{\Pr(x)}.

The resulting posterior distribution combines prior information with the information from the data through the likelihood. The denominator,

p(x)=∫p(x∣θ) p(θ) dθ,p(x) = \int p(x \mid \theta)\,p(\theta)\,d\theta,

(or the corresponding sum for discrete variables) is the marginal distribution of the data, also called the marginal likelihood or evidence, and ensures that the posterior integrates or sums to one. The posterior forms the basis for Bayesian inference, from which we can compute summaries such as the posterior mean, variance, or mode.

What you should know after reading this chapter

You should be able to:

  1. Calculate the transpose of a matrix, the dot product aTb\mathbf{a}^T \mathbf{b} and the outer product abT\mathbf{a} \mathbf{b}^T of two vectors, and the product of two matrices using cij=∑kaikbkjc_{ij} = \sum_{k} a_{ik} b_{kj}.

  2. Calculate the L2-norm ∥x∥2\|\mathbf{x}\|_2 of a vector and the projection projvu\text{proj}_{\mathbf{v}} \mathbf{u} of one vector onto another.

  3. Define an eigenvalue λ\lambda and eigenvector v\mathbf{v} of a matrix through Av=λv\mathbf{A} \mathbf{v} = \lambda \mathbf{v}, and the gradient ∇f(x)\nabla f(\mathbf{x}) as the vector of partial derivatives that points in the direction of steepest ascent.

  4. Calculate the mean, variance and covariance of a small dataset, and explain the difference between the maximum likelihood estimates (dividing by nn) and the unbiased estimates (dividing by n−1n-1).

  5. Write down the pdfs of the uniform and normal distributions, explain how a pdf and a cdf F(x)=Pr⁡(X≤x)F(x) = \Pr(X \leq x) give probabilities, and calculate the expectation value E[X]E[X] of a discrete random variable.

  6. Calculate marginal and conditional probabilities from a joint distribution, by summing or integrating out the other variable.

  7. Contrast a likelihood L(θ∣x)L(\theta \mid x), where the data are fixed and θ\theta varies, with a probability distribution over θ\theta. Define the maximum likelihood estimate, and identify the prior, likelihood, posterior and evidence in Bayes’ theorem.