// // Math.NET Numerics, part of the Math.NET Project // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // Copyright (c) 2009-2010 Math.NET // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without // restriction, including without limitation the rights to use, // copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // namespace MathNet.Numerics.Distributions { using System; using System.Collections.Generic; using Properties; /// /// This structure represents the type over which the distribution /// is defined. /// public struct MeanPrecisionPair { /// /// The mean value. /// double _mean; /// /// The precision value. /// double _precision; /// /// Initializes a new instance of the struct. /// /// The mean of the pair. /// The precision of the pair. public MeanPrecisionPair(double m, double p) { _mean = m; _precision = p; } /// /// Gets or sets the mean of the pair. /// public double Mean { get { return _mean; } set { _mean = value; } } /// /// Gets or sets the precision of the pair. /// public double Precision { get { return _precision; } set { _precision = value; } } } /// /// The distribution is the conjugate prior distribution for the /// distribution. It specifies a prior over the mean and precision of the distribution. /// It is parameterized by four numbers: the mean location, the mean scale, the precision shape and the /// precision inverse scale. /// The distribution NG(mu, tau | mloc,mscale,psscale,pinvscale) = Normal(mu | mloc, 1/(mscale*tau)) * Gamma(tau | psscale,pinvscale). /// The following degenerate cases are special: when the precision is known, /// the precision shape will encode the value of the precision while the precision inverse scale is positive /// infinity. When the mean is known, the mean location will encode the value of the mean while the scale /// will be positive infinity. A completely degenerate NormalGamma distribution with known mean and precision is possible as well. /// Wikipedia - Normal-Gamma distribution. /// /// The distribution will use the by default. /// Users can get/set the random number generator by using the property. /// The statistics classes will check all the incoming parameters whether they are in the allowed /// range. This might involve heavy computation. Optionally, by setting Control.CheckDistributionParameters /// to false, all parameter checks can be turned off. public class NormalGamma { /// /// The location of the mean. /// double _meanLocation; /// /// The scale of the mean. /// double _meanScale; /// /// The shape of the precision. /// double _precisionShape; /// /// The inverse scale of the precision. /// double _precisionInvScale; /// /// The distribution's random number generator. /// Random _random; /// /// Initializes a new instance of the class. /// /// The location of the mean. /// The scale of the mean. /// The shape of the precision. /// The inverse scale of the precision. public NormalGamma(double meanLocation, double meanScale, double precisionShape, double precisionInverseScale) { _random = new Random(); SetParameters(meanLocation, meanScale, precisionShape, precisionInverseScale); } /// /// Initializes a new instance of the class. /// /// The location of the mean. /// The scale of the mean. /// The shape of the precision. /// The inverse scale of the precision. /// The random number generator which is used to draw random samples. public NormalGamma(double meanLocation, double meanScale, double precisionShape, double precisionInverseScale, Random randomSource) { _random = randomSource ?? new Random(); SetParameters(meanLocation, meanScale, precisionShape, precisionInverseScale); } /// /// Checks whether the parameters of the distribution are valid. /// /// The location of the mean. /// The scale of the mean. /// The shape of the precision. /// The inverse scale of the precision. /// true when the parameters are valid, false otherwise. static bool IsValidParameterSet(double meanLocation, double meanScale, double precShape, double precInvScale) { return (meanScale > 0.0) && (precShape > 0.0) && (precInvScale > 0.0) && !Double.IsNaN(meanLocation); } /// /// Sets the parameters of the distribution after checking their validity. /// /// The location of the mean. /// The scale of the mean. /// The shape of the precision. /// The inverse scale of the precision. /// When the parameters don't pass the function. void SetParameters(double meanLocation, double meanScale, double precShape, double precInvScale) { if (Control.CheckDistributionParameters && !IsValidParameterSet(meanLocation, meanScale, precShape, precInvScale)) { throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters); } _meanLocation = meanLocation; _meanScale = meanScale; _precisionShape = precShape; _precisionInvScale = precInvScale; } /// /// A string representation of the distribution. /// /// a string representation of the distribution. public override string ToString() { return "NormalGamma(Mean Location = " + _meanLocation + ", Mean Scale = " + _meanScale + ", Precision Shape = " + _precisionShape + ", Precision Inverse Scale = " + _precisionInvScale + ")"; } /// /// Gets or sets the location of the mean. /// public double MeanLocation { get { return _meanLocation; } set { SetParameters(value, _meanScale, _precisionShape, _precisionInvScale); } } /// /// Gets or sets the scale of the mean. /// public double MeanScale { get { return _meanScale; } set { SetParameters(_meanLocation, value, _precisionShape, _precisionInvScale); } } /// /// Gets or sets the shape of the precision. /// public double PrecisionShape { get { return _precisionShape; } set { SetParameters(_meanLocation, _meanScale, value, _precisionInvScale); } } /// /// Gets or sets the inverse scale of the precision. /// public double PrecisionInverseScale { get { return _precisionInvScale; } set { SetParameters(_meanLocation, _meanScale, _precisionShape, value); } } /// /// Gets or sets the random number generator which is used to draw random samples. /// public Random RandomSource { get { return _random; } set { if (value == null) { throw new ArgumentNullException(); } _random = value; } } /// /// Returns the marginal distribution for the mean of the NormalGamma distribution. /// /// the marginal distribution for the mean of the NormalGamma distribution. public StudentT MeanMarginal() { if (Double.IsPositiveInfinity(_precisionInvScale)) { return new StudentT(_meanLocation, 1.0/(_meanScale*_precisionShape), Double.PositiveInfinity); } return new StudentT(_meanLocation, Math.Sqrt(_precisionInvScale/(_meanScale*_precisionShape)), 2.0*_precisionShape); } /// /// Returns the marginal distribution for the precision of the distribution. /// /// The marginal distribution for the precision of the distribution/ public Gamma PrecisionMarginal() { return new Gamma(_precisionShape, _precisionInvScale); } /// /// Gets the mean of the distribution. /// /// The mean of the distribution. public MeanPrecisionPair Mean { get { return Double.IsPositiveInfinity(_precisionInvScale) ? new MeanPrecisionPair(_meanLocation, _precisionShape) : new MeanPrecisionPair(_meanLocation, _precisionShape/_precisionInvScale); } } /// /// Gets the variance of the distribution. /// /// The mean of the distribution. public MeanPrecisionPair Variance { get { return new MeanPrecisionPair(_precisionInvScale/(_meanScale*(_precisionShape - 1)), _precisionShape/Math.Sqrt(_precisionInvScale)); } } /// /// Evaluates the probability density function for a NormalGamma distribution. /// /// The mean/precision pair of the distribution /// Density value public double Density(MeanPrecisionPair mp) { return Density(mp.Mean, mp.Precision); } /// /// Evaluates the probability density function for a NormalGamma distribution. /// /// The mean of the distribution /// The precision of the distribution /// Density value public double Density(double mean, double prec) { if (Double.IsPositiveInfinity(_precisionInvScale) && _meanScale == 0.0) { throw new NotSupportedException(); } if (Double.IsPositiveInfinity(_precisionInvScale)) { throw new NotSupportedException(); } if (_meanScale <= 0.0) { throw new NotSupportedException(); } // double e = -0.5 * prec * (mean - _meanLocation) * (mean - _meanLocation) - prec * _precisionInvScale; // return Math.Pow(prec * _precisionInvScale, _precisionShape) * Math.Exp(e) / (Constants.Sqrt2Pi * Math.Sqrt(prec) * SpecialFunctions.Gamma(_precisionShape)); double e = -(0.5*prec*_meanScale*(mean - _meanLocation)*(mean - _meanLocation)) - (prec*_precisionInvScale); return Math.Pow(prec*_precisionInvScale, _precisionShape)*Math.Exp(e)*Math.Sqrt(_meanScale) /(Constants.Sqrt2Pi*Math.Sqrt(prec)*SpecialFunctions.Gamma(_precisionShape)); } /// /// Evaluates the log probability density function for a NormalGamma distribution. /// /// The mean/precision pair of the distribution /// The log of the density value public double DensityLn(MeanPrecisionPair mp) { return DensityLn(mp.Mean, mp.Precision); } /// /// Evaluates the log probability density function for a NormalGamma distribution. /// /// The mean of the distribution /// The precision of the distribution /// The log of the density value public double DensityLn(double mean, double prec) { if (Double.IsPositiveInfinity(_precisionInvScale) && _meanScale == 0.0) { throw new NotSupportedException(); } if (Double.IsPositiveInfinity(_precisionInvScale)) { throw new NotSupportedException(); } if (_meanScale <= 0.0) { throw new NotSupportedException(); } // double e = -0.5 * prec * (mean - _meanLocation) * (mean - _meanLocation) - prec * _precisionInvScale; // return (_precisionShape - 0.5) * Math.Log(prec) + _precisionShape * Math.Log(_precisionInvScale) + e - Constants.LogSqrt2Pi - SpecialFunctions.GammaLn(_precisionShape); double e = -(0.5*prec*_meanScale*(mean - _meanLocation)*(mean - _meanLocation)) - (prec*_precisionInvScale); return ((_precisionShape - 0.5)*Math.Log(prec)) + (_precisionShape*Math.Log(_precisionInvScale)) - (0.5*Math.Log(_meanScale)) + e - Constants.LogSqrt2Pi - SpecialFunctions.GammaLn(_precisionShape); } /// /// Generates a sample from the NormalGamma distribution. /// /// a sample from the distribution. public MeanPrecisionPair Sample() { return Sample(RandomSource, _meanLocation, _meanScale, _precisionShape, _precisionInvScale); } /// /// Generates a sequence of samples from the NormalGamma distribution /// /// a sequence of samples from the distribution. public IEnumerable Samples() { while (true) { yield return Sample(RandomSource, _meanLocation, _meanScale, _precisionShape, _precisionInvScale); } } /// /// Generates a sample from the NormalGamma distribution. /// /// The random number generator to use. /// The location of the mean. /// The scale of the mean. /// The shape of the precision. /// The inverse scale of the precision. /// a sample from the distribution. 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; } /// /// Generates a sequence of samples from the NormalGamma distribution /// /// The random number generator to use. /// The location of the mean. /// The scale of the mean. /// The shape of the precision. /// The inverse scale of the precision. /// a sequence of samples from the distribution. public static IEnumerable 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; } } } }