Mathematical and Statistical Inference

7MBM1008 - AI for Imaging and Spatial Data

Dr. Ayan Das

November 20, 2026

Self Introduction

What do I do ?

  • Computer Scientist
  • Work in Generative AI
  • AI for Healthcare/Dentistry
  • Focused on Image modalities
  • Bit of fundamentals AI research

Module Information

Statistics for Spatial Data.

Similarities

  • Modelling collective behaviour
  • Recognising patterns in data
  • Dealing with inherent randomness

Lecture Outline

---
config:
  look: handDrawn
  themeVariables:
    fontSize: 22px
---
graph TD
    subgraph P1["Part I: Stat/Prob"]
        direction LR
        a1["Random<br/>variables"]
        a2[Distributions]
        a3["Parameter<br/>estimation"]
        a4["Bayesian<br/>thinking"]
        a5["Joint &<br/>conditional"]
        a6["Hypothesis<br/>testing"]
        a1 ~~~ a2 ~~~ a3 ~~~ a4 ~~~ a5 ~~~ a6
    end

    subgraph P2["Part II: Spatial Stat."]
        direction LR
        b1["Point<br/>patterns"]
        b2["Spatial<br/>graphs"]
        b3["Neighbourhood<br/>structure"]
        b4["Spatial<br/>autocorrelation"]
        b5["Generative<br/>models"]
        b1 ~~~ b2 ~~~ b3 ~~~ b4 ~~~ b5
    end

    P1 == "builds on" ==> P2

  • Part I: fast-paced recap
  • Part II: slower, in depth
  • More intuition, less rigorous math
  • lots of interactive diagrams
  • sometimes simplified for illustration

Statistics

“.. the science of collecting, analyzing, organizing and interpreting data to make decision under uncertainty.”

Random Variable

Numerical description of a random phenomenon.

Flipping a coin

Textual Description

\(X\) = “outcome of flipping a coin” / “heads or tails on top when flipped ?”

Known Possible Outcomes (Sample Space \(\Omega\))

\(\Omega = \{ \text{heads}, \text{tails} \}\)

Numerical Mapping, \(X: \Omega \rightarrow \mathbb{Z}\)

Every outcome is mapped to a number, e.g. \(\text{Heads} \rightarrow 1, \quad \text{Tails} \rightarrow 0\)

Trials/Observations/Measurements

Each time the outcome is unpredictable, but strictly within its sample space

\(\{ 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, \ldots, 1 \}\)

Random Variable

Numerical description of a random phenomenon.

Rolling a die.

Textual Description

\(X\) = “outcome of rolling a die” / “number of dots on top when rolled”

Known Possible Outcomes (Sample Space \(\Omega\))

\(\Omega = \{ \text{Rolling a 1}, \text{Rolling a 2}, \ldots, \text{Rolling a 6} \}\)

Numerical Mapping, \(X: \Omega \rightarrow \mathbb{Z}\)

Every outcome is mapped to a number, e.g.

\(\text{Rolling a 1} \rightarrow 1, \quad \text{Rolling a 2} \rightarrow 2, \quad \ldots, \quad \text{Rolling a 6} \rightarrow 6\)

Trials/Observations/Measurements

Each time the outcome is unpredictable, but strictly within its sample space.

\(\{ 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, \ldots, 1 \}\)

Question for you

I again rolled a die 50 times ..

array([2, 3, 4, 1, 1, 1, 5, 4, 3, 5, 1, 5, 1, 2, 1, 3, 5, 1, 4, 2, 5, 1,
       1, 4, 2, 2, 1, 1, 2, 1, 2, 1, 4, 1, 3, 2, 1, 1, 5, 1, 2, 1, 1, 2,
       2, 1, 1, 1, 1, 1])

Probability Distribution

How likely is each outcome of a random variable ?

For discrete RVs, its functional form is the “Probability Mass Function” or PMF.

\(P(X = x)\) or \(p(x)\) or \(p_X(x)\) reads as “the probability that \(X\) takes the value \(x\)”.

Properties of Probability

  1. \(P(X = x) \in [0.0, 1.0]\) for all \(x \in \Omega\)
  2. \(\sum_{x \in \Omega} P(X = x) = 1\)

Different PMFs for same RV

Let’s consider the die roll example.

The \(\sim\) Notations

\(X \sim p_X(x)\) reads as “\(X\) is distributed according to \(p_X(x)\)”. Similarly, \(X \sim q_X(x)\) reads as “\(X\) is distributed according to \(q_X(x)\)”.

# simulate 40 die rolls
random.choice([1, 2, 3, 4, 5, 6], size=40, p=[0.5, 0.2, 0.1, 0.1, 0.1, 0.0])
array([3, 2, 1, 1, 1, 1, 1, 2, 1, 1, 3, 2, 4, 2, 2, 2, 1, 1, 1, 4, 1, 1,
       1, 1, 1, 2, 4, 5, 1, 2, 2, 1, 4, 1, 5, 1, 2, 1, 4, 1])

Unknown parameters

We assumed the probabilities for the ‘biased’ die. But, in reality, we don’t know the probabilities.

\[ X \sim q_X(x; \theta) \]

\[ \text{where we try to find } \theta = [\theta_1, \theta_2, \theta_3, \theta_4, \theta_5, \theta_6] \]

We have access to \(n\) observations

\[ \{ x_1 = 1, x_2 = 2, x_3 = 1, x_4 = 1, x_5 = 5, \ldots, x_n = 1 \} \]

Statistical Inference

Estimating \(\theta\) from the observations is the goal of “statistical inference” \[ \theta \approx \mathrm{Inference}(\{ x_1, x_2, \ldots, x_n \}) \]

Estimating PMF from Observations

PMF is often unknown, and all we have are observations.



Histogram

\[ H(x) = \text{# of times } x\text{ appears in observations} \]

Normalised Histogram (Probabilities)

\[ \hat{q}_X(x) = \frac{H(x)}{\text{# of observations}} \]

More observations, better estimates

Another example: Event Count

Textual Description

\(X\) = “number of events occurring in a fixed interval”

Known Possible Outcomes (Sample Space \(\Omega\))

\(X \in \{0, 1, 2, 3, \ldots\}\)

Numerical Mapping, \(X: \Omega \rightarrow \mathbb{N}\)

Every outcome is already numerical, so \(X: \Omega \to \{0, 1, 2, 3, \ldots\}\).

A popular choice for modelling event counts is the Poisson distribution \[ P(X = k) = \frac{e^{-\lambda} \lambda^k}{k!} \]

(Continuous) Random Variable

Height of a person

Textual Description

\(X\) = “height of a person (in cm)”

Known Possible Outcomes (Sample Space \(\Omega\))

\(X \in [0, \infty)\) (Theoretically)

\(X \in [0, 270]\) (Practically)

Numerical Mapping

The outcomes are already numerical, so \(X: \Omega \to [0, 270]\).

Trials/Observations/Measurements

We can measure the height of multiple persons, and each time the outcome is unpredictable, but strictly within its sample space.

\(\{ 192.91, 169.49, 164.93, 171.01, 179.19, 184.12, 165.93, 164.26, \ldots \}\)

Probability Density Function (PDF)

Analogous to PMF (for discrete RVs), but for continuous RVs.

The PDF \(f_X(x)\) is “density”, not “probability”.

\[ P(x - dx \leq X \leq x + dx) \approx f_X(x) \cdot dx \]

Inference from observations

Estimating PDF, aka “density estimation” problem.

Histogram

\[ H(x) = \text{# of observations in bin } [x - dx, x + dx] \]

Normalised Histogram (Probabilities)

\[ \hat{f}_X(x) = \frac{H(x)}{\text{total # of observations}} \]

Assume a parametric form

Restricting the possibilities.

Assume a gaussian density for the height distribution, i.e. \(X \sim N(\mu, \sigma^2)\). \[ f_X(x; \theta = (\mu, \sigma)) = \frac{1}{\sigma \sqrt{2\pi}} e^{-\frac{(x - \mu)^2}{2\sigma^2}} \]

Model

Simplifying assumption we make as statisticians. We restrict the possible densities to a specific family.

Trade-off

We simplify the problem, reducing the number of parameters to estimate, but we loose flexibility. In fact, we could be wrong in our assumption.

A wrong model/assumption

Heights from two different countries, but we assume a gaussian PDF.


Available PDF models

Pareto Distribution

Pearson Type IX Distribution

Laplace Distribution

Gamma Distribution

Maximum Likelihood Estimation (MLE)

Given a parametric model \(f_X(x; \theta)\), we want to find the parameter \(\theta\) that maximizes the likelihood of the observed data.

\[ \hat{\theta} = \arg\max_{\theta} L(\theta) = \arg\max_{\theta} \prod_{x_i \in D} f_X(x_i; \theta) \]

we achieve this by iteratively updating \(\theta\) using gradient ascent

\[ \theta_{t+1} = \theta_t + \eta \nabla_\theta L(\theta_t) \]

The realm of Machine Learning

  • MLE is fundamental to many ML algorithms, e.g. linear regression, logistic regression, naive bayes, etc.
  • Deep Learning is essentially MLE with neural networks as the parametric model, with ‘backpropagation’ for optimization.
  • The backbone behind modern Generative AI like LLM, Diffusion Models, etc.

Kernel Density Estimation (KDE)

‘Non-parametric’ density estimation technique.

\[ \begin{aligned} &f_{\mathrm{KDE}}(x; h) = \frac{1}{n} \sum_{x_i \in D} K_h(x - x_i) \\ &= \frac{1}{n} \sum_{x_i \in D} f_{\mathrm{Gaussian}}(x; \mu = x - x_i, \sigma = h) \end{aligned} \]

Kernel

  • A kernel is a smooth non-negative function.
  • The kernel is often a Gaussian, but others possible too.
  • The kernel is centered at each observation \(x_i\).
  • The bandwidth \(h\) controls the smoothness of the final density.

Downsides

  • Complexity grows with number of observations.
  • Bandwidth is hyperparameter. Not always easy to choose.

Important Definitions

Mean (Expectation)

The “center of mass” of the distribution.

\[ \mathbb{E}[X] = \mathbb{E}_{f_X(x)}[X] = \int_{\Omega} x f_X(x) dx \]

Variance

The “spread” of the distribution.

\[ \begin{aligned} \text{Var}_{f_X(x)}[X] &= \mathbb{E}[(X - \mathbb{E}[X])^2] \\ &= \int_{\Omega} (x - \mathbb{E}[X])^2 f_X(x) dx \end{aligned} \]

Joint Distribution

\[ P(x, y) \]

captures the relationship between two random variables.

Conditional Distribution

\[ P(X | Y = y) \]

is the distribution of \(X\) given that \(Y\) takes a specific value \(y\).

Bayes’ Theorem: A Primer

We observe data \(X\). We want the hidden parameter \(\theta\) of the model behind it.

Forward and inverse

\[ \begin{aligned} \theta \ &\longrightarrow\ X &&: \ p(X \mid \theta) \ \text{(likelihood)} \\ \theta \ &\longleftarrow\ X &&: \ p(\theta \mid X) \ \text{(posterior)} \end{aligned} \]

  • \(\theta\): model parameter, hidden
  • \(X\): observation, what we measure

Bayes’ theorem

\[ p(\theta \mid X) = \frac{p(X \mid \theta)\, p(\theta)}{p(X)} \ \propto\ p(X \mid \theta)\, p(\theta) \]

Likelihood \(p(X \mid \theta)\)

How well \(\theta\) explains \(X\). The same quantity MLE maximises.

Prior \(p(\theta)\)

What we believe about \(\theta\) before seeing \(X\).

Posterior \(p(\theta \mid X)\)

What we believe after seeing \(X\).

Evidence \(p(X) = \int p(X \mid \theta)\, p(\theta)\, d\theta\)

Only a normaliser, it does not depend on \(\theta\).

The solver

\[ p(\theta \mid X) = \texttt{Solver}\big(X,\ p(X \mid \theta),\ p(\theta)\big) \]

  • In: data \(X\), a likelihood model we can evaluate, a prior (no \(p(X)\) needed)
  • Out: the whole posterior distribution, not a single value. Compare \(\hat\theta = \texttt{MLE}\big(X,\ p(X \mid \theta)\big)\), which returns one number.

(Continuous) Random “Vector”

More than one random variable, i.e. a vector of random variables.

Textual Description

\(C = \text{spatial location of a cell} = \begin{bmatrix} \text{x-coordinate} \\ \text{y-coordinate} \end{bmatrix}\)

Known Possible Outcomes (Sample Space \(\Omega\))

\(\Omega = [0, \mathrm{width}] \times [0, \mathrm{height}]\)

Numerical Mapping

\(C: \Omega \to [0, \mathrm{width}] \times [0, \mathrm{height}]\).

Trials/Observations/Measurements

Observations are now vectors e.g. \(\begin{bmatrix} 3.2 \\ 7.1 \end{bmatrix}, \begin{bmatrix} 6.5 \\ 4.8 \end{bmatrix}, \ldots\).

2D parametric Density Model

The 2D Gaussian PDF is defined as

\[ f_C(\bar{c}; \theta = (\bar{\mu}, \Sigma)) = \frac{1}{2\pi |\Sigma|^{1/2}} \exp\Big(-\frac{1}{2} (\bar{c} - \bar\mu)^T \Sigma^{-1} (\bar{c} - \bar\mu)\Big) \]

\[ \text{where, } \bar\mu = \begin{bmatrix} \mu_1 \\ \mu_2 \end{bmatrix}, \quad \Sigma = \begin{bmatrix} \sigma_1^2 & \rho \sigma_1 \sigma_2 \\ \rho \sigma_1 \sigma_2 & \sigma_2^2 \end{bmatrix} \]

2D Kernel Density Estimation

Non-parametric density estimation for 2D data.

\[ \begin{aligned} &f_{\mathrm{KDE}}(\bar{c}; h) = \frac{1}{n} \sum_{\bar{c}_i \in D} K_h(\bar{c} - \bar{c}_i) \\ &= \frac{1}{n} \sum_{\bar{c}_i \in D} f_{\mathrm{Gaussian2D}}(\bar{c}; \mu = \bar{c} - \bar{c}_i, \Sigma = h \mathrm{I}) \end{aligned} \]

Significant or Pure luck ?

Remember the die I rolled 50 times ..

Observation

14 ones in 50 rolls

The skeptic’s claim

“The die is fair”: \(P(X = 1) = \frac{1}{6}\)

So we expect only \(50 \times \frac{1}{6} \approx 8.3\) ones.

The question

If the die were fair, how surprising is 14 ones ?

Hypothesis Testing: Ask the simulator

Pretend the die is fair, and let luck play out many times.

Assume the skeptic is right

Fair die, \(P(X = 1) = \frac{1}{6}\)

Simulate

Roll 50 times, count the ones. Repeat many times.

Compare

How often does luck alone give at least as many ones as we saw ?


p-value

Probability of a result at least this extreme, if the skeptic is right. Usual threshold: \(p < 0.05\) → “significant”, reject the claim.

“Spatial” Statistics

Analysing data with spatial coordinates, to understand how location shapes patterns, relationships and variation.

Some realistic examples1

Motivating further algorithms in spatial statistics.

Some realistic examples1

Motivating further algorithms in spatial statistics.

Complete Spatial Randomness (CSR)

The 2D version of poisson “event count” explained earlier.

Spatial Poisson Process

Events fall independently and uniformly, at an average density of \(\lambda\) per unit area.

\[ P(N(A) = k) = \frac{e^{-\lambda |A|} (\lambda |A|)^k}{k!} \] where \(N(A)\) = number of points in region \(A\), and \(|A|\) = its area.


The null model

Given the number of points, their locations have “No spatial pattern” – means CSR.


Clustered, random or regular?

Same number of points, very different organisation.


Clustered

Attraction

e.g. immune cell aggregates

Random (CSR)

Independence

no spatial interaction

Regular

Repulsion

e.g. evenly spaced neurons

Ripley’s K function

Average number of other points within distance \(r\) of a point, per unit density.

\[ \hat K(r) = \frac{|A|}{n(n-1)} \sum_{i \neq j} \mathbf{1}(d_{ij} \le r) \qquad\quad K_{\text{CSR}}(r) = \pi r^2 \]

Above the dashed line: clustered. Below it: regular.
Circle = the count around one point; curve = average over all points (edge-corrected).

Ripley’s K: is the lumpiness real?

Null Hypothesis (\(H_0\)): CSR, so \(K(r = 0.1) = \pi \, 0.1^2 \approx 0.031\).



p-value

Large \(p\) → cannot reject “no clustering” (null hypothesis \(H_0\)): the lumps are plausible by luck alone.

Graph basics

Points alone have no notion of next to. A graph adds it.

Path and distance

Chain of edges from \(u\) to \(v\).

\(d(u, v)\) = fewest hops.

Connected

Every node reaches every other.

Isolated node: \(\deg(v) = 0\).

Graph \(G = (V, E)\)

Nodes \(V\) = cells. Edges \(E\) = pairs of neighbouring cells.

Undirected: \(i \sim j \Leftrightarrow j \sim i\).

Neighbours and degree

\(N(v)\) = cells joined to \(v\) by an edge.

\(\deg(v) = |N(v)|\)

Adjacency matrix

\(A_{ij} = 1\) if \(i \sim j\), else \(0\).

Row sum = degree.

Building a spatial neighbours graph

Same cells, two rules for adding edges, both based on Euclidean distance.

k nearest neighbours (\(k = 4\))

Link each cell to its \(k\) closest (either direction counts). Fixed count, variable reach.

Radius (\(r = 0.12\))

Link each cell to all cells within \(r\). Fixed reach, variable count. Cells with no neighbour are red.

Co-occurrence score

Do two cell types sit next to each other more than by chance?

The question

Around an A cell, which cell types show up more (or less) often than chance?

Eyeballing is not enough. We want a number, at every distance.

Score of B around A

  1. Pick a radius \(r\)
  2. Collect all cells within \(r\) of an A cell
  3. \(p(B \mid A, r)\): fraction of them that are B
  4. \(p(B \mid r)\): fraction of B among all neighbours at \(r\)
  5. Score\((r) = p(B \mid A, r) \,/\, p(B \mid r)\)

Co-occurrence score: over distance

Score of each cell type around A, at every radius.

Radius

Cell type

Degree centrality

How many other cells does the red cluster touch?


Compact

Dispersed

Degree centrality: the score

Fraction of the other cells that touch the cluster.

\[ C_D(S) = \frac{|N(S) \setminus S|}{|V| - |S|} \]

\(S\) cluster, \(N(S)\) its neighbours, \(V\) all cells.

Reading it

  • Low: compact, few outsiders in contact
  • High: spread out, in contact with everyone
  • Grows with \(|S|\)

Compact

Dispersed

Closeness centrality

Same cells, same cluster size. How far is the red cluster from everyone else?


Centre

Corner

Closeness centrality: the score

Number in each cell: hops to the nearest red cell.

\[ C(S) = \frac{|V \setminus S|}{\sum_{v \notin S} d(S, v)} \]

\(d(S, v) = \min_{u \in S} d(u, v)\), in hops. Same as 1 / (mean hops).

Reading it

  • Low: tucked away, far from most cells
  • High: a few hops from everything
  • Grows with \(|S|\)

Centre

Corner

Gene expression: Spatial Variablility

Each row is one spot on the tissue: where it sits, and how much of each gene was detected.

Spatial transcriptomics

  • Position: \((x, y)\) of the spot
  • Expression: one count per gene

GFAP

SNAP25

Moran’s I

Do neighbouring cells have similar counts?

\[ I = \frac{N}{W}\,\frac{\sum_{i}\sum_{j} w_{ij}\,(x_i - \bar{x})(x_j - \bar{x})}{\sum_{i} (x_i - \bar{x})^2} \]

  • \(x_i\): count in cell \(i\), \(\bar{x}\): mean count
  • \(N\): number of cells
  • \(w_{ij}\): 1 if \(i\) and \(j\) are neighbours, else 0
  • \(W = \sum_{ij} w_{ij}\): total weight

Reading it

  • \(I > 0\): neighbours alike
  • \(I \approx 0\): no pattern
  • \(I < 0\): neighbours differ

Modelling the spatial pattern: CAR

Moran’s \(I\) tells us whether neighbours are alike. Now we want the underlying pattern itself – Conditional Autoregression (CAR) simplified.

Goal

Find a function \(\phi\) over the tissue that captures the general spatial pattern of the gene, separated from noise.

\[ y_i = \phi_i + \varepsilon_i, \qquad \varepsilon_i \sim N(0, \sigma^2) \]

\[ \phi_i \mid \phi_{-i} \sim N\!\left(\rho\,\frac{\sum_j A_{ij}\,\phi_j}{\deg(i)},\ \frac{\tau^2}{\deg(i)}\right) \]

  • \(y_i\): measured expression at spot \(i\) (centred)
  • \(\phi_i\): the true expression, the hidden pattern
  • \(\varepsilon_i\): measurement noise
  • \(A_{ij}\), \(\deg(i)\): the kNN graph
  • \(\rho\): tie to neighbours, \(\tau\): spread

Assumptions

  • A spot is like the average of its neighbours
  • More neighbours, less uncertainty (\(\tau^2/\deg(i)\))
  • Noise is independent and Gaussian

Posterior Inference

We observe \(\{ y_i \} \triangleq Y\). We want the hidden pattern \(\{ \phi_i \} \triangleq \Phi\).

Hierarchical model

\[ \theta = (\rho, \tau, \sigma) \ \longrightarrow\ \Phi \ \longrightarrow\ Y \]

  • Observed: \(Y\) (measured expression), \(A\) (graph)
  • Latent: \(\Phi\) (true expression), \(\theta\) (spatial strength, spread, noise)

Likelihood

\[ p(Y \mid \Phi, \sigma) = \prod_{i=1}^{N} N(y_i;\ \phi_i,\ \sigma^2) \]

Prior (CAR)

\[ p(\Phi \mid \rho, \tau) = N\!\left(0,\ \left[\tau^{-2}(D - \rho A)\right]^{-1}\right) \]

Posterior

\[ p(\Phi, \theta \mid Y)\ \propto\ p(Y \mid \Phi, \sigma)\ p(\Phi \mid \rho, \tau)\ p(\theta) \]

What we want: the true expression only

\[ p(\Phi \mid Y) = \int p(\Phi, \theta \mid Y)\, d\theta \]

How: MCMC as a black box

\[ \begin{aligned} p(\Phi, \theta \mid Y) = \texttt{MCMC}\big(&Y,\ A, \\ &p(Y \mid \Phi, \sigma),\ p(\Phi \mid \rho, \tau),\ p(\theta)\big) \end{aligned} \]

  • In: data \(Y\), graph \(A\), the model terms (no \(p(Y)\) needed)
  • Out: the posterior distribution itself, not a single value

Reading the output

All the information is in \(p(\Phi \mid Y)\). If we only want a single map, read off its mean, \(\hat\Phi = \mathbb{E}[\Phi \mid Y] = \int \Phi\, p(\Phi \mid Y)\, d\Phi\).

Its spread, \(\mathrm{Var}[\Phi \mid Y]\), is the uncertainty.

Inference: input and output

Same spots, before and after.

Input: observed \(Y\), noisy counts

Output: \(\hat\Phi = \mathbb{E}[\Phi \mid Y]\), the smooth pattern

Output: hyperparameters, \(p(\theta \mid Y)\)

Conclusion

Which method for which question?

Question Method Output Rests on (Part I)
Is this point pattern random, clustered or regular? Ripley’s K K(r) vs πr², p-value Poisson process / CSR, simulation test
Who are my neighbours? Neighbours graph Adjacency, degree Distance, graph basics
Do two cell types sit together more than chance? Co-occurrence Conditional ÷ marginal, over distance Marginal, conditional, independence
Which cells are well connected or central? Centrality One score per cell Graph basics
Do neighbouring cells have similar expression? Moran’s I One score in [−1, 1] Mean, variance, covariance
What is the underlying spatial pattern, apart from noise? CAR model Posterior of φ (via MCMC) Bayes: likelihood, prior, posterior