locked
Learning Mixture of Gamma Distribution RRS feed

  • Question

  • Hi, Infer.net people,

            I cant get rid of a exception(due to Factor.Difference) for implementing two mixtures of Gamma.  The code is below, and I want to estimate the rate parameter, and the data is generated by two gamma distribution , which is mirror to each other. I have tried all 3 inference algorithms.

    Thank you for the help.

                double[] data = new double[100];
                double shape = 2;
                double rate = 0.5;
                for (int i = 0; i < data.Length; i++) data[i] = Math.Pow(-1,i)*Rand.Gamma(shape) / rate;
                var rateRandV = Variable.Random<double>(Gamma.Uniform());// Variable.GammaFromMeanAndVariance(rate * 2, 10);
                
    
                for (int i = 0; i < data.Length; i++)
                {
                    var Z = Variable.Bernoulli(0.5);
                    Variable<double> x;
                    using(Variable.If(Z))
                    {
                        x = Variable.GammaFromShapeAndRate(shape, rateRandV);
                    }
                    using (Variable.IfNot(Z))
                    {
                        x = -Variable.GammaFromShapeAndRate(shape, rateRandV);
                    }
                    x.ObservedValue = data[i];
                }
                InferenceEngine engine = new InferenceEngine(new VariationalMessagePassing());
                Console.WriteLine("rate=" + engine.Infer(rateRandV));

    Friday, October 24, 2014 8:46 PM

All replies

  • Try using SetTo inside the conditionals, as explained here. For how to work with arrays and ranges, look here. For a solution to a similar problem, look here

    Friday, October 24, 2014 10:24 PM
  • Thanks for the reply.  But the exception is not due to the SetTo Issue. And it is a compile fail.

    Error 0: This model is not supported with VariationalMessagePassing due to Factor.Difference(double difference, double a, double b). Try using a different algorithm or expressing the model differently in

    Factor.Difference(0.0, vdouble5_use)

    Details: [0] System.ArgumentException: Gamma is not assignable from Gaussian for result of method DoubleMinusVmpOp.BAverageLogarithm

      // Sample data from standard Gaussian
                double[] data = new double[100];
                double shape = 2;
                double rate = 0.5;
                for (int i = 0; i < data.Length; i++) data[i] = Math.Pow(-1,i)*Rand.Gamma(shape) / rate;
                var rateRandV = Variable.Random<double>(Gamma.Uniform()); 
                
    
                for (int i = 0; i < data.Length; i++)
                {
                    var Z = Variable.Bernoulli(0.5);
                    Variable<double> x = Variable.New<double>();
                    using(Variable.If(Z))
                    {
                        x.SetTo(Variable.GammaFromShapeAndRate(shape, rateRandV));
                    }
                    using (Variable.IfNot(Z))
                    {
                         x.SetTo( -Variable.GammaFromShapeAndRate(shape, rateRandV));
                    }
                    x.ObservedValue = data[i];
                }
                InferenceEngine engine = new InferenceEngine(new VariationalMessagePassing());
                Console.WriteLine("rate=" + engine.Infer(rateRandV));

    Saturday, October 25, 2014 1:12 AM
  • Negating a Gamma variable is not supported.
    Saturday, October 25, 2014 11:53 AM
    Owner