Comments (6)
Hi, sorry for the slow answer, I was taking holidays.
- please post a MRE (minimal reproducible example), so that I can better understand what you tried to do and how it failed.
- yes, but first, not all loops are slow, only looks with a "lean body" (when the body of the loop does very little computation). If, instead, you loop over something expensive, you are not going to lose much.
Anyway, particles makes it possible to run several particle filters in parallel (using all your CPU cores, instead of just one), to see how it works, have a look at module utils:
https://particles-sequential-monte-carlo-in-python.readthedocs.io/en/latest/_autosummary/particles.utils.html#module-particles.utils
and function multiSMC in the core module, which is illustrated in this tutorial:
https://particles-sequential-monte-carlo-in-python.readthedocs.io/en/latest/notebooks/advanced_tutorial_ssm.html#Running-many-particle-filters-in-one-go
from particles.
A minimal code would be:
class ToyModelWithMissingData(ssms.StateSpaceModel):
def PX0(self):
return dists.Normal(scale=self.sigmaX)
def PX(self, t, xp):
return dists.Normal(loc=xp, scale=self.sigmaX)
def PY(self, t, xp, x):
if t <= 10:
return dists.FlatNormal(loc=x)
else:
return dists.Normal(loc=x, scale=self.sigmaY)
Now, if I run the Bootstrap for an instance of the class (toy_model), and some simulated data:
from particles.collectors import Moments
fk_model = ssm.Bootstrap(ssm=toy_model, data=data)
pf = particles.SMC(
fk=fk_model, N=100, collect=[Moments()])
pf.run()
Then, everything gets populated by nans. Momentarily, I fixed it replacing FlatNormal
by Dirac(loc=np.zeros_like(x))
.
from particles.
Ok, I tried to fill in the blanks, in order to turn your pieces of code into an actual MRE, this is what I got, it seems to work for me?
import particles # was missing
from particles.collectors import Moments
from particles import distributions as dists # was missing
from particles import state_space_models as ssms # was missing
class ToyModelWithMissingData(ssms.StateSpaceModel):
def PX0(self):
return dists.Normal(scale=self.sigmaX)
def PX(self, t, xp):
return dists.Normal(loc=xp, scale=self.sigmaX)
def PY(self, t, xp, x):
if t <= 10:
return dists.FlatNormal(loc=x)
else:
return dists.Normal(loc=x, scale=self.sigmaY)
toy_model = ToyModelWithMissingData(sigmaX=0.5, sigmaY=0.1) # was missing
data = np.ones(30) # artificial data, was missing
fk_model = ssms.Bootstrap(ssm=toy_model, data=data) # fixed typo
pf = particles.SMC(fk=fk_model, N=100, collect=[Moments()])
pf.run()
print(pf.summaries.moments) # prints filtering mean/var at each time t (I don't get Nans)
from particles.
I was careless with my MRE. Now that I try to reproduce it, I realize that in my case states, data = toy_model.simulate(100)
, so it contains nans in the first 10 entries. In your data = np.ones(30)
there is no nan, and it also runs well for me.
from particles.
ok, this is an actual bug then, FlatNormal.logpdf should not return Nan when a data point is Nan.
I pushed a fix on the experimental branch. Let me know if this works for you. This issue will close automatically when the fix is propagated to the master branch.
from particles.
Thank you! It is running well, I tested the code
import matplotlib.pyplot as plt
import particles
from particles.collectors import Moments
from particles import distributions as dists
from particles import state_space_models as ssms
class ToyModelWithMissingData(ssms.StateSpaceModel):
def PX0(self):
return dists.Normal(scale=self.sigmaX)
def PX(self, t, xp):
return dists.Normal(loc=xp, scale=self.sigmaX)
def PY(self, t, xp, x):
if t <= 10:
return dists.FlatNormal(loc=x)
else:
return dists.Normal(loc=x, scale=self.sigmaY)
toy_model = ToyModelWithMissingData(sigmaX=0.5, sigmaY=0.1)
states, data = toy_model.simulate(100)
fk_model = ssms.Bootstrap(ssm=toy_model, data=data)
pf = particles.SMC(
fk=fk_model, N=100, collect=[Moments()],
store_history=True)
pf.run()
plt.plot(states, label='data')
plt.plot([m['mean'] for m in pf.summaries.moments], label='filter')
plt.legend()
plt.show()
It estimates well the original states for t>10
.
from particles.
Related Issues (20)
- Error in guided particle filtering for pre-implemented stochastic volatility model. HOT 4
- Restricting sampling of parameters HOT 4
- Information request regarding particle attribute and predictive steps HOT 1
- nsteps in backward_sampling_mcmc
- Categorical distribution
- Automate upper_bound_log_pt
- Mixture error? HOT 3
- Potential Bug in SMC2 HOT 1
- Forum Creation HOT 3
- Not working given example in the Kalman file HOT 1
- Bug in BayesianVS_gprior HOT 1
- Feature request: parallelisation over the particles? HOT 3
- smc_samplers.AdaptiveTempering may raise an error before reaching exponent == 1. HOT 1
- Sensor fusion - multiple `PY` depending on which sensor is returning HOT 6
- How to use this module to implement Particle Metropolis Hastings? HOT 6
- Is there a fast way to define a vector of parameters? HOT 17
- Question : Is it possible to use control commands `u` in `PX`? HOT 2
- How to get the distribution of x in the lib? HOT 1
- Fixing seeds and reproducibility
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from particles.