Browse Source

Distributions: formatting cleanup (no code changes)

pull/163/head
Christoph Ruegg 13 years ago
parent
commit
f304fac8e2
  1. 28
      src/Numerics/Distributions/Continuous/Beta.cs
  2. 12
      src/Numerics/Distributions/Continuous/Cauchy.cs
  3. 19
      src/Numerics/Distributions/Continuous/Chi.cs
  4. 21
      src/Numerics/Distributions/Continuous/ChiSquare.cs
  5. 20
      src/Numerics/Distributions/Continuous/ContinuousUniform.cs
  6. 55
      src/Numerics/Distributions/Continuous/Erlang.cs
  7. 17
      src/Numerics/Distributions/Continuous/Exponential.cs
  8. 16
      src/Numerics/Distributions/Continuous/FisherSnedecor.cs
  9. 53
      src/Numerics/Distributions/Continuous/Gamma.cs
  10. 20
      src/Numerics/Distributions/Continuous/InverseGamma.cs
  11. 17
      src/Numerics/Distributions/Continuous/Laplace.cs
  12. 37
      src/Numerics/Distributions/Continuous/LogNormal.cs
  13. 59
      src/Numerics/Distributions/Continuous/Normal.cs
  14. 20
      src/Numerics/Distributions/Continuous/Pareto.cs
  15. 21
      src/Numerics/Distributions/Continuous/Rayleigh.cs
  16. 36
      src/Numerics/Distributions/Continuous/Stable.cs
  17. 46
      src/Numerics/Distributions/Continuous/StudentT.cs
  18. 28
      src/Numerics/Distributions/Continuous/Weibull.cs
  19. 9
      src/Numerics/Distributions/Discrete/Bernoulli.cs
  20. 24
      src/Numerics/Distributions/Discrete/Binomial.cs
  21. 8
      src/Numerics/Distributions/Discrete/Categorical.cs
  22. 58
      src/Numerics/Distributions/Discrete/ConwayMaxwellPoisson.cs
  23. 18
      src/Numerics/Distributions/Discrete/DiscreteUniform.cs
  24. 18
      src/Numerics/Distributions/Discrete/Geometric.cs
  25. 21
      src/Numerics/Distributions/Discrete/Hypergeometric.cs
  26. 23
      src/Numerics/Distributions/Discrete/NegativeBinomial.cs
  27. 28
      src/Numerics/Distributions/Discrete/Poisson.cs
  28. 20
      src/Numerics/Distributions/Discrete/Zipf.cs
  29. 47
      src/Numerics/Distributions/Multivariate/Dirichlet.cs
  30. 64
      src/Numerics/Distributions/Multivariate/InverseWishart.cs
  31. 63
      src/Numerics/Distributions/Multivariate/MatrixNormal.cs
  32. 53
      src/Numerics/Distributions/Multivariate/Multinomial.cs
  33. 118
      src/Numerics/Distributions/Multivariate/NormalGamma.cs
  34. 64
      src/Numerics/Distributions/Multivariate/Wishart.cs

28
src/Numerics/Distributions/Continuous/Beta.cs

@ -204,7 +204,7 @@ namespace MathNet.Numerics.Distributions
return 0.0;
}
return _shapeA / (_shapeA + _shapeB);
return _shapeA/(_shapeA + _shapeB);
}
}
@ -213,7 +213,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Variance
{
get { return (_shapeA * _shapeB) / ((_shapeA + _shapeB) * (_shapeA + _shapeB) * (_shapeA + _shapeB + 1.0)); }
get { return (_shapeA*_shapeB)/((_shapeA + _shapeB)*(_shapeA + _shapeB)*(_shapeA + _shapeB + 1.0)); }
}
/// <summary>
@ -221,7 +221,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double StdDev
{
get { return Math.Sqrt((_shapeA * _shapeB) / ((_shapeA + _shapeB) * (_shapeA + _shapeB) * (_shapeA + _shapeB + 1.0))); }
get { return Math.Sqrt((_shapeA*_shapeB)/((_shapeA + _shapeB)*(_shapeA + _shapeB)*(_shapeA + _shapeB + 1.0))); }
}
/// <summary>
@ -247,9 +247,9 @@ namespace MathNet.Numerics.Distributions
}
return SpecialFunctions.BetaLn(_shapeA, _shapeB)
- ((_shapeA - 1.0) * SpecialFunctions.DiGamma(_shapeA))
- ((_shapeB - 1.0) * SpecialFunctions.DiGamma(_shapeB))
+ ((_shapeA + _shapeB - 2.0) * SpecialFunctions.DiGamma(_shapeA + _shapeB));
- ((_shapeA - 1.0)*SpecialFunctions.DiGamma(_shapeA))
- ((_shapeB - 1.0)*SpecialFunctions.DiGamma(_shapeB))
+ ((_shapeA + _shapeB - 2.0)*SpecialFunctions.DiGamma(_shapeA + _shapeB));
}
}
@ -290,8 +290,8 @@ namespace MathNet.Numerics.Distributions
return -2.0;
}
return 2.0 * (_shapeB - _shapeA) * Math.Sqrt(_shapeA + _shapeB + 1.0)
/ ((_shapeA + _shapeB + 2.0) * Math.Sqrt(_shapeA * _shapeB));
return 2.0*(_shapeB - _shapeA)*Math.Sqrt(_shapeA + _shapeB + 1.0)
/((_shapeA + _shapeB + 2.0)*Math.Sqrt(_shapeA*_shapeB));
}
}
@ -341,7 +341,7 @@ namespace MathNet.Numerics.Distributions
return 0.5;
}
return (_shapeA - 1) / (_shapeA + _shapeB - 2);
return (_shapeA - 1)/(_shapeA + _shapeB - 2);
}
}
@ -421,8 +421,8 @@ namespace MathNet.Numerics.Distributions
return 1.0;
}
var b = SpecialFunctions.Gamma(_shapeA + _shapeB) / (SpecialFunctions.Gamma(_shapeA) * SpecialFunctions.Gamma(_shapeB));
return b * Math.Pow(x, _shapeA - 1.0) * Math.Pow(1.0 - x, _shapeB - 1.0);
var b = SpecialFunctions.Gamma(_shapeA + _shapeB)/(SpecialFunctions.Gamma(_shapeA)*SpecialFunctions.Gamma(_shapeB));
return b*Math.Pow(x, _shapeA - 1.0)*Math.Pow(1.0 - x, _shapeB - 1.0);
}
/// <summary>
@ -478,8 +478,8 @@ namespace MathNet.Numerics.Distributions
}
var a = SpecialFunctions.GammaLn(_shapeA + _shapeB) - SpecialFunctions.GammaLn(_shapeA) - SpecialFunctions.GammaLn(_shapeB);
var b = x == 0.0 ? (_shapeA == 1.0 ? 0.0 : Double.NegativeInfinity) : (_shapeA - 1.0) * Math.Log(x);
var c = x == 1.0 ? (_shapeB == 1.0 ? 0.0 : Double.NegativeInfinity) : (_shapeB - 1.0) * Math.Log(1.0 - x);
var b = x == 0.0 ? (_shapeA == 1.0 ? 0.0 : Double.NegativeInfinity) : (_shapeA - 1.0)*Math.Log(x);
var c = x == 1.0 ? (_shapeB == 1.0 ? 0.0 : Double.NegativeInfinity) : (_shapeB - 1.0)*Math.Log(1.0 - x);
return a + b + c;
}
@ -557,7 +557,7 @@ namespace MathNet.Numerics.Distributions
{
var x = Gamma.SampleUnchecked(rnd, a, 1.0);
var y = Gamma.SampleUnchecked(rnd, b, 1.0);
return x / (x + y);
return x/(x + y);
}
/// <summary>

12
src/Numerics/Distributions/Continuous/Cauchy.cs

@ -127,7 +127,6 @@ namespace MathNet.Numerics.Distributions
public double Location
{
get { return Median; }
set { SetParameters(value, _scale); }
}
@ -137,7 +136,6 @@ namespace MathNet.Numerics.Distributions
public double Scale
{
get { return _scale; }
set { SetParameters(Median, value); }
}
@ -198,7 +196,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Entropy
{
get { return Math.Log(4.0 * Constants.Pi * _scale); }
get { return Math.Log(4.0*Constants.Pi*_scale); }
}
/// <summary>
@ -216,7 +214,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return ((1.0 / Constants.Pi) * Math.Atan((x - Median) / _scale)) + 0.5;
return ((1.0/Constants.Pi)*Math.Atan((x - Median)/_scale)) + 0.5;
}
#endregion
@ -259,7 +257,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the density at <paramref name="x"/>.</returns>
public double Density(double x)
{
return 1.0 / (Constants.Pi * _scale * (1.0 + (((x - Median) / _scale) * ((x - Median) / _scale))));
return 1.0/(Constants.Pi*_scale*(1.0 + (((x - Median)/_scale)*((x - Median)/_scale))));
}
/// <summary>
@ -269,7 +267,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the log density at <paramref name="x"/>.</returns>
public double DensityLn(double x)
{
return -Math.Log(Constants.Pi * _scale * (1.0 + (((x - Median) / _scale) * ((x - Median) / _scale))));
return -Math.Log(Constants.Pi*_scale*(1.0 + (((x - Median)/_scale)*((x - Median)/_scale))));
}
#endregion
@ -284,7 +282,7 @@ namespace MathNet.Numerics.Distributions
internal static double SampleUnchecked(Random rnd, double location, double scale)
{
var u = rnd.NextDouble();
return location + (scale * Math.Tan(Constants.Pi * (u - 0.5)));
return location + (scale*Math.Tan(Constants.Pi*(u - 0.5)));
}
/// <summary>

19
src/Numerics/Distributions/Continuous/Chi.cs

@ -111,7 +111,6 @@ namespace MathNet.Numerics.Distributions
public double DegreesOfFreedom
{
get { return _dof; }
set { SetParameters(value); }
}
@ -148,7 +147,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Mean
{
get { return Math.Sqrt(2) * (SpecialFunctions.Gamma((_dof + 1.0) / 2.0) / SpecialFunctions.Gamma(_dof / 2.0)); }
get { return Math.Sqrt(2)*(SpecialFunctions.Gamma((_dof + 1.0)/2.0)/SpecialFunctions.Gamma(_dof/2.0)); }
}
/// <summary>
@ -156,7 +155,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Variance
{
get { return _dof - (Mean * Mean); }
get { return _dof - (Mean*Mean); }
}
/// <summary>
@ -172,7 +171,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Entropy
{
get { return SpecialFunctions.GammaLn(_dof / 2.0) + ((_dof - Math.Log(2) - ((_dof - 1.0) * SpecialFunctions.DiGamma(_dof / 2.0))) / 2.0); }
get { return SpecialFunctions.GammaLn(_dof/2.0) + ((_dof - Math.Log(2) - ((_dof - 1.0)*SpecialFunctions.DiGamma(_dof/2.0)))/2.0); }
}
/// <summary>
@ -183,7 +182,7 @@ namespace MathNet.Numerics.Distributions
get
{
var sigma = StdDev;
return (Mean * (1.0 - (2.0 * (sigma * sigma)))) / (sigma * sigma * sigma);
return (Mean*(1.0 - (2.0*(sigma*sigma))))/(sigma*sigma*sigma);
}
}
@ -194,7 +193,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return SpecialFunctions.GammaLowerIncomplete(_dof / 2.0, x * x / 2.0) / SpecialFunctions.Gamma(_dof / 2.0);
return SpecialFunctions.GammaLowerIncomplete(_dof/2.0, x*x/2.0)/SpecialFunctions.Gamma(_dof/2.0);
}
#endregion
@ -248,7 +247,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the density at <paramref name="x"/>.</returns>
public double Density(double x)
{
return (Math.Pow(2.0, 1.0 - (_dof / 2.0)) * Math.Pow(x, _dof - 1.0) * Math.Exp(-x * x / 2.0)) / SpecialFunctions.Gamma(_dof / 2.0);
return (Math.Pow(2.0, 1.0 - (_dof/2.0))*Math.Pow(x, _dof - 1.0)*Math.Exp(-x*x/2.0))/SpecialFunctions.Gamma(_dof/2.0);
}
/// <summary>
@ -258,7 +257,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the log density at <paramref name="x"/>.</returns>
public double DensityLn(double x)
{
return ((1.0 - (_dof / 2.0)) * Math.Log(2.0)) + ((_dof - 1.0) * Math.Log(x)) - (x * x / 2.0) - SpecialFunctions.GammaLn(_dof / 2.0);
return ((1.0 - (_dof/2.0))*Math.Log(2.0)) + ((_dof - 1.0)*Math.Log(x)) - (x*x/2.0) - SpecialFunctions.GammaLn(_dof/2.0);
}
#endregion
@ -286,7 +285,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public double Sample()
{
return SampleUnchecked(RandomSource, (int)_dof);
return SampleUnchecked(RandomSource, (int) _dof);
}
/// <summary>
@ -295,7 +294,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sequence of samples from the distribution.</returns>
public IEnumerable<double> Samples()
{
var dof = (int)_dof;
var dof = (int) _dof;
while (true)
{
yield return SampleUnchecked(RandomSource, dof);

21
src/Numerics/Distributions/Continuous/ChiSquare.cs

@ -103,7 +103,6 @@ namespace MathNet.Numerics.Distributions
public double DegreesOfFreedom
{
get { return Mean; }
set { SetParameters(value); }
}
@ -145,7 +144,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Variance
{
get { return 2.0 * Mean; }
get { return 2.0*Mean; }
}
/// <summary>
@ -153,7 +152,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double StdDev
{
get { return Math.Sqrt(2.0 * Mean); }
get { return Math.Sqrt(2.0*Mean); }
}
/// <summary>
@ -161,7 +160,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Entropy
{
get { return (Mean / 2.0) + Math.Log(2.0 * SpecialFunctions.Gamma(Mean / 2.0)) + ((1.0 - (Mean / 2.0)) * SpecialFunctions.DiGamma(Mean / 2.0)); }
get { return (Mean/2.0) + Math.Log(2.0*SpecialFunctions.Gamma(Mean/2.0)) + ((1.0 - (Mean/2.0))*SpecialFunctions.DiGamma(Mean/2.0)); }
}
/// <summary>
@ -169,7 +168,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Skewness
{
get { return Math.Sqrt(8.0 / Mean); }
get { return Math.Sqrt(8.0/Mean); }
}
/// <summary>
@ -179,7 +178,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return SpecialFunctions.GammaLowerIncomplete(Mean / 2.0, x / 2.0) / SpecialFunctions.Gamma(Mean / 2.0);
return SpecialFunctions.GammaLowerIncomplete(Mean/2.0, x/2.0)/SpecialFunctions.Gamma(Mean/2.0);
}
#endregion
@ -199,7 +198,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Median
{
get { return Mean - (2.0 / 3.0); }
get { return Mean - (2.0/3.0); }
}
/// <summary>
@ -225,7 +224,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the density at <paramref name="x"/>.</returns>
public double Density(double x)
{
return (Math.Pow(x, (Mean / 2.0) - 1.0) * Math.Exp(-x / 2.0)) / (Math.Pow(2.0, Mean / 2.0) * SpecialFunctions.Gamma(Mean / 2.0));
return (Math.Pow(x, (Mean/2.0) - 1.0)*Math.Exp(-x/2.0))/(Math.Pow(2.0, Mean/2.0)*SpecialFunctions.Gamma(Mean/2.0));
}
/// <summary>
@ -235,7 +234,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the log density at <paramref name="x"/>.</returns>
public double DensityLn(double x)
{
return (-x / 2.0) + (((Mean / 2.0) - 1.0) * Math.Log(x)) - ((Mean / 2.0) * Math.Log(2)) - SpecialFunctions.GammaLn(Mean / 2.0);
return (-x/2.0) + (((Mean/2.0) - 1.0)*Math.Log(x)) - ((Mean/2.0)*Math.Log(2)) - SpecialFunctions.GammaLn(Mean/2.0);
}
#endregion
@ -252,7 +251,7 @@ namespace MathNet.Numerics.Distributions
if (Math.Floor(dof) == dof && dof < Int32.MaxValue)
{
double sum = 0;
var n = (int)dof;
var n = (int) dof;
for (var i = 0; i < n; i++)
{
sum += Math.Pow(Normal.Sample(rnd, 0.0, 1.0), 2);
@ -261,7 +260,7 @@ namespace MathNet.Numerics.Distributions
}
//Call the gamma function (see http://en.wikipedia.org/wiki/Gamma_distribution#Specializations
//for a justification)
return Gamma.SampleUnchecked(rnd, dof / 2.0, .5);
return Gamma.SampleUnchecked(rnd, dof/2.0, .5);
}
/// <summary>

20
src/Numerics/Distributions/Continuous/ContinuousUniform.cs

@ -145,7 +145,6 @@ namespace MathNet.Numerics.Distributions
public double Lower
{
get { return _lower; }
set { SetParameters(value, _upper); }
}
@ -155,7 +154,6 @@ namespace MathNet.Numerics.Distributions
public double Upper
{
get { return _upper; }
set { SetParameters(_lower, value); }
}
@ -183,7 +181,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Mean
{
get { return (_lower + _upper) / 2.0; }
get { return (_lower + _upper)/2.0; }
}
/// <summary>
@ -191,7 +189,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Variance
{
get { return (_upper - _lower) * (_upper - _lower) / 12.0; }
get { return (_upper - _lower)*(_upper - _lower)/12.0; }
}
/// <summary>
@ -199,7 +197,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double StdDev
{
get { return (_upper - _lower) / Math.Sqrt(12.0); }
get { return (_upper - _lower)/Math.Sqrt(12.0); }
}
/// <summary>
@ -229,7 +227,7 @@ namespace MathNet.Numerics.Distributions
/// <value></value>
public double Mode
{
get { return (_lower + _upper) / 2.0; }
get { return (_lower + _upper)/2.0; }
}
/// <summary>
@ -238,7 +236,7 @@ namespace MathNet.Numerics.Distributions
/// <value></value>
public double Median
{
get { return (_lower + _upper) / 2.0; }
get { return (_lower + _upper)/2.0; }
}
/// <summary>
@ -266,7 +264,7 @@ namespace MathNet.Numerics.Distributions
{
if (x >= _lower && x <= _upper)
{
return 1.0 / (_upper - _lower);
return 1.0/(_upper - _lower);
}
return 0.0;
@ -304,7 +302,7 @@ namespace MathNet.Numerics.Distributions
return 1.0;
}
return (x - _lower) / (_upper - _lower);
return (x - _lower)/(_upper - _lower);
}
#endregion
@ -318,7 +316,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a uniformly distributed random number.</returns>
internal static double SampleUnchecked(Random rnd, double lower, double upper)
{
return lower + (rnd.NextDouble() * (upper - lower));
return lower + (rnd.NextDouble()*(upper - lower));
}
/// <summary>
@ -379,4 +377,4 @@ namespace MathNet.Numerics.Distributions
}
}
}
}
}

55
src/Numerics/Distributions/Continuous/Erlang.cs

@ -90,7 +90,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a normal distribution.</returns>
public static Erlang WithShapeScale(int shape, double scale)
{
return new Erlang(shape, 1.0 / scale);
return new Erlang(shape, 1.0/scale);
}
/// <summary>
@ -142,8 +142,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public int Shape
{
get { return (int)_shape; }
get { return (int) _shape; }
set { SetParameters(value, _invScale); }
}
@ -152,11 +151,10 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Scale
{
get { return 1.0 / _invScale; }
get { return 1.0/_invScale; }
set
{
var invScale = 1.0 / value;
var invScale = 1.0/value;
if (Double.IsNegativeInfinity(invScale))
{
@ -173,7 +171,6 @@ namespace MathNet.Numerics.Distributions
public double InvScale
{
get { return _invScale; }
set { SetParameters(_shape, value); }
}
@ -222,7 +219,7 @@ namespace MathNet.Numerics.Distributions
return Double.NaN;
}
return _shape / _invScale;
return _shape/_invScale;
}
}
@ -243,7 +240,7 @@ namespace MathNet.Numerics.Distributions
return Double.NaN;
}
return _shape / (_invScale * _invScale);
return _shape/(_invScale*_invScale);
}
}
@ -264,7 +261,7 @@ namespace MathNet.Numerics.Distributions
return Double.NaN;
}
return Math.Sqrt(_shape) / _invScale;
return Math.Sqrt(_shape)/_invScale;
}
}
@ -285,7 +282,7 @@ namespace MathNet.Numerics.Distributions
return Double.NaN;
}
return _shape - Math.Log(_invScale) + SpecialFunctions.GammaLn(_shape) + ((1.0 - _shape) * SpecialFunctions.DiGamma(_shape));
return _shape - Math.Log(_invScale) + SpecialFunctions.GammaLn(_shape) + ((1.0 - _shape)*SpecialFunctions.DiGamma(_shape));
}
}
@ -306,7 +303,7 @@ namespace MathNet.Numerics.Distributions
return Double.NaN;
}
return 2.0 / Math.Sqrt(_shape);
return 2.0/Math.Sqrt(_shape);
}
}
@ -327,7 +324,7 @@ namespace MathNet.Numerics.Distributions
return 0.0;
}
return SpecialFunctions.GammaLowerRegularized(_shape, x * _invScale);
return SpecialFunctions.GammaLowerRegularized(_shape, x*_invScale);
}
#endregion
@ -356,7 +353,7 @@ namespace MathNet.Numerics.Distributions
return Double.NaN;
}
return (_shape - 1.0) / _invScale;
return (_shape - 1.0)/_invScale;
}
}
@ -403,10 +400,10 @@ namespace MathNet.Numerics.Distributions
if (_shape == 1.0)
{
return _invScale * Math.Exp(-_invScale * x);
return _invScale*Math.Exp(-_invScale*x);
}
return Math.Pow(_invScale, _shape) * Math.Pow(x, _shape - 1.0) * Math.Exp(-_invScale * x) / SpecialFunctions.Gamma(_shape);
return Math.Pow(_invScale, _shape)*Math.Pow(x, _shape - 1.0)*Math.Exp(-_invScale*x)/SpecialFunctions.Gamma(_shape);
}
/// <summary>
@ -428,10 +425,10 @@ namespace MathNet.Numerics.Distributions
if (_shape == 1.0)
{
return Math.Log(_invScale) - (_invScale * x);
return Math.Log(_invScale) - (_invScale*x);
}
return (_shape * Math.Log(_invScale)) + ((_shape - 1.0) * Math.Log(x)) - (_invScale * x) - SpecialFunctions.GammaLn(_shape);
return (_shape*Math.Log(_invScale)) + ((_shape - 1.0)*Math.Log(x)) - (_invScale*x) - SpecialFunctions.GammaLn(_shape);
}
#endregion
@ -460,32 +457,32 @@ namespace MathNet.Numerics.Distributions
if (shape < 1.0)
{
a = shape + 1.0;
alphafix = Math.Pow(rnd.NextDouble(), 1.0 / shape);
alphafix = Math.Pow(rnd.NextDouble(), 1.0/shape);
}
var d = a - (1.0 / 3.0);
var c = 1.0 / Math.Sqrt(9.0 * d);
var d = a - (1.0/3.0);
var c = 1.0/Math.Sqrt(9.0*d);
while (true)
{
var x = Normal.Sample(rnd, 0.0, 1.0);
var v = 1.0 + (c * x);
var v = 1.0 + (c*x);
while (v <= 0.0)
{
x = Normal.Sample(rnd, 0.0, 1.0);
v = 1.0 + (c * x);
v = 1.0 + (c*x);
}
v = v * v * v;
v = v*v*v;
var u = rnd.NextDouble();
x = x * x;
if (u < 1.0 - (0.0331 * x * x))
x = x*x;
if (u < 1.0 - (0.0331*x*x))
{
return alphafix * d * v / invScale;
return alphafix*d*v/invScale;
}
if (Math.Log(u) < (0.5 * x) + (d * (1.0 - v + Math.Log(v))))
if (Math.Log(u) < (0.5*x) + (d*(1.0 - v + Math.Log(v))))
{
return alphafix * d * v / invScale;
return alphafix*d*v/invScale;
}
}
}

17
src/Numerics/Distributions/Continuous/Exponential.cs

@ -113,7 +113,6 @@ namespace MathNet.Numerics.Distributions
public double Lambda
{
get { return _lambda; }
set { SetParameters(value); }
}
@ -150,7 +149,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Mean
{
get { return 1.0 / _lambda; }
get { return 1.0/_lambda; }
}
/// <summary>
@ -158,7 +157,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Variance
{
get { return 1.0 / (_lambda * _lambda); }
get { return 1.0/(_lambda*_lambda); }
}
/// <summary>
@ -166,7 +165,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double StdDev
{
get { return 1.0 / _lambda; }
get { return 1.0/_lambda; }
}
/// <summary>
@ -194,7 +193,7 @@ namespace MathNet.Numerics.Distributions
{
if (x >= 0.0)
{
return 1.0 - Math.Exp(-_lambda * x);
return 1.0 - Math.Exp(-_lambda*x);
}
return 0.0;
@ -217,7 +216,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Median
{
get { return Math.Log(2.0) / _lambda; }
get { return Math.Log(2.0)/_lambda; }
}
/// <summary>
@ -245,7 +244,7 @@ namespace MathNet.Numerics.Distributions
{
if (x >= 0.0)
{
return _lambda * Math.Exp(-_lambda * x);
return _lambda*Math.Exp(-_lambda*x);
}
return 0.0;
@ -258,7 +257,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the log density at <paramref name="x"/>.</returns>
public double DensityLn(double x)
{
return Math.Log(_lambda) - (_lambda * x);
return Math.Log(_lambda) - (_lambda*x);
}
#endregion
@ -277,7 +276,7 @@ namespace MathNet.Numerics.Distributions
r = rnd.NextDouble();
}
return -Math.Log(r) / lambda;
return -Math.Log(r)/lambda;
}
/// <summary>

16
src/Numerics/Distributions/Continuous/FisherSnedecor.cs

@ -122,7 +122,6 @@ namespace MathNet.Numerics.Distributions
public double DegreeOfFreedom1
{
get { return _d1; }
set { SetParameters(value, _d2); }
}
@ -132,7 +131,6 @@ namespace MathNet.Numerics.Distributions
public double DegreeOfFreedom2
{
get { return _d2; }
set { SetParameters(_d1, value); }
}
@ -176,7 +174,7 @@ namespace MathNet.Numerics.Distributions
throw new NotSupportedException();
}
return _d2 / (_d2 - 2.0);
return _d2/(_d2 - 2.0);
}
}
@ -192,7 +190,7 @@ namespace MathNet.Numerics.Distributions
throw new NotSupportedException();
}
return (2.0 * _d2 * _d2 * (_d1 + _d2 - 2.0)) / (_d1 * (_d2 - 2.0) * (_d2 - 2.0) * (_d2 - 4.0));
return (2.0*_d2*_d2*(_d1 + _d2 - 2.0))/(_d1*(_d2 - 2.0)*(_d2 - 2.0)*(_d2 - 4.0));
}
}
@ -224,7 +222,7 @@ namespace MathNet.Numerics.Distributions
throw new NotSupportedException();
}
return (((2.0 * _d1) + _d2 - 2.0) * Math.Sqrt(8.0 * (_d2 - 4.0))) / ((_d2 - 6.0) * Math.Sqrt(_d1 * (_d1 + _d2 - 2.0)));
return (((2.0*_d1) + _d2 - 2.0)*Math.Sqrt(8.0*(_d2 - 4.0)))/((_d2 - 6.0)*Math.Sqrt(_d1*(_d1 + _d2 - 2.0)));
}
}
@ -235,7 +233,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return SpecialFunctions.BetaRegularized(_d1 / 2.0, _d2 / 2.0, _d1 * x / ((_d1 * x) + _d2));
return SpecialFunctions.BetaRegularized(_d1/2.0, _d2/2.0, _d1*x/((_d1*x) + _d2));
}
#endregion
@ -254,7 +252,7 @@ namespace MathNet.Numerics.Distributions
throw new NotSupportedException();
}
return (_d2 * (_d1 - 2.0)) / (_d1 * (_d2 + 2.0));
return (_d2*(_d1 - 2.0))/(_d1*(_d2 + 2.0));
}
}
@ -289,7 +287,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the density at <paramref name="x"/>.</returns>
public double Density(double x)
{
return Math.Sqrt(Math.Pow(_d1 * x, _d1) * Math.Pow(_d2, _d2) / Math.Pow((_d1 * x) + _d2, _d1 + _d2)) / (x * SpecialFunctions.Beta(_d1 / 2.0, _d2 / 2.0));
return Math.Sqrt(Math.Pow(_d1*x, _d1)*Math.Pow(_d2, _d2)/Math.Pow((_d1*x) + _d2, _d1 + _d2))/(x*SpecialFunctions.Beta(_d1/2.0, _d2/2.0));
}
/// <summary>
@ -313,7 +311,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a <c>FisherSnedecor</c> distributed random number.</returns>
internal static double SampleUnchecked(Random rnd, double d1, double d2)
{
return (ChiSquare.Sample(rnd, d1) / d1) / (ChiSquare.Sample(rnd, d2) / d2);
return (ChiSquare.Sample(rnd, d1)/d1)/(ChiSquare.Sample(rnd, d2)/d2);
}
/// <summary>

53
src/Numerics/Distributions/Continuous/Gamma.cs

@ -96,7 +96,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a normal distribution.</returns>
public static Gamma WithShapeScale(double shape, double scale)
{
return new Gamma(shape, 1.0 / scale);
return new Gamma(shape, 1.0/scale);
}
/// <summary>
@ -159,7 +159,6 @@ namespace MathNet.Numerics.Distributions
public double Shape
{
get { return _shape; }
set { SetParameters(value, _invScale); }
}
@ -168,11 +167,10 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Scale
{
get { return 1.0 / _invScale; }
get { return 1.0/_invScale; }
set
{
var invScale = 1.0 / value;
var invScale = 1.0/value;
if (Double.IsNegativeInfinity(invScale))
{
@ -189,7 +187,6 @@ namespace MathNet.Numerics.Distributions
public double InvScale
{
get { return _invScale; }
set { SetParameters(_shape, value); }
}
@ -229,7 +226,7 @@ namespace MathNet.Numerics.Distributions
return Double.NaN;
}
return _shape / _invScale;
return _shape/_invScale;
}
}
@ -250,7 +247,7 @@ namespace MathNet.Numerics.Distributions
return Double.NaN;
}
return _shape / (_invScale * _invScale);
return _shape/(_invScale*_invScale);
}
}
@ -271,7 +268,7 @@ namespace MathNet.Numerics.Distributions
return Double.NaN;
}
return Math.Sqrt(_shape / (_invScale * _invScale));
return Math.Sqrt(_shape/(_invScale*_invScale));
}
}
@ -292,7 +289,7 @@ namespace MathNet.Numerics.Distributions
return Double.NaN;
}
return _shape - Math.Log(_invScale) + SpecialFunctions.GammaLn(_shape) + ((1.0 - _shape) * SpecialFunctions.DiGamma(_shape));
return _shape - Math.Log(_invScale) + SpecialFunctions.GammaLn(_shape) + ((1.0 - _shape)*SpecialFunctions.DiGamma(_shape));
}
}
@ -313,7 +310,7 @@ namespace MathNet.Numerics.Distributions
return Double.NaN;
}
return 2.0 / Math.Sqrt(_shape);
return 2.0/Math.Sqrt(_shape);
}
}
@ -338,7 +335,7 @@ namespace MathNet.Numerics.Distributions
return Double.NaN;
}
return (_shape - 1.0) / _invScale;
return (_shape - 1.0)/_invScale;
}
}
@ -385,10 +382,10 @@ namespace MathNet.Numerics.Distributions
if (_shape == 1.0)
{
return _invScale * Math.Exp(-_invScale * x);
return _invScale*Math.Exp(-_invScale*x);
}
return Math.Pow(_invScale, _shape) * Math.Pow(x, _shape - 1.0) * Math.Exp(-_invScale * x) / SpecialFunctions.Gamma(_shape);
return Math.Pow(_invScale, _shape)*Math.Pow(x, _shape - 1.0)*Math.Exp(-_invScale*x)/SpecialFunctions.Gamma(_shape);
}
/// <summary>
@ -410,10 +407,10 @@ namespace MathNet.Numerics.Distributions
if (_shape == 1.0)
{
return Math.Log(_invScale) - (_invScale * x);
return Math.Log(_invScale) - (_invScale*x);
}
return (_shape * Math.Log(_invScale)) + ((_shape - 1.0) * Math.Log(x)) - (_invScale * x) - SpecialFunctions.GammaLn(_shape);
return (_shape*Math.Log(_invScale)) + ((_shape - 1.0)*Math.Log(x)) - (_invScale*x) - SpecialFunctions.GammaLn(_shape);
}
/// <summary>
@ -433,7 +430,7 @@ namespace MathNet.Numerics.Distributions
return 0.0;
}
return SpecialFunctions.GammaLowerRegularized(_shape, x * _invScale);
return SpecialFunctions.GammaLowerRegularized(_shape, x*_invScale);
}
#endregion
@ -462,32 +459,32 @@ namespace MathNet.Numerics.Distributions
if (shape < 1.0)
{
a = shape + 1.0;
alphafix = Math.Pow(rnd.NextDouble(), 1.0 / shape);
alphafix = Math.Pow(rnd.NextDouble(), 1.0/shape);
}
var d = a - (1.0 / 3.0);
var c = 1.0 / Math.Sqrt(9.0 * d);
var d = a - (1.0/3.0);
var c = 1.0/Math.Sqrt(9.0*d);
while (true)
{
var x = Normal.Sample(rnd, 0.0, 1.0);
var v = 1.0 + (c * x);
var v = 1.0 + (c*x);
while (v <= 0.0)
{
x = Normal.Sample(rnd, 0.0, 1.0);
v = 1.0 + (c * x);
v = 1.0 + (c*x);
}
v = v * v * v;
v = v*v*v;
var u = rnd.NextDouble();
x = x * x;
if (u < 1.0 - (0.0331 * x * x))
x = x*x;
if (u < 1.0 - (0.0331*x*x))
{
return alphafix * d * v / invScale;
return alphafix*d*v/invScale;
}
if (Math.Log(u) < (0.5 * x) + (d * (1.0 - v + Math.Log(v))))
if (Math.Log(u) < (0.5*x) + (d*(1.0 - v + Math.Log(v))))
{
return alphafix * d * v / invScale;
return alphafix*d*v/invScale;
}
}
}

20
src/Numerics/Distributions/Continuous/InverseGamma.cs

@ -132,7 +132,6 @@ namespace MathNet.Numerics.Distributions
public double Shape
{
get { return _shape; }
set { SetParameters(value, _scale); }
}
@ -142,7 +141,6 @@ namespace MathNet.Numerics.Distributions
public double Scale
{
get { return _scale; }
set { SetParameters(_shape, value); }
}
@ -186,7 +184,7 @@ namespace MathNet.Numerics.Distributions
throw new NotSupportedException();
}
return _scale / (_shape - 1.0);
return _scale/(_shape - 1.0);
}
}
@ -202,7 +200,7 @@ namespace MathNet.Numerics.Distributions
throw new NotSupportedException();
}
return _scale * _scale / ((_shape - 1.0) * (_shape - 1.0) * (_shape - 2.0));
return _scale*_scale/((_shape - 1.0)*(_shape - 1.0)*(_shape - 2.0));
}
}
@ -211,7 +209,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double StdDev
{
get { return _scale / (Math.Abs(_shape - 1.0) * Math.Sqrt(_shape - 2.0)); }
get { return _scale/(Math.Abs(_shape - 1.0)*Math.Sqrt(_shape - 2.0)); }
}
/// <summary>
@ -219,7 +217,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Entropy
{
get { return _shape + Math.Log(_scale) + SpecialFunctions.GammaLn(_shape) - ((1 + _shape) * SpecialFunctions.DiGamma(_shape)); }
get { return _shape + Math.Log(_scale) + SpecialFunctions.GammaLn(_shape) - ((1 + _shape)*SpecialFunctions.DiGamma(_shape)); }
}
/// <summary>
@ -234,7 +232,7 @@ namespace MathNet.Numerics.Distributions
throw new NotSupportedException();
}
return (4 * Math.Sqrt(_shape - 2)) / (_shape - 3);
return (4*Math.Sqrt(_shape - 2))/(_shape - 3);
}
}
@ -245,7 +243,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return SpecialFunctions.GammaUpperRegularized(_shape, _scale / x);
return SpecialFunctions.GammaUpperRegularized(_shape, _scale/x);
}
#endregion
@ -257,7 +255,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Mode
{
get { return _scale / (_shape + 1.0); }
get { return _scale/(_shape + 1.0); }
}
/// <summary>
@ -294,7 +292,7 @@ namespace MathNet.Numerics.Distributions
{
if (x >= 0.0)
{
return Math.Pow(_scale, _shape) * Math.Pow(x, -_shape - 1.0) * Math.Exp(-_scale / x) / SpecialFunctions.Gamma(_shape);
return Math.Pow(_scale, _shape)*Math.Pow(x, -_shape - 1.0)*Math.Exp(-_scale/x)/SpecialFunctions.Gamma(_shape);
}
return 0.0;
@ -321,7 +319,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a random number from the distribution.</returns>
internal static double SampleUnchecked(Random rnd, double shape, double scale)
{
return 1.0 / Gamma.Sample(rnd, shape, scale);
return 1.0/Gamma.Sample(rnd, shape, scale);
}
/// <summary>

17
src/Numerics/Distributions/Continuous/Laplace.cs

@ -59,7 +59,6 @@ namespace MathNet.Numerics.Distributions
public double Location
{
get { return Mean; }
set { SetParameters(value, _scale); }
}
@ -69,14 +68,14 @@ namespace MathNet.Numerics.Distributions
public double Scale
{
get { return _scale; }
set { SetParameters(Mean, value); }
}
/// <summary>
/// Initializes a new instance of the <see cref="Laplace"/> class (location = 0, scale = 1).
/// </summary>
public Laplace() : this(0.0, 1.0)
public Laplace()
: this(0.0, 1.0)
{
}
@ -181,7 +180,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Variance
{
get { return 2.0 * _scale * _scale; }
get { return 2.0*_scale*_scale; }
}
/// <summary>
@ -189,7 +188,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double StdDev
{
get { return Math.Sqrt(2.0) * _scale; }
get { return Math.Sqrt(2.0)*_scale; }
}
/// <summary>
@ -197,7 +196,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Entropy
{
get { return Math.Log(2.0 * Constants.E * _scale); }
get { return Math.Log(2.0*Constants.E*_scale); }
}
/// <summary>
@ -215,7 +214,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return 0.5 * (1.0 + (Math.Sign(x - Mean) * (1.0 - Math.Exp(-Math.Abs(x - Mean) / _scale))));
return 0.5*(1.0 + (Math.Sign(x - Mean)*(1.0 - Math.Exp(-Math.Abs(x - Mean)/_scale))));
}
#endregion
@ -261,7 +260,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the density at <paramref name="x"/>.</returns>
public double Density(double x)
{
return Math.Exp(-Math.Abs(x - Mean) / _scale) / (2.0 * _scale);
return Math.Exp(-Math.Abs(x - Mean)/_scale)/(2.0*_scale);
}
/// <summary>
@ -286,7 +285,7 @@ namespace MathNet.Numerics.Distributions
internal static double SampleUnchecked(Random rnd, double location, double scale)
{
var u = rnd.NextDouble() - 0.5;
return location - (scale * Math.Sign(u) * Math.Log(1.0 - (2.0 * Math.Abs(u))));
return location - (scale*Math.Sign(u)*Math.Log(1.0 - (2.0*Math.Abs(u))));
}
/// <summary>

37
src/Numerics/Distributions/Continuous/LogNormal.cs

@ -131,7 +131,6 @@ namespace MathNet.Numerics.Distributions
public double Mu
{
get { return _mu; }
set { SetParameters(value, _sigma); }
}
@ -141,7 +140,6 @@ namespace MathNet.Numerics.Distributions
public double Sigma
{
get { return _sigma; }
set { SetParameters(_mu, value); }
}
@ -153,7 +151,6 @@ namespace MathNet.Numerics.Distributions
public Random RandomSource
{
get { return _random; }
set
{
if (value == null)
@ -170,7 +167,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Mean
{
get { return Math.Exp(_mu + (_sigma * _sigma / 2.0)); }
get { return Math.Exp(_mu + (_sigma*_sigma/2.0)); }
}
/// <summary>
@ -180,8 +177,8 @@ namespace MathNet.Numerics.Distributions
{
get
{
var sigma2 = _sigma * _sigma;
return (Math.Exp(sigma2) - 1.0) * Math.Exp(_mu + _mu + sigma2);
var sigma2 = _sigma*_sigma;
return (Math.Exp(sigma2) - 1.0)*Math.Exp(_mu + _mu + sigma2);
}
}
@ -192,8 +189,8 @@ namespace MathNet.Numerics.Distributions
{
get
{
var sigma2 = _sigma * _sigma;
return Math.Sqrt((Math.Exp(sigma2) - 1.0) * Math.Exp(_mu + _mu + sigma2));
var sigma2 = _sigma*_sigma;
return Math.Sqrt((Math.Exp(sigma2) - 1.0)*Math.Exp(_mu + _mu + sigma2));
}
}
@ -212,8 +209,8 @@ namespace MathNet.Numerics.Distributions
{
get
{
var expsigma2 = Math.Exp(_sigma * _sigma);
return (expsigma2 + 2.0) * Math.Sqrt(expsigma2 - 1);
var expsigma2 = Math.Exp(_sigma*_sigma);
return (expsigma2 + 2.0)*Math.Sqrt(expsigma2 - 1);
}
}
@ -226,7 +223,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Mode
{
get { return Math.Exp(_mu - (_sigma * _sigma)); }
get { return Math.Exp(_mu - (_sigma*_sigma)); }
}
/// <summary>
@ -265,8 +262,8 @@ namespace MathNet.Numerics.Distributions
return 0.0;
}
var a = (Math.Log(x) - _mu) / _sigma;
return Math.Exp(-0.5 * a * a) / (x * _sigma * Constants.Sqrt2Pi);
var a = (Math.Log(x) - _mu)/_sigma;
return Math.Exp(-0.5*a*a)/(x*_sigma*Constants.Sqrt2Pi);
}
/// <summary>
@ -281,8 +278,8 @@ namespace MathNet.Numerics.Distributions
return Double.NegativeInfinity;
}
var a = (Math.Log(x) - _mu) / _sigma;
return (-0.5 * a * a) - Math.Log(x * _sigma) - Constants.LogSqrt2Pi;
var a = (Math.Log(x) - _mu)/_sigma;
return (-0.5*a*a) - Math.Log(x*_sigma) - Constants.LogSqrt2Pi;
}
/// <summary>
@ -297,7 +294,7 @@ namespace MathNet.Numerics.Distributions
return 0.0;
}
return 0.5 * (1.0 + SpecialFunctions.Erf((Math.Log(x) - _mu) / (_sigma * Constants.Sqrt2)));
return 0.5*(1.0 + SpecialFunctions.Erf((Math.Log(x) - _mu)/(_sigma*Constants.Sqrt2)));
}
#endregion
@ -320,8 +317,8 @@ namespace MathNet.Numerics.Distributions
while (true)
{
var sample = Normal.SampleUncheckedBoxMuller(RandomSource);
yield return Math.Exp(_mu + (_sigma * sample.Item1));
yield return Math.Exp(_mu + (_sigma * sample.Item2));
yield return Math.Exp(_mu + (_sigma*sample.Item1));
yield return Math.Exp(_mu + (_sigma*sample.Item2));
}
}
@ -359,8 +356,8 @@ namespace MathNet.Numerics.Distributions
while (true)
{
var sample = Normal.SampleUncheckedBoxMuller(rng);
yield return Math.Exp(mu + (sigma * sample.Item1));
yield return Math.Exp(mu + (sigma * sample.Item2));
yield return Math.Exp(mu + (sigma*sample.Item1));
yield return Math.Exp(mu + (sigma*sample.Item2));
}
}
}

59
src/Numerics/Distributions/Continuous/Normal.cs

@ -61,17 +61,19 @@ namespace MathNet.Numerics.Distributions
/// and standard deviation 1.0. The distribution will
/// be initialized with the default <seealso cref="System.Random"/> random number generator.
/// </summary>
public Normal() : this(0.0, 1.0)
public Normal()
: this(0.0, 1.0)
{
}
/// <summary>
/// Initializes a new instance of the Normal class. This is a normal distribution with mean 0.0
/// and standard deviation 1.0. The distribution will
/// be initialized with the default <seealso cref="System.Random"/> random number generator.
/// </summary>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Normal(Random randomSource) : this(0.0, 1.0, randomSource)
public Normal(Random randomSource)
: this(0.0, 1.0, randomSource)
{
}
@ -133,7 +135,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a normal distribution.</returns>
public static Normal WithMeanPrecision(double mean, double precision)
{
return new Normal(mean, 1.0 / Math.Sqrt(precision));
return new Normal(mean, 1.0/Math.Sqrt(precision));
}
/// <summary>
@ -183,11 +185,11 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Precision
{
get { return 1.0 / (_stdDev * _stdDev); }
get { return 1.0/(_stdDev*_stdDev); }
set
{
var sdev = 1.0 / Math.Sqrt(value);
var sdev = 1.0/Math.Sqrt(value);
// Handle the case when the precision is -0.
if (Double.IsInfinity(sdev))
@ -224,7 +226,6 @@ namespace MathNet.Numerics.Distributions
public double Mean
{
get { return _mean; }
set { SetParameters(value, _stdDev); }
}
@ -233,8 +234,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Variance
{
get { return _stdDev * _stdDev; }
get { return _stdDev*_stdDev; }
set { SetParameters(_mean, Math.Sqrt(value)); }
}
@ -244,7 +244,6 @@ namespace MathNet.Numerics.Distributions
public double StdDev
{
get { return _stdDev; }
set { SetParameters(_mean, value); }
}
@ -309,8 +308,8 @@ namespace MathNet.Numerics.Distributions
/// <returns>the density at <paramref name="x"/>.</returns>
internal static double Density(double mean, double sdev, double x)
{
var d = (x - mean) / sdev;
return Math.Exp(-0.5 * d * d) / (Constants.Sqrt2Pi * sdev);
var d = (x - mean)/sdev;
return Math.Exp(-0.5*d*d)/(Constants.Sqrt2Pi*sdev);
}
/// <summary>
@ -322,8 +321,8 @@ namespace MathNet.Numerics.Distributions
/// <returns>the log density at <paramref name="x"/>.</returns>
internal static double DensityLn(double mean, double sdev, double x)
{
var d = (x - mean) / sdev;
return (-0.5 * d * d) - Math.Log(sdev) - Constants.LogSqrt2Pi;
var d = (x - mean)/sdev;
return (-0.5*d*d) - Math.Log(sdev) - Constants.LogSqrt2Pi;
}
/// <summary>
@ -355,7 +354,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
internal static double CumulativeDistribution(double mean, double sdev, double x)
{
return 0.5 * (1.0 + SpecialFunctions.Erf((x - mean) / (sdev * Constants.Sqrt2)));
return 0.5*(1.0 + SpecialFunctions.Erf((x - mean)/(sdev*Constants.Sqrt2)));
}
/// <summary>
@ -377,11 +376,9 @@ namespace MathNet.Numerics.Distributions
/// <returns>the inverse cumulative density at <paramref name="p"/>.</returns>
public double InverseCumulativeDistribution(double p)
{
return _mean - (_stdDev * Math.Sqrt(2.0) * SpecialFunctions.ErfcInv(2.0 * p));
return _mean - (_stdDev*Math.Sqrt(2.0)*SpecialFunctions.ErfcInv(2.0*p));
}
/// <summary>
/// Samples a pair of standard normal distributed random variables using the <i>Box-Muller</i> algorithm.
/// </summary>
@ -389,18 +386,18 @@ namespace MathNet.Numerics.Distributions
/// <returns>a pair of random numbers from the standard normal distribution.</returns>
internal static Tuple<double, double> SampleUncheckedBoxMuller(Random rnd)
{
var v1 = (2.0 * rnd.NextDouble()) - 1.0;
var v2 = (2.0 * rnd.NextDouble()) - 1.0;
var r = (v1 * v1) + (v2 * v2);
var v1 = (2.0*rnd.NextDouble()) - 1.0;
var v2 = (2.0*rnd.NextDouble()) - 1.0;
var r = (v1*v1) + (v2*v2);
while (r >= 1.0 || r == 0.0)
{
v1 = (2.0 * rnd.NextDouble()) - 1.0;
v2 = (2.0 * rnd.NextDouble()) - 1.0;
r = (v1 * v1) + (v2 * v2);
v1 = (2.0*rnd.NextDouble()) - 1.0;
v2 = (2.0*rnd.NextDouble()) - 1.0;
r = (v1*v1) + (v2*v2);
}
var fac = Math.Sqrt(-2.0 * Math.Log(r) / r);
return new Tuple<double, double>(v1 * fac, v2 * fac);
var fac = Math.Sqrt(-2.0*Math.Log(r)/r);
return new Tuple<double, double>(v1*fac, v2*fac);
}
/// <summary>
@ -412,7 +409,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a random number from the distribution.</returns>
internal static double SampleUnchecked(Random rnd, double mean, double stddev)
{
return mean + (stddev * SampleUncheckedBoxMuller(rnd).Item1);
return mean + (stddev*SampleUncheckedBoxMuller(rnd).Item1);
}
/// <summary>
@ -433,8 +430,8 @@ namespace MathNet.Numerics.Distributions
while (true)
{
var sample = SampleUncheckedBoxMuller(RandomSource);
yield return _mean + (_stdDev * sample.Item1);
yield return _mean + (_stdDev * sample.Item2);
yield return _mean + (_stdDev*sample.Item1);
yield return _mean + (_stdDev*sample.Item2);
}
}
@ -472,8 +469,8 @@ namespace MathNet.Numerics.Distributions
while (true)
{
var sample = SampleUncheckedBoxMuller(rnd);
yield return mean + (stddev * sample.Item1);
yield return mean + (stddev * sample.Item2);
yield return mean + (stddev*sample.Item1);
yield return mean + (stddev*sample.Item2);
}
}
}

20
src/Numerics/Distributions/Continuous/Pareto.cs

@ -127,7 +127,6 @@ namespace MathNet.Numerics.Distributions
public double Scale
{
get { return _scale; }
set { SetParameters(value, _shape); }
}
@ -137,7 +136,6 @@ namespace MathNet.Numerics.Distributions
public double Shape
{
get { return _shape; }
set { SetParameters(_scale, value); }
}
@ -181,7 +179,7 @@ namespace MathNet.Numerics.Distributions
throw new NotSupportedException();
}
return _shape * _scale / (_shape - 1.0);
return _shape*_scale/(_shape - 1.0);
}
}
@ -197,7 +195,7 @@ namespace MathNet.Numerics.Distributions
return double.PositiveInfinity;
}
return _scale * _scale * _shape / ((_shape - 1.0) * (_shape - 1.0) * (_shape - 2.0));
return _scale*_scale*_shape/((_shape - 1.0)*(_shape - 1.0)*(_shape - 2.0));
}
}
@ -206,7 +204,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double StdDev
{
get { return (_scale * Math.Sqrt(_shape)) / (Math.Abs(_shape - 1.0) * Math.Sqrt(_shape - 2.0)); }
get { return (_scale*Math.Sqrt(_shape))/(Math.Abs(_shape - 1.0)*Math.Sqrt(_shape - 2.0)); }
}
/// <summary>
@ -214,7 +212,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Entropy
{
get { return Math.Log(_shape / _scale) - (1.0 / _shape) - 1.0; }
get { return Math.Log(_shape/_scale) - (1.0/_shape) - 1.0; }
}
/// <summary>
@ -222,7 +220,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Skewness
{
get { return (2.0 * (_shape + 1.0) / (_shape - 3.0)) * Math.Sqrt((_shape - 2.0) / _shape); }
get { return (2.0*(_shape + 1.0)/(_shape - 3.0))*Math.Sqrt((_shape - 2.0)/_shape); }
}
/// <summary>
@ -232,7 +230,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return 1.0 - Math.Pow(_scale / x, _shape);
return 1.0 - Math.Pow(_scale/x, _shape);
}
#endregion
@ -252,7 +250,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Median
{
get { return _scale * Math.Pow(2.0, 1.0 / _shape); }
get { return _scale*Math.Pow(2.0, 1.0/_shape); }
}
/// <summary>
@ -278,7 +276,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the density at <paramref name="x"/>.</returns>
public double Density(double x)
{
return _shape * Math.Pow(_scale, _shape) / Math.Pow(x, _shape + 1.0);
return _shape*Math.Pow(_scale, _shape)/Math.Pow(x, _shape + 1.0);
}
/// <summary>
@ -302,7 +300,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a random number from the Pareto distribution.</returns>
internal static double SampleUnchecked(Random rnd, double scale, double shape)
{
return scale * Math.Pow(rnd.NextDouble(), -1.0 / shape);
return scale*Math.Pow(rnd.NextDouble(), -1.0/shape);
}
/// <summary>

21
src/Numerics/Distributions/Continuous/Rayleigh.cs

@ -118,7 +118,6 @@ namespace MathNet.Numerics.Distributions
public double Scale
{
get { return _scale; }
set { SetParameters(value); }
}
@ -155,7 +154,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Mean
{
get { return _scale * Math.Sqrt(Constants.PiOver2); }
get { return _scale*Math.Sqrt(Constants.PiOver2); }
}
/// <summary>
@ -163,7 +162,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Variance
{
get { return (2.0 - Constants.PiOver2) * _scale * _scale; }
get { return (2.0 - Constants.PiOver2)*_scale*_scale; }
}
/// <summary>
@ -171,7 +170,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double StdDev
{
get { return Math.Sqrt(2.0 - Constants.PiOver2) * _scale; }
get { return Math.Sqrt(2.0 - Constants.PiOver2)*_scale; }
}
/// <summary>
@ -179,7 +178,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Entropy
{
get { return 1.0 + Math.Log(_scale / Math.Sqrt(2)) + (Constants.EulerMascheroni / 2.0); }
get { return 1.0 + Math.Log(_scale/Math.Sqrt(2)) + (Constants.EulerMascheroni/2.0); }
}
/// <summary>
@ -187,7 +186,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Skewness
{
get { return (2.0 * Math.Sqrt(Constants.Pi) * (Constants.Pi - 3.0)) / Math.Pow(4.0 - Constants.Pi, 1.5); }
get { return (2.0*Math.Sqrt(Constants.Pi)*(Constants.Pi - 3.0))/Math.Pow(4.0 - Constants.Pi, 1.5); }
}
/// <summary>
@ -197,7 +196,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return 1.0 - Math.Exp(-x * x / (2.0 * _scale * _scale));
return 1.0 - Math.Exp(-x*x/(2.0*_scale*_scale));
}
#endregion
@ -217,7 +216,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Median
{
get { return _scale * Math.Sqrt(Math.Log(4.0)); }
get { return _scale*Math.Sqrt(Math.Log(4.0)); }
}
/// <summary>
@ -243,7 +242,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the density at <paramref name="x"/>.</returns>
public double Density(double x)
{
return (x / (_scale * _scale)) * Math.Exp(-x * x / (2.0 * _scale * _scale));
return (x/(_scale*_scale))*Math.Exp(-x*x/(2.0*_scale*_scale));
}
/// <summary>
@ -253,7 +252,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the log density at <paramref name="x"/>.</returns>
public double DensityLn(double x)
{
return Math.Log(x / (_scale * _scale)) - (x * x / (2.0 * _scale * _scale));
return Math.Log(x/(_scale*_scale)) - (x*x/(2.0*_scale*_scale));
}
#endregion
@ -266,7 +265,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a random number from the Rayleigh distribution.</returns>
internal static double SampleUnchecked(Random rnd, double scale)
{
return scale * Math.Sqrt(-2.0 * Math.Log(rnd.NextDouble()));
return scale*Math.Sqrt(-2.0*Math.Log(rnd.NextDouble()));
}
/// <summary>

36
src/Numerics/Distributions/Continuous/Stable.cs

@ -155,7 +155,6 @@ namespace MathNet.Numerics.Distributions
public double Alpha
{
get { return _alpha; }
set { SetParameters(value, _beta, _scale, _location); }
}
@ -165,7 +164,6 @@ namespace MathNet.Numerics.Distributions
public double Beta
{
get { return _beta; }
set { SetParameters(_alpha, value, _scale, _location); }
}
@ -175,7 +173,6 @@ namespace MathNet.Numerics.Distributions
public double Scale
{
get { return _scale; }
set { SetParameters(_alpha, _beta, value, _location); }
}
@ -185,7 +182,6 @@ namespace MathNet.Numerics.Distributions
public double Location
{
get { return _location; }
set { SetParameters(_alpha, _beta, _scale, value); }
}
@ -242,7 +238,7 @@ namespace MathNet.Numerics.Distributions
{
if (_alpha == 2)
{
return 2.0 * _scale * _scale;
return 2.0*_scale*_scale;
}
return Double.PositiveInfinity;
@ -258,7 +254,7 @@ namespace MathNet.Numerics.Distributions
{
if (_alpha == 2)
{
return Math.Sqrt(2.0) * _scale;
return Math.Sqrt(2.0)*_scale;
}
return Double.PositiveInfinity;
@ -329,7 +325,7 @@ namespace MathNet.Numerics.Distributions
static double LevyCumulativeDistribution(double scale, double location, double x)
{
// The parameters scale and location must be correct
return SpecialFunctions.Erfc(Math.Sqrt(scale / (2 * (x - location))));
return SpecialFunctions.Erfc(Math.Sqrt(scale/(2*(x - location))));
}
#endregion
@ -434,7 +430,7 @@ namespace MathNet.Numerics.Distributions
throw new NotSupportedException();
}
return (Math.Sqrt(scale / Constants.Pi2) * Math.Exp(-scale / (2 * (x - location)))) / Math.Pow(x - location, 1.5);
return (Math.Sqrt(scale/Constants.Pi2)*Math.Exp(-scale/(2*(x - location))))/Math.Pow(x - location, 1.5);
}
/// <summary>
@ -465,23 +461,23 @@ namespace MathNet.Numerics.Distributions
if (!1.0.AlmostEqual(alpha))
{
var theta = (1.0 / alpha) * Math.Atan(beta * Math.Tan(Constants.PiOver2 * alpha));
var angle = alpha * (randTheta + theta);
var part1 = beta * Math.Tan(Constants.PiOver2 * alpha);
var theta = (1.0/alpha)*Math.Atan(beta*Math.Tan(Constants.PiOver2*alpha));
var angle = alpha*(randTheta + theta);
var part1 = beta*Math.Tan(Constants.PiOver2*alpha);
var factor = Math.Pow(1.0 + (part1 * part1), 1.0 / (2.0 * alpha));
var factor1 = Math.Sin(angle) / Math.Pow(Math.Cos(randTheta), (1.0 / alpha));
var factor2 = Math.Pow(Math.Cos(randTheta - angle) / randW, (1 - alpha) / alpha);
var factor = Math.Pow(1.0 + (part1*part1), 1.0/(2.0*alpha));
var factor1 = Math.Sin(angle)/Math.Pow(Math.Cos(randTheta), (1.0/alpha));
var factor2 = Math.Pow(Math.Cos(randTheta - angle)/randW, (1 - alpha)/alpha);
return location + scale * (factor * factor1 * factor2);
return location + scale*(factor*factor1*factor2);
}
else
{
var part1 = Constants.PiOver2 + (beta * randTheta);
var summand = part1 * Math.Tan(randTheta);
var subtrahend = beta * Math.Log(Constants.PiOver2 * randW * Math.Cos(randTheta) / part1);
var part1 = Constants.PiOver2 + (beta*randTheta);
var summand = part1*Math.Tan(randTheta);
var subtrahend = beta*Math.Log(Constants.PiOver2*randW*Math.Cos(randTheta)/part1);
return location + scale * ((2.0 / Math.PI) * (summand - subtrahend));
return location + scale*((2.0/Math.PI)*(summand - subtrahend));
}
}
@ -548,4 +544,4 @@ namespace MathNet.Numerics.Distributions
}
}
}
}
}

46
src/Numerics/Distributions/Continuous/StudentT.cs

@ -77,7 +77,8 @@ namespace MathNet.Numerics.Distributions
/// scale 1.0 and degrees of freedom 1. The distribution will
/// be initialized with the default <seealso cref="System.Random"/> random number generator.
/// </summary>
public StudentT() : this(0.0, 1.0, 1.0)
public StudentT()
: this(0.0, 1.0, 1.0)
{
}
@ -161,7 +162,6 @@ namespace MathNet.Numerics.Distributions
public double Location
{
get { return _location; }
set { SetParameters(value, _scale, _dof); }
}
@ -171,7 +171,6 @@ namespace MathNet.Numerics.Distributions
public double Scale
{
get { return _scale; }
set { SetParameters(_location, value, _dof); }
}
@ -181,7 +180,6 @@ namespace MathNet.Numerics.Distributions
public double DegreesOfFreedom
{
get { return _dof; }
set { SetParameters(_location, _scale, value); }
}
@ -221,12 +219,12 @@ namespace MathNet.Numerics.Distributions
{
if (Double.IsPositiveInfinity(_dof))
{
return _scale * _scale;
return _scale*_scale;
}
if (_dof > 2.0)
{
return _dof * _scale * _scale / (_dof - 2.0);
return _dof*_scale*_scale/(_dof - 2.0);
}
return _dof > 1.0 ? Double.PositiveInfinity : Double.NaN;
@ -242,12 +240,12 @@ namespace MathNet.Numerics.Distributions
{
if (Double.IsPositiveInfinity(_dof))
{
return Math.Sqrt(_scale * _scale);
return Math.Sqrt(_scale*_scale);
}
if (_dof > 2.0)
{
return Math.Sqrt(_dof * _scale * _scale / (_dof - 2.0));
return Math.Sqrt(_dof*_scale*_scale/(_dof - 2.0));
}
return _dof > 1.0 ? Double.PositiveInfinity : Double.NaN;
@ -266,7 +264,7 @@ namespace MathNet.Numerics.Distributions
throw new NotSupportedException();
}
return (((_dof + 1.0) / 2.0) * (SpecialFunctions.DiGamma((1.0 + _dof) / 2.0) - SpecialFunctions.DiGamma(_dof / 2.0))) + Math.Log(Math.Sqrt(_dof) * SpecialFunctions.Beta(_dof / 2.0, 1.0 / 2.0));
return (((_dof + 1.0)/2.0)*(SpecialFunctions.DiGamma((1.0 + _dof)/2.0) - SpecialFunctions.DiGamma(_dof/2.0))) + Math.Log(Math.Sqrt(_dof)*SpecialFunctions.Beta(_dof/2.0, 1.0/2.0));
}
}
@ -335,11 +333,11 @@ namespace MathNet.Numerics.Distributions
return Normal.Density(_location, _scale, x);
}
var d = (x - _location) / _scale;
return Math.Exp(SpecialFunctions.GammaLn((_dof + 1.0) / 2.0) - SpecialFunctions.GammaLn(_dof / 2.0))
* Math.Pow(1.0 + (d * d / _dof), -0.5 * (_dof + 1.0))
/ Math.Sqrt(_dof * Math.PI)
/ _scale;
var d = (x - _location)/_scale;
return Math.Exp(SpecialFunctions.GammaLn((_dof + 1.0)/2.0) - SpecialFunctions.GammaLn(_dof/2.0))
*Math.Pow(1.0 + (d*d/_dof), -0.5*(_dof + 1.0))
/Math.Sqrt(_dof*Math.PI)
/_scale;
}
/// <summary>
@ -355,11 +353,11 @@ namespace MathNet.Numerics.Distributions
return Normal.DensityLn(_location, _scale, x);
}
var d = (x - _location) / _scale;
return SpecialFunctions.GammaLn((_dof + 1.0) / 2.0)
- (0.5 * ((_dof + 1.0) * Math.Log(1.0 + (d * d / _dof))))
- SpecialFunctions.GammaLn(_dof / 2.0)
- (0.5 * Math.Log(_dof * Math.PI)) - Math.Log(_scale);
var d = (x - _location)/_scale;
return SpecialFunctions.GammaLn((_dof + 1.0)/2.0)
- (0.5*((_dof + 1.0)*Math.Log(1.0 + (d*d/_dof))))
- SpecialFunctions.GammaLn(_dof/2.0)
- (0.5*Math.Log(_dof*Math.PI)) - Math.Log(_scale);
}
/// <summary>
@ -375,9 +373,9 @@ namespace MathNet.Numerics.Distributions
return Normal.CumulativeDistribution(_location, _scale, x);
}
var k = (x - _location) / _scale;
var h = _dof / (_dof + (k * k));
var ib = 0.5 * SpecialFunctions.BetaRegularized(_dof / 2.0, 0.5, h);
var k = (x - _location)/_scale;
var h = _dof/(_dof + (k*k));
var ib = 0.5*SpecialFunctions.BetaRegularized(_dof/2.0, 0.5, h);
return x <= _location ? ib : 1.0 - ib;
}
@ -396,8 +394,8 @@ namespace MathNet.Numerics.Distributions
internal static double SampleUnchecked(Random rnd, double location, double scale, double dof)
{
var n = Normal.SampleUncheckedBoxMuller(rnd).Item1;
var g = Gamma.SampleUnchecked(rnd, 0.5 * dof, 0.5);
return location + (scale * n * Math.Sqrt(dof / g));
var g = Gamma.SampleUnchecked(rnd, 0.5*dof, 0.5);
return location + (scale*n*Math.Sqrt(dof/g));
}
/// <summary>

28
src/Numerics/Distributions/Continuous/Weibull.cs

@ -143,7 +143,6 @@ namespace MathNet.Numerics.Distributions
public double Shape
{
get { return _shape; }
set { SetParameters(value, _scale); }
}
@ -153,7 +152,6 @@ namespace MathNet.Numerics.Distributions
public double Scale
{
get { return _scale; }
set { SetParameters(_shape, value); }
}
@ -181,7 +179,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Mean
{
get { return _scale * SpecialFunctions.Gamma(1.0 + (1.0 / _shape)); }
get { return _scale*SpecialFunctions.Gamma(1.0 + (1.0/_shape)); }
}
/// <summary>
@ -189,7 +187,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Variance
{
get { return (_scale * _scale * SpecialFunctions.Gamma(1.0 + (2.0 / _shape))) - (Mean * Mean); }
get { return (_scale*_scale*SpecialFunctions.Gamma(1.0 + (2.0/_shape))) - (Mean*Mean); }
}
/// <summary>
@ -205,7 +203,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Entropy
{
get { return (Constants.EulerMascheroni * (1.0 - (1.0 / _shape))) + Math.Log(_scale / _shape) + 1.0; }
get { return (Constants.EulerMascheroni*(1.0 - (1.0/_shape))) + Math.Log(_scale/_shape) + 1.0; }
}
/// <summary>
@ -217,9 +215,9 @@ namespace MathNet.Numerics.Distributions
{
double mu = Mean;
double sigma = StdDev;
double sigma2 = sigma * sigma;
double sigma3 = sigma2 * sigma;
return ((_scale * _scale * _scale * SpecialFunctions.Gamma(1.0 + (3.0 / _shape))) - (3.0 * sigma2 * mu) - (mu * mu * mu)) / sigma3;
double sigma2 = sigma*sigma;
double sigma3 = sigma2*sigma;
return ((_scale*_scale*_scale*SpecialFunctions.Gamma(1.0 + (3.0/_shape))) - (3.0*sigma2*mu) - (mu*mu*mu))/sigma3;
}
}
@ -239,7 +237,7 @@ namespace MathNet.Numerics.Distributions
return 0.0;
}
return _scale * Math.Pow((_shape - 1.0) / _shape, 1.0 / _shape);
return _scale*Math.Pow((_shape - 1.0)/_shape, 1.0/_shape);
}
}
@ -248,7 +246,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Median
{
get { return _scale * Math.Pow(Constants.Ln2, 1.0 / _shape); }
get { return _scale*Math.Pow(Constants.Ln2, 1.0/_shape); }
}
/// <summary>
@ -278,10 +276,10 @@ namespace MathNet.Numerics.Distributions
{
if (x == 0.0 && _shape == 1.0)
{
return _shape / _scale;
return _shape/_scale;
}
return _shape * Math.Pow(x / _scale, _shape - 1.0) * Math.Exp(-Math.Pow(x, _shape) * _scalePowShapeInv) / _scale;
return _shape*Math.Pow(x/_scale, _shape - 1.0)*Math.Exp(-Math.Pow(x, _shape)*_scalePowShapeInv)/_scale;
}
return 0.0;
@ -301,7 +299,7 @@ namespace MathNet.Numerics.Distributions
return Math.Log(_shape) - Math.Log(_scale);
}
return Math.Log(_shape) + ((_shape - 1.0) * Math.Log(x / _scale)) - (Math.Pow(x, _shape) * _scalePowShapeInv) - Math.Log(_scale);
return Math.Log(_shape) + ((_shape - 1.0)*Math.Log(x/_scale)) - (Math.Pow(x, _shape)*_scalePowShapeInv) - Math.Log(_scale);
}
return double.NegativeInfinity;
@ -319,7 +317,7 @@ namespace MathNet.Numerics.Distributions
return 0.0;
}
return -SpecialFunctions.ExponentialMinusOne(-Math.Pow(x, _shape) * _scalePowShapeInv);
return -SpecialFunctions.ExponentialMinusOne(-Math.Pow(x, _shape)*_scalePowShapeInv);
}
#endregion
@ -335,7 +333,7 @@ namespace MathNet.Numerics.Distributions
internal static double SampleUnchecked(Random rnd, double shape, double scale)
{
var x = rnd.NextDouble();
return scale * Math.Pow(-Math.Log(x), 1.0 / shape);
return scale*Math.Pow(-Math.Log(x), 1.0/shape);
}
/// <summary>

9
src/Numerics/Distributions/Discrete/Bernoulli.cs

@ -120,7 +120,6 @@ namespace MathNet.Numerics.Distributions
public double P
{
get { return _p; }
set { SetParameters(value); }
}
@ -156,7 +155,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double StdDev
{
get { return Math.Sqrt(_p * (1.0 - _p)); }
get { return Math.Sqrt(_p*(1.0 - _p)); }
}
/// <summary>
@ -164,7 +163,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Variance
{
get { return _p * (1.0 - _p); }
get { return _p*(1.0 - _p); }
}
/// <summary>
@ -172,7 +171,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Entropy
{
get { return -(_p * Math.Log(_p)) - ((1.0 - _p) * Math.Log(1.0 - _p)); }
get { return -(_p*Math.Log(_p)) - ((1.0 - _p)*Math.Log(1.0 - _p)); }
}
/// <summary>
@ -180,7 +179,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Skewness
{
get { return (1.0 - (2.0 * _p)) / Math.Sqrt(_p * (1.0 - _p)); }
get { return (1.0 - (2.0*_p))/Math.Sqrt(_p*(1.0 - _p)); }
}
/// <summary>

24
src/Numerics/Distributions/Discrete/Binomial.cs

@ -138,7 +138,6 @@ namespace MathNet.Numerics.Distributions
public double P
{
get { return _p; }
set { SetParameters(value, _n); }
}
@ -148,7 +147,6 @@ namespace MathNet.Numerics.Distributions
public int N
{
get { return _n; }
set { SetParameters(_p, value); }
}
@ -176,7 +174,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Mean
{
get { return _p * _n; }
get { return _p*_n; }
}
/// <summary>
@ -184,7 +182,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double StdDev
{
get { return Math.Sqrt(_p * (1.0 - _p) * _n); }
get { return Math.Sqrt(_p*(1.0 - _p)*_n); }
}
/// <summary>
@ -192,7 +190,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Variance
{
get { return _p * (1.0 - _p) * _n; }
get { return _p*(1.0 - _p)*_n; }
}
/// <summary>
@ -211,7 +209,7 @@ namespace MathNet.Numerics.Distributions
for (var i = 0; i <= _n; i++)
{
var p = Probability(i);
e -= p * Math.Log(p);
e -= p*Math.Log(p);
}
return e;
@ -223,7 +221,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Skewness
{
get { return (1.0 - (2.0 * _p)) / Math.Sqrt(_n * _p * (1.0 - _p)); }
get { return (1.0 - (2.0*_p))/Math.Sqrt(_n*_p*(1.0 - _p)); }
}
/// <summary>
@ -260,9 +258,9 @@ namespace MathNet.Numerics.Distributions
}
var cdf = 0.0;
for (var i = 0; i <= (int)Math.Floor(x); i++)
for (var i = 0; i <= (int) Math.Floor(x); i++)
{
cdf += Combinatorics.Combinations(_n, i) * Math.Pow(_p, i) * Math.Pow(1.0 - _p, _n - i);
cdf += Combinatorics.Combinations(_n, i)*Math.Pow(_p, i)*Math.Pow(1.0 - _p, _n - i);
}
return cdf;
@ -289,7 +287,7 @@ namespace MathNet.Numerics.Distributions
return 0;
}
return (int)Math.Floor((_n + 1) * _p);
return (int) Math.Floor((_n + 1)*_p);
}
}
@ -298,7 +296,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public int Median
{
get { return (int)Math.Floor(_p * _n); }
get { return (int) Math.Floor(_p*_n); }
}
/// <summary>
@ -338,7 +336,7 @@ namespace MathNet.Numerics.Distributions
return 0.0;
}
return SpecialFunctions.Binomial(_n, k) * Math.Pow(_p, k) * Math.Pow(1.0 - _p, _n - k);
return SpecialFunctions.Binomial(_n, k)*Math.Pow(_p, k)*Math.Pow(1.0 - _p, _n - k);
}
/// <summary>
@ -378,7 +376,7 @@ namespace MathNet.Numerics.Distributions
return Double.NegativeInfinity;
}
return SpecialFunctions.BinomialLn(_n, k) + (k * Math.Log(_p)) + ((_n - k) * Math.Log(1.0 - _p));
return SpecialFunctions.BinomialLn(_n, k) + (k*Math.Log(_p)) + ((_n - k)*Math.Log(1.0 - _p));
}
#endregion

8
src/Numerics/Distributions/Discrete/Categorical.cs

@ -197,7 +197,7 @@ namespace MathNet.Numerics.Distributions
/// exactly in a floating point representation.</remarks>
public double[] P
{
get { return (double[])_pmfNormalized.Clone(); }
get { return (double[]) _pmfNormalized.Clone(); }
set { SetParameters(value); }
}
@ -249,7 +249,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Entropy
{
get { return _pmfNormalized.Sum(p => p * Math.Log(p)); }
get { return _pmfNormalized.Sum(p => p*Math.Log(p)); }
}
/// <summary>
@ -315,7 +315,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public int Median
{
get { return (int)_pmfNormalized.Median(); }
get { return (int) _pmfNormalized.Median(); }
}
/// <summary>
@ -388,7 +388,7 @@ namespace MathNet.Numerics.Distributions
internal static int SampleUnchecked(Random rnd, double[] cdfUnnormalized)
{
// TODO : use binary search to speed up this procedure.
var u = rnd.NextDouble() * cdfUnnormalized[cdfUnnormalized.Length - 1];
var u = rnd.NextDouble()*cdfUnnormalized[cdfUnnormalized.Length - 1];
var idx = 0;
while (u > cdfUnnormalized[idx])

58
src/Numerics/Distributions/Discrete/ConwayMaxwellPoisson.cs

@ -152,7 +152,6 @@ namespace MathNet.Numerics.Distributions
public double Lambda
{
get { return _lambda; }
set { SetParameters(value, _nu); }
}
@ -163,7 +162,6 @@ namespace MathNet.Numerics.Distributions
public double Nu
{
get { return _nu; }
set { SetParameters(_lambda, value); }
}
@ -219,28 +217,28 @@ namespace MathNet.Numerics.Distributions
var z = 1 + _lambda;
// The probability of the next term.
var a1 = _lambda * _lambda / Math.Pow(2, _nu);
var a1 = _lambda*_lambda/Math.Pow(2, _nu);
// The unnormalized mean.
var zx = _lambda;
// The contribution of the next term to the mean.
var ax1 = 2 * a1;
var ax1 = 2*a1;
for (var i = 3; i < 1000; i++)
{
var e = _lambda / Math.Pow(i, _nu);
var ex = _lambda / Math.Pow(i, _nu - 1) / (i - 1);
var a2 = a1 * e;
var ax2 = ax1 * ex;
var e = _lambda/Math.Pow(i, _nu);
var ex = _lambda/Math.Pow(i, _nu - 1)/(i - 1);
var a2 = a1*e;
var ax2 = ax1*ex;
var m = zx / z;
var upper = (zx + (ax1 / (1 - (ax2 / ax1)))) / z;
var lower = zx / (z + (a1 / (1 - (a2 / a1))));
var m = zx/z;
var upper = (zx + (ax1/(1 - (ax2/ax1))))/z;
var lower = zx/(z + (a1/(1 - (a2/a1))));
if ((ax2 < ax1) && (a2 < a1))
{
var r = (upper - lower) / m;
var r = (upper - lower)/m;
if (r < Tolerance)
{
break;
@ -253,7 +251,7 @@ namespace MathNet.Numerics.Distributions
ax1 = ax2;
}
_mean = zx / z;
_mean = zx/z;
return _mean;
}
}
@ -280,28 +278,28 @@ namespace MathNet.Numerics.Distributions
var z = 1 + _lambda;
// The probability of the next term.
var a1 = _lambda * _lambda / Math.Pow(2, _nu);
var a1 = _lambda*_lambda/Math.Pow(2, _nu);
// The unnormalized second moment.
var zxx = _lambda;
// The contribution of the next term to the second moment.
var axx1 = 4 * a1;
var axx1 = 4*a1;
for (var i = 3; i < 1000; i++)
{
var e = _lambda / Math.Pow(i, _nu);
var exx = _lambda / Math.Pow(i, _nu - 2) / (i - 1) / (i - 1);
var a2 = a1 * e;
var axx2 = axx1 * exx;
var e = _lambda/Math.Pow(i, _nu);
var exx = _lambda/Math.Pow(i, _nu - 2)/(i - 1)/(i - 1);
var a2 = a1*e;
var axx2 = axx1*exx;
var m = zxx / z;
var upper = (zxx + (axx1 / (1 - (axx2 / axx1)))) / z;
var lower = zxx / (z + (a1 / (1 - (a2 / a1))));
var m = zxx/z;
var upper = (zxx + (axx1/(1 - (axx2/axx1))))/z;
var lower = zxx/(z + (a1/(1 - (a2/a1))));
if ((axx2 < axx1) && (a2 < a1))
{
var r = (upper - lower) / m;
var r = (upper - lower)/m;
if (r < Tolerance)
{
break;
@ -315,7 +313,7 @@ namespace MathNet.Numerics.Distributions
}
var mean = Mean;
_variance = (zxx / z) - (mean * mean);
_variance = (zxx/z) - (mean*mean);
return _variance;
}
}
@ -405,7 +403,7 @@ namespace MathNet.Numerics.Distributions
/// </returns>
public double Probability(int k)
{
return Math.Pow(_lambda, k) / Math.Pow(SpecialFunctions.Factorial(k), _nu) / Z;
return Math.Pow(_lambda, k)/Math.Pow(SpecialFunctions.Factorial(k), _nu)/Z;
}
/// <summary>
@ -459,10 +457,10 @@ namespace MathNet.Numerics.Distributions
for (var i = 2; i < 1000; i++)
{
// The new addition for term i.
var e = lambda / Math.Pow(i, nu);
var e = lambda/Math.Pow(i, nu);
// The new term.
t = t * e;
t = t*e;
// The updated normalization constant.
z = z + t;
@ -470,7 +468,7 @@ namespace MathNet.Numerics.Distributions
// The stopping criterion.
if (e < 1)
{
if (t / (1 - e) / z < Tolerance)
if (t/(1 - e)/z < Tolerance)
{
break;
}
@ -493,14 +491,14 @@ namespace MathNet.Numerics.Distributions
internal static int SampleUnchecked(Random rnd, double lambda, double nu, double z)
{
var u = rnd.NextDouble();
var p = 1.0 / z;
var p = 1.0/z;
var cdf = p;
var i = 0;
while (u > cdf)
{
i++;
p = p * lambda / Math.Pow(i, nu);
p = p*lambda/Math.Pow(i, nu);
cdf += p;
}

18
src/Numerics/Distributions/Discrete/DiscreteUniform.cs

@ -130,7 +130,6 @@ namespace MathNet.Numerics.Distributions
public int LowerBound
{
get { return _lower; }
set { SetParameters(value, _upper); }
}
@ -140,7 +139,6 @@ namespace MathNet.Numerics.Distributions
public int UpperBound
{
get { return _upper; }
set { SetParameters(_lower, value); }
}
@ -168,7 +166,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Mean
{
get { return (_lower + _upper) / 2.0; }
get { return (_lower + _upper)/2.0; }
}
/// <summary>
@ -176,7 +174,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double StdDev
{
get { return Math.Sqrt((((_upper - _lower + 1.0) * (_upper - _lower + 1.0)) - 1.0) / 12.0); }
get { return Math.Sqrt((((_upper - _lower + 1.0)*(_upper - _lower + 1.0)) - 1.0)/12.0); }
}
/// <summary>
@ -184,7 +182,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Variance
{
get { return (((_upper - _lower + 1.0) * (_upper - _lower + 1.0)) - 1.0) / 12.0; }
get { return (((_upper - _lower + 1.0)*(_upper - _lower + 1.0)) - 1.0)/12.0; }
}
/// <summary>
@ -236,7 +234,7 @@ namespace MathNet.Numerics.Distributions
return 1.0;
}
return Math.Min(1.0, (Math.Floor(x) - _lower + 1) / (_upper - _lower + 1));
return Math.Min(1.0, (Math.Floor(x) - _lower + 1)/(_upper - _lower + 1));
}
#endregion
@ -248,7 +246,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public int Mode
{
get { return (int)Math.Floor((_lower + _upper) / 2.0); }
get { return (int) Math.Floor((_lower + _upper)/2.0); }
}
/// <summary>
@ -256,7 +254,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public int Median
{
get { return (int)Math.Floor((_lower + _upper) / 2.0); }
get { return (int) Math.Floor((_lower + _upper)/2.0); }
}
/// <summary>
@ -270,7 +268,7 @@ namespace MathNet.Numerics.Distributions
{
if (k >= _lower && k <= _upper)
{
return 1.0 / (_upper - _lower + 1);
return 1.0/(_upper - _lower + 1);
}
return 0.0;
@ -304,7 +302,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>A random sample from the discrete uniform distribution.</returns>
internal static int SampleUnchecked(Random rnd, int lower, int upper)
{
return (rnd.Next() % (upper - lower + 1)) + lower;
return (rnd.Next()%(upper - lower + 1)) + lower;
}
/// <summary>

18
src/Numerics/Distributions/Discrete/Geometric.cs

@ -150,7 +150,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Mean
{
get { return 1.0 / _p; }
get { return 1.0/_p; }
}
/// <summary>
@ -158,7 +158,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Variance
{
get { return (1.0 - _p) / (_p * _p); }
get { return (1.0 - _p)/(_p*_p); }
}
/// <summary>
@ -166,7 +166,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double StdDev
{
get { return Math.Sqrt(1.0 - _p) / _p; }
get { return Math.Sqrt(1.0 - _p)/_p; }
}
/// <summary>
@ -174,7 +174,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Entropy
{
get { return ((-_p * Math.Log(_p, 2.0)) - ((1.0 - _p) * Math.Log(1.0 - _p, 2.0))) / _p; }
get { return ((-_p*Math.Log(_p, 2.0)) - ((1.0 - _p)*Math.Log(1.0 - _p, 2.0)))/_p; }
}
/// <summary>
@ -183,7 +183,7 @@ namespace MathNet.Numerics.Distributions
/// <remarks>Throws a not supported exception.</remarks>
public double Skewness
{
get { return (2.0 - _p) / Math.Sqrt(1.0 - _p); }
get { return (2.0 - _p)/Math.Sqrt(1.0 - _p); }
}
/// <summary>
@ -213,7 +213,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public int Median
{
get { return (int)Math.Ceiling(-Constants.Ln2 / Math.Log(1 - _p)); }
get { return (int) Math.Ceiling(-Constants.Ln2/Math.Log(1 - _p)); }
}
/// <summary>
@ -246,7 +246,7 @@ namespace MathNet.Numerics.Distributions
return 0.0;
}
return Math.Pow(1.0 - _p, k - 1) * _p;
return Math.Pow(1.0 - _p, k - 1)*_p;
}
/// <summary>
@ -263,7 +263,7 @@ namespace MathNet.Numerics.Distributions
return Double.NegativeInfinity;
}
return ((k - 1) * Math.Log(1.0 - _p)) + Math.Log(_p);
return ((k - 1)*Math.Log(1.0 - _p)) + Math.Log(_p);
}
#endregion
@ -278,7 +278,7 @@ namespace MathNet.Numerics.Distributions
/// </returns>
internal static int SampleUnchecked(Random rnd, double p)
{
return p == 1.0 ? 1 : (int)Math.Ceiling(-Math.Log(1.0 - rnd.NextDouble(), 1.0 - p));
return p == 1.0 ? 1 : (int) Math.Ceiling(-Math.Log(1.0 - rnd.NextDouble(), 1.0 - p));
}
/// <summary>

21
src/Numerics/Distributions/Discrete/Hypergeometric.cs

@ -196,7 +196,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Mean
{
get { return (double)_m * _n / _populationSize; }
get { return (double) _m*_n/_populationSize; }
}
/// <summary>
@ -204,7 +204,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Variance
{
get { return _n * _m * (_populationSize - _n) * (_populationSize - _m) / (_populationSize * _populationSize * (_populationSize - 1.0)); }
get { return _n*_m*(_populationSize - _n)*(_populationSize - _m)/(_populationSize*_populationSize*(_populationSize - 1.0)); }
}
/// <summary>
@ -228,7 +228,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Skewness
{
get { return (Math.Sqrt(_populationSize - 1.0) * (_populationSize - (2 * _n)) * (_populationSize - (2 * _m))) / (Math.Sqrt(_n * _m * (_populationSize - _m) * (_populationSize - _n)) * (_populationSize - 2.0)); }
get { return (Math.Sqrt(_populationSize - 1.0)*(_populationSize - (2*_n))*(_populationSize - (2*_m)))/(Math.Sqrt(_n*_m*(_populationSize - _m)*(_populationSize - _n))*(_populationSize - 2.0)); }
}
/// <summary>
@ -251,13 +251,13 @@ namespace MathNet.Numerics.Distributions
}
var sum = 0.0;
var k = (int)Math.Ceiling(x - alpha) - 1;
var k = (int) Math.Ceiling(x - alpha) - 1;
for (var i = alpha; i <= alpha + k; i++)
{
sum += SpecialFunctions.Binomial(_m, i) * SpecialFunctions.Binomial(_populationSize - _m, _n - i);
sum += SpecialFunctions.Binomial(_m, i)*SpecialFunctions.Binomial(_populationSize - _m, _n - i);
}
return sum / SpecialFunctions.Binomial(_populationSize, _n);
return sum/SpecialFunctions.Binomial(_populationSize, _n);
}
#endregion
@ -269,7 +269,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public int Mode
{
get { return (_n + 1) * (_m + 1) / (_populationSize + 2); }
get { return (_n + 1)*(_m + 1)/(_populationSize + 2); }
}
/// <summary>
@ -305,7 +305,7 @@ namespace MathNet.Numerics.Distributions
/// </returns>
public double Probability(int k)
{
return SpecialFunctions.Binomial(_m, k) * SpecialFunctions.Binomial(_populationSize - _m, _n - k) / SpecialFunctions.Binomial(_populationSize, _n);
return SpecialFunctions.Binomial(_m, k)*SpecialFunctions.Binomial(_populationSize - _m, _n - k)/SpecialFunctions.Binomial(_populationSize, _n);
}
/// <summary>
@ -336,7 +336,7 @@ namespace MathNet.Numerics.Distributions
do
{
var p = (double)m / size;
var p = (double) m/size;
var r = rnd.NextDouble();
if (r < p)
{
@ -346,8 +346,7 @@ namespace MathNet.Numerics.Distributions
size--;
n--;
}
while (0 < n);
} while (0 < n);
return x;
}

23
src/Numerics/Distributions/Discrete/NegativeBinomial.cs

@ -172,7 +172,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Mean
{
get { return _r * (1.0 - _p) / _p; }
get { return _r*(1.0 - _p)/_p; }
}
/// <summary>
@ -180,7 +180,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Variance
{
get { return _r * (1.0 - _p) / (_p * _p); }
get { return _r*(1.0 - _p)/(_p*_p); }
}
/// <summary>
@ -188,7 +188,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double StdDev
{
get { return Math.Sqrt(_r * (1.0 - _p)) / _p; }
get { return Math.Sqrt(_r*(1.0 - _p))/_p; }
}
/// <summary>
@ -204,7 +204,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Skewness
{
get { return (2.0 - _p) / Math.Sqrt(_r * (1.0 - _p)); }
get { return (2.0 - _p)/Math.Sqrt(_r*(1.0 - _p)); }
}
/// <summary>
@ -226,7 +226,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public int Mode
{
get { return _r > 1.0 ? (int)Math.Floor((_r - 1.0) * (1.0 - _p) / _p) : 0; }
get { return _r > 1.0 ? (int) Math.Floor((_r - 1.0)*(1.0 - _p)/_p) : 0; }
}
/// <summary>
@ -265,8 +265,8 @@ namespace MathNet.Numerics.Distributions
var ln = SpecialFunctions.GammaLn(_r + k)
- SpecialFunctions.GammaLn(_r)
- SpecialFunctions.GammaLn(k + 1.0)
+ (_r * Math.Log(_p))
+ (k * Math.Log(1.0 - _p));
+ (_r*Math.Log(_p))
+ (k*Math.Log(1.0 - _p));
return Math.Exp(ln);
}
@ -282,8 +282,8 @@ namespace MathNet.Numerics.Distributions
var ln = SpecialFunctions.GammaLn(_r + k)
- SpecialFunctions.GammaLn(_r)
- SpecialFunctions.GammaLn(k + 1.0)
+ (_r * Math.Log(_p))
+ (k * Math.Log(1.0 - _p));
+ (_r*Math.Log(_p))
+ (k*Math.Log(1.0 - _p));
return ln;
}
@ -305,9 +305,8 @@ namespace MathNet.Numerics.Distributions
do
{
k = k + 1;
p1 = p1 * rnd.NextDouble();
}
while (p1 >= c);
p1 = p1*rnd.NextDouble();
} while (p1 >= c);
return k - 1;
}

28
src/Numerics/Distributions/Discrete/Poisson.cs

@ -167,7 +167,7 @@ namespace MathNet.Numerics.Distributions
/// <remarks>Approximation, see Wikipedia <a href="http://en.wikipedia.org/wiki/Poisson_distribution">Poisson distribution</a></remarks>
public double Entropy
{
get { return (0.5 * Math.Log(2 * Constants.Pi * Constants.E * _lambda)) - (1.0 / (12.0 * _lambda)) - (1.0 / (24.0 * _lambda * _lambda)) - (19.0 / (360.0 * _lambda * _lambda * _lambda)); }
get { return (0.5*Math.Log(2*Constants.Pi*Constants.E*_lambda)) - (1.0/(12.0*_lambda)) - (1.0/(24.0*_lambda*_lambda)) - (19.0/(360.0*_lambda*_lambda*_lambda)); }
}
/// <summary>
@ -175,7 +175,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Skewness
{
get { return 1.0 / Math.Sqrt(_lambda); }
get { return 1.0/Math.Sqrt(_lambda); }
}
/// <summary>
@ -213,7 +213,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public int Mode
{
get { return (int)Math.Floor(_lambda); }
get { return (int) Math.Floor(_lambda); }
}
/// <summary>
@ -222,7 +222,7 @@ namespace MathNet.Numerics.Distributions
/// <remarks>Approximation, see Wikipedia <a href="http://en.wikipedia.org/wiki/Poisson_distribution">Poisson distribution</a></remarks>
public int Median
{
get { return (int)Math.Floor(_lambda + (1.0 / 3.0) - (0.02 / _lambda)); }
get { return (int) Math.Floor(_lambda + (1.0/3.0) - (0.02/_lambda)); }
}
/// <summary>
@ -232,7 +232,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the probability mass at location <paramref name="k"/>.</returns>
public double Probability(int k)
{
return Math.Exp(-_lambda + (k * Math.Log(_lambda)) - SpecialFunctions.FactorialLn(k));
return Math.Exp(-_lambda + (k*Math.Log(_lambda)) - SpecialFunctions.FactorialLn(k));
}
/// <summary>
@ -242,7 +242,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the log probability mass at location <paramref name="k"/>.</returns>
public double ProbabilityLn(int k)
{
return -_lambda + (k * Math.Log(_lambda)) - SpecialFunctions.FactorialLn(k);
return -_lambda + (k*Math.Log(_lambda)) - SpecialFunctions.FactorialLn(k);
}
#endregion
@ -287,26 +287,26 @@ namespace MathNet.Numerics.Distributions
/// The article is on pages 29-35. The algorithm given here is on page 32. </remarks>
static int DoSampleLarge(Random rnd, double lambda)
{
var c = 0.767 - (3.36 / lambda);
var beta = Math.PI / Math.Sqrt(3.0 * lambda);
var alpha = beta * lambda;
var c = 0.767 - (3.36/lambda);
var beta = Math.PI/Math.Sqrt(3.0*lambda);
var alpha = beta*lambda;
var k = Math.Log(c) - lambda - Math.Log(beta);
for (;;)
{
var u = rnd.NextDouble();
var x = (alpha - Math.Log((1.0 - u) / u)) / beta;
var n = (int)Math.Floor(x + 0.5);
var x = (alpha - Math.Log((1.0 - u)/u))/beta;
var n = (int) Math.Floor(x + 0.5);
if (n < 0)
{
continue;
}
var v = rnd.NextDouble();
var y = alpha - (beta * x);
var y = alpha - (beta*x);
var temp = 1.0 + Math.Exp(y);
var lhs = y + Math.Log(v / (temp * temp));
var rhs = k + (n * Math.Log(lambda)) - SpecialFunctions.FactorialLn(n);
var lhs = y + Math.Log(v/(temp*temp));
var rhs = k + (n*Math.Log(lambda)) - SpecialFunctions.FactorialLn(n);
if (lhs <= rhs)
{
return n;

20
src/Numerics/Distributions/Discrete/Zipf.cs

@ -120,7 +120,6 @@ namespace MathNet.Numerics.Distributions
public double S
{
get { return _s; }
set { SetParameters(value, _n); }
}
@ -130,7 +129,6 @@ namespace MathNet.Numerics.Distributions
public int N
{
get { return _n; }
set { SetParameters(_s, value); }
}
@ -167,7 +165,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Mean
{
get { return SpecialFunctions.GeneralHarmonic(_n, _s - 1.0) / SpecialFunctions.GeneralHarmonic(_n, _s); }
get { return SpecialFunctions.GeneralHarmonic(_n, _s - 1.0)/SpecialFunctions.GeneralHarmonic(_n, _s); }
}
/// <summary>
@ -183,7 +181,7 @@ namespace MathNet.Numerics.Distributions
}
var generalHarmonicsNS = SpecialFunctions.GeneralHarmonic(_n, _s);
return (SpecialFunctions.GeneralHarmonic(_n, _s - 2) * SpecialFunctions.GeneralHarmonic(_n, _s)) - (Math.Pow(SpecialFunctions.GeneralHarmonic(_n, _s - 1), 2) / (generalHarmonicsNS * generalHarmonicsNS));
return (SpecialFunctions.GeneralHarmonic(_n, _s - 2)*SpecialFunctions.GeneralHarmonic(_n, _s)) - (Math.Pow(SpecialFunctions.GeneralHarmonic(_n, _s - 1), 2)/(generalHarmonicsNS*generalHarmonicsNS));
}
}
@ -205,10 +203,10 @@ namespace MathNet.Numerics.Distributions
double sum = 0;
for (var i = 0; i < _n; i++)
{
sum += Math.Log(i + 1) / Math.Pow(i + 1, _s);
sum += Math.Log(i + 1)/Math.Pow(i + 1, _s);
}
return ((_s / SpecialFunctions.GeneralHarmonic(_n, _s)) * sum) + Math.Log(SpecialFunctions.GeneralHarmonic(_n, _s));
return ((_s/SpecialFunctions.GeneralHarmonic(_n, _s))*sum) + Math.Log(SpecialFunctions.GeneralHarmonic(_n, _s));
}
}
@ -224,7 +222,7 @@ namespace MathNet.Numerics.Distributions
throw new NotSupportedException();
}
return ((SpecialFunctions.GeneralHarmonic(_n, _s - 3) * Math.Pow(SpecialFunctions.GeneralHarmonic(_n, _s), 2)) - (SpecialFunctions.GeneralHarmonic(_n, _s - 1) * ((3 * SpecialFunctions.GeneralHarmonic(_n, _s - 2) * SpecialFunctions.GeneralHarmonic(_n, _s)) - Math.Pow(SpecialFunctions.GeneralHarmonic(_n, _s - 1), 2)))) / Math.Pow((SpecialFunctions.GeneralHarmonic(_n, _s - 2) * SpecialFunctions.GeneralHarmonic(_n, _s)) - Math.Pow(SpecialFunctions.GeneralHarmonic(_n, _s - 1), 2), 1.5);
return ((SpecialFunctions.GeneralHarmonic(_n, _s - 3)*Math.Pow(SpecialFunctions.GeneralHarmonic(_n, _s), 2)) - (SpecialFunctions.GeneralHarmonic(_n, _s - 1)*((3*SpecialFunctions.GeneralHarmonic(_n, _s - 2)*SpecialFunctions.GeneralHarmonic(_n, _s)) - Math.Pow(SpecialFunctions.GeneralHarmonic(_n, _s - 1), 2))))/Math.Pow((SpecialFunctions.GeneralHarmonic(_n, _s - 2)*SpecialFunctions.GeneralHarmonic(_n, _s)) - Math.Pow(SpecialFunctions.GeneralHarmonic(_n, _s - 1), 2), 1.5);
}
}
@ -240,7 +238,7 @@ namespace MathNet.Numerics.Distributions
return 0.0;
}
return SpecialFunctions.GeneralHarmonic((int)x, _s) / SpecialFunctions.GeneralHarmonic(_n, _s);
return SpecialFunctions.GeneralHarmonic((int) x, _s)/SpecialFunctions.GeneralHarmonic(_n, _s);
}
#endregion
@ -288,7 +286,7 @@ namespace MathNet.Numerics.Distributions
/// </returns>
public double Probability(int k)
{
return (1.0 / Math.Pow(k, _s)) / SpecialFunctions.GeneralHarmonic(_n, _s);
return (1.0/Math.Pow(k, _s))/SpecialFunctions.GeneralHarmonic(_n, _s);
}
/// <summary>
@ -320,12 +318,12 @@ namespace MathNet.Numerics.Distributions
r = rnd.NextDouble();
}
var p = 1.0 / SpecialFunctions.GeneralHarmonic(n, s);
var p = 1.0/SpecialFunctions.GeneralHarmonic(n, s);
int i;
var sum = 0.0;
for (i = 1; i <= n; i++)
{
sum += p / Math.Pow(i, s);
sum += p/Math.Pow(i, s);
if (sum >= r)
{
break;

47
src/Numerics/Distributions/Multivariate/Dirichlet.cs

@ -48,12 +48,12 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// The Dirichlet distribution parameters.
/// </summary>
private double[] _alpha;
double[] _alpha;
/// <summary>
/// The distribution's random number generator.
/// </summary>
private Random _random;
Random _random;
/// <summary>
/// Initializes a new instance of the Dirichlet class. The distribution will
@ -134,7 +134,7 @@ namespace MathNet.Numerics.Distributions
{
return false;
}
if (t > 0.0)
{
allzero = false;
@ -149,14 +149,14 @@ namespace MathNet.Numerics.Distributions
/// </summary>
/// <param name="alpha">The parameters of the Dirichlet distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
private void SetParameters(double[] alpha)
void SetParameters(double[] alpha)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(alpha))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_alpha = (double[])alpha.Clone();
_alpha = (double[]) alpha.Clone();
}
/// <summary>
@ -175,10 +175,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public int Dimension
{
get
{
return _alpha.Length;
}
get { return _alpha.Length; }
}
/// <summary>
@ -186,26 +183,16 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double[] Alpha
{
get
{
return _alpha;
}
set
{
SetParameters(value);
}
get { return _alpha; }
set { SetParameters(value); }
}
/// <summary>
/// Gets the sum of the Dirichlet parameters.
/// </summary>
private double AlphaSum
double AlphaSum
{
get
{
return _alpha.Sum();
}
get { return _alpha.Sum(); }
}
/// <summary>
@ -219,7 +206,7 @@ namespace MathNet.Numerics.Distributions
var parm = new double[Dimension];
for (var i = 0; i < Dimension; i++)
{
parm[i] = _alpha[i] / sum;
parm[i] = _alpha[i]/sum;
}
return parm;
@ -237,7 +224,7 @@ namespace MathNet.Numerics.Distributions
var v = new double[_alpha.Length];
for (var i = 0; i < _alpha.Length; i++)
{
v[i] = _alpha[i] * (s - _alpha[i]) / (s * s * (s + 1.0));
v[i] = _alpha[i]*(s - _alpha[i])/(s*s*(s + 1.0));
}
return v;
@ -251,8 +238,8 @@ namespace MathNet.Numerics.Distributions
{
get
{
var num = _alpha.Sum(t => (t - 1) * SpecialFunctions.DiGamma(t));
return SpecialFunctions.GammaLn(AlphaSum) + ((AlphaSum - Dimension) * SpecialFunctions.DiGamma(AlphaSum)) - num;
var num = _alpha.Sum(t => (t - 1)*SpecialFunctions.DiGamma(t));
return SpecialFunctions.GammaLn(AlphaSum) + ((AlphaSum - Dimension)*SpecialFunctions.DiGamma(AlphaSum)) - num;
}
}
@ -297,7 +284,7 @@ namespace MathNet.Numerics.Distributions
return 0.0;
}
term += (_alpha[i] - 1.0) * Math.Log(xi) - SpecialFunctions.GammaLn(_alpha[i]);
term += (_alpha[i] - 1.0)*Math.Log(xi) - SpecialFunctions.GammaLn(_alpha[i]);
sumxi += xi;
sumalpha += _alpha[i];
}
@ -310,7 +297,7 @@ namespace MathNet.Numerics.Distributions
return 0.0;
}
term += (_alpha[_alpha.Length - 1] - 1.0) * Math.Log(1.0 - sumxi) - SpecialFunctions.GammaLn(_alpha[_alpha.Length - 1]);
term += (_alpha[_alpha.Length - 1] - 1.0)*Math.Log(1.0 - sumxi) - SpecialFunctions.GammaLn(_alpha[_alpha.Length - 1]);
sumalpha += _alpha[_alpha.Length - 1];
}
else if (!sumxi.AlmostEqualInDecimalPlaces(1.0, 8))
@ -355,7 +342,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public static double[] Sample(Random rnd, double[] alpha)
{
if (Control.CheckDistributionParameters && ! IsValidParameterSet(alpha))
if (Control.CheckDistributionParameters && !IsValidParameterSet(alpha))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}

64
src/Numerics/Distributions/Multivariate/InverseWishart.cs

@ -47,22 +47,22 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// The degrees of freedom for the inverse Wishart distribution.
/// </summary>
private double _nu;
double _nu;
/// <summary>
/// The scale matrix for the inverse Wishart distribution.
/// </summary>
private Matrix<double> _s;
Matrix<double> _s;
/// <summary>
/// Caches the Cholesky factorization of the scale matrix.
/// </summary>
private Cholesky<double> _chol;
Cholesky<double> _chol;
/// <summary>
/// The distribution's random number generator.
/// </summary>
private Random _random;
Random _random;
/// <summary>
/// Initializes a new instance of the <see cref="InverseWishart"/> class.
@ -102,7 +102,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="nu">The degrees of freedom for the Wishart distribution.</param>
/// <param name="s">The scale matrix for the Wishart distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
private void SetParameters(double nu, Matrix<double> s)
void SetParameters(double nu, Matrix<double> s)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(nu, s))
{
@ -120,7 +120,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="nu">The degrees of freedom for the Wishart distribution.</param>
/// <param name="s">The scale matrix for the Wishart distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
private static bool IsValidParameterSet(double nu, Matrix<double> s)
static bool IsValidParameterSet(double nu, Matrix<double> s)
{
if (s.RowCount != s.ColumnCount)
{
@ -148,15 +148,8 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Nu
{
get
{
return _nu;
}
set
{
SetParameters(value, _s);
}
get { return _nu; }
set { SetParameters(value, _s); }
}
/// <summary>
@ -164,15 +157,8 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public Matrix<double> S
{
get
{
return _s;
}
set
{
SetParameters(_nu, value);
}
get { return _s; }
set { SetParameters(_nu, value); }
}
/// <summary>
@ -198,10 +184,7 @@ namespace MathNet.Numerics.Distributions
/// <value>The mean of the distribution.</value>
public Matrix<double> Mean
{
get
{
return _s * (1.0 / (_nu - _s.RowCount - 1.0));
}
get { return _s*(1.0/(_nu - _s.RowCount - 1.0)); }
}
/// <summary>
@ -211,10 +194,7 @@ namespace MathNet.Numerics.Distributions
/// <remarks>A. O'Hagan, and J. J. Forster (2004). Kendall's Advanced Theory of Statistics: Bayesian Inference. 2B (2 ed.). Arnold. ISBN 0-340-80752-0.</remarks>
public Matrix<double> Mode
{
get
{
return _s * (1.0 / (_nu + _s.RowCount + 1.0));
}
get { return _s*(1.0/(_nu + _s.RowCount + 1.0)); }
}
/// <summary>
@ -231,9 +211,9 @@ namespace MathNet.Numerics.Distributions
{
for (var j = 0; j < res.ColumnCount; j++)
{
var num1 = ((_nu - _s.RowCount + 1) * _s.At(i, j) * _s.At(i, j)) + ((_nu - _s.RowCount - 1) * _s.At(i, i) * _s.At(j, j));
var num2 = (_nu - _s.RowCount) * (_nu - _s.RowCount - 1) * (_nu - _s.RowCount - 1) * (_nu - _s.RowCount - 3);
res.At(i, j, num1 / num2);
var num1 = ((_nu - _s.RowCount + 1)*_s.At(i, j)*_s.At(i, j)) + ((_nu - _s.RowCount - 1)*_s.At(i, i)*_s.At(j, j));
var num2 = (_nu - _s.RowCount)*(_nu - _s.RowCount - 1)*(_nu - _s.RowCount - 1)*(_nu - _s.RowCount - 3);
res.At(i, j, num1/num2);
}
}
@ -261,17 +241,17 @@ namespace MathNet.Numerics.Distributions
var sXi = chol.Solve(S);
// Compute the multivariate Gamma function.
var gp = Math.Pow(Constants.Pi, p * (p - 1.0) / 4.0);
var gp = Math.Pow(Constants.Pi, p*(p - 1.0)/4.0);
for (var j = 1; j <= p; j++)
{
gp *= SpecialFunctions.Gamma((_nu + 1.0 - j) / 2.0);
gp *= SpecialFunctions.Gamma((_nu + 1.0 - j)/2.0);
}
return Math.Pow(dX, -(_nu + p + 1.0) / 2.0)
* Math.Exp(-0.5 * sXi.Trace())
* Math.Pow(_chol.Determinant, _nu / 2.0)
/ Math.Pow(2.0, _nu * p / 2.0)
/ gp;
return Math.Pow(dX, -(_nu + p + 1.0)/2.0)
*Math.Exp(-0.5*sXi.Trace())
*Math.Pow(_chol.Determinant, _nu/2.0)
/Math.Pow(2.0, _nu*p/2.0)
/gp;
}
/// <summary>

63
src/Numerics/Distributions/Multivariate/MatrixNormal.cs

@ -48,22 +48,22 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// The mean of the matrix normal distribution.
/// </summary>
private Matrix<double> _m;
Matrix<double> _m;
/// <summary>
/// The covariance matrix for the rows.
/// </summary>
private Matrix<double> _v;
Matrix<double> _v;
/// <summary>
/// The covariance matrix for the columns.
/// </summary>
private Matrix<double> _k;
Matrix<double> _k;
/// <summary>
/// The distribution's random number generator.
/// </summary>
private Random _random;
Random _random;
/// <summary>
/// Initializes a new instance of the <see cref="MatrixNormal"/> class.
@ -109,15 +109,8 @@ namespace MathNet.Numerics.Distributions
/// <value>The mean of the distribution.</value>
public Matrix<double> Mean
{
get
{
return _m;
}
set
{
SetParameters(value, _v, _k);
}
get { return _m; }
set { SetParameters(value, _v, _k); }
}
/// <summary>
@ -126,15 +119,8 @@ namespace MathNet.Numerics.Distributions
/// <value>The row covariance.</value>
public Matrix<double> RowCovariance
{
get
{
return _v;
}
set
{
SetParameters(_m, value, _k);
}
get { return _v; }
set { SetParameters(_m, value, _k); }
}
/// <summary>
@ -143,15 +129,8 @@ namespace MathNet.Numerics.Distributions
/// <value>The column covariance.</value>
public Matrix<double> ColumnCovariance
{
get
{
return _k;
}
set
{
SetParameters(_m, _v, value);
}
get { return _k; }
set { SetParameters(_m, _v, value); }
}
/// <summary>
@ -161,7 +140,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="v">The covariance matrix for the rows.</param>
/// <param name="k">The covariance matrix for the columns.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
private void SetParameters(Matrix<double> m, Matrix<double> v, Matrix<double> k)
void SetParameters(Matrix<double> m, Matrix<double> v, Matrix<double> k)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(m, v, k))
{
@ -180,7 +159,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="v">The covariance matrix for the rows.</param>
/// <param name="k">The covariance matrix for the columns.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
private static bool IsValidParameterSet(Matrix<double> m, Matrix<double> v, Matrix<double> k)
static bool IsValidParameterSet(Matrix<double> m, Matrix<double> v, Matrix<double> k)
{
var n = m.RowCount;
var p = m.ColumnCount;
@ -247,10 +226,10 @@ namespace MathNet.Numerics.Distributions
var cholV = Cholesky<double>.Create(_v);
var cholK = Cholesky<double>.Create(_k);
return Math.Exp(-0.5 * cholV.Solve(a.Transpose() * cholK.Solve(a)).Trace())
/ Math.Pow(2.0 * Constants.Pi, x.RowCount * x.ColumnCount / 2.0)
/ Math.Pow(cholV.Determinant, x.RowCount / 2.0)
/ Math.Pow(cholK.Determinant, x.ColumnCount / 2.0);
return Math.Exp(-0.5*cholV.Solve(a.Transpose()*cholK.Solve(a)).Trace())
/Math.Pow(2.0*Constants.Pi, x.RowCount*x.ColumnCount/2.0)
/Math.Pow(cholV.Determinant, x.RowCount/2.0)
/Math.Pow(cholK.Determinant, x.ColumnCount/2.0);
}
/// <summary>
@ -285,7 +264,7 @@ namespace MathNet.Numerics.Distributions
var vki = v.KroneckerProduct(k.Inverse());
// Sample a vector valued random variable with VKi as the covariance.
var vector = SampleVectorNormal(rnd, new DenseVector(n * p), vki);
var vector = SampleVectorNormal(rnd, new DenseVector(n*p), vki);
// Unstack the vector v and add the mean.
var r = m.Clone();
@ -293,7 +272,7 @@ namespace MathNet.Numerics.Distributions
{
for (var j = 0; j < p; j++)
{
r.At(i, j, r.At(i, j) + vector[(j * n) + i]);
r.At(i, j, r.At(i, j) + vector[(j*n) + i]);
}
}
@ -307,7 +286,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="mean">The mean of the vector normal distribution.</param>
/// <param name="covariance">The covariance matrix of the vector normal distribution.</param>
/// <returns>a sequence of samples from defined distribution.</returns>
private static Vector<double> SampleVectorNormal(Random rnd, Vector<double> mean, Matrix<double> covariance)
static Vector<double> SampleVectorNormal(Random rnd, Vector<double> mean, Matrix<double> covariance)
{
var chol = Cholesky<double>.Create(covariance);
return SampleVectorNormal(rnd, mean, chol);
@ -320,7 +299,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="mean">The mean of the vector normal distribution.</param>
/// <param name="cholesky">The Cholesky factorization of the covariance matrix.</param>
/// <returns>a sequence of samples from defined distribution.</returns>
private static Vector<double> SampleVectorNormal(Random rnd, Vector<double> mean, Cholesky<double> cholesky)
static Vector<double> SampleVectorNormal(Random rnd, Vector<double> mean, Cholesky<double> cholesky)
{
var count = mean.Count;
@ -337,7 +316,7 @@ namespace MathNet.Numerics.Distributions
}
// Return the transformed variable.
return mean + (cholesky.Factor * v);
return mean + (cholesky.Factor*v);
}
}
}

53
src/Numerics/Distributions/Multivariate/Multinomial.cs

@ -51,17 +51,17 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Stores the normalized multinomial probabilities.
/// </summary>
private double[] _p;
double[] _p;
/// <summary>
/// The number of trials.
/// </summary>
private int _n;
int _n;
/// <summary>
/// The distribution's random number generator.
/// </summary>
private Random _random;
Random _random;
/// <summary>
/// Initializes a new instance of the Multinomial class.
@ -98,7 +98,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
/// <param name="h">Histogram instance</param>
/// <param name="n">The number of trials.</param>
/// <exception cref="ArgumentOutOfRangeException">If any of the probabilities are negative or do not sum to one.</exception>
/// <exception cref="ArgumentOutOfRangeException">If any of the probabilities are negative or do not sum to one.</exception>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="n"/> is negative.</exception>
public Multinomial(Histogram h, int n)
{
@ -137,7 +137,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="n">The number of trials.</param>
/// <returns>If any of the probabilities are negative returns <c>false</c>,
/// if the sum of parameters is 0.0, or if the number of trials is negative; otherwise <c>true</c>.</returns>
private static bool IsValidParameterSet(IEnumerable<double> p, int n)
static bool IsValidParameterSet(IEnumerable<double> p, int n)
{
var sum = 0.0;
foreach (var t in p)
@ -165,14 +165,14 @@ namespace MathNet.Numerics.Distributions
/// as this is often impossible using floating point arithmetic.</param>
/// <param name="n">The number of trials.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
private void SetParameters(double[] p, int n)
void SetParameters(double[] p, int n)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(p, n))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_p = (double[])p.Clone();
_p = (double[]) p.Clone();
_n = n;
}
@ -181,15 +181,8 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double[] P
{
get
{
return (double[])_p.Clone();
}
set
{
SetParameters(value, _n);
}
get { return (double[]) _p.Clone(); }
set { SetParameters(value, _n); }
}
/// <summary>
@ -197,15 +190,8 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public int N
{
get
{
return _n;
}
set
{
SetParameters(_p, value);
}
get { return _n; }
set { SetParameters(_p, value); }
}
/// <summary>
@ -230,10 +216,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public Vector<double> Mean
{
get
{
return _n * (DenseVector)P;
}
get { return _n*(DenseVector) P; }
}
/// <summary>
@ -244,10 +227,10 @@ namespace MathNet.Numerics.Distributions
get
{
// Do not use _p, because operations below will modify _p array. Use P or _p.Clone().
var res = (DenseVector)P;
var res = (DenseVector) P;
for (var i = 0; i < res.Count; i++)
{
res[i] *= _n * (1 - res[i]);
res[i] *= _n*(1 - res[i]);
}
return res;
@ -262,10 +245,10 @@ namespace MathNet.Numerics.Distributions
get
{
// Do not use _p, because operations below will modify _p array. Use P or _p.Clone().
var res = (DenseVector)P;
var res = (DenseVector) P;
for (var i = 0; i < res.Count; i++)
{
res[i] = (1.0 - (2.0 * res[i])) / Math.Sqrt(_n * (1.0 - res[i]) * res[i]);
res[i] = (1.0 - (2.0*res[i]))/Math.Sqrt(_n*(1.0 - res[i])*res[i]);
}
return res;
@ -300,7 +283,7 @@ namespace MathNet.Numerics.Distributions
num *= Math.Pow(_p[i], x[i]);
}
return coef * num;
return coef*num;
}
return 0.0;
@ -328,7 +311,7 @@ namespace MathNet.Numerics.Distributions
if (x.Sum() == _n)
{
var coef = Math.Log(SpecialFunctions.Multinomial(_n, x));
var num = x.Select((t, i) => t * Math.Log(_p[i])).Sum();
var num = x.Select((t, i) => t*Math.Log(_p[i])).Sum();
return coef + num;
}

118
src/Numerics/Distributions/Multivariate/NormalGamma.cs

@ -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;
}

64
src/Numerics/Distributions/Multivariate/Wishart.cs

@ -49,22 +49,22 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// The degrees of freedom for the Wishart distribution.
/// </summary>
private double _nu;
double _nu;
/// <summary>
/// The scale matrix for the Wishart distribution.
/// </summary>
private Matrix<double> _s;
Matrix<double> _s;
/// <summary>
/// Caches the Cholesky factorization of the scale matrix.
/// </summary>
private Cholesky<double> _chol;
Cholesky<double> _chol;
/// <summary>
/// The distribution's random number generator.
/// </summary>
private Random _random;
Random _random;
/// <summary>
/// Initializes a new instance of the <see cref="Wishart"/> class.
@ -95,7 +95,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="nu">The degrees of freedom for the Wishart distribution.</param>
/// <param name="s">The scale matrix for the Wishart distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
private void SetParameters(double nu, Matrix<double> s)
void SetParameters(double nu, Matrix<double> s)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(nu, s))
{
@ -113,7 +113,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="nu">The degrees of freedom for the Wishart distribution.</param>
/// <param name="s">The scale matrix for the Wishart distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
private static bool IsValidParameterSet(double nu, Matrix<double> s)
static bool IsValidParameterSet(double nu, Matrix<double> s)
{
if (s.RowCount != s.ColumnCount)
{
@ -141,15 +141,8 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Nu
{
get
{
return _nu;
}
set
{
SetParameters(value, _s);
}
get { return _nu; }
set { SetParameters(value, _s); }
}
/// <summary>
@ -157,15 +150,8 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public Matrix<double> S
{
get
{
return _s;
}
set
{
SetParameters(_nu, value);
}
get { return _s; }
set { SetParameters(_nu, value); }
}
/// <summary>
@ -200,10 +186,7 @@ namespace MathNet.Numerics.Distributions
/// <value>The mean of the distribution.</value>
public Matrix<double> Mean
{
get
{
return _nu * _s;
}
get { return _nu*_s; }
}
/// <summary>
@ -212,10 +195,7 @@ namespace MathNet.Numerics.Distributions
/// <value>The mode of the distribution.</value>
public Matrix<double> Mode
{
get
{
return (_nu - _s.RowCount - 1.0) * _s;
}
get { return (_nu - _s.RowCount - 1.0)*_s; }
}
/// <summary>
@ -231,7 +211,7 @@ namespace MathNet.Numerics.Distributions
{
for (var j = 0; j < res.ColumnCount; j++)
{
res.At(i, j, _nu * ((_s.At(i, j) * _s.At(i, j)) + (_s.At(i, i) * _s.At(j, j))));
res.At(i, j, _nu*((_s.At(i, j)*_s.At(i, j)) + (_s.At(i, i)*_s.At(j, j))));
}
}
@ -258,17 +238,17 @@ namespace MathNet.Numerics.Distributions
var siX = _chol.Solve(x);
// Compute the multivariate Gamma function.
var gp = Math.Pow(Constants.Pi, p * (p - 1.0) / 4.0);
var gp = Math.Pow(Constants.Pi, p*(p - 1.0)/4.0);
for (var j = 1; j <= p; j++)
{
gp *= SpecialFunctions.Gamma((_nu + 1.0 - j) / 2.0);
gp *= SpecialFunctions.Gamma((_nu + 1.0 - j)/2.0);
}
return Math.Pow(dX, (_nu - p - 1.0) / 2.0)
* Math.Exp(-0.5 * siX.Trace())
/ Math.Pow(2.0, _nu * p / 2.0)
/ Math.Pow(_chol.Determinant, _nu / 2.0)
/ gp;
return Math.Pow(dX, (_nu - p - 1.0)/2.0)
*Math.Exp(-0.5*siX.Trace())
/Math.Pow(2.0, _nu*p/2.0)
/Math.Pow(_chol.Determinant, _nu/2.0)
/gp;
}
/// <summary>
@ -311,7 +291,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="s">The S parameter to use.</param>
/// <param name="chol">The cholesky decomposition to use.</param>
/// <returns>a random number from the distribution.</returns>
private static Matrix<double> DoSample(Random rnd, double nu, Matrix<double> s, Cholesky<double> chol)
static Matrix<double> DoSample(Random rnd, double nu, Matrix<double> s, Cholesky<double> chol)
{
var count = s.RowCount;
@ -332,7 +312,7 @@ namespace MathNet.Numerics.Distributions
}
var factor = chol.Factor;
return factor * a * a.Transpose() * factor.Transpose();
return factor*a*a.Transpose()*factor.Transpose();
}
}
}

Loading…
Cancel
Save