---
id: wiki-2026-0508-kalman-filter-and-state-tracking
title: Kalman Filter and State Tracking
category: 10_Wiki/Topics
status: verified
canonical_id: self
aliases: [KF, EKF, UKF, Bayesian Tracking]
duplicate_of: none
source_trust_level: A
confidence_score: 0.95
verification_status: applied
tags: [filter, state-estimation, bayesian, control]
raw_sources: []
last_reinforced: 2026-05-10
github_commit: pending
tech_stack:
  language: python
  framework: filterpy
---

# Kalman Filter and State Tracking

## 매 한 줄
> **"매 noisy observation 매 hidden state 의 optimal Bayesian estimate"**. Kalman filter 매 1960 Rudolf Kálmán — 매 linear-Gaussian system 의 closed-form recursive Bayesian update. 매 2026 SLAM, sensor fusion, AV path-planning, HFT spread tracking, GPS, IMU 매 ubiquitous.

## 매 핵심

### 매 model
- **State**: $x_k = F x_{k-1} + B u_k + w_k$, $w \sim N(0, Q)$.
- **Observation**: $z_k = H x_k + v_k$, $v \sim N(0, R)$.
- **Predict**: $\hat{x}_k^- = F\hat{x}_{k-1}$, $P_k^- = FP_{k-1}F^T + Q$.
- **Update**: $K = P^-H^T(HP^-H^T + R)^{-1}$, $\hat{x} = \hat{x}^- + K(z - H\hat{x}^-)$.

### 매 variant
- **Linear KF**: 매 linear F, H, Gaussian noise.
- **EKF (Extended)**: 매 Jacobian linearization — 매 SLAM 매 standard.
- **UKF (Unscented)**: 매 sigma-point — 매 high non-linearity.
- **Particle Filter**: 매 fully non-linear, non-Gaussian — 매 multi-modal.
- **IMM (Interacting Multiple Model)**: 매 mode switching (radar tracking).

### 매 응용
1. 매 GPS + IMU sensor fusion (smartphone, drone).
2. 매 SLAM (autonomous vehicle, robot).
3. 매 finance — pair-trade hedge ratio drift.
4. 매 AR/VR head-pose prediction.

## 💻 패턴

### 1. Vanilla Linear KF (numpy)
```python
import numpy as np

class KF:
    def __init__(self, F, H, Q, R, x0, P0):
        self.F, self.H, self.Q, self.R = F, H, Q, R
        self.x, self.P = x0, P0

    def predict(self, u=0, B=None):
        self.x = self.F @ self.x + (B @ u if B is not None else 0)
        self.P = self.F @ self.P @ self.F.T + self.Q

    def update(self, z):
        y = z - self.H @ self.x  # innovation
        S = self.H @ self.P @ self.H.T + self.R
        K = self.P @ self.H.T @ np.linalg.inv(S)
        self.x = self.x + K @ y
        self.P = (np.eye(len(self.x)) - K @ self.H) @ self.P
```

### 2. Constant-Velocity Model
```python
dt = 0.1
F = np.array([[1, dt], [0, 1]])      # [pos, vel]
H = np.array([[1, 0]])                # observe pos only
Q = np.array([[0.001, 0], [0, 0.01]])
R = np.array([[0.5]])
kf = KF(F, H, Q, R, np.array([0,0]), np.eye(2))
for z in measurements:
    kf.predict()
    kf.update(np.array([z]))
```

### 3. Extended KF (Non-linear)
```python
def ekf_update(x, P, z, h, H_jac, R):
    H = H_jac(x)
    y = z - h(x)
    S = H @ P @ H.T + R
    K = P @ H.T @ np.linalg.inv(S)
    x = x + K @ y
    P = (np.eye(len(x)) - K @ H) @ P
    return x, P
```

### 4. Unscented KF (sigma-points)
```python
from filterpy.kalman import UnscentedKalmanFilter, MerweScaledSigmaPoints
sp = MerweScaledSigmaPoints(n=4, alpha=0.1, beta=2, kappa=-1)
ukf = UnscentedKalmanFilter(dim_x=4, dim_z=2, dt=0.1, fx=fx, hx=hx, points=sp)
```

### 5. Particle Filter
```python
def particle_filter(particles, weights, z, motion, observe, R):
    particles = motion(particles)  # propagate
    weights *= observe(particles, z, R)
    weights /= weights.sum()
    if 1.0 / np.sum(weights**2) < len(particles)/2:
        idx = np.random.choice(len(particles), size=len(particles), p=weights)
        particles, weights = particles[idx], np.ones_like(weights)/len(weights)
    return particles, weights
```

### 6. Sensor Fusion (GPS + IMU)
```python
# 매 IMU prediction (high freq), GPS update (low freq)
while True:
    imu = read_imu()        # 200 Hz
    kf.predict_with_imu(imu)
    if gps_available():     # 10 Hz
        gps = read_gps()
        kf.update(gps)
```

### 7. Smoothing (RTS)
```python
# 매 forward filter → backward smooth — 매 offline analysis
xs, Ps = filter_forward(zs)
xs_sm, Ps_sm = rts_smoother(xs, Ps, F, Q)  # 매 future info 활용
```

## 매 결정 기준
| 상황 | Filter |
|---|---|
| Linear, Gaussian | Linear KF |
| Mild non-linearity | EKF |
| Strong non-linearity | UKF |
| Multi-modal / non-Gaussian | Particle Filter |
| Mode switching | IMM |
| Offline analysis | RTS smoother |

**기본값**: 매 EKF + constant-velocity, 매 sensor fusion 매 standard starting point.

## 🔗 Graph
- 부모: [[Probability Theory]] · [[Optimal-Control-Theory]] · [[Linear-Algebra-Foundations]]
- 변형: [[Particle-Filter-Algorithms]]
- 응용: [[Autonomous-Vehicle-Path-Planning]] · [[High-Frequency-Trading-Models]]
- Adjacent: [[Bayesian-Inference]] · [[Signal-Processing-Foundations]] · [[Gimbals-and-Orientation]]

## 🤖 LLM 활용
**언제**: 매 sensor fusion architecture 설계, 매 noise model tuning 가이드, 매 SLAM debugging.
**언제 X**: 매 highly non-Gaussian (use particle), 매 deterministic system.

## ❌ 안티패턴
- **Q, R untuned**: 매 filter divergence 또는 over-smoothing.
- **EKF on heavy non-linearity**: 매 Jacobian 부정확 → divergence. 매 UKF 또는 PF.
- **No covariance inflation**: 매 P → 0 → filter overconfident.
- **Joseph form 무시**: 매 numerical loss of symmetry.

## 🧪 검증 / 중복
- Verified (Welch & Bishop, *KF Intro*; Thrun et al., *Probabilistic Robotics*; filterpy docs).
- 신뢰도 A.

## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — full KF/EKF/UKF/PF + sensor fusion patterns |
