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(Random rnd, double meanLocation, double meanScale, double precisionShape, double precisionInverseScale)
        {
            if (Control.CheckDistributionParameters && !IsValidParameterSet(meanLocation, meanScale, precisionShape, precisionInverseScale))
            {
                throw new ArgumentOutOfRangeException(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>
        /// Generates a sequence of samples from the NormalGamma 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="precisionInvScale">The inverse scale of the precision.</param>
        /// <returns>a sequence of samples from the distribution.</returns>
        public static IEnumerable <MeanPrecisionPair> Samples(System.Random rnd, double meanLocation, double meanScale, double precisionShape, double precisionInvScale)
        {
            if (Control.CheckDistributionParameters && !IsValidParameterSet(meanLocation, meanScale, precisionShape, precisionInvScale))
            {
                throw new ArgumentException(Resources.InvalidDistributionParameters);
            }

            while (true)
            {
                var mp = new MeanPrecisionPair();

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

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

                yield return(mp);
            }
        }
Example #3
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));
 }
Example #4
0
        /// <summary>
        /// Generates a sequence of samples from the NormalGamma 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="precisionInvScale">The inverse scale of the precision.</param>
        /// <returns>a sequence of samples from the distribution.</returns>
        public static IEnumerable<MeanPrecisionPair> Samples(Random rnd, double meanLocation, double meanScale, double precisionShape, double precisionInvScale)
        {
            if (Control.CheckDistributionParameters && !IsValidParameterSet(meanLocation, meanScale, precisionShape, precisionInvScale))
            {
                throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
            }

            while (true)
            {
                var mp = new MeanPrecisionPair();

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

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

                yield return mp;
            }
        }
Example #5
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);
 }
Example #6
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;
        }
        public void SampleFollowsCorrectDistribution()
        {
            var cd = new NormalGamma(1.0, 4.0, 7.0, 3.5);

            // Sample from the distribution.
            var samples = new MeanPrecisionPair[CommonDistributionTests.NumberOfTestSamples];
            for (var i = 0; i < CommonDistributionTests.NumberOfTestSamples; i++)
            {
                samples[i] = cd.Sample();
            }

            // Extract the mean and precisions.
            var means = samples.Select(mp => mp.Mean).ToArray();
            var precs = samples.Select(mp => mp.Precision).ToArray();
            var meanMarginal = cd.MeanMarginal();
            var precMarginal = cd.PrecisionMarginal();

            // Check the precision distribution.
            CommonDistributionTests.ContinuousVapnikChervonenkisTest(
                CommonDistributionTests.ErrorTolerance,
                CommonDistributionTests.ErrorProbability,
                precs,
                precMarginal);

            // Check the mean distribution.
            CommonDistributionTests.ContinuousVapnikChervonenkisTest(
                CommonDistributionTests.ErrorTolerance,
                CommonDistributionTests.ErrorProbability,
                means,
                meanMarginal);
        }
        /// <summary>
        /// Generates a sample from the NormalGamma 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="precShape">The shape of the precision.</param>
        /// <param name="precInvScale">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 precShape, double precInvScale)
        {
            if (Control.CheckDistributionParameters && !IsValidParameterSet(meanLocation, meanScale, precShape, precInvScale))
            {
                throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
            }

            MeanPrecisionPair mp = new MeanPrecisionPair();

            // Sample the precision.
            if(Double.IsPositiveInfinity(precInvScale))
            {
                mp.Precision = precShape;
            }
            else
            {
                mp.Precision = Gamma.Sample(rnd, precShape, precInvScale);
            }

            // Sample the mean.
            if (meanScale == 0.0)
            {
                mp.Mean = meanLocation;
            }
            else
            {
                mp.Mean = Normal.Sample(rnd, meanLocation, System.Math.Sqrt(1.0 / (meanScale * mp.Precision)));
            }

            return mp;
        }