Giter VIP home page Giter VIP logo

Comments (6)

nchopin avatar nchopin commented on May 20, 2024

Hi, sorry for the slow answer, I was taking holidays.

  1. please post a MRE (minimal reproducible example), so that I can better understand what you tried to do and how it failed.
  2. 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.

poncev avatar poncev commented on May 20, 2024

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.

nchopin avatar nchopin commented on May 20, 2024

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.

poncev avatar poncev commented on May 20, 2024

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.

nchopin avatar nchopin commented on May 20, 2024

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.

poncev avatar poncev commented on May 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.