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

Questions about explicit sampling #726

Open
toncho11 opened this issue Oct 27, 2017 · 7 comments
Open

Questions about explicit sampling #726

toncho11 opened this issue Oct 27, 2017 · 7 comments

Comments

@toncho11
Copy link

toncho11 commented Oct 27, 2017

Hi,

Two questions:

  1. I get this warning: Warning: Sampling element Normal(200.0, 50.0) even though no sampler defined for this universe.
    Is this a problem?

  2. I use this function:

def GetSampleFromNormal(mean : Int, std : Int): Long =
{
val sample = Normal(mean,std);

Forward(sample);

println("system: wait time ms: " + sample.value.toLong.toInt);

return sample.value.toLong;

}

but I wonder does it make a difference if I

  • I call the function GetSampleFromNormal from time to time
  • I draw the samples just one time without leaving the function GetSampleFromNormal

In the second case I am sure I will get a Normal distribution with parameters specified, but when calling the function GetSampleFromNormal from time to time the way it is coded now I am not sure.

@bruttenberg
Copy link
Collaborator

bruttenberg commented Oct 27, 2017 via email

@toncho11
Copy link
Author

Hi,

Thanks for your quick reply. Sure it is the variance.

Yes, I am using a factored algorithm in this universe to calculate the probabilities of a Bayes Net. All my variables are of type Flip in my model. I am using:

VariableElimination.probability(e.probVariable, true)

for each variable in my Bayes net. Is there a more optimized way to calculate the probabilities of several variables all-together? And I am also drawing samples from a Normal distribution.

@bruttenberg
Copy link
Collaborator

bruttenberg commented Oct 27, 2017 via email

@toncho11
Copy link
Author

toncho11 commented Oct 27, 2017

I still do not understand if the warning is important.

It seems like it is not possible instead of:

target1, target2,

to give a: val list = scala.List[com.cra.figaro.language.Element[_]] ?

@mhoward2718
Copy link
Contributor

Generally, you shouldn't use element.value in your code. This variable tracks internal state for Figaro's sampling algorithms. It can be useful for debugging, but you should almost never use it as part of a model. If your model is gives incorrect results, this could be one reason why.

If you want to round the value of a Normal to an integer, as you do in your GetSampleFromNormal method, you should use Apply or Chain, and use a Figaro algorithm to infer its expected or most likely value..

For example, you could do:

def makeRoundedNormal(mean: Int, variance: Int) : Element[Int] = {
   val n = Normal(mean, variance)
   def round(d: Double) = d.toInt
   val nRounded= Apply(n,round)
   nRounded
}
 

@mhoward2718
Copy link
Contributor

Also - You can convert a collection to a variable length list of arguments. So you could do something like:

val targets = List(target1,target2,target2)
val algorithm = VariableElimination(targets:_*)
...
targets.map{ t => algorithm,probability(t,true) }

@toncho11
Copy link
Author

toncho11 commented Oct 29, 2017

Thanks! It helps!

For the Normal dist sampling: if I use the expected value ... I still get a double value that I need to round ... so it is better to use the MPV. I consider the Most Probable Value = Most Likely Value.

So this is the final version with MPV (Most Probable Value)

 def GetSampleFromNormal(mean : Int, variance : Int): Long =
  {
    val samplerLong = makeRoundedNormal(mean, variance);

    val algorithm = MPEBeliefPropagation(10) //10 iterations
    
    algorithm.start()
    
    val mlv = algorithm.mostLikelyValue(samplerLong)
    
    algorithm.kill()
    
    return mlv;
  }

Is it OK? It feels a bit heavy all this. Actually there is a slight difference between expected value and most likely value.

And for the Flip:

def GetSampleFromFlip(prob : Double): Boolean =
  {
    val sample = Flip(prob);

    Forward(sample);

    return sample.value;
  }

Should it be improved in a similar way as for the normal? I think it is correct (see above) because the output of Flip is discrete already (true or false).

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

3 participants