# Particle filter Prediction using pymc3

**URL:** <https://discourse.pymc.io/t/particle-filter-prediction-using-pymc3/6122>\
**Category:** Questions\
**Created:** [October 27, 2020, 9:13am UTC](https://discourse.pymc.io/t/particle-filter-prediction-using-pymc3/6122 "2020-10-27T09:13:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![VIDYA](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/vidya/32/3314_2.png) [@VIDYA](https://discourse.pymc.io/u/VIDYA)\
**Post date:** [October 27, 2020, 9:13am UTC](https://discourse.pymc.io/t/particle-filter-prediction-using-pymc3/6122/1 "2020-10-27T09:13:30Z")

</div>

Hi all,

I’m trying to implement a particle filter for time series data prediction.(traffic data)

The code I used for implementing it is as shown below:

```python
####function to update prior using previous posterior

def from_posterior(param, samples):
   smin, smax = np.min(samples), np.max(samples)
    width = smax - smin
    x = np.linspace(smin, smax, 100)
    y = stats.gaussian_kde(samples)(x)

    # what was never sampled should have a small probability but not 0,
    # so we'll extend the domain and use linear approximation of density on it
    x = np.concatenate([[x[0] - 3 * width], x, [x[-1] + 3 * width]])
    y = np.concatenate([[0], y, [0]])
    return Interpolated(param, x, y)

##############################################
########
##Model building#######
###########################

####Bayesian Model Building#####
n=30
data=fulldata[0:n]
model_bay = pm.Model() 
with model_bay: 
# Prior distribution for mu. 
	mu = np.mean(data) 
# Prior distribution for sigma2. 
	sigma = np.std(data) 
#Parametrization for the shape parameter. 
	alpha = mu **2/sigma** 2 
# Parametrization for the scale parameter. 
	beta = mu/sigma**2 
# Prior distribution for lambda. 
	lam = pm.Gamma('lam',alpha=alpha,beta=beta) 
# Likelihood function for the data. 
	passngr = pm.Poisson('passngr',lam,observed=data)  
	trace=pm.sample()  
	y_predb = pm.sample_posterior_predictive(trace)	#no of draws x no of chains

#taking samples
	data_predb = az.from_pymc3(trace=trace, posterior_predictive=y_predb)                   

##################################
###########Sequential Monte Carlo
###Updating prior using the posterior data

model_part = pm.Model()  
with model_part:  
# Prior distribution for mu.  
	mu = theano.shared(mu)  
# Prir distribution for sigma2.  
	sigma = theano.shared(sigma)  
	#Parametrization for the shape parameter.  
	alpha = mu **2/sigma** 2  
#Parametrization for the scale parameter.  
	beta = mu/sigma**2  
# Prior distribution for lambda.  
	lam = from_posterior('lam',data_predb['posterior']['lam'][0])  
# Likelihood function for the data.  
	passngr = pm.Poisson('passngr',lam,observed=data)   
	tracenew=pm.sample()     
       

	az_trace = az.from_pymc3(trace=tracenew)
#predicting data using posteriorn
	y_pred = pm.sample_posterior_predictive(tracenew)	#no of draws x no of chains

#taking samples
	data_pred = az.from_pymc3(trace=tracenew, posterior_predictive=y_pred)

	az.plot_ppc(data_pred, figsize=(12, 6), mean=False)

	pm.autocorrplot(tracenew)
	pm.plot_posterior(tracenew) 
	az.plot_trace(az_trace, compact=True, kind="rank_vlines")

```

Is this implementation of particle filter correct?  
Please help me if any correction required

So what i felt is that the samples from y\_pred[‘passngr’] should be similar to the data that i’ve taken.

Is this logic correct?  
how can i predict the data using this model?  
Ie if I’ve the data y(t-1), how can i predict y(t)

Please help.I’m trying to solve this out for months.Please please help me to sort this out

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [October 27, 2020, 9:28am UTC](https://discourse.pymc.io/t/particle-filter-prediction-using-pymc3/6122/2 "2020-10-27T09:28:19Z")

</div>

What you wrote down is more similar to the sequential update in [https://docs.pymc.io/notebooks/updating\_priors.html](https://docs.pymc.io/notebooks/updating_priors.html), which is fine in itself, but it is not exactly Particle filtering, which usually contains weighted (re)sample / importance sampling of the particles.  
Depending on what you want to do, if you are trying to inference on some time series data, I recommend taking a look at [https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python](https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python) instead. PyMC3 currently does not have a good support for Particle filtering for sequence data.

---

<div class="post-metadata">

**Author:** ![VIDYA](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/vidya/32/3314_2.png) [@VIDYA](https://discourse.pymc.io/u/VIDYA)\
**Post date:** [October 27, 2020, 9:57am UTC](https://discourse.pymc.io/t/particle-filter-prediction-using-pymc3/6122/3 "2020-10-27T09:57:14Z")

</div>

Thank you for your fast reply  
Ok, let me check that
