Kalman Filter
🔥 Vibe Prompt
"Track a vehicle's position and velocity from noisy GPS (σ=30m) and IMU (σ=20m). Compare raw vs filtered."
import numpy as np
class KalmanFilter:
def __init__(self):
self.x = np.array([[0], [0], [0], [0]]) # pos_x, pos_y, vel_x, vel_y
self.P = np.eye(4) * 100
self.F = np.array([[1,0,1,0],[0,1,0,1],[0,0,1,0],[0,0,0,1]])
self.H = np.array([[1,0,0,0],[0,1,0,0]])
self.R = np.eye(2) * 30 # GPS noise
self.Q = np.eye(4) * 0.1
def predict(self):
self.x = self.F @ self.x
self.P = self.F @ self.P @ self.F.T + self.Q
def update(self, z):
y = z - self.H @ self.x
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(4) - K @ self.H) @ self.P
kf = KalmanFilter()
true_pos = np.array([100, 100])
true_vel = np.array([5, 3])
for t in range(20):
true_pos = true_pos + true_vel # True movement
gps = true_pos + np.random.normal(0, 30, 2) # Noisy GPS
kf.predict()
kf.update(gps)
error_raw = np.linalg.norm(gps - true_pos)
error_filt = np.linalg.norm(kf.x[:2].flatten() - true_pos)
print(f"t={t}: raw_error={error_raw:.1f}, filt_error={error_filt:.1f}")
Summary
| Aspect | Detail | |--------|--------| | State | What we're tracking (position, velocity, etc.) | | Predict | Motion model (F) + process noise (Q) | | Update | Measurement (z) + measurement noise (R) | | Kalman Gain (K) | Balances trust between prediction and measurement | | Extensions | EKF (non-linear), UKF (unscented), particle filter (non-Gaussian) |
Applications
| Domain | Application | |--------|-------------| | 🛰️ GPS/IMU | Drone, self-driving car, smartphone position | | 🚀 Aerospace | Rocket guidance, satellite orbit determination | | 🤖 Robotics | SLAM (Simultaneous Localization And Mapping) | | 📈 Finance | Hidden Markov models for regime detection | | 🫀 Medical | ECG denoising, blood glucose monitoring |
Gradient Descent Course Complete! 🎉
- ✅ Gradient Descent Basics
- ✅ Momentum & Adam
- ✅ FFT / DFT
- ✅ PID Control
- ✅ Kalmanum & Adam\n- ✅ FFT / DFT\n- ✅ PID Control\n- ✅ Kalman Filter"}]}
Key Points
- Understand the core concepts thoroughly
- Practice with hands-on code examples
- Apply knowledge to real-world problems
- Review and reinforce through exercises
Further Learning
- Official documentation
- Open source projects on GitHub
- Community forums and discussions
- Related courses and tutorials
梯度下降實戰要點
梯度下降是機器學習中最核心的最佳化演算法。理解它的變體(SGD、Momentum、Adam)對於訓練深度學習模型至關重要。
核心概念
- 梯度方向是函數增加最快的方向
- 學習率決定每一步的大小
- SGD 用隨機樣本近似梯度
- Momentum 加速收斂
- Adam 結合 Momentum + RMSProp
卡爾曼濾波:從雜訊中過濾出真相
GPS 訊號有誤差(5-15 公尺)、速度感測器有雜訊。卡爾曼濾波結合不完美的資訊,估計出比任何單一感測器都準確的狀態。
預測-更新循環
預測:根據物理模型預測下一時刻狀態
計算預測的不確定性
更新:取得感測器讀數
計算 Kalman Gain(相信預測 vs 相信感測器)
用感測器讀數修正預測
與梯度下降的關聯
Kalman Filter 可以視為「線上版本的梯度下降」——每次有新資料就更新估計。用在自駕車、機器人定位、金融時間序列。
課程總結
這堂課從基本梯度下降到 Kalman Filter——涵蓋了最佳化和控制領域的核心演算法。