Particle filter Prediction using pymc3

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:

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

What you wrote down is more similar to the sequential update in 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 instead. PyMC3 currently does not have a good support for Particle filtering for sequence data.

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