Skip to content

Commit b800124

Browse files
committed
updated python pf to fully-adapted for LGSS model
Former-commit-id: b762a17 [formerly b762a17 [formerly 0dbd426]] Former-commit-id: b4796563e982ee43a6381ba2282ffb458d56df10 Former-commit-id: d227e5c
1 parent 87f4b3b commit b800124

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

python/example1-lgss.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,11 @@
8888
# Using the Kalman filter
8989
xHatFilteredKalmanFilter = kalmanFilter(y, theta, initialState, 0.01)
9090

91-
9291
plt.subplot(3, 1, 3)
93-
plt.plot(xHatFilteredKalmanFilter - xHatFilteredParticleFilter, color='#7570B3', linewidth=1.5)
92+
plt.plot(xHatFilteredKalmanFilter[1:T] - xHatFilteredParticleFilter[0:T-1], color='#7570B3', linewidth=1.5)
9493
plt.xlabel("time")
9594
plt.ylabel("difference in estimate")
96-
95+
plt.show()
9796

9897
##############################################################################
9998
# End of file

python/helpers/stateEstimation.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def particleFilter(y, theta, noParticles, initialState):
133133
#=====================================================================
134134
# Run main loop
135135
#=====================================================================
136-
for t in range(1, T):
136+
for t in range(1, T - 1):
137137

138138
#=============================================================
139139
# Resample particles
@@ -149,14 +149,19 @@ def particleFilter(y, theta, noParticles, initialState):
149149
#=============================================================
150150
# Propagate particles
151151
#=============================================================
152-
particles[:, t] = theta[0] * particles[newAncestors, t - 1] + theta[1] * randn(1, noParticles)
152+
part1 = (theta[1]**(-2) + theta[2]**(-2))**(-1)
153+
part2 = theta[2]**(-2) * y[t]
154+
part2 = part2 + theta[1]**(-2) * theta[0] * particles[newAncestors, t - 1]
155+
particles[:, t] = part1 * part2 + np.sqrt(part1) * randn(1, noParticles)
153156

154157
#=================================================================
155158
# Weight particles
156159
#=================================================================
157160

158161
# Compute log-weights
159-
weights[:, t] = norm.logpdf(y[t - 1], particles[:, t], theta[2])
162+
yhatMean = theta[0] * particles[:, t]
163+
yhatVariance = np.sqrt(theta[1]**2 + theta[2]**2)
164+
weights[:, t] = norm.logpdf(y[t + 1], yhatMean, yhatVariance)
160165

161166
# Rescale log-weights and recover weights
162167
maxWeight = np.max(weights[:, t])

0 commit comments

Comments
 (0)