示例#1
0
 /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="IsGreaterThanOp"]/message_doc[@name="IsGreaterThanAverageConditional(int, Poisson)"]/*'/>
 public static Bernoulli IsGreaterThanAverageConditional(int a, [Proper] Poisson b)
 {
     if (b.IsPointMass)
     {
         return(Bernoulli.PointMass(a > b.Point));
     }
     if (b.Precision == 1)
     {
         if (a <= 0)
         {
             return(Bernoulli.PointMass(false));
         }
         else
         {
             return(new Bernoulli(MMath.GammaUpper(a, b.Rate)));
         }
     }
     else
     {
         double sum = 0;
         for (int i = 0; i < a; i++)
         {
             sum += Math.Exp(b.GetLogProb(i));
         }
         if (sum > 1)
         {
             sum = 1; // this can happen due to round-off errors
         }
         return(new Bernoulli(sum));
     }
 }
示例#2
0
 /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="IsGreaterThanOp"]/message_doc[@name="IsGreaterThanAverageConditional(Poisson, int)"]/*'/>
 public static Bernoulli IsGreaterThanAverageConditional([Proper] Poisson a, int b)
 {
     if (a.IsPointMass)
     {
         return(Bernoulli.PointMass(a.Point > b));
     }
     if (a.Precision == 1)
     {
         if (b < 0)
         {
             return(Bernoulli.PointMass(true));
         }
         else
         {
             return(new Bernoulli(MMath.GammaLower(b + 1, a.Rate)));
         }
     }
     else
     {
         double sum = 0;
         for (int i = 0; i <= b; i++)
         {
             sum += Math.Exp(a.GetLogProb(i));
         }
         if (sum > 1)
         {
             sum = 1; // this can happen due to round-off errors
         }
         return(new Bernoulli(1 - sum));
     }
 }
示例#3
0
        /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="IsGreaterThanOp"]/message_doc[@name="AAverageConditional(Bernoulli, Poisson, int)"]/*'/>
        public static Poisson BAverageConditional([SkipIfUniform] Bernoulli isGreaterThan, int a, [Proper] Poisson b)
        {
            if (b.IsPointMass || !b.IsProper())
            {
                return(Poisson.Uniform());
            }
            double bMean = b.GetMean();
            double sum   = 0;
            double bsum  = 0;

            if (b.Precision == 1)
            {
                if (a > 0)
                {
                    sum  = MMath.GammaUpper(a, b.Rate);
                    bsum = (sum - Math.Exp(b.GetLogProb(a - 1))) * b.Rate;
                }
            }
            else
            {
                for (int i = 0; i < a; i++)
                {
                    double p = Math.Exp(b.GetLogProb(i));
                    sum  += p;
                    bsum += i * p;
                }
                if (sum > 1)
                {
                    sum = 1; // this can happen due to round-off errors
                }
                if (bsum > bMean)
                {
                    bsum = bMean;
                }
            }
            double  pT     = isGreaterThan.GetProbTrue();
            double  pF     = 1 - pT;
            double  Z      = pT * sum + pF * (1 - sum);
            double  bZ     = pT * bsum + pF * (bMean - bsum);
            Poisson result = new Poisson(bZ / Z);

            result.SetToRatio(result, b, false);
            return(result);
        }
示例#4
0
        /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="IsGreaterThanOp"]/message_doc[@name="AAverageConditional(Bernoulli, Poisson, int)"]/*'/>
        public static Poisson AAverageConditional([SkipIfUniform] Bernoulli isGreaterThan, [Proper] Poisson a, int b)
        {
            if (a.IsPointMass || !a.IsProper())
            {
                return(Poisson.Uniform());
            }
            double aMean = a.GetMean();
            double sum   = 0;
            double asum  = 0;

            if (a.Precision == 1)
            {
                if (b >= 0)
                {
                    sum  = MMath.GammaUpper(b + 1, a.Rate);
                    asum = (sum - Math.Exp(a.GetLogProb(b))) * a.Rate;
                }
            }
            else
            {
                for (int i = 0; i <= b; i++)
                {
                    double p = Math.Exp(a.GetLogProb(i));
                    sum  += p;
                    asum += i * p;
                }
                if (sum > 1)
                {
                    sum = 1; // this can happen due to round-off errors
                }
                if (asum > aMean)
                {
                    asum = aMean;
                }
            }
            double  pT     = isGreaterThan.GetProbTrue();
            double  pF     = 1 - pT;
            double  Z      = pF * sum + pT * (1 - sum);
            double  aZ     = pF * asum + pT * (aMean - asum);
            Poisson result = new Poisson(aZ / Z);

            result.SetToRatio(result, a, false);
            return(result);
        }