Esempio n. 1
0
        /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="ExpOp_Laplace"]/message_doc[@name="DAverageConditional(Gamma, Gaussian, Gaussian)"]/*'/>
        public static Gaussian DAverageConditional([SkipIfUniform] Gamma exp, [Proper] Gaussian d, Gaussian to_d)
        {
            if (exp.IsPointMass)
            {
                return(ExpOp.DAverageConditional(exp.Point));
            }
            Gaussian dPost  = d * to_d;
            double   dhat   = dPost.GetMean();
            double   ehat   = Math.Exp(dhat);
            double   a      = exp.Shape;
            double   b      = exp.Rate;
            double   dlogf  = (a - 1) - b * ehat;
            double   ddlogf = -b * ehat;
            double   r      = -ddlogf;

            if (ForceProper && r < 0)
            {
                r = 0;
            }
            return(Gaussian.FromNatural(r * dhat + dlogf, r));
        }
Esempio n. 2
0
        /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="ExpOp_Laplace2"]/message_doc[@name="DAverageConditional(Gamma, Gaussian, double)"]/*'/>
        public static Gaussian DAverageConditional([SkipIfUniform] Gamma exp, [Proper] Gaussian d, double x)
        {
            if (exp.IsPointMass)
            {
                return(ExpOp.DAverageConditional(exp.Point));
            }
            double expx    = Math.Exp(x);
            double a       = exp.Shape;
            double b       = exp.Rate;
            double ddlogf  = -b * expx;
            double dddlogf = ddlogf;
            double d4logf  = ddlogf;
            double dlogf   = (a - 1) + ddlogf;
            double v       = 1 / (d.Precision - ddlogf);
            // this is Laplace's estimate of the posterior mean and variance
            double mpost = x + 0.5 * dddlogf * v * v;
            double vpost = v + 0.5 * d4logf * v * v * v + dddlogf * dddlogf * v * v * v * v;
            //double vpost = v + 0.25 * dddlogf * dddlogf * v * v * v * v;
            Gaussian result = Gaussian.FromMeanAndVariance(mpost, vpost);

            result.SetToRatio(result, d, ExpOp.ForceProper);
            return(result);
        }
Esempio n. 3
0
        /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="LogOp_EP"]/message_doc[@name="LogAverageConditional(Gaussian, Gamma, Gaussian)"]/*'/>
        public static Gaussian LogAverageConditional([Proper] Gaussian log, [SkipIfUniform] Gamma d, Gaussian result)
        {
            var g = Gamma.FromShapeAndRate(d.Shape + 1, d.Rate);

            return(ExpOp.DAverageConditional(g, log, result));
        }
Esempio n. 4
0
        public static Gaussian DAverageConditional([SkipIfUniform] Gamma exp, [Proper] Gaussian d)
        {
            // as a function of d, the factor is Ga(exp(d); shape, rate) = exp(d*(shape-1) -rate*exp(d))
            if (exp.IsUniform())
            {
                return(Gaussian.Uniform());
            }
            if (exp.IsPointMass)
            {
                return(ExpOp.DAverageConditional(exp.Point));
            }
            if (exp.Rate < 0)
            {
                throw new ImproperMessageException(exp);
            }
            if (exp.Rate == 0)
            {
                return(Gaussian.FromNatural(exp.Shape - 1, 0));
            }
            if (d.IsUniform())
            {
                if (exp.Shape <= 1)
                {
                    throw new ArgumentException("The posterior has infinite variance due to input of Exp distributed as " + d + " and output of Exp distributed as " + exp +
                                                " (shape <= 1)");
                }
                // posterior for d is a shifted log-Gamma distribution:
                // exp((a-1)*d - b*exp(d)) =propto exp(a*(d+log(b)) - exp(d+log(b)))
                // we find the Gaussian with same moments.
                // u = d+log(b)
                // E[u] = digamma(a-1)
                // E[d] = E[u]-log(b) = digamma(a-1)-log(b)
                // var(d) = var(u) = trigamma(a-1)
                double lnRate = Math.Log(exp.Rate);
                return(new Gaussian(MMath.Digamma(exp.Shape - 1) - lnRate, MMath.Trigamma(exp.Shape - 1)));
            }
            double aMinus1 = exp.Shape - 1;
            double b       = exp.Rate;

            if (d.IsPointMass)
            {
                double x      = d.Point;
                double expx   = Math.Exp(x);
                double dlogf  = aMinus1 - b * expx;
                double ddlogf = -b * expx;
                return(Gaussian.FromDerivatives(x, dlogf, ddlogf, true));
            }
            double dmode, dmin, dmax;

            GetIntegrationBounds(exp, d, out dmode, out dmin, out dmax);
            double expmode = Math.Exp(dmode);
            int    n       = QuadratureNodeCount;
            double inc     = (dmax - dmin) / (n - 1);
            MeanVarianceAccumulator mva = new MeanVarianceAccumulator();

            for (int i = 0; i < n; i++)
            {
                double x          = dmin + i * inc;
                double xMinusMode = x - dmode;
                double diff       = aMinus1 * xMinusMode - b * (Math.Exp(x) - expmode)
                                    - 0.5 * ((x * x - dmode * dmode) * d.Precision - 2 * xMinusMode * d.MeanTimesPrecision);
                double p = Math.Exp(diff);
                mva.Add(x, p);
                if (double.IsNaN(mva.Variance))
                {
                    throw new Exception();
                }
            }
            double   dMean     = mva.Mean;
            double   dVariance = mva.Variance;
            Gaussian result    = Gaussian.FromMeanAndVariance(dMean, dVariance);

            result.SetToRatio(result, d, true);
            return(result);
        }