xtp(xt|xt−1) p(xt|x0:t−1,y1:t)
x~xw~wx
p(xt|y1:t)tt
সাধারণ মডেলটি বিবেচনা করুন:
Xt=Xt−1+ηt,ηt∼N(0,1)
X0∼N(0,1)
Yt=Xt+εt,εt∼N(0,1)
YXp(Xt|Y1,...,Yt)
Xt|Xt−1∼N(Xt−1,1)
X0∼N(0,1)
Yt|Xt∼N(Xt,1)
অ্যালগরিদম প্রয়োগ করা:
NX(i)0∼N(0,1)
X(i)1|X(i)0∼N(X(i)0,1)N
w~(i)t=ϕ(yt;x(i)t,1)ϕ(x;μ,σ2)μσ2yt
wtxx(i)0:t
আমরা পুরো সিরিজটি প্রক্রিয়া না করা অবধি, কণাগুলির পুনরায় মডেল করা সংস্করণটি নিয়ে এগিয়ে যাওয়া, দ্বিতীয় ধাপে ফিরে যান।
আর এর একটি বাস্তবায়ন নিম্নলিখিত:
# Simulate some fake data
set.seed(123)
tau <- 100
x <- cumsum(rnorm(tau))
y <- x + rnorm(tau)
# Begin particle filter
N <- 1000
x.pf <- matrix(rep(NA,(tau+1)*N),nrow=tau+1)
# 1. Initialize
x.pf[1, ] <- rnorm(N)
m <- rep(NA,tau)
for (t in 2:(tau+1)) {
# 2. Importance sampling step
x.pf[t, ] <- x.pf[t-1,] + rnorm(N)
#Likelihood
w.tilde <- dnorm(y[t-1], mean=x.pf[t, ])
#Normalize
w <- w.tilde/sum(w.tilde)
# NOTE: This step isn't part of your description of the algorithm, but I'm going to compute the mean
# of the particle distribution here to compare with the Kalman filter later. Note that this is done BEFORE resampling
m[t-1] <- sum(w*x.pf[t,])
# 3. Resampling step
s <- sample(1:N, size=N, replace=TRUE, prob=w)
# Note: resample WHOLE path, not just x.pf[t, ]
x.pf <- x.pf[, s]
}
plot(x)
lines(m,col="red")
# Let's do the Kalman filter to compare
library(dlm)
lines(dropFirst(dlmFilter(y, dlmModPoly(order=1))$m), col="blue")
legend("topleft", legend = c("Actual x", "Particle filter (mean)", "Kalman filter"), col=c("black","red","blue"), lwd=1)
ফলাফল গ্রাফ:
একটি দরকারী টিউটোরিয়াল হ'ল ডুসেট এবং জোহেনসেনের একটি, এখানে দেখুন ।