@ -39,12 +39,12 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// The mean value.
/// </summary>
private double _ mean ;
double _ mean ;
/// <summary>
/// The precision value.
/// </summary>
private double _ precision ;
double _ precision ;
/// <summary>
/// Initializes a new instance of the <see cref="MeanPrecisionPair"/> struct.
@ -62,15 +62,9 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Mean
{
get
{
return _ mean ;
}
get { return _ mean ; }
set
{
_ mean = value ;
}
set { _ mean = value ; }
}
/// <summary>
@ -78,15 +72,9 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Precision
{
get
{
return _ precision ;
}
get { return _ precision ; }
set
{
_ precision = value ;
}
set { _ precision = value ; }
}
}
@ -112,27 +100,27 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// The location of the mean.
/// </summary>
private double _ meanLocation ;
double _ meanLocation ;
/// <summary>
/// The scale of the mean.
/// </summary>
private double _ meanScale ;
double _ meanScale ;
/// <summary>
/// The shape of the precision.
/// </summary>
private double _ precisionShape ;
double _ precisionShape ;
/// <summary>
/// The inverse scale of the precision.
/// </summary>
private double _ precisionInvScale ;
double _ precisionInvScale ;
/// <summary>
/// The distribution's random number generator.
/// </summary>
private Random _ random ;
Random _ random ;
/// <summary>
/// Initializes a new instance of the <see cref="NormalGamma"/> class.
@ -169,7 +157,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="precShape">The shape of the precision.</param>
/// <param name="precInvScale">The inverse scale of the precision.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
private static bool IsValidParameterSet ( double meanLocation , double meanScale , double precShape , double precInvScale )
static bool IsValidParameterSet ( double meanLocation , double meanScale , double precShape , double precInvScale )
{
if ( meanScale < = 0.0 | | precShape < = 0.0 | | precInvScale < = 0.0
| | Double . IsNaN ( meanLocation ) | | Double . IsNaN ( meanScale ) | | Double . IsNaN ( precShape )
@ -189,7 +177,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="precShape">The shape of the precision.</param>
/// <param name="precInvScale">The inverse scale of the precision.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
private void SetParameters ( double meanLocation , double meanScale , double precShape , double precInvScale )
void SetParameters ( double meanLocation , double meanScale , double precShape , double precInvScale )
{
if ( Control . CheckDistributionParameters & & ! IsValidParameterSet ( meanLocation , meanScale , precShape , precInvScale ) )
{
@ -209,7 +197,7 @@ namespace MathNet.Numerics.Distributions
public override string ToString ( )
{
return "NormalGamma(Mean Location = " + _ meanLocation + ", Mean Scale = " + _ meanScale +
", Precision Shape = " + _ precisionShape + ", Precision Inverse Scale = " + _ precisionInvScale + ")" ;
", Precision Shape = " + _ precisionShape + ", Precision Inverse Scale = " + _ precisionInvScale + ")" ;
}
/// <summary>
@ -217,15 +205,8 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double MeanLocation
{
get
{
return _ meanLocation ;
}
set
{
SetParameters ( value , _ meanScale , _ precisionShape , _ precisionInvScale ) ;
}
get { return _ meanLocation ; }
set { SetParameters ( value , _ meanScale , _ precisionShape , _ precisionInvScale ) ; }
}
/// <summary>
@ -233,15 +214,8 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double MeanScale
{
get
{
return _ meanScale ;
}
set
{
SetParameters ( _ meanLocation , value , _ precisionShape , _ precisionInvScale ) ;
}
get { return _ meanScale ; }
set { SetParameters ( _ meanLocation , value , _ precisionShape , _ precisionInvScale ) ; }
}
/// <summary>
@ -249,15 +223,8 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double PrecisionShape
{
get
{
return _ precisionShape ;
}
set
{
SetParameters ( _ meanLocation , _ meanScale , value , _ precisionInvScale ) ;
}
get { return _ precisionShape ; }
set { SetParameters ( _ meanLocation , _ meanScale , value , _ precisionInvScale ) ; }
}
/// <summary>
@ -265,15 +232,8 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double PrecisionInverseScale
{
get
{
return _ precisionInvScale ;
}
set
{
SetParameters ( _ meanLocation , _ meanScale , _ precisionShape , value ) ;
}
get { return _ precisionInvScale ; }
set { SetParameters ( _ meanLocation , _ meanScale , _ precisionShape , value ) ; }
}
/// <summary>
@ -301,10 +261,10 @@ namespace MathNet.Numerics.Distributions
{
if ( Double . IsPositiveInfinity ( _ precisionInvScale ) )
{
return new StudentT ( _ meanLocation , 1.0 / ( _ meanScale * _ precisionShape ) , Double . PositiveInfinity ) ;
return new StudentT ( _ meanLocation , 1.0 / ( _ meanScale * _ precisionShape ) , Double . PositiveInfinity ) ;
}
return new StudentT ( _ meanLocation , Math . Sqrt ( _ precisionInvScale / ( _ meanScale * _ precisionShape ) ) , 2.0 * _ precisionShape ) ;
return new StudentT ( _ meanLocation , Math . Sqrt ( _ precisionInvScale / ( _ meanScale * _ precisionShape ) ) , 2.0 * _ precisionShape ) ;
}
/// <summary>
@ -322,10 +282,7 @@ namespace MathNet.Numerics.Distributions
/// <value>The mean of the distribution.</value>
public MeanPrecisionPair Mean
{
get
{
return Double . IsPositiveInfinity ( _ precisionInvScale ) ? new MeanPrecisionPair ( _ meanLocation , _ precisionShape ) : new MeanPrecisionPair ( _ meanLocation , _ precisionShape / _ precisionInvScale ) ;
}
get { return Double . IsPositiveInfinity ( _ precisionInvScale ) ? new MeanPrecisionPair ( _ meanLocation , _ precisionShape ) : new MeanPrecisionPair ( _ meanLocation , _ precisionShape / _ precisionInvScale ) ; }
}
/// <summary>
@ -334,10 +291,7 @@ namespace MathNet.Numerics.Distributions
/// <value>The mean of the distribution.</value>
public MeanPrecisionPair Variance
{
get
{
return new MeanPrecisionPair ( _ precisionInvScale / ( _ meanScale * ( _ precisionShape - 1 ) ) , _ precisionShape / Math . Sqrt ( _ precisionInvScale ) ) ;
}
get { return new MeanPrecisionPair ( _ precisionInvScale / ( _ meanScale * ( _ precisionShape - 1 ) ) , _ precisionShape / Math . Sqrt ( _ precisionInvScale ) ) ; }
}
/// <summary>
@ -375,9 +329,9 @@ namespace MathNet.Numerics.Distributions
// 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 ) ) ;
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 ) ) ;
}
/// <summary>
@ -402,12 +356,12 @@ namespace MathNet.Numerics.Distributions
{
throw new NotSupportedException ( ) ;
}
if ( Double . IsPositiveInfinity ( _ precisionInvScale ) )
{
throw new NotSupportedException ( ) ;
}
if ( _ meanScale < = 0.0 )
{
throw new NotSupportedException ( ) ;
@ -415,8 +369,8 @@ namespace MathNet.Numerics.Distributions
// 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 ) ;
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 ) ;
}
/// <summary>
@ -462,7 +416,7 @@ namespace MathNet.Numerics.Distributions
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 ) ) ) ;
mp . Mean = meanScale = = 0.0 ? meanLocation : Normal . Sample ( rnd , meanLocation , Math . Sqrt ( 1.0 / ( meanScale * mp . Precision ) ) ) ;
return mp ;
}
@ -491,7 +445,7 @@ namespace MathNet.Numerics.Distributions
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 ) ) ) ;
mp . Mean = meanScale = = 0.0 ? meanLocation : Normal . Sample ( rnd , meanLocation , Math . Sqrt ( 1.0 / ( meanScale * mp . Precision ) ) ) ;
yield return mp ;
}