Mathematical Formulation of CausalImpact Analysis Using Structural Time Series and Gibbs Sampling

| December 4, 2024

The state-space model, priors, and Gibbs sampling steps behind CausalImpact, plus how to turn posterior samples into a causal effect estimate.

This post contains affiliate links for tools I use in production. If you buy through them I earn a commission at no extra cost to you. Recommendations are based on my own experience.

Table of Contents

1. Overview

CausalImpact Analysis estimates the causal effect of an intervention by comparing observed data against a counterfactual: what would have happened without the intervention. It builds that counterfactual with a Structural Time Series (STS) model, then fits the model with Gibbs Sampling, a Bayesian inference technique, to get posterior distributions over the model parameters.


2. Structural Time Series (STS) Model

The Structural Time Series (STS) model offers a robust framework for modeling time-series data by decomposing it into various components such as trend, seasonality, and regression effects.

2.1. State-Space Representation

The STS model is formulated within a state-space framework, comprising two primary equations: the State Transition Equation and the Observation Equation.

a. State Vector (xt\mathbf{x}_t)

The state vector encapsulates all latent (unobserved) components influencing the observed data at time tt:

xt=[ℓts1,ts2,t⋮sK,tβt]\mathbf{x}_t = \begin{bmatrix} \ell_t \\ s_{1,t} \\ s_{2,t} \\ \vdots \\ s_{K,t} \\ \boldsymbol{\beta}_t \\ \end{bmatrix}

Components:

  • ℓt\ell_t: Local Level capturing the underlying trend at time tt.
  • sk,ts_{k,t}: Seasonal Component kk at time tt for k=1,…,Kk = 1, \dots, K.
  • βt\boldsymbol{\beta}_t: Regression Coefficients representing the influence of covariates at time tt.

b. State Transition Equation

The evolution of the state vector over time is governed by:

xt=Gxt−1+wt\mathbf{x}_t = \mathbf{G} \mathbf{x}_{t-1} + \mathbf{w}_t

Where:

  • G\mathbf{G}: State Transition Matrix dictating how each state evolves.
  • wt\mathbf{w}_t: State Noise Vector, modeled as:
wt∼N(0,W)\mathbf{w}_t \sim \mathcal{N}(\mathbf{0}, \mathbf{W})

Detailed Structure:

Assuming independent evolution of each component:

  • Local Level:

    ℓt=ℓt−1+ηt,ηt∼N(0,σℓ2)\ell_t = \ell_{t-1} + \eta_t, \quad \eta_t \sim \mathcal{N}(0, \sigma_\ell^2)
  • Seasonal Components:

    sk,t=sk,t−m+ϵk,t,ϵk,t∼N(0,σsk2)s_{k,t} = s_{k,t-m} + \epsilon_{k,t}, \quad \epsilon_{k,t} \sim \mathcal{N}(0, \sigma_{s_k}^2)
  • Regression Coefficients:

    βt=βt−1+ξt,ξt∼N(0,Σβ)\boldsymbol{\beta}_t = \boldsymbol{\beta}_{t-1} + \boldsymbol{\xi}_t, \quad \boldsymbol{\xi}_t \sim \mathcal{N}(\mathbf{0}, \mathbf{\Sigma}_\beta)

Thus, the transition matrices are defined as:

G=[10…00⊤01…00⊤⋮⋮⋱⋮⋮00…10⊤00…0I],W=[σℓ20…00σs12I…0⋮⋮⋱⋮00…Σβ]\mathbf{G} = \begin{bmatrix} 1 & 0 & \dots & 0 & \mathbf{0}^\top \\ 0 & 1 & \dots & 0 & \mathbf{0}^\top \\ \vdots & \vdots & \ddots & \vdots & \vdots \\ 0 & 0 & \dots & 1 & \mathbf{0}^\top \\ \mathbf{0} & \mathbf{0} & \dots & \mathbf{0} & \mathbf{I} \end{bmatrix}, \quad \mathbf{W} = \begin{bmatrix} \sigma_\ell^2 & \mathbf{0} & \dots & \mathbf{0} \\ \mathbf{0} & \sigma_{s_1}^2 \mathbf{I} & \dots & \mathbf{0} \\ \vdots & \vdots & \ddots & \vdots \\ \mathbf{0} & \mathbf{0} & \dots & \mathbf{\Sigma}_\beta \end{bmatrix}

Parameters:

  • σℓ2\sigma_\ell^2: Variance of the local level noise.
  • σsk2\sigma_{s_k}^2: Variance of the kk-th seasonal component noise.
  • Σβ\mathbf{\Sigma}_\beta: Covariance matrix for the regression coefficients.
  • I\mathbf{I}: Identity matrix of appropriate dimension.

2.2. Observation Equation

The Observation Equation links the latent state vector to the observed data:

yt=F⊤xt+εt,εt∼N(0,σε2)y_t = \mathbf{F}^\top \mathbf{x}_t + \varepsilon_t, \quad \varepsilon_t \sim \mathcal{N}(0, \sigma_\varepsilon^2)

Where:

  • F\mathbf{F}: Observation Matrix, defined as:
F=[10K⊤Xt⊤]⊤\mathbf{F} = \begin{bmatrix} 1 & \mathbf{0}_{K}^\top & \mathbf{X}_t^\top \end{bmatrix}^\top
  • Xt\mathbf{X}_t: Covariate vector at time tt.
  • σε2\sigma_\varepsilon^2: Variance of the observation noise.

3. Priors and Hyperparameters

In Bayesian analysis, priors represent initial beliefs about the model parameters before observing the data. Proper specification of priors is essential as they influence the posterior distributions.

3.1. Local Level Variance Prior

σℓ2∼Inverse-Gamma(αℓ,βℓ)\sigma_\ell^2 \sim \text{Inverse-Gamma}(\alpha_\ell, \beta_\ell)
  • αℓ\alpha_\ell: Shape parameter.
  • βℓ\beta_\ell: Scale parameter.

3.2. Observation Noise Variance Prior

σε2∼Inverse-Gamma(αε,βε)\sigma_\varepsilon^2 \sim \text{Inverse-Gamma}(\alpha_\varepsilon, \beta_\varepsilon)
  • αε\alpha_\varepsilon: Shape parameter.
  • βε\beta_\varepsilon: Scale parameter.

3.3. Regression Weights Prior

Assuming a multivariate normal prior for regression coefficients β\boldsymbol{\beta}:

β∼N(0,Λ−1)\boldsymbol{\beta} \sim \mathcal{N}(\mathbf{0}, \mathbf{\Lambda}^{-1})
  • Λ\mathbf{\Lambda}: Precision matrix, often derived from the design matrix X\mathbf{X}:
Λ=0.01×0.5×(X⊤X)N\mathbf{\Lambda} = 0.01 \times \frac{0.5 \times (\mathbf{X}^\top \mathbf{X})}{N}
  • NN: Number of observations.

3.4. Initial State Priors

ℓ0∼N(y0,σy2)\ell_0 \sim \mathcal{N}(y_0, \sigma_y^2) sk,0∼N(0,σsk2)s_{k,0} \sim \mathcal{N}(0, \sigma_{s_k}^2) β0∼N(0,Σβ)\boldsymbol{\beta}_0 \sim \mathcal{N}(\mathbf{0}, \mathbf{\Sigma}_\beta)
  • y0y_0: Initial observed value.
  • σy2\sigma_y^2: Variance of the initial level.
  • σsk2\sigma_{s_k}^2: Variance of the initial seasonal component kk.
  • Σβ\mathbf{\Sigma}_\beta: Covariance matrix for the initial regression coefficients.

4. Bayesian Inference via Gibbs Sampling

Gibbs Sampling is a Markov Chain Monte Carlo (MCMC) method used to sample from the joint posterior distribution of model parameters and latent states.

4.1. Posterior Distribution

The objective is to sample from the joint posterior distribution:

p(x1:T,θ∣y1:T)p(\mathbf{x}_{1:T}, \boldsymbol{\theta} \mid \mathbf{y}_{1:T})

Where:

  • x1:T\mathbf{x}_{1:T}: State vectors from time 11 to TT.
  • θ\boldsymbol{\theta}: Model parameters (e.g., σℓ2,σε2,Λ\sigma_\ell^2, \sigma_\varepsilon^2, \mathbf{\Lambda}).
  • y1:T\mathbf{y}_{1:T}: Observed data from time 11 to TT.

Using Bayes’ theorem:

p(x1:T,θ∣y)∝p(y∣x,θ)⋅p(x1:T∣θ)⋅p(θ)p(\mathbf{x}_{1:T}, \boldsymbol{\theta} \mid \mathbf{y}) \propto p(\mathbf{y} \mid \mathbf{x}, \boldsymbol{\theta}) \cdot p(\mathbf{x}_{1:T} \mid \boldsymbol{\theta}) \cdot p(\boldsymbol{\theta})

4.2. Gibbs Sampling Steps

Gibbs Sampling iteratively samples each parameter conditioned on the current values of all other parameters.

Step 1: Sample Local Level Variance (σℓ2\sigma_\ell^2)

σℓ2∣x1:T,y1:T∼Inverse-Gamma(αℓ∗,βℓ∗)\sigma_\ell^2 \mid \mathbf{x}_{1:T}, \mathbf{y}_{1:T} \sim \text{Inverse-Gamma}\left(\alpha_\ell^*, \beta_\ell^*\right)

Where:

αℓ∗=αℓ+T2\alpha_\ell^* = \alpha_\ell + \frac{T}{2} βℓ∗=βℓ+12∑t=1T(ℓt−ℓt−1)2\beta_\ell^* = \beta_\ell + \frac{1}{2} \sum_{t=1}^T (\ell_t - \ell_{t-1})^2

Step 2: Sample Observation Noise Variance (σε2\sigma_\varepsilon^2)

σε2∣x1:T,y1:T∼Inverse-Gamma(αε∗,βε∗)\sigma_\varepsilon^2 \mid \mathbf{x}_{1:T}, \mathbf{y}_{1:T} \sim \text{Inverse-Gamma}\left(\alpha_\varepsilon^*, \beta_\varepsilon^*\right)

Where:

αε∗=αε+T2\alpha_\varepsilon^* = \alpha_\varepsilon + \frac{T}{2} βε∗=βε+12∑t=1T(yt−F⊤xt)2\beta_\varepsilon^* = \beta_\varepsilon + \frac{1}{2} \sum_{t=1}^T (y_t - \mathbf{F}^\top \mathbf{x}_t)^2

Step 3: Sample Regression Weights (β\boldsymbol{\beta})

Assuming time-invariant regression coefficients:

β∣x1:T,y1:T,σε2∼N(m,V)\boldsymbol{\beta} \mid \mathbf{x}_{1:T}, \mathbf{y}_{1:T}, \sigma_\varepsilon^2 \sim \mathcal{N}\left(\mathbf{m}, \mathbf{V}\right)

Where:

V=(Λ+X⊤Xσε2)−1\mathbf{V} = \left(\mathbf{\Lambda} + \frac{\mathbf{X}^\top \mathbf{X}}{\sigma_\varepsilon^2}\right)^{-1} m=V(X⊤yσε2)\mathbf{m} = \mathbf{V} \left(\frac{\mathbf{X}^\top \mathbf{y}}{\sigma_\varepsilon^2}\right)

Step 4: Sample State Vectors (x1:T\mathbf{x}_{1:T})

Utilize Forward-Backward Sampling or similar algorithms to sample the latent states given current parameter estimates and observed data.

4.3. Multiple MCMC Chains

To ensure convergence and robustness:

  • Run Multiple Gibbs Chains (CC chains): Each with different initializations.
  • Combine Samples Across Chains: Aggregate after convergence to form the posterior distribution.

5. Posterior Predictive Inference

With posterior samples, derive predictions for the counterfactual scenario (y^t\hat{y}_t) and assess the impact of the intervention.

5.1. Posterior Means

For each time tt, the posterior mean prediction is:

y^t=E[yt∣y1:T]\hat{y}_t = \mathbb{E}[y_t \mid \mathbf{y}_{1:T}]

In matrix form:

y^=F⊤xt\hat{\mathbf{y}} = \mathbf{F}^\top \mathbf{x}_t

5.2. Credible Intervals

Compute the α\alpha-credible intervals (e.g., 95%) for y^t\hat{y}_t:

y^t(q)=Quantile(y^t,q),q∈{α2,1−α2}\hat{y}_t^{(q)} = \text{Quantile}\left(\hat{y}_t, q\right), \quad q \in \left\{ \frac{\alpha}{2}, 1 - \frac{\alpha}{2} \right\}

6. Causal Effect Estimation

Assess the intervention’s impact by comparing observed data with model predictions.

6.1. Point Effects

The immediate difference at time tt:

Point Effectt=yt−y^t\text{Point Effect}_t = y_t - \hat{y}_t

6.2. Cumulative Effects

Total impact from intervention start TstartT_{\text{start}} to time tt:

Cumulative Effectt=∑τ=Tstartt(yτ−y^τ)\text{Cumulative Effect}_t = \sum_{\tau=T_{\text{start}}}^{t} (y_\tau - \hat{y}_\tau)

6.3. Summary Statistics

Over the post-intervention period TpostT_{\text{post}}:

  • Average Predicted Outcome:

    yˉpred=1N∑t∈Tposty^t\bar{y}_{\text{pred}} = \frac{1}{N} \sum_{t \in T_{\text{post}}} \hat{y}_t
  • Cumulative Predicted Outcome:

    Ypred=∑t∈Tposty^tY_{\text{pred}} = \sum_{t \in T_{\text{post}}} \hat{y}_t
  • Absolute Effect:

    Absolute Effect=yˉobs−yˉpred\text{Absolute Effect} = \bar{y}_{\text{obs}} - \bar{y}_{\text{pred}}
  • Relative Effect:

    Relative Effect=yˉobsyˉpred−1\text{Relative Effect} = \frac{\bar{y}_{\text{obs}}}{\bar{y}_{\text{pred}}} - 1
  • P-value Calculation:

    p-value=min⁡(#(ypred(s)≥yobs)S,#(ypred(s)≤yobs)S)p\text{-value} = \min\left( \frac{\#(y_{\text{pred}}^{(s)} \geq y_{\text{obs}})}{S}, \frac{\#(y_{\text{pred}}^{(s)} \leq y_{\text{obs}})}{S} \right)

    Where SS is the total number of posterior samples.


7. Matrix Operations and Linear Algebra

Efficient computation and representation of the STS model rely heavily on matrix operations.

7.1. State Transition Matrix (G\mathbf{G})

G=[10…00⊤01…00⊤⋮⋮⋱⋮⋮00…10⊤00…0I]\mathbf{G} = \begin{bmatrix} 1 & 0 & \dots & 0 & \mathbf{0}^\top \\ 0 & 1 & \dots & 0 & \mathbf{0}^\top \\ \vdots & \vdots & \ddots & \vdots & \vdots \\ 0 & 0 & \dots & 1 & \mathbf{0}^\top \\ \mathbf{0} & \mathbf{0} & \dots & \mathbf{0} & \mathbf{I} \end{bmatrix}
  • Diagonal elements set to 1 for identity transitions.
  • Off-diagonal elements are 0, except for potential seasonal dependencies.

7.2. Observation Matrix (F\mathbf{F})

F⊤=[10KXt⊤]\mathbf{F}^\top = \begin{bmatrix} 1 \\ \mathbf{0}_{K} \\ \mathbf{X}_t^\top \\ \end{bmatrix}
  • Incorporates the local level and seasonal components directly.
  • Includes regression coefficients via Xt⊤\mathbf{X}_t^\top.

7.3. Covariance Matrices (W\mathbf{W})

W=[σℓ20…00σs12I…0⋮⋮⋱⋮00…Σβ]\mathbf{W} = \begin{bmatrix} \sigma_\ell^2 & \mathbf{0} & \dots & \mathbf{0} \\ \mathbf{0} & \sigma_{s_1}^2 \mathbf{I} & \dots & \mathbf{0} \\ \vdots & \vdots & \ddots & \vdots \\ \mathbf{0} & \mathbf{0} & \dots & \mathbf{\Sigma}_\beta \end{bmatrix}
  • Diagonal matrix with variances for each state component.
  • Σβ\mathbf{\Sigma}_\beta represents the covariance matrix for regression weights.

7.4. Precision Matrix for Regression Weights (Λ\mathbf{\Lambda})

Λ=0.01×0.5×(X⊤X)N\mathbf{\Lambda} = 0.01 \times \frac{0.5 \times (\mathbf{X}^\top \mathbf{X})}{N}
  • Derived from the design matrix X\mathbf{X} (covariates).
  • Controls the prior variance of regression weights.

7.5. Likelihood Function

For the entire dataset, the likelihood is:

p(y∣x,θ)=∏t=1TN(yt∣F⊤xt,σε2)p(\mathbf{y} \mid \mathbf{x}, \boldsymbol{\theta}) = \prod_{t=1}^T \mathcal{N}(y_t \mid \mathbf{F}^\top \mathbf{x}_t, \sigma_\varepsilon^2)

7.6. Posterior Distribution

Using Bayes’ theorem:

p(x1:T,θ∣y)∝p(y∣x,θ)⋅p(x1:T∣θ)⋅p(θ)p(\mathbf{x}_{1:T}, \boldsymbol{\theta} \mid \mathbf{y}) \propto p(\mathbf{y} \mid \mathbf{x}, \boldsymbol{\theta}) \cdot p(\mathbf{x}_{1:T} \mid \boldsymbol{\theta}) \cdot p(\boldsymbol{\theta})

Where:

  • p(y∣x,θ)p(\mathbf{y} \mid \mathbf{x}, \boldsymbol{\theta}): Likelihood.
  • p(x1:T∣θ)p(\mathbf{x}_{1:T} \mid \boldsymbol{\theta}): Prior on states.
  • p(θ)p(\boldsymbol{\theta}): Priors on parameters.

8. Data Standardization and Scaling

Proper data preprocessing ensures that the model accurately captures patterns without being skewed by varying scales.

8.1. Standardizing Data

yt′=yt−μyσyy_t' = \frac{y_t - \mu_y}{\sigma_y}
  • μy\mu_y: Mean of the pre-intervention data.
  • σy\sigma_y: Standard deviation of the pre-intervention data.

8.2. Scaling Priors

  • Level Scale (σℓ\sigma_\ell):

    σℓ=prior_level_sd×σy\sigma_\ell = \text{prior\_level\_sd} \times \sigma_y
  • Seasonal Drift Scales (σs\sigma_s):

    σs=0.01×σy\sigma_s = 0.01 \times \sigma_y

9. Seasonal Effects Handling

Seasonality is a common feature in time-series data, representing periodic fluctuations.

9.1. Seasonal Components (sk,ts_{k,t})

  • Number of Seasons (mm): Defines the periodicity (e.g., m=12m=12 for monthly data with yearly seasonality).
  • Steps per Season (nn): Granularity within each season (e.g., weekly steps within a yearly cycle).

9.2. Seasonal Drift (σsk\sigma_{s_k})

Allows seasonal trends to gradually change over time:

sk,t=sk,t−m+ϵk,t,ϵk,t∼N(0,σsk2)s_{k,t} = s_{k,t-m} + \epsilon_{k,t}, \quad \epsilon_{k,t} \sim \mathcal{N}(0, \sigma_{s_k}^2)

10. Summary

A CausalImpact implementation is a Bayesian STS model with these pieces:

  1. State-space model: the dynamics of latent states, local level, seasonal components, and regression coefficients, that drive the observed data.
  2. Priors: Inverse-Gamma priors for variances, Normal priors for regression weights and initial states, which bring in domain knowledge and regularize the fit.
  3. Gibbs sampling: iterative sampling from each parameter’s conditional posterior, converging to the joint posterior over parameters and latent states.
  4. Posterior predictive inference: posterior mean predictions and credible intervals for the counterfactual.
  5. Causal effect estimation: point and cumulative effects from comparing observed data against the counterfactual.
  6. Matrix operations: the linear algebra that makes state transitions, observations, and parameter updates tractable at scale.

Written out in full mathematical detail like this, the model is easier to extend, whether that means adding covariates, changing the seasonal structure, or swapping in a different sampler.


11. Appendix

Derivation of Sampling the Local Level Variance (σℓ2\sigma_\ell^2)

This section provides a detailed mathematical derivation of the sampling step for the Local Level Variance (σℓ2\sigma_\ell^2) within the Gibbs Sampling procedure.

1. Model Setup

1.1. State Transition Equation

The evolution of the Local Level component is given by:

ℓt=ℓt−1+ηt,ηt∼N(0,σℓ2)\ell_t = \ell_{t-1} + \eta_t, \quad \eta_t \sim \mathcal{N}(0, \sigma_\ell^2)
1.2. Prior for σℓ2\sigma_\ell^2

An Inverse-Gamma prior is assumed:

σℓ2∼Inverse-Gamma(αℓ,βℓ)\sigma_\ell^2 \sim \text{Inverse-Gamma}(\alpha_\ell, \beta_\ell)

2. Likelihood Function

Given the state transition, the likelihood of observed states {ℓt}t=1T\{\ell_t\}_{t=1}^T is:

p({ℓt}t=1T∣{ℓt−1}t=1T,σℓ2)=∏t=1TN(ℓt∣ℓt−1,σℓ2)p(\{\ell_t\}_{t=1}^T \mid \{\ell_{t-1}\}_{t=1}^T, \sigma_\ell^2) = \prod_{t=1}^T \mathcal{N}(\ell_t \mid \ell_{t-1}, \sigma_\ell^2)

Expanding the Normal density:

p({ℓt}t=1T∣σℓ2)=(2πσℓ2)−T/2exp⁡(−12σℓ2∑t=1T(ℓt−ℓt−1)2)p(\{\ell_t\}_{t=1}^T \mid \sigma_\ell^2) = (2\pi \sigma_\ell^2)^{-T/2} \exp\left( -\frac{1}{2\sigma_\ell^2} \sum_{t=1}^T (\ell_t - \ell_{t-1})^2 \right)

3. Posterior Distribution

Applying Bayes’ theorem:

p(σℓ2∣data)∝p(data∣σℓ2)⋅p(σℓ2)p(\sigma_\ell^2 \mid \text{data}) \propto p(\text{data} \mid \sigma_\ell^2) \cdot p(\sigma_\ell^2)

Substituting the likelihood and prior:

p(σℓ2∣data)∝(σℓ2)−T/2exp⁡(−12σℓ2∑t=1T(ℓt−ℓt−1)2)⋅(σℓ2)−αℓ−1exp⁡(−βℓσℓ2)p(\sigma_\ell^2 \mid \text{data}) \propto (\sigma_\ell^2)^{-T/2} \exp\left( -\frac{1}{2\sigma_\ell^2} \sum_{t=1}^T (\ell_t - \ell_{t-1})^2 \right) \cdot (\sigma_\ell^2)^{-\alpha_\ell -1} \exp\left( -\frac{\beta_\ell}{\sigma_\ell^2} \right)

Combining like terms:

p(σℓ2∣data)∝(σℓ2)−(αℓ+T2+1)exp⁡(−1σℓ2(12∑t=1T(ℓt−ℓt−1)2+βℓ))p(\sigma_\ell^2 \mid \text{data}) \propto (\sigma_\ell^2)^{-\left(\alpha_\ell + \frac{T}{2} + 1\right)} \exp\left( -\frac{1}{\sigma_\ell^2} \left( \frac{1}{2} \sum_{t=1}^T (\ell_t - \ell_{t-1})^2 + \beta_\ell \right) \right)

4. Identifying the Posterior Distribution

Recognizing the form of the Inverse-Gamma distribution:

Inverse-Gamma(x∣α′,β′)=β′α′Γ(α′)x−α′−1exp⁡(−β′x)\text{Inverse-Gamma}(x \mid \alpha', \beta') = \frac{{\beta'}^{\alpha'}}{\Gamma(\alpha')} x^{-\alpha' -1} \exp\left( -\frac{\beta'}{x} \right)

We identify the posterior parameters:

αℓ∗=αℓ+T2\alpha_\ell^* = \alpha_\ell + \frac{T}{2} βℓ∗=βℓ+12∑t=1T(ℓt−ℓt−1)2\beta_\ell^* = \beta_\ell + \frac{1}{2} \sum_{t=1}^T (\ell_t - \ell_{t-1})^2

Thus, the conditional posterior is:

σℓ2∣data∼Inverse-Gamma(αℓ∗,βℓ∗)\sigma_\ell^2 \mid \text{data} \sim \text{Inverse-Gamma}(\alpha_\ell^*, \beta_\ell^*)

Summary of the Sampling Step:

σℓ2∣data∼Inverse-Gamma(αℓ+T2,βℓ+12∑t=1T(ℓt−ℓt−1)2)\sigma_\ell^2 \mid \text{data} \sim \text{Inverse-Gamma}\left(\alpha_\ell + \frac{T}{2}, \beta_\ell + \frac{1}{2} \sum_{t=1}^T (\ell_t - \ell_{t-1})^2 \right)

This conjugate relationship between the Normal likelihood and the Inverse-Gamma prior facilitates efficient Gibbs Sampling, enabling straightforward updates of σℓ2\sigma_\ell^2 in each iteration.


References:

  • Harvey, A. C. (1990). Forecasting, Structural Time Series Models and the Kalman Filter. Cambridge University Press.
  • Bishop, C. M. (2006). Pattern Recognition and Machine Learning. Springer.
  • Gelman, A., et al. (2013). Bayesian Data Analysis (3rd ed.). CRC Press.

If you are putting a model like this into production, the next steps below cover the data and observability layer, and the newsletter is where the production playbook for generative and statistical models ships first.


Next steps: scaling to production

If you take this into production, these are the pieces I would add first.

  • Supabase Supabase is a hosted Postgres platform with authentication and storage built in. Postgres with pgvector for embeddings, so you do not run a separate vector store.
  • Datadog Datadog aggregates metrics, logs, and traces for infrastructure monitoring. Traces and cost metrics across model calls, so latency and spend are visible per request.
  • Vercel Vercel hosts frontend applications with a global edge network and CI/CD. Deploys the frontend and edge functions that sit in front of the model API.

Deploying generative AI models to production

Get the free playbook on shipping generative AI models to production.