Browse Source

Distributions: throw ArgumentException (instead of out-of-range) for bad distribution parameters

provider
Christoph Ruegg 13 years ago
parent
commit
96c0469dfb
  1. 12
      src/Numerics/Distributions/Cauchy.cs
  2. 10
      src/Numerics/Distributions/Chi.cs
  3. 12
      src/Numerics/Distributions/Exponential.cs
  4. 12
      src/Numerics/Distributions/Rayleigh.cs

12
src/Numerics/Distributions/Cauchy.cs

@ -279,7 +279,7 @@ namespace MathNet.Numerics.Distributions
/// <seealso cref="Density"/>
public static double PDF(double location, double scale, double x)
{
if (scale <= 0.0) throw new ArgumentOutOfRangeException("scale", Resources.InvalidDistributionParameters);
if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters);
return 1.0/(Constants.Pi*scale*(1.0 + (((x - location)/scale)*((x - location)/scale))));
}
@ -294,7 +294,7 @@ namespace MathNet.Numerics.Distributions
/// <seealso cref="DensityLn"/>
public static double PDFLn(double location, double scale, double x)
{
if (scale <= 0.0) throw new ArgumentOutOfRangeException("scale", Resources.InvalidDistributionParameters);
if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters);
return -Math.Log(Constants.Pi*scale*(1.0 + (((x - location)/scale)*((x - location)/scale))));
}
@ -309,7 +309,7 @@ namespace MathNet.Numerics.Distributions
/// <seealso cref="CumulativeDistribution"/>
public static double CDF(double location, double scale, double x)
{
if (scale <= 0.0) throw new ArgumentOutOfRangeException("scale", Resources.InvalidDistributionParameters);
if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters);
return Math.Atan((x - location)/scale)/Constants.Pi + 0.5;
}
@ -325,7 +325,7 @@ namespace MathNet.Numerics.Distributions
/// <seealso cref="InverseCumulativeDistribution"/>
public static double InvCDF(double location, double scale, double p)
{
if (scale <= 0.0) throw new ArgumentOutOfRangeException("scale", Resources.InvalidDistributionParameters);
if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters);
return p <= 0.0 ? double.NegativeInfinity : p >= 1.0 ? double.PositiveInfinity
: location + scale*Math.Tan((p - 0.5)*Constants.Pi);
@ -340,7 +340,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public static double Sample(System.Random rnd, double location, double scale)
{
if (scale <= 0.0) throw new ArgumentOutOfRangeException("scale", Resources.InvalidDistributionParameters);
if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters);
return location + scale*Math.Tan(Constants.Pi*(rnd.NextDouble() - 0.5));
}
@ -354,7 +354,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(System.Random rnd, double location, double scale)
{
if (scale <= 0.0) throw new ArgumentOutOfRangeException("scale", Resources.InvalidDistributionParameters);
if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters);
while (true)
{

10
src/Numerics/Distributions/Chi.cs

@ -276,7 +276,7 @@ namespace MathNet.Numerics.Distributions
/// <seealso cref="Density"/>
public static double PDF(double freedom, double x)
{
if (freedom <= 0.0) throw new ArgumentOutOfRangeException("freedom", Resources.InvalidDistributionParameters);
if (freedom <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters);
return (Math.Pow(2.0, 1.0 - (freedom/2.0))*Math.Pow(x, freedom - 1.0)*Math.Exp(-x*x/2.0))/SpecialFunctions.Gamma(freedom/2.0);
}
@ -290,7 +290,7 @@ namespace MathNet.Numerics.Distributions
/// <seealso cref="DensityLn"/>
public static double PDFLn(double freedom, double x)
{
if (freedom <= 0.0) throw new ArgumentOutOfRangeException("freedom", Resources.InvalidDistributionParameters);
if (freedom <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters);
return ((1.0 - (freedom/2.0))*Math.Log(2.0)) + ((freedom - 1.0)*Math.Log(x)) - (x*x/2.0) - SpecialFunctions.GammaLn(freedom/2.0);
}
@ -304,7 +304,7 @@ namespace MathNet.Numerics.Distributions
/// <seealso cref="CumulativeDistribution"/>
public static double CDF(double freedom, double x)
{
if (freedom <= 0.0) throw new ArgumentOutOfRangeException("freedom", Resources.InvalidDistributionParameters);
if (freedom <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters);
return SpecialFunctions.GammaLowerIncomplete(freedom/2.0, x*x/2.0)/SpecialFunctions.Gamma(freedom/2.0);
}
@ -317,7 +317,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public static double Sample(System.Random rnd, int freedom)
{
if (freedom <= 0) throw new ArgumentOutOfRangeException("freedom", Resources.InvalidDistributionParameters);
if (freedom <= 0) throw new ArgumentException(Resources.InvalidDistributionParameters);
return SampleUnchecked(rnd, freedom);
}
@ -330,7 +330,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(System.Random rnd, int freedom)
{
if (freedom <= 0) throw new ArgumentOutOfRangeException("freedom", Resources.InvalidDistributionParameters);
if (freedom <= 0) throw new ArgumentException(Resources.InvalidDistributionParameters);
while (true)
{

12
src/Numerics/Distributions/Exponential.cs

@ -273,7 +273,7 @@ namespace MathNet.Numerics.Distributions
/// <seealso cref="Density"/>
public static double PDF(double rate, double x)
{
if (rate < 0.0) throw new ArgumentOutOfRangeException("rate", Resources.InvalidDistributionParameters);
if (rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters);
return x < 0.0 ? 0.0 : rate*Math.Exp(-rate*x);
}
@ -287,7 +287,7 @@ namespace MathNet.Numerics.Distributions
/// <seealso cref="DensityLn"/>
public static double PDFLn(double rate, double x)
{
if (rate < 0.0) throw new ArgumentOutOfRangeException("rate", Resources.InvalidDistributionParameters);
if (rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters);
return Math.Log(rate) - (rate*x);
}
@ -301,7 +301,7 @@ namespace MathNet.Numerics.Distributions
/// <seealso cref="CumulativeDistribution"/>
public static double CDF(double rate, double x)
{
if (rate < 0.0) throw new ArgumentOutOfRangeException("rate", Resources.InvalidDistributionParameters);
if (rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters);
return x < 0.0 ? 0.0 : 1.0 - Math.Exp(-rate*x);
}
@ -316,7 +316,7 @@ namespace MathNet.Numerics.Distributions
/// <seealso cref="InverseCumulativeDistribution"/>
public static double InvCDF(double rate, double p)
{
if (rate < 0.0) throw new ArgumentOutOfRangeException("rate", Resources.InvalidDistributionParameters);
if (rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters);
return p >= 1.0 ? double.PositiveInfinity : -Math.Log(1 - p)/rate;
}
@ -329,7 +329,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>A random number from this distribution.</returns>
public static double Sample(System.Random rnd, double rate)
{
if (rate < 0.0) throw new ArgumentOutOfRangeException("rate", Resources.InvalidDistributionParameters);
if (rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters);
return SampleUnchecked(rnd, rate);
}
@ -342,7 +342,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(System.Random rnd, double rate)
{
if (rate < 0.0) throw new ArgumentOutOfRangeException("rate", Resources.InvalidDistributionParameters);
if (rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters);
while (true)
{

12
src/Numerics/Distributions/Rayleigh.cs

@ -261,7 +261,7 @@ namespace MathNet.Numerics.Distributions
/// <seealso cref="Density"/>
public static double PDF(double scale, double x)
{
if (scale <= 0.0) throw new ArgumentOutOfRangeException("scale", Resources.InvalidDistributionParameters);
if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters);
return (x/(scale*scale))*Math.Exp(-x*x/(2.0*scale*scale));
}
@ -275,7 +275,7 @@ namespace MathNet.Numerics.Distributions
/// <seealso cref="DensityLn"/>
public static double PDFLn(double scale, double x)
{
if (scale <= 0.0) throw new ArgumentOutOfRangeException("scale", Resources.InvalidDistributionParameters);
if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters);
return Math.Log(x/(scale*scale)) - (x*x/(2.0*scale*scale));
}
@ -289,7 +289,7 @@ namespace MathNet.Numerics.Distributions
/// <seealso cref="CumulativeDistribution"/>
public static double CDF(double scale, double x)
{
if (scale <= 0.0) throw new ArgumentOutOfRangeException("scale", Resources.InvalidDistributionParameters);
if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters);
return 1.0 - Math.Exp(-x*x/(2.0*scale*scale));
}
@ -304,7 +304,7 @@ namespace MathNet.Numerics.Distributions
/// <seealso cref="InverseCumulativeDistribution"/>
public static double InvCDF(double scale, double p)
{
if (scale <= 0.0) throw new ArgumentOutOfRangeException("scale", Resources.InvalidDistributionParameters);
if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters);
return scale*Math.Sqrt(-2*Math.Log(1 - p));
}
@ -317,7 +317,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public static double Sample(System.Random rnd, double scale)
{
if (scale <= 0.0) throw new ArgumentOutOfRangeException("scale", Resources.InvalidDistributionParameters);
if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters);
return scale*Math.Sqrt(-2.0*Math.Log(rnd.NextDouble()));
}
@ -330,7 +330,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(System.Random rnd, double scale)
{
if (scale <= 0.0) throw new ArgumentOutOfRangeException("scale", Resources.InvalidDistributionParameters);
if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters);
while (true)
{

Loading…
Cancel
Save