---
# System prepended metadata

title: Spatio-Temporal Statistics
tags: [time series, statistics, data science]

---

# Spatio-Temporal Statistics

### kriging method
Kriging is a geostatistical method used for spatial interpolation. The basic idea behind kriging is to estimate the value of a variable at an unobserved location based on the observed values at other locations, taking into account the spatial correlation between the observations.

The kriging estimator of a variable Z at an unobserved location s is given by:

$$
\hat{Z}(s) = \sum_{i=1}^{n} \lambda_i Z(s_i)
$$

where $Z(s_i)$ is the observed value of the variable at the $i$th location, $\lambda_i$ are the weights assigned to each observation, and $n$ is the number of observed locations used in the estimation.

The weights $\lambda_i$ are calculated based on the spatial correlation structure of the variable, which is typically modeled using a covariance function $C(h)$ that describes how the correlation between two observations depends on their spatial separation $h$.

A common form of covariance function is the spherical model:

$$
C(h) = \begin{cases}
C_0 \left(1.5 \frac{h}{r} - 0.5 \left(\frac{h}{r}\right)^3\right) & \text{if } h \leq r \\
C_0 & \text{if } h > r
\end{cases}
$$

where $C_0$ is the variance of the variable, $r$ is the range parameter (a measure of the spatial correlation range), and $h$ is the spatial separation between two locations.

The kriging weights can then be calculated using the following system of linear equations:

$$
\begin{bmatrix}
C(h_{1,1}) & C(h_{1,2}) & \cdots & C(h_{1,n}) \\
C(h_{2,1}) & C(h_{2,2}) & \cdots & C(h_{2,n}) \\
\vdots & \vdots & \ddots & \vdots \\
C(h_{n,1}) & C(h_{n,2}) & \cdots & C(h_{n,n})
\end{bmatrix}
\begin{bmatrix}
\lambda_1 \\
\lambda_2 \\
\vdots \\
\lambda_n
\end{bmatrix}=\begin{bmatrix}
C(h_{1,s}) \\
C(h_{2,s}) \\
\vdots \\
C(h_{n,s})
\end{bmatrix}
$$

where $h_{i,j}$ is the spatial separation between the $i$th and $j$th observation and $h_{i,s}$ is the spatial separation between the $i$th observation and the unobserved location s.

Once the weights are determined, the kriging estimator can be calculated using the equation mentioned above.

Here is the pseudo-code for kriging:

1. Choose a covariance function C(h) and estimate its parameters (e.g., C0 and r).
2. Calculate the spatial separation between all pairs of observed locations and between each observed location and the unobserved location s.
3. Build the system of linear equations and solve for the kriging weights.
4. Calculate the kriging estimator using the equation mentioned above.

Note that kriging is a computationally intensive method, especially for large datasets, so various optimizations (such as using efficient algorithms to solve the linear system) are commonly employed to speed up the calculations.

### lattice process
Lattice processes are stochastic processes defined on a lattice or grid, where the values of the process are specified at each point on the lattice. The values of the process at a point are typically assumed to depend only on the values at neighboring points. The process is often assumed to be stationary, meaning that its statistical properties do not vary with position on the lattice.

One way to mathematically represent a lattice process is to use a spatial covariance function, which describes the dependence of the process values at different points on the lattice. For a stationary process, the covariance between the values at two points, $\mathbf{x}$ and $\mathbf{x}'$, can be written as:

$$
C(\mathbf{x}, \mathbf{x}') = \text{cov}(Z(\mathbf{x}), Z(\mathbf{x}')),
$$

where $Z(\mathbf{x})$ and $Z(\mathbf{x}')$ are the values of the process at points $\mathbf{x}$ and $\mathbf{x}'$, respectively.

There are many possible choices of spatial covariance functions for lattice processes. One common choice is the isotropic exponential covariance function, which is given by:

$$
C(\mathbf{x}, \mathbf{x}') = \sigma^2 \exp\left(-\frac{\|\mathbf{x} - \mathbf{x}'\|}{\phi}\right),
$$

where $\sigma^2$ is the variance of the process, $\phi$ is a correlation length parameter, and $\|\cdot\|$ denotes the Euclidean distance between the points.

To simulate a lattice process, one approach is to first generate a Gaussian random field with the desired covariance function, and then transform the field to the desired marginal distribution. This can be done using the Cholesky decomposition of the covariance matrix, which allows us to generate correlated Gaussian random variables.

The pseudo-code for simulating a lattice process with the isotropic exponential covariance function is as follows:

1. Set the lattice size and correlation length parameter
2. Define the covariance function $C(\mathbf{x}, \mathbf{x}') = \sigma^2 \exp\left(-\frac{\|\mathbf{x} - \mathbf{x}'\|}{\phi}\right)$
3. Compute the covariance matrix $\mathbf{C}$ for all pairs of lattice points using the covariance function
4. Compute the Cholesky decomposition $\mathbf{L}$ of $\mathbf{C}$: $\mathbf{C} = \mathbf{L}\mathbf{L}^\top$
5. Generate a vector of independent standard normal random variables $\mathbf{u}$
6. Compute the vector $\mathbf{z} = \mathbf{L}\mathbf{u}$, which has the desired covariance structure
7. Transform the vector $\mathbf{z}$ to the desired marginal distribution (e.g., by using the inverse cumulative distribution function of a normal distribution)

This procedure generates a realization of the lattice process, where the values of the process at each lattice point are given by the corresponding element of the vector $\mathbf{z}$.

### Empirical Orthogonal Functions
Empirical Orthogonal Functions (EOF) and Principal Component Analysis (PCA) are mathematically equivalent. EOF is a technique commonly used in geosciences and oceanography to analyze spatio-temporal data, while PCA is a more general technique used in many fields to analyze multivariate data.

The study of EOFs is related to PCA in the sense that the “traits” of the multivariate data vector now are spatially indexed, and the samples are usually taken over time.

### Spatio-Temporal Canonical Correlation Analysis
Spatio-Temporal Canonical Correlation Analysis (ST-CCA) is an extension of Canonical Correlation Analysis (CCA) to spatio-temporal data. ST-CCA seeks to find linear combinations of two sets of variables, one set being spatially correlated and the other set being temporally correlated, which are maximally correlated across both space and time.

Mathematically, let $\{\mathbf{x}_{i,t}\}$ be a spatio-temporal dataset consisting of $n$ spatial locations and $T$ time points. Let $\mathbf{X}_t = [\mathbf{x}_{1,t}, \mathbf{x}_{2,t}, \dots, \mathbf{x}_{n,t}]$ be the spatial data matrix at time $t$, and let $\mathbf{Y}_t = [\mathbf{y}_{1,t}, \mathbf{y}_{2,t}, \dots, \mathbf{y}_{m,t}]$ be the temporal data matrix at time $t$. In Spatio-Temporal Canonical Correlation Analysis (ST-CCA), $\mathbf{x}_{i,t}$ and $\mathbf{y}_{i,t}$ represent the observations at location $i$ and time $t$ for the two sets of variables being analyzed. The variables in $\mathbf{x}_{i,t}$ are typically the spatial variables, such as temperature or air quality measurements at location $i$ and time $t$, while the variables in $\mathbf{y}_{i,t}$ are typically the temporal variables, such as daily or hourly measurements over a period of time. By analyzing the relationship between these two sets of variables, ST-CCA aims to identify the underlying spatio-temporal patterns and correlations in the data.

The goal of ST-CCA is to find linear combinations $\mathbf{a}$ and $\mathbf{b}$ of the spatial and temporal data matrices, respectively, that maximize the correlation coefficient:

$$\rho = \frac{\mathbf{a}^T\mathbf{S}_{xy}\mathbf{b}}{\sqrt{\mathbf{a}^T\mathbf{S}_{xx}\mathbf{a}}\sqrt{\mathbf{b}^T\mathbf{S}_{yy}\mathbf{b}}}$$

where $\mathbf{S}_{xx}$, $\mathbf{S}_{yy}$, and $\mathbf{S}_{xy}$ are the spatial covariance matrix, temporal covariance matrix, and cross-covariance matrix, respectively. The optimal values of $\mathbf{a}$ and $\mathbf{b}$ are found by solving the generalized eigenvalue problem:

$$(\mathbf{S}_{xx})^{-1/2}\mathbf{S}_{xy}(\mathbf{S}_{yy})^{-1/2}\mathbf{b} = \lambda\mathbf{a}$$

where $\lambda$ is the canonical correlation coefficient. The ST-CCA algorithm finds the first $k$ pairs of canonical correlations by iteratively solving the above equation and orthogonalizing the resulting pairs.

The pseudo code for ST-CCA is as follows:

```
Input: Spatial data matrix X, temporal data matrix Y, number of canonical pairs k

1. Compute the spatial covariance matrix Sxx
2. Compute the temporal covariance matrix Syy
3. Compute the cross-covariance matrix Sxy
4. Compute the matrix B = inv(sqrt(Syy)) * Sxy * inv(sqrt(Sxx))
5. Compute the first k pairs of canonical correlation coefficients and vectors by solving the eigenvalue problem: 
    (Sxx)^(-1/2) * Sxy * (Syy)^(-1/2) * b = lambda * a
6. Compute the k pairs of canonical variates by projecting the spatial and temporal data matrices onto the canonical vectors:
    u_i = X * a_i
    v_i = Y * b_i
7. Return the k pairs of canonical correlation coefficients and variates
```