Example #1
0
        /// <summary>
        /// Generates a sample from the <c>NormalGamma</c> distribution.
        /// </summary>
        /// <param name="rnd">The random number generator to use.</param>
        /// <param name="meanLocation">The location of the mean.</param>
        /// <param name="meanScale">The scale of the mean.</param>
        /// <param name="precisionShape">The shape of the precision.</param>
        /// <param name="precisionInverseScale">The inverse scale of the precision.</param>
        /// <returns>a sample from the distribution.</returns>
        public static MeanPrecisionPair Sample(System.Random rnd, double meanLocation, double meanScale, double precisionShape, double precisionInverseScale)
        {
            if (Control.CheckDistributionParameters && !IsValidParameterSet(meanLocation, meanScale, precisionShape, precisionInverseScale))
            {
                throw new ArgumentException(Resources.InvalidDistributionParameters);
            }

            var mp = new MeanPrecisionPair();

            // Sample the precision.
            mp.Precision = double.IsPositiveInfinity(precisionInverseScale) ? precisionShape : Gamma.Sample(rnd, precisionShape, precisionInverseScale);

            // Sample the mean.
            mp.Mean = meanScale == 0.0 ? meanLocation : Normal.Sample(rnd, meanLocation, Math.Sqrt(1.0 / (meanScale * mp.Precision)));

            return(mp);
        }
Example #2
0
 /// <summary>
 /// Evaluates the log probability density function for a NormalGamma distribution.
 /// </summary>
 /// <param name="mp">The mean/precision pair of the distribution</param>
 /// <returns>The log of the density value</returns>
 public double DensityLn(MeanPrecisionPair mp)
 {
     return(DensityLn(mp.Mean, mp.Precision));
 }