# Bayesian state-space movement
# https://tidyecology.com/posts/bayesian-state-space-movement/
#
# The R code of this Tidy Ecology tutorial, chunk by chunk, in the order of the post.
# Run it top to bottom in a fresh R session. Code the post shows but does not run
# is commented out. The prose, and the numbers it quotes, are in the post itself.
# Post last modified: 2026-10-15

## ---- sim ----
library(MASS)
set.seed(6142)
n <- 200L
gamma.true <- 0.7; sig.true <- 4; tau.true <- 6   # persistence, process SD, obs SD
x <- numeric(n); v <- numeric(n)
x[1] <- 0; v[1] <- rnorm(1, 0, sig.true / sqrt(1 - gamma.true^2))
for (t in 2:n) {
  x[t] <- x[t-1] + v[t-1]
  v[t] <- gamma.true * v[t-1] + rnorm(1, 0, sig.true)
}
y <- x + rnorm(n, 0, tau.true)                     # noisy observations

## ---- naive ----
path.true <- sum(abs(diff(x)))
path.naive <- sum(abs(diff(y)))

## ---- ffbs ----
Hm <- matrix(c(1, 0), 1, 2)                        # observe position only
ffbs <- function(y, gamma, sig2, tau2) {
  n <- length(y)
  A <- matrix(c(1, 0, 1, gamma), 2, 2)             # state transition
  Q <- matrix(c(0, 0, 0, sig2), 2, 2)              # process noise on velocity
  mp <- vector("list", n); Pp <- vector("list", n) # predicted moments
  mf <- vector("list", n); Pf <- vector("list", n) # filtered moments
  m0 <- c(y[1], 0); P0 <- diag(c(100, 100))
  for (t in 1:n) {
    if (t == 1) { mpred <- m0; Ppred <- P0 } else {
      mpred <- A %*% mf[[t-1]]; Ppred <- A %*% Pf[[t-1]] %*% t(A) + Q
    }
    mp[[t]] <- mpred; Pp[[t]] <- Ppred
    S <- Hm %*% Ppred %*% t(Hm) + tau2
    K <- Ppred %*% t(Hm) / as.numeric(S)
    mf[[t]] <- mpred + K %*% (y[t] - Hm %*% mpred)
    Pf[[t]] <- (diag(2) - K %*% Hm) %*% Ppred
    Pf[[t]] <- (Pf[[t]] + t(Pf[[t]])) / 2           # keep symmetric
  }
  s <- matrix(0, n, 2)
  s[n, ] <- mvrnorm(1, mf[[n]], Pf[[n]])
  for (t in (n-1):1) {
    J <- Pf[[t]] %*% t(A) %*% solve(Pp[[t+1]])
    mb <- mf[[t]] + J %*% (s[t+1, ] - mp[[t+1]])
    Pb <- Pf[[t]] - J %*% A %*% Pf[[t]]
    Pb <- (Pb + t(Pb)) / 2
    s[t, ] <- mvrnorm(1, mb, Pb)
  }
  s
}

## ---- gibbs ----
n.iter <- 4000L; burn <- 1000L
gam.s <- numeric(n.iter); sig.s <- numeric(n.iter); tau.s <- numeric(n.iter)
path.s <- numeric(n.iter); xstore <- matrix(0, n.iter, n)
gamma <- 0.5; sig2 <- var(diff(y)); tau2 <- var(diff(y)) / 2
for (it in 1:n.iter) {
  s <- ffbs(y, gamma, sig2, tau2)
  xs <- s[, 1]; vs <- s[, 2]
  # process variance from velocity innovations
  eta <- vs[2:n] - gamma * vs[1:(n-1)]
  sig2 <- 1 / rgamma(1, 0.001 + (n-1)/2, 0.001 + sum(eta^2)/2)
  # observation variance from residuals
  r <- y - xs
  tau2 <- 1 / rgamma(1, 0.001 + n/2, 0.001 + sum(r^2)/2)
  # persistence: Gaussian regression v_t on v_{t-1}, prior N(0, 100)
  vprec <- sum(vs[1:(n-1)]^2) / sig2 + 1/100
  vmean <- (sum(vs[1:(n-1)] * vs[2:n]) / sig2) / vprec
  gamma <- rnorm(1, vmean, sqrt(1/vprec))
  gam.s[it] <- gamma; sig.s[it] <- sqrt(sig2); tau.s[it] <- sqrt(tau2)
  path.s[it] <- sum(abs(diff(xs))); xstore[it, ] <- xs
}
keep <- (burn+1):n.iter
gam.p <- gam.s[keep]; sig.p <- sig.s[keep]; tau.p <- tau.s[keep]; path.p <- path.s[keep]

## ---- summary ----
gm <- median(gam.p); gci <- quantile(gam.p, c(.025, .975))
sm <- median(sig.p); sci <- quantile(sig.p, c(.025, .975))
tm <- median(tau.p); tci <- quantile(tau.p, c(.025, .975))
pm <- median(path.p); pci <- quantile(path.p, c(.025, .975))

## ---- fig-path ----
library(ggplot2)
ink <- "#16241d"; forest <- "#275139"; sage <- "#93a87f"; rust <- "#b5534e"; line <- "#dad9ca"
theme_te <- theme_minimal(base_size = 12) +
  theme(
        plot.background  = element_rect(fill = "#f5f4ee", colour = NA),
        panel.background = element_rect(fill = "#f5f4ee", colour = NA),
        panel.grid.minor = element_blank(),
        panel.grid.major = element_line(colour = line, linewidth = 0.3),
        plot.title = element_text(colour = ink, face = "bold", size = 13),
        plot.subtitle = element_text(colour = "#5d6b61", size = 10),
        axis.title = element_text(colour = ink), axis.text = element_text(colour = "#46604a"),
        legend.position = "none")
x.mean <- colMeans(xstore[keep, ])
x.lo <- apply(xstore[keep, ], 2, quantile, 0.025)
x.hi <- apply(xstore[keep, ], 2, quantile, 0.975)
dpath <- data.frame(t = 1:n, truex = x, obsy = y, mx = x.mean, lo = x.lo, hi = x.hi)
ggplot(dpath, aes(t)) +
  geom_ribbon(aes(ymin = lo, ymax = hi), fill = sage, alpha = 0.4) +
  geom_point(aes(y = obsy), colour = "#9aa39a", size = 0.7, alpha = 0.7) +
  geom_line(aes(y = truex), colour = rust, linewidth = 0.7) +
  geom_line(aes(y = mx), colour = forest, linewidth = 0.7) +
  labs(title = "Latent movement path recovered from noisy positions",
       subtitle = "Grey: raw observations. Red: true path. Green with band: posterior path.",
       x = "Time step", y = "Position") + theme_te

## ---- fig-ident ----
ggplot(data.frame(sigma = sig.p, tau = tau.p), aes(sigma, tau)) +
  geom_point(colour = forest, alpha = 0.15, size = 0.6) +
  geom_density_2d(colour = ink, linewidth = 0.3, bins = 6) +
  geom_point(aes(x = sig.true, y = tau.true), colour = rust, size = 3) +
  labs(title = "Joint posterior of process and observation noise",
       subtitle = "Negative correlation is the process-versus-observation trade-off. Red: truth.",
       x = "Process SD (sigma)", y = "Observation SD (tau)") + theme_te
