@@ -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