Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stateful sampling #749

Closed
gfrison opened this issue Jan 9, 2019 · 8 comments
Closed

stateful sampling #749

gfrison opened this issue Jan 9, 2019 · 8 comments

Comments

@gfrison
Copy link

gfrison commented Jan 9, 2019

Hello,

I'm just taking a look to the framework for a possible adoption in a use case.
Specifically, I need to do sampling over a Bayesian graph which brings states along inquiries. What I'm wondering is how Figaro can handle state machines for handling stateful messaging protocols.
.observe() may impose a value over an element, but how can I actually keep track of specific parameters given by the environment when a sample is generated?

Thank you

@AliOC
Copy link
Collaborator

AliOC commented Jan 11, 2019 via email

@gfrison
Copy link
Author

gfrison commented Jan 14, 2019

Hello!

I would like to create a model in Figaro which take an input and returns an output based on probabilistic rules I implemented. This model should make inferences not only on the given input, but also previous inputs.
I'm interested in sampling from a model, which reacts on a given input and its history.
For example, let's take a share price in a stock market. I'd provide inputs like the return of investments on that company likewise any other information required, and the model will infer some prices as output. Some parameters would be affected by also past values of inputs, that's why I'm wondering how can I managed those entities in Figaro. You may say that's the realm of machine learning by using LSTM or RNN, but I want to create a hand-crafted model for that.
Is that possible in Figaro? Could you please point me to an example?
That would be great.

Thank you,

@cadet354
Copy link

@apfeffer
Copy link
Contributor

apfeffer commented Feb 1, 2019 via email

@gfrison
Copy link
Author

gfrison commented Feb 5, 2019

Hello,

I tried with some dumb examples with ParticleFilter but I don't get the expected results. For example:

  val cpd = (action:Boolean)=>action match {
    case true => Flip(.9)
    case false => Flip(.1)
  }
  val transition = (prev: Universe) => {
    val act = prev.get[Boolean]("server")
    val curr = Universe.createNew()
    Chain(act, cpd)("client", curr)
    Apply(act, (actt: Boolean) => actt)("server", curr)
    curr
  }

  def main(args: Array[String]): Unit = {
    val initial = Universe.createNew()
    Uniform(true, false)("server", initial)
    Uniform(true, false)("client", initial)
    val pf = ParticleFilter(initial, transition, 100)
    pf.start()
    pf.advanceTime()
    println("probability client=true:" + pf.currentProbability("client", true))
    pf.advanceTime(List(NamedEvidence("server", Observation(true))))
    println("probability client=true after [true] observation:" + pf.currentProbability("client", true))
    pf.advanceTime(List(NamedEvidence("server", Observation(false))))
    println("probability client=true after [false] observation:" + pf.currentProbability("client", true))
    pf.kill()
  }

I get the following:

probability client=true:0.5
probability client=true after [true] observation:0.94
probability client=true after [false] observation:0.89

It seems that first observation is correctly set, but it misses the second one. I expect, by setting the "server" observation to false, to obtain a "client=true" probability close to 0.1, but it is not.
What I am doing wrong? Could you please suggest me how to change variables in the universe?

Thank you,

@gfrison
Copy link
Author

gfrison commented Feb 5, 2019

after few changes I got it running:

  val action2happy = (action: ServerAct) => action match {
    case ServerAct.veryCheap => Flip(.9)
    case _ => Flip(.1)
  }
  val happy2action: Boolean => Element[ServerAct] = (state: Boolean) => state match {
    case true => Constant(ServerAct.veryCheap)
    case _ => Uniform(ServerAct.values.filter(s => s != ServerAct.veryCheap): _*)
  }
  val transition = (prev: Universe) => {
    val act: Element[ServerAct] = prev.get[ServerAct]("stimulus")
    val curr = Universe.createNew()
    val nc: Element[Boolean] = Chain(act, action2happy)("happy", curr)
    Chain(nc, happy2action)("stimulus", curr)
    curr
  }

  def main(args: Array[String]): Unit = {
    val initial = Universe.createNew()
    Uniform(ServerAct.values: _*)("stimulus", initial)
    Uniform(true, false)("happy", initial)
    val pf = ParticleFilter(initial, transition, 100)
    pf.start()
    pf.advanceTime()
    println("probability happy=true:" + pf.currentProbability("happy", true))
    pf.advanceTime(List(NamedEvidence("stimulus", Observation(ServerAct.veryCheap))))
    println("probability happy=true after [veryCheap] stimulus: " + pf.currentProbability("happy", true))
    pf.advanceTime(List(NamedEvidence("stimulus", Observation(ServerAct.veryFast))))
    println("probability happy=true after [veryFast] stimulus: " + pf.currentProbability("happy", true))
    pf.kill()
  }

but... My purpose is to have a dynamic Bayesian network that react on external time-serie events. In this case to make it working (being happy after veryCheap event, and unhappy after veryFast) I have to counter-deducting the cause (the stimulus) from what actually I want in output ('happy').
How can I avoid the happy2action function, which set the stimulus according to what the stimulus has caused? It seems like a dog-tail chasing. I suppose I'm not using the proper paradigm for modelling an indefinitely sequence of time-serie events.
What do you think?

@apfeffer
Copy link
Contributor

apfeffer commented Feb 8, 2019 via email

@gfrison
Copy link
Author

gfrison commented Feb 11, 2019

if I set a named reference with advanceTime(List(NamedReference("x",...), I don't find in the Universe => Universe function when I invoke Universe.get("x").
Is there a way to get it through Universe.get?

@gfrison gfrison closed this as completed Feb 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants