locked
PoissionDistribution RRS feed

  • Question

  • I tried to fit a poission distribution to data.
    The conjugate prior for the parameter lambda is modeld as a gamma distribution.

    pseudo code:

    _lambda = Variable.GammaFromShapeAndScale( 20, 2 ) for k in range(100):     Variable.Poisson( _lambda ).observedValue = Poisson(10).Sample()

    ie = InferenceEngine(VariationalMessagePassing()) _lambda = ie.Infer(_lambda)

    I didnt have success. The mean of the gamma does not converge to the data.

    Did I miss something ?

    Thursday, August 14, 2014 7:25 AM

Answers

  • I don't understand your pseudo-code - best to post real code. This works for me for both EP and VMP:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    using MicrosoftResearch.Infer;
    using MicrosoftResearch.Infer.Distributions;
    using MicrosoftResearch.Infer.Models;
    using MicrosoftResearch.Infer.Utils;
    
    namespace PoissonTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Generate some data from a ground truth
                double trueLambda = 10;
                var data = Util.ArrayInit(100, i => Poisson.Sample(trueLambda));
    
                // Model
                var lambda = Variable.GammaFromShapeAndRate(2, 2);
                var N = Variable.New<int>();
                var n = new Range(N);
                var x = Variable.Array<int>(n);
                x[n] = Variable.Poisson(lambda).ForEach(n);
    
                // Attach data
                N.ObservedValue = data.Length;
                x.ObservedValue = data;
    
                // Inference
                //var engine = new InferenceEngine(new VariationalMessagePassing());
                var engine = new InferenceEngine(new ExpectationPropagation());
                var inferredLambda = engine.Infer<Gamma>(lambda);
    
                Console.WriteLine("Expected = {0}, Inferred = {1}", trueLambda, inferredLambda.GetMean());
            }
        }
    }

    • Marked as answer by Geri9 Sunday, August 17, 2014 8:28 AM
    Thursday, August 14, 2014 8:25 AM
    Owner

All replies

  • I don't understand your pseudo-code - best to post real code. This works for me for both EP and VMP:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    using MicrosoftResearch.Infer;
    using MicrosoftResearch.Infer.Distributions;
    using MicrosoftResearch.Infer.Models;
    using MicrosoftResearch.Infer.Utils;
    
    namespace PoissonTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Generate some data from a ground truth
                double trueLambda = 10;
                var data = Util.ArrayInit(100, i => Poisson.Sample(trueLambda));
    
                // Model
                var lambda = Variable.GammaFromShapeAndRate(2, 2);
                var N = Variable.New<int>();
                var n = new Range(N);
                var x = Variable.Array<int>(n);
                x[n] = Variable.Poisson(lambda).ForEach(n);
    
                // Attach data
                N.ObservedValue = data.Length;
                x.ObservedValue = data;
    
                // Inference
                //var engine = new InferenceEngine(new VariationalMessagePassing());
                var engine = new InferenceEngine(new ExpectationPropagation());
                var inferredLambda = engine.Infer<Gamma>(lambda);
    
                Console.WriteLine("Expected = {0}, Inferred = {1}", trueLambda, inferredLambda.GetMean());
            }
        }
    }

    • Marked as answer by Geri9 Sunday, August 17, 2014 8:28 AM
    Thursday, August 14, 2014 8:25 AM
    Owner
  • The reason is that your prior is too strong.  John's prior is a safer choice.
    Thursday, August 14, 2014 12:14 PM
    Owner
  • Hi, Thanks to all your messages.

    The problem was the line:

    >> Variable.Poisson( _lambda ).observedValue = Poisson(10).Sample()

    The lowercase "o" doesn't give an Error in IronPython !

    So none of my data wasn't observed ever , which didnt change the prior !

    Thanks

    Sunday, August 17, 2014 8:30 AM