예제 #1
0
        /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="GammaPowerProductOp_Laplace"]/message_doc[@name="BAverageConditional(GammaPower, GammaPower, GammaPower, Gamma, GammaPower)"]/*'/>
        public static GammaPower BAverageConditional([SkipIfUniform] GammaPower product, [Proper] GammaPower A, [Proper] GammaPower B, Gamma q, GammaPower result)
        {
            if (B.Shape < A.Shape)
            {
                return(AAverageConditional(product, B, A, q, result));
            }
            if (A.IsPointMass)
            {
                return(GammaProductOp.BAverageConditional(product, A.Point, result));
            }
            if (B.IsPointMass)
            {
                return(GammaPower.Uniform(result.Power)); // TODO
            }
            if (product.IsUniform())
            {
                return(product);
            }
            if (q.IsUniform())
            {
                q = Q(product, A, B);
            }
            double bPoint = q.GetMean();

            // derivatives of b
            double[] bDerivatives = new double[] { bPoint, 1, 0, 0 };
            double   bMean, bVariance;

            GaussianOp_Laplace.LaplaceMoments(q, bDerivatives, dlogfs(bPoint, product, A), out bMean, out bVariance);
            GammaPower bMarginal = GammaPower.FromGamma(Gamma.FromMeanAndVariance(bMean, bVariance), result.Power);

            result.SetToRatio(bMarginal, B, GammaProductOp_Laplace.ForceProper);
            return(result);
        }
        /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="GammaPowerProductOp_Laplace"]/message_doc[@name="BAverageConditional(GammaPower, GammaPower, GammaPower, Gamma, GammaPower)"]/*'/>
        public static GammaPower BAverageConditional([SkipIfUniform] GammaPower product, [Proper] GammaPower A, [Proper] GammaPower B, Gamma q, GammaPower result)
        {
            if (B.Shape < A.Shape)
            {
                return(AAverageConditional(product, B, A, q, result));
            }
            if (A.IsPointMass)
            {
                return(GammaProductOp.BAverageConditional(product, A.Point, result));
            }
            if (B.IsPointMass)
            {
                return(GammaPower.Uniform(result.Power)); // TODO
            }
            if (product.IsUniform())
            {
                return(product);
            }
            if (q.IsUniform())
            {
                q = Q(product, A, B);
            }
            double     qPoint = q.GetMean();
            GammaPower bMarginal;
            // threshold ensures 6/qPoint^4 does not overflow
            double threshold = Math.Sqrt(Math.Sqrt(6 / double.MaxValue));

            if (result.Power < 0 && qPoint > threshold)
            {
                double iqMean, iqVariance;
                GetIQMoments(product, A, q, qPoint, out iqMean, out iqVariance);
                GammaPower iqMarginal = GammaPower.FromMeanAndVariance(iqMean, iqVariance, -1);
                bMarginal = GammaPower.FromShapeAndRate(iqMarginal.Shape, iqMarginal.Rate, result.Power);
            }
            else
            {
                // B.Shape >= A.Shape therefore Q is the approximate distribution of B^(1/B.Power).
                // We compute the approximate moments of q = b^(1/b.Power) to get a Gamma distribution and then raise to B.Power.
                double qMean, qVariance;
                GetQMoments(product, A, q, qPoint, out qMean, out qVariance);
                bMarginal = GammaPower.FromGamma(Gamma.FromMeanAndVariance(qMean, qVariance), result.Power);
            }
            result.SetToRatio(bMarginal, B, GammaProductOp_Laplace.ForceProper);
            return(result);
        }
 /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="ProductGaussianGammaVmpOp"]/message_doc[@name="BAverageLogarithm(double, double)"]/*'/>
 public static Gamma BAverageLogarithm(double Product, double A)
 {
     return(GammaProductOp.BAverageConditional(Product, A));
 }