Example #1
0
 public double Sample()
 {
     if (IsPointMass)
     {
         return(Point);
     }
     else
     {
         return(Sample(Gaussian.GetMean(), Gaussian.Precision, LowerBound, UpperBound));
     }
 }
        /// <summary>
        /// The maximum difference between the parameters of this Gaussian
        /// and that Gaussian
        /// </summary>
        /// <param name="thatd">That Gaussian</param>
        /// <returns>The maximum difference</returns>
        /// <remarks><c>a.MaxDiff(b) == b.MaxDiff(a)</c></remarks>
        public double MaxDiff(object thatd)
        {
            if (!(thatd is Gaussian))
            {
                return(Double.PositiveInfinity);
            }
            Gaussian that = (Gaussian)thatd;

#if true
            double diff1 = MMath.AbsDiff(MeanTimesPrecision, that.MeanTimesPrecision);
            double diff2 = MMath.AbsDiff(Precision, that.Precision);
#else
            double diff1 = Math.Abs(GetMean() - that.GetMean());
            double diff2 = Math.Abs(Math.Sqrt(GetVariance()) - Math.Sqrt(that.GetVariance()));
#endif
            return(Math.Max(diff1, diff2));
        }
 /// <summary>
 /// Sets the parameters to represent the ratio of two Gaussians, forcing the precision to be non-negative.
 /// </summary>
 /// <param name="numerator">The numerator Gaussian</param>
 /// <param name="denominator">The denominator Gaussian</param>
 /// <remarks>
 /// The result is always proper.  Multiplying the result and <paramref name="denominator"/> will always give the same mean
 /// as <paramref name="numerator"/>, but the variance may be smaller than <paramref name="numerator"/>.
 /// </remarks>
 public void SetToRatioProper(Gaussian numerator, Gaussian denominator)
 {
     if (numerator.IsPointMass)
     {
         if (denominator.IsPointMass)
         {
             if (numerator.Point.Equals(denominator.Point))
             {
                 SetToUniform();
             }
             else
             {
                 throw new DivideByZeroException();
             }
         }
         else
         {
             Point = numerator.Point;
         }
     }
     else if (denominator.IsPointMass)
     {
         throw new DivideByZeroException();
     }
     else
     {
         Precision = numerator.Precision - denominator.Precision;
         if (Precision < 0)
         {
             Precision          = 0;
             MeanTimesPrecision = numerator.GetMean() * denominator.Precision - denominator.MeanTimesPrecision;
         }
         else
         {
             MeanTimesPrecision = numerator.MeanTimesPrecision - denominator.MeanTimesPrecision;
         }
     }
 }