diff --git a/src/Numerics/Distributions/Continuous/Beta.cs b/src/Numerics/Distributions/Continuous/Beta.cs index adb9e0cb..245f9701 100644 --- a/src/Numerics/Distributions/Continuous/Beta.cs +++ b/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 /// 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)); } } /// @@ -221,7 +221,7 @@ namespace MathNet.Numerics.Distributions /// 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))); } } /// @@ -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); } /// @@ -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); } /// diff --git a/src/Numerics/Distributions/Continuous/Cauchy.cs b/src/Numerics/Distributions/Continuous/Cauchy.cs index a45d6041..d3197e23 100644 --- a/src/Numerics/Distributions/Continuous/Cauchy.cs +++ b/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 /// public double Entropy { - get { return Math.Log(4.0 * Constants.Pi * _scale); } + get { return Math.Log(4.0*Constants.Pi*_scale); } } /// @@ -216,7 +214,7 @@ namespace MathNet.Numerics.Distributions /// the cumulative density at . 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 /// the density at . 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)))); } /// @@ -269,7 +267,7 @@ namespace MathNet.Numerics.Distributions /// the log density at . 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))); } /// diff --git a/src/Numerics/Distributions/Continuous/Chi.cs b/src/Numerics/Distributions/Continuous/Chi.cs index 8d7550b6..b4b75f8c 100644 --- a/src/Numerics/Distributions/Continuous/Chi.cs +++ b/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 /// 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)); } } /// @@ -156,7 +155,7 @@ namespace MathNet.Numerics.Distributions /// public double Variance { - get { return _dof - (Mean * Mean); } + get { return _dof - (Mean*Mean); } } /// @@ -172,7 +171,7 @@ namespace MathNet.Numerics.Distributions /// 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); } } /// @@ -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 /// the cumulative density at . 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 /// the density at . 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); } /// @@ -258,7 +257,7 @@ namespace MathNet.Numerics.Distributions /// the log density at . 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 /// a sample from the distribution. public double Sample() { - return SampleUnchecked(RandomSource, (int)_dof); + return SampleUnchecked(RandomSource, (int) _dof); } /// @@ -295,7 +294,7 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public IEnumerable Samples() { - var dof = (int)_dof; + var dof = (int) _dof; while (true) { yield return SampleUnchecked(RandomSource, dof); diff --git a/src/Numerics/Distributions/Continuous/ChiSquare.cs b/src/Numerics/Distributions/Continuous/ChiSquare.cs index 4252708b..5cd2f70a 100644 --- a/src/Numerics/Distributions/Continuous/ChiSquare.cs +++ b/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 /// public double Variance { - get { return 2.0 * Mean; } + get { return 2.0*Mean; } } /// @@ -153,7 +152,7 @@ namespace MathNet.Numerics.Distributions /// public double StdDev { - get { return Math.Sqrt(2.0 * Mean); } + get { return Math.Sqrt(2.0*Mean); } } /// @@ -161,7 +160,7 @@ namespace MathNet.Numerics.Distributions /// 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)); } } /// @@ -169,7 +168,7 @@ namespace MathNet.Numerics.Distributions /// public double Skewness { - get { return Math.Sqrt(8.0 / Mean); } + get { return Math.Sqrt(8.0/Mean); } } /// @@ -179,7 +178,7 @@ namespace MathNet.Numerics.Distributions /// the cumulative density at . 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 /// public double Median { - get { return Mean - (2.0 / 3.0); } + get { return Mean - (2.0/3.0); } } /// @@ -225,7 +224,7 @@ namespace MathNet.Numerics.Distributions /// the density at . 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)); } /// @@ -235,7 +234,7 @@ namespace MathNet.Numerics.Distributions /// the log density at . 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); } /// diff --git a/src/Numerics/Distributions/Continuous/ContinuousUniform.cs b/src/Numerics/Distributions/Continuous/ContinuousUniform.cs index a6dec444..97c84210 100644 --- a/src/Numerics/Distributions/Continuous/ContinuousUniform.cs +++ b/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 /// public double Mean { - get { return (_lower + _upper) / 2.0; } + get { return (_lower + _upper)/2.0; } } /// @@ -191,7 +189,7 @@ namespace MathNet.Numerics.Distributions /// public double Variance { - get { return (_upper - _lower) * (_upper - _lower) / 12.0; } + get { return (_upper - _lower)*(_upper - _lower)/12.0; } } /// @@ -199,7 +197,7 @@ namespace MathNet.Numerics.Distributions /// public double StdDev { - get { return (_upper - _lower) / Math.Sqrt(12.0); } + get { return (_upper - _lower)/Math.Sqrt(12.0); } } /// @@ -229,7 +227,7 @@ namespace MathNet.Numerics.Distributions /// public double Mode { - get { return (_lower + _upper) / 2.0; } + get { return (_lower + _upper)/2.0; } } /// @@ -238,7 +236,7 @@ namespace MathNet.Numerics.Distributions /// public double Median { - get { return (_lower + _upper) / 2.0; } + get { return (_lower + _upper)/2.0; } } /// @@ -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 /// a uniformly distributed random number. internal static double SampleUnchecked(Random rnd, double lower, double upper) { - return lower + (rnd.NextDouble() * (upper - lower)); + return lower + (rnd.NextDouble()*(upper - lower)); } /// @@ -379,4 +377,4 @@ namespace MathNet.Numerics.Distributions } } } -} \ No newline at end of file +} diff --git a/src/Numerics/Distributions/Continuous/Erlang.cs b/src/Numerics/Distributions/Continuous/Erlang.cs index 188f9922..16c51057 100644 --- a/src/Numerics/Distributions/Continuous/Erlang.cs +++ b/src/Numerics/Distributions/Continuous/Erlang.cs @@ -90,7 +90,7 @@ namespace MathNet.Numerics.Distributions /// a normal distribution. public static Erlang WithShapeScale(int shape, double scale) { - return new Erlang(shape, 1.0 / scale); + return new Erlang(shape, 1.0/scale); } /// @@ -142,8 +142,7 @@ namespace MathNet.Numerics.Distributions /// public int Shape { - get { return (int)_shape; } - + get { return (int) _shape; } set { SetParameters(value, _invScale); } } @@ -152,11 +151,10 @@ namespace MathNet.Numerics.Distributions /// 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); } /// @@ -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; } } } diff --git a/src/Numerics/Distributions/Continuous/Exponential.cs b/src/Numerics/Distributions/Continuous/Exponential.cs index dd982fd0..6bdb7fd6 100644 --- a/src/Numerics/Distributions/Continuous/Exponential.cs +++ b/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 /// public double Mean { - get { return 1.0 / _lambda; } + get { return 1.0/_lambda; } } /// @@ -158,7 +157,7 @@ namespace MathNet.Numerics.Distributions /// public double Variance { - get { return 1.0 / (_lambda * _lambda); } + get { return 1.0/(_lambda*_lambda); } } /// @@ -166,7 +165,7 @@ namespace MathNet.Numerics.Distributions /// public double StdDev { - get { return 1.0 / _lambda; } + get { return 1.0/_lambda; } } /// @@ -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 /// public double Median { - get { return Math.Log(2.0) / _lambda; } + get { return Math.Log(2.0)/_lambda; } } /// @@ -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 /// the log density at . 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; } /// diff --git a/src/Numerics/Distributions/Continuous/FisherSnedecor.cs b/src/Numerics/Distributions/Continuous/FisherSnedecor.cs index 93718b62..ddcfd74f 100644 --- a/src/Numerics/Distributions/Continuous/FisherSnedecor.cs +++ b/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 /// the cumulative density at . 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 /// the density at . 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)); } /// @@ -313,7 +311,7 @@ namespace MathNet.Numerics.Distributions /// a FisherSnedecor distributed random number. 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); } /// diff --git a/src/Numerics/Distributions/Continuous/Gamma.cs b/src/Numerics/Distributions/Continuous/Gamma.cs index 67e51e31..7d445739 100644 --- a/src/Numerics/Distributions/Continuous/Gamma.cs +++ b/src/Numerics/Distributions/Continuous/Gamma.cs @@ -96,7 +96,7 @@ namespace MathNet.Numerics.Distributions /// a normal distribution. public static Gamma WithShapeScale(double shape, double scale) { - return new Gamma(shape, 1.0 / scale); + return new Gamma(shape, 1.0/scale); } /// @@ -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 /// 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); } /// @@ -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); } /// @@ -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; } } } diff --git a/src/Numerics/Distributions/Continuous/InverseGamma.cs b/src/Numerics/Distributions/Continuous/InverseGamma.cs index 651aa753..b6bbda07 100644 --- a/src/Numerics/Distributions/Continuous/InverseGamma.cs +++ b/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 /// 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)); } } /// @@ -219,7 +217,7 @@ namespace MathNet.Numerics.Distributions /// 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)); } } /// @@ -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 /// the cumulative density at . 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 /// public double Mode { - get { return _scale / (_shape + 1.0); } + get { return _scale/(_shape + 1.0); } } /// @@ -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 /// a random number from the distribution. 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); } /// diff --git a/src/Numerics/Distributions/Continuous/Laplace.cs b/src/Numerics/Distributions/Continuous/Laplace.cs index d31972d6..4d0be04a 100644 --- a/src/Numerics/Distributions/Continuous/Laplace.cs +++ b/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); } } /// /// Initializes a new instance of the class (location = 0, scale = 1). /// - public Laplace() : this(0.0, 1.0) + public Laplace() + : this(0.0, 1.0) { } @@ -181,7 +180,7 @@ namespace MathNet.Numerics.Distributions /// public double Variance { - get { return 2.0 * _scale * _scale; } + get { return 2.0*_scale*_scale; } } /// @@ -189,7 +188,7 @@ namespace MathNet.Numerics.Distributions /// public double StdDev { - get { return Math.Sqrt(2.0) * _scale; } + get { return Math.Sqrt(2.0)*_scale; } } /// @@ -197,7 +196,7 @@ namespace MathNet.Numerics.Distributions /// public double Entropy { - get { return Math.Log(2.0 * Constants.E * _scale); } + get { return Math.Log(2.0*Constants.E*_scale); } } /// @@ -215,7 +214,7 @@ namespace MathNet.Numerics.Distributions /// the cumulative density at . 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 /// the density at . 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); } /// @@ -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)))); } /// diff --git a/src/Numerics/Distributions/Continuous/LogNormal.cs b/src/Numerics/Distributions/Continuous/LogNormal.cs index 54267679..d5a88a74 100644 --- a/src/Numerics/Distributions/Continuous/LogNormal.cs +++ b/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 /// public double Mean { - get { return Math.Exp(_mu + (_sigma * _sigma / 2.0)); } + get { return Math.Exp(_mu + (_sigma*_sigma/2.0)); } } /// @@ -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 /// public double Mode { - get { return Math.Exp(_mu - (_sigma * _sigma)); } + get { return Math.Exp(_mu - (_sigma*_sigma)); } } /// @@ -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); } /// @@ -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; } /// @@ -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)); } } } diff --git a/src/Numerics/Distributions/Continuous/Normal.cs b/src/Numerics/Distributions/Continuous/Normal.cs index 362ba370..ba145633 100644 --- a/src/Numerics/Distributions/Continuous/Normal.cs +++ b/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 random number generator. /// - public Normal() : this(0.0, 1.0) + public Normal() + : this(0.0, 1.0) { } - + /// /// 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 random number generator. /// /// The random number generator which is used to draw random samples. - 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 /// a normal distribution. 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)); } /// @@ -183,11 +185,11 @@ namespace MathNet.Numerics.Distributions /// 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 /// 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 /// the density at . 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); } /// @@ -322,8 +321,8 @@ namespace MathNet.Numerics.Distributions /// the log density at . 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; } /// @@ -355,7 +354,7 @@ namespace MathNet.Numerics.Distributions /// the cumulative density at . 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))); } /// @@ -377,11 +376,9 @@ namespace MathNet.Numerics.Distributions /// the inverse cumulative density at . 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)); } - - /// /// Samples a pair of standard normal distributed random variables using the Box-Muller algorithm. /// @@ -389,18 +386,18 @@ namespace MathNet.Numerics.Distributions /// a pair of random numbers from the standard normal distribution. internal static Tuple 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(v1 * fac, v2 * fac); + var fac = Math.Sqrt(-2.0*Math.Log(r)/r); + return new Tuple(v1*fac, v2*fac); } /// @@ -412,7 +409,7 @@ namespace MathNet.Numerics.Distributions /// a random number from the distribution. internal static double SampleUnchecked(Random rnd, double mean, double stddev) { - return mean + (stddev * SampleUncheckedBoxMuller(rnd).Item1); + return mean + (stddev*SampleUncheckedBoxMuller(rnd).Item1); } /// @@ -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); } } } diff --git a/src/Numerics/Distributions/Continuous/Pareto.cs b/src/Numerics/Distributions/Continuous/Pareto.cs index 33fda60e..f7932cb2 100644 --- a/src/Numerics/Distributions/Continuous/Pareto.cs +++ b/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 /// 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)); } } /// @@ -214,7 +212,7 @@ namespace MathNet.Numerics.Distributions /// 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; } } /// @@ -222,7 +220,7 @@ namespace MathNet.Numerics.Distributions /// 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); } } /// @@ -232,7 +230,7 @@ namespace MathNet.Numerics.Distributions /// the cumulative density at . 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 /// public double Median { - get { return _scale * Math.Pow(2.0, 1.0 / _shape); } + get { return _scale*Math.Pow(2.0, 1.0/_shape); } } /// @@ -278,7 +276,7 @@ namespace MathNet.Numerics.Distributions /// the density at . 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); } /// @@ -302,7 +300,7 @@ namespace MathNet.Numerics.Distributions /// a random number from the Pareto distribution. 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); } /// diff --git a/src/Numerics/Distributions/Continuous/Rayleigh.cs b/src/Numerics/Distributions/Continuous/Rayleigh.cs index 68b296f2..a7c29470 100644 --- a/src/Numerics/Distributions/Continuous/Rayleigh.cs +++ b/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 /// public double Mean { - get { return _scale * Math.Sqrt(Constants.PiOver2); } + get { return _scale*Math.Sqrt(Constants.PiOver2); } } /// @@ -163,7 +162,7 @@ namespace MathNet.Numerics.Distributions /// public double Variance { - get { return (2.0 - Constants.PiOver2) * _scale * _scale; } + get { return (2.0 - Constants.PiOver2)*_scale*_scale; } } /// @@ -171,7 +170,7 @@ namespace MathNet.Numerics.Distributions /// public double StdDev { - get { return Math.Sqrt(2.0 - Constants.PiOver2) * _scale; } + get { return Math.Sqrt(2.0 - Constants.PiOver2)*_scale; } } /// @@ -179,7 +178,7 @@ namespace MathNet.Numerics.Distributions /// 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); } } /// @@ -187,7 +186,7 @@ namespace MathNet.Numerics.Distributions /// 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); } } /// @@ -197,7 +196,7 @@ namespace MathNet.Numerics.Distributions /// the cumulative density at . 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 /// public double Median { - get { return _scale * Math.Sqrt(Math.Log(4.0)); } + get { return _scale*Math.Sqrt(Math.Log(4.0)); } } /// @@ -243,7 +242,7 @@ namespace MathNet.Numerics.Distributions /// the density at . 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)); } /// @@ -253,7 +252,7 @@ namespace MathNet.Numerics.Distributions /// the log density at . 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 /// a random number from the Rayleigh distribution. 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())); } /// diff --git a/src/Numerics/Distributions/Continuous/Stable.cs b/src/Numerics/Distributions/Continuous/Stable.cs index e24491b7..9e83926c 100644 --- a/src/Numerics/Distributions/Continuous/Stable.cs +++ b/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); } /// @@ -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 } } } -} +} \ No newline at end of file diff --git a/src/Numerics/Distributions/Continuous/StudentT.cs b/src/Numerics/Distributions/Continuous/StudentT.cs index 1a2ef64b..de726169 100644 --- a/src/Numerics/Distributions/Continuous/StudentT.cs +++ b/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 random number generator. /// - 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; } /// @@ -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); } /// @@ -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)); } /// diff --git a/src/Numerics/Distributions/Continuous/Weibull.cs b/src/Numerics/Distributions/Continuous/Weibull.cs index 86b16e4d..34bad113 100644 --- a/src/Numerics/Distributions/Continuous/Weibull.cs +++ b/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 /// public double Mean { - get { return _scale * SpecialFunctions.Gamma(1.0 + (1.0 / _shape)); } + get { return _scale*SpecialFunctions.Gamma(1.0 + (1.0/_shape)); } } /// @@ -189,7 +187,7 @@ namespace MathNet.Numerics.Distributions /// 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); } } /// @@ -205,7 +203,7 @@ namespace MathNet.Numerics.Distributions /// 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; } } /// @@ -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 /// public double Median { - get { return _scale * Math.Pow(Constants.Ln2, 1.0 / _shape); } + get { return _scale*Math.Pow(Constants.Ln2, 1.0/_shape); } } /// @@ -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); } /// diff --git a/src/Numerics/Distributions/Discrete/Bernoulli.cs b/src/Numerics/Distributions/Discrete/Bernoulli.cs index 112abad8..3811f8d0 100644 --- a/src/Numerics/Distributions/Discrete/Bernoulli.cs +++ b/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 /// public double StdDev { - get { return Math.Sqrt(_p * (1.0 - _p)); } + get { return Math.Sqrt(_p*(1.0 - _p)); } } /// @@ -164,7 +163,7 @@ namespace MathNet.Numerics.Distributions /// public double Variance { - get { return _p * (1.0 - _p); } + get { return _p*(1.0 - _p); } } /// @@ -172,7 +171,7 @@ namespace MathNet.Numerics.Distributions /// 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)); } } /// @@ -180,7 +179,7 @@ namespace MathNet.Numerics.Distributions /// 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)); } } /// diff --git a/src/Numerics/Distributions/Discrete/Binomial.cs b/src/Numerics/Distributions/Discrete/Binomial.cs index 1d8bb3ae..b1ff288b 100644 --- a/src/Numerics/Distributions/Discrete/Binomial.cs +++ b/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 /// public double Mean { - get { return _p * _n; } + get { return _p*_n; } } /// @@ -184,7 +182,7 @@ namespace MathNet.Numerics.Distributions /// public double StdDev { - get { return Math.Sqrt(_p * (1.0 - _p) * _n); } + get { return Math.Sqrt(_p*(1.0 - _p)*_n); } } /// @@ -192,7 +190,7 @@ namespace MathNet.Numerics.Distributions /// public double Variance { - get { return _p * (1.0 - _p) * _n; } + get { return _p*(1.0 - _p)*_n; } } /// @@ -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 /// 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)); } } /// @@ -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 /// public int Median { - get { return (int)Math.Floor(_p * _n); } + get { return (int) Math.Floor(_p*_n); } } /// @@ -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); } /// @@ -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 diff --git a/src/Numerics/Distributions/Discrete/Categorical.cs b/src/Numerics/Distributions/Discrete/Categorical.cs index 8f2067aa..17cebe1e 100644 --- a/src/Numerics/Distributions/Discrete/Categorical.cs +++ b/src/Numerics/Distributions/Discrete/Categorical.cs @@ -197,7 +197,7 @@ namespace MathNet.Numerics.Distributions /// exactly in a floating point representation. public double[] P { - get { return (double[])_pmfNormalized.Clone(); } + get { return (double[]) _pmfNormalized.Clone(); } set { SetParameters(value); } } @@ -249,7 +249,7 @@ namespace MathNet.Numerics.Distributions /// public double Entropy { - get { return _pmfNormalized.Sum(p => p * Math.Log(p)); } + get { return _pmfNormalized.Sum(p => p*Math.Log(p)); } } /// @@ -315,7 +315,7 @@ namespace MathNet.Numerics.Distributions /// public int Median { - get { return (int)_pmfNormalized.Median(); } + get { return (int) _pmfNormalized.Median(); } } /// @@ -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]) diff --git a/src/Numerics/Distributions/Discrete/ConwayMaxwellPoisson.cs b/src/Numerics/Distributions/Discrete/ConwayMaxwellPoisson.cs index 122d6cfe..c341fb01 100644 --- a/src/Numerics/Distributions/Discrete/ConwayMaxwellPoisson.cs +++ b/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 /// 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; } /// @@ -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; } diff --git a/src/Numerics/Distributions/Discrete/DiscreteUniform.cs b/src/Numerics/Distributions/Discrete/DiscreteUniform.cs index 09dc0ba9..cc6e37ab 100644 --- a/src/Numerics/Distributions/Discrete/DiscreteUniform.cs +++ b/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 /// public double Mean { - get { return (_lower + _upper) / 2.0; } + get { return (_lower + _upper)/2.0; } } /// @@ -176,7 +174,7 @@ namespace MathNet.Numerics.Distributions /// 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); } } /// @@ -184,7 +182,7 @@ namespace MathNet.Numerics.Distributions /// 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; } } /// @@ -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 /// public int Mode { - get { return (int)Math.Floor((_lower + _upper) / 2.0); } + get { return (int) Math.Floor((_lower + _upper)/2.0); } } /// @@ -256,7 +254,7 @@ namespace MathNet.Numerics.Distributions /// public int Median { - get { return (int)Math.Floor((_lower + _upper) / 2.0); } + get { return (int) Math.Floor((_lower + _upper)/2.0); } } /// @@ -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 /// A random sample from the discrete uniform distribution. internal static int SampleUnchecked(Random rnd, int lower, int upper) { - return (rnd.Next() % (upper - lower + 1)) + lower; + return (rnd.Next()%(upper - lower + 1)) + lower; } /// diff --git a/src/Numerics/Distributions/Discrete/Geometric.cs b/src/Numerics/Distributions/Discrete/Geometric.cs index b5b142c8..c085eb38 100644 --- a/src/Numerics/Distributions/Discrete/Geometric.cs +++ b/src/Numerics/Distributions/Discrete/Geometric.cs @@ -150,7 +150,7 @@ namespace MathNet.Numerics.Distributions /// public double Mean { - get { return 1.0 / _p; } + get { return 1.0/_p; } } /// @@ -158,7 +158,7 @@ namespace MathNet.Numerics.Distributions /// public double Variance { - get { return (1.0 - _p) / (_p * _p); } + get { return (1.0 - _p)/(_p*_p); } } /// @@ -166,7 +166,7 @@ namespace MathNet.Numerics.Distributions /// public double StdDev { - get { return Math.Sqrt(1.0 - _p) / _p; } + get { return Math.Sqrt(1.0 - _p)/_p; } } /// @@ -174,7 +174,7 @@ namespace MathNet.Numerics.Distributions /// 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; } } /// @@ -183,7 +183,7 @@ namespace MathNet.Numerics.Distributions /// Throws a not supported exception. public double Skewness { - get { return (2.0 - _p) / Math.Sqrt(1.0 - _p); } + get { return (2.0 - _p)/Math.Sqrt(1.0 - _p); } } /// @@ -213,7 +213,7 @@ namespace MathNet.Numerics.Distributions /// 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)); } } /// @@ -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; } /// @@ -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 /// 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)); } /// diff --git a/src/Numerics/Distributions/Discrete/Hypergeometric.cs b/src/Numerics/Distributions/Discrete/Hypergeometric.cs index f5d0f679..a6396a49 100644 --- a/src/Numerics/Distributions/Discrete/Hypergeometric.cs +++ b/src/Numerics/Distributions/Discrete/Hypergeometric.cs @@ -196,7 +196,7 @@ namespace MathNet.Numerics.Distributions /// public double Mean { - get { return (double)_m * _n / _populationSize; } + get { return (double) _m*_n/_populationSize; } } /// @@ -204,7 +204,7 @@ namespace MathNet.Numerics.Distributions /// 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)); } } /// @@ -228,7 +228,7 @@ namespace MathNet.Numerics.Distributions /// 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)); } } /// @@ -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 /// public int Mode { - get { return (_n + 1) * (_m + 1) / (_populationSize + 2); } + get { return (_n + 1)*(_m + 1)/(_populationSize + 2); } } /// @@ -305,7 +305,7 @@ namespace MathNet.Numerics.Distributions /// 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); } /// @@ -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; } diff --git a/src/Numerics/Distributions/Discrete/NegativeBinomial.cs b/src/Numerics/Distributions/Discrete/NegativeBinomial.cs index 8de4d9f5..31b731ba 100644 --- a/src/Numerics/Distributions/Discrete/NegativeBinomial.cs +++ b/src/Numerics/Distributions/Discrete/NegativeBinomial.cs @@ -172,7 +172,7 @@ namespace MathNet.Numerics.Distributions /// public double Mean { - get { return _r * (1.0 - _p) / _p; } + get { return _r*(1.0 - _p)/_p; } } /// @@ -180,7 +180,7 @@ namespace MathNet.Numerics.Distributions /// public double Variance { - get { return _r * (1.0 - _p) / (_p * _p); } + get { return _r*(1.0 - _p)/(_p*_p); } } /// @@ -188,7 +188,7 @@ namespace MathNet.Numerics.Distributions /// public double StdDev { - get { return Math.Sqrt(_r * (1.0 - _p)) / _p; } + get { return Math.Sqrt(_r*(1.0 - _p))/_p; } } /// @@ -204,7 +204,7 @@ namespace MathNet.Numerics.Distributions /// 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)); } } /// @@ -226,7 +226,7 @@ namespace MathNet.Numerics.Distributions /// 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; } } /// @@ -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; } diff --git a/src/Numerics/Distributions/Discrete/Poisson.cs b/src/Numerics/Distributions/Discrete/Poisson.cs index 08669df2..34f2f5c8 100644 --- a/src/Numerics/Distributions/Discrete/Poisson.cs +++ b/src/Numerics/Distributions/Discrete/Poisson.cs @@ -167,7 +167,7 @@ namespace MathNet.Numerics.Distributions /// Approximation, see Wikipedia Poisson distribution 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)); } } /// @@ -175,7 +175,7 @@ namespace MathNet.Numerics.Distributions /// public double Skewness { - get { return 1.0 / Math.Sqrt(_lambda); } + get { return 1.0/Math.Sqrt(_lambda); } } /// @@ -213,7 +213,7 @@ namespace MathNet.Numerics.Distributions /// public int Mode { - get { return (int)Math.Floor(_lambda); } + get { return (int) Math.Floor(_lambda); } } /// @@ -222,7 +222,7 @@ namespace MathNet.Numerics.Distributions /// Approximation, see Wikipedia Poisson distribution 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)); } } /// @@ -232,7 +232,7 @@ namespace MathNet.Numerics.Distributions /// the probability mass at location . 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)); } /// @@ -242,7 +242,7 @@ namespace MathNet.Numerics.Distributions /// the log probability mass at location . 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. 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; diff --git a/src/Numerics/Distributions/Discrete/Zipf.cs b/src/Numerics/Distributions/Discrete/Zipf.cs index fc923684..48f40bff 100644 --- a/src/Numerics/Distributions/Discrete/Zipf.cs +++ b/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 /// 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); } } /// @@ -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 /// 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); } /// @@ -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; diff --git a/src/Numerics/Distributions/Multivariate/Dirichlet.cs b/src/Numerics/Distributions/Multivariate/Dirichlet.cs index f9f05849..fb730b9c 100644 --- a/src/Numerics/Distributions/Multivariate/Dirichlet.cs +++ b/src/Numerics/Distributions/Multivariate/Dirichlet.cs @@ -48,12 +48,12 @@ namespace MathNet.Numerics.Distributions /// /// The Dirichlet distribution parameters. /// - private double[] _alpha; + double[] _alpha; /// /// The distribution's random number generator. /// - private Random _random; + Random _random; /// /// 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 /// /// The parameters of the Dirichlet distribution. /// When the parameters don't pass the function. - 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(); } /// @@ -175,10 +175,7 @@ namespace MathNet.Numerics.Distributions /// public int Dimension { - get - { - return _alpha.Length; - } + get { return _alpha.Length; } } /// @@ -186,26 +183,16 @@ namespace MathNet.Numerics.Distributions /// public double[] Alpha { - get - { - return _alpha; - } - - set - { - SetParameters(value); - } + get { return _alpha; } + set { SetParameters(value); } } /// /// Gets the sum of the Dirichlet parameters. /// - private double AlphaSum + double AlphaSum { - get - { - return _alpha.Sum(); - } + get { return _alpha.Sum(); } } /// @@ -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 /// a sample from the distribution. public static double[] Sample(Random rnd, double[] alpha) { - if (Control.CheckDistributionParameters && ! IsValidParameterSet(alpha)) + if (Control.CheckDistributionParameters && !IsValidParameterSet(alpha)) { throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters); } diff --git a/src/Numerics/Distributions/Multivariate/InverseWishart.cs b/src/Numerics/Distributions/Multivariate/InverseWishart.cs index 86d16785..acdee249 100644 --- a/src/Numerics/Distributions/Multivariate/InverseWishart.cs +++ b/src/Numerics/Distributions/Multivariate/InverseWishart.cs @@ -47,22 +47,22 @@ namespace MathNet.Numerics.Distributions /// /// The degrees of freedom for the inverse Wishart distribution. /// - private double _nu; + double _nu; /// /// The scale matrix for the inverse Wishart distribution. /// - private Matrix _s; + Matrix _s; /// /// Caches the Cholesky factorization of the scale matrix. /// - private Cholesky _chol; + Cholesky _chol; /// /// The distribution's random number generator. /// - private Random _random; + Random _random; /// /// Initializes a new instance of the class. @@ -102,7 +102,7 @@ namespace MathNet.Numerics.Distributions /// The degrees of freedom for the Wishart distribution. /// The scale matrix for the Wishart distribution. /// When the parameters don't pass the function. - private void SetParameters(double nu, Matrix s) + void SetParameters(double nu, Matrix s) { if (Control.CheckDistributionParameters && !IsValidParameterSet(nu, s)) { @@ -120,7 +120,7 @@ namespace MathNet.Numerics.Distributions /// The degrees of freedom for the Wishart distribution. /// The scale matrix for the Wishart distribution. /// true when the parameters are valid, false otherwise. - private static bool IsValidParameterSet(double nu, Matrix s) + static bool IsValidParameterSet(double nu, Matrix s) { if (s.RowCount != s.ColumnCount) { @@ -148,15 +148,8 @@ namespace MathNet.Numerics.Distributions /// public double Nu { - get - { - return _nu; - } - - set - { - SetParameters(value, _s); - } + get { return _nu; } + set { SetParameters(value, _s); } } /// @@ -164,15 +157,8 @@ namespace MathNet.Numerics.Distributions /// public Matrix S { - get - { - return _s; - } - - set - { - SetParameters(_nu, value); - } + get { return _s; } + set { SetParameters(_nu, value); } } /// @@ -198,10 +184,7 @@ namespace MathNet.Numerics.Distributions /// The mean of the distribution. public Matrix Mean { - get - { - return _s * (1.0 / (_nu - _s.RowCount - 1.0)); - } + get { return _s*(1.0/(_nu - _s.RowCount - 1.0)); } } /// @@ -211,10 +194,7 @@ namespace MathNet.Numerics.Distributions /// 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. public Matrix Mode { - get - { - return _s * (1.0 / (_nu + _s.RowCount + 1.0)); - } + get { return _s*(1.0/(_nu + _s.RowCount + 1.0)); } } /// @@ -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; } /// diff --git a/src/Numerics/Distributions/Multivariate/MatrixNormal.cs b/src/Numerics/Distributions/Multivariate/MatrixNormal.cs index c152c7a4..68eeb7e3 100644 --- a/src/Numerics/Distributions/Multivariate/MatrixNormal.cs +++ b/src/Numerics/Distributions/Multivariate/MatrixNormal.cs @@ -48,22 +48,22 @@ namespace MathNet.Numerics.Distributions /// /// The mean of the matrix normal distribution. /// - private Matrix _m; + Matrix _m; /// /// The covariance matrix for the rows. /// - private Matrix _v; + Matrix _v; /// /// The covariance matrix for the columns. /// - private Matrix _k; + Matrix _k; /// /// The distribution's random number generator. /// - private Random _random; + Random _random; /// /// Initializes a new instance of the class. @@ -109,15 +109,8 @@ namespace MathNet.Numerics.Distributions /// The mean of the distribution. public Matrix Mean { - get - { - return _m; - } - - set - { - SetParameters(value, _v, _k); - } + get { return _m; } + set { SetParameters(value, _v, _k); } } /// @@ -126,15 +119,8 @@ namespace MathNet.Numerics.Distributions /// The row covariance. public Matrix RowCovariance { - get - { - return _v; - } - - set - { - SetParameters(_m, value, _k); - } + get { return _v; } + set { SetParameters(_m, value, _k); } } /// @@ -143,15 +129,8 @@ namespace MathNet.Numerics.Distributions /// The column covariance. public Matrix ColumnCovariance { - get - { - return _k; - } - - set - { - SetParameters(_m, _v, value); - } + get { return _k; } + set { SetParameters(_m, _v, value); } } /// @@ -161,7 +140,7 @@ namespace MathNet.Numerics.Distributions /// The covariance matrix for the rows. /// The covariance matrix for the columns. /// When the parameters don't pass the function. - private void SetParameters(Matrix m, Matrix v, Matrix k) + void SetParameters(Matrix m, Matrix v, Matrix k) { if (Control.CheckDistributionParameters && !IsValidParameterSet(m, v, k)) { @@ -180,7 +159,7 @@ namespace MathNet.Numerics.Distributions /// The covariance matrix for the rows. /// The covariance matrix for the columns. /// true when the parameters are valid, false otherwise. - private static bool IsValidParameterSet(Matrix m, Matrix v, Matrix k) + static bool IsValidParameterSet(Matrix m, Matrix v, Matrix k) { var n = m.RowCount; var p = m.ColumnCount; @@ -247,10 +226,10 @@ namespace MathNet.Numerics.Distributions var cholV = Cholesky.Create(_v); var cholK = Cholesky.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); } /// @@ -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 /// The mean of the vector normal distribution. /// The covariance matrix of the vector normal distribution. /// a sequence of samples from defined distribution. - private static Vector SampleVectorNormal(Random rnd, Vector mean, Matrix covariance) + static Vector SampleVectorNormal(Random rnd, Vector mean, Matrix covariance) { var chol = Cholesky.Create(covariance); return SampleVectorNormal(rnd, mean, chol); @@ -320,7 +299,7 @@ namespace MathNet.Numerics.Distributions /// The mean of the vector normal distribution. /// The Cholesky factorization of the covariance matrix. /// a sequence of samples from defined distribution. - private static Vector SampleVectorNormal(Random rnd, Vector mean, Cholesky cholesky) + static Vector SampleVectorNormal(Random rnd, Vector mean, Cholesky 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); } } } diff --git a/src/Numerics/Distributions/Multivariate/Multinomial.cs b/src/Numerics/Distributions/Multivariate/Multinomial.cs index 8db431c6..44a5c7ac 100644 --- a/src/Numerics/Distributions/Multivariate/Multinomial.cs +++ b/src/Numerics/Distributions/Multivariate/Multinomial.cs @@ -51,17 +51,17 @@ namespace MathNet.Numerics.Distributions /// /// Stores the normalized multinomial probabilities. /// - private double[] _p; + double[] _p; /// /// The number of trials. /// - private int _n; + int _n; /// /// The distribution's random number generator. /// - private Random _random; + Random _random; /// /// Initializes a new instance of the Multinomial class. @@ -98,7 +98,7 @@ namespace MathNet.Numerics.Distributions /// /// Histogram instance /// The number of trials. - /// If any of the probabilities are negative or do not sum to one. + /// If any of the probabilities are negative or do not sum to one. /// If is negative. public Multinomial(Histogram h, int n) { @@ -137,7 +137,7 @@ namespace MathNet.Numerics.Distributions /// The number of trials. /// If any of the probabilities are negative returns false, /// if the sum of parameters is 0.0, or if the number of trials is negative; otherwise true. - private static bool IsValidParameterSet(IEnumerable p, int n) + static bool IsValidParameterSet(IEnumerable 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. /// The number of trials. /// When the parameters don't pass the function. - 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 /// public double[] P { - get - { - return (double[])_p.Clone(); - } - - set - { - SetParameters(value, _n); - } + get { return (double[]) _p.Clone(); } + set { SetParameters(value, _n); } } /// @@ -197,15 +190,8 @@ namespace MathNet.Numerics.Distributions /// public int N { - get - { - return _n; - } - - set - { - SetParameters(_p, value); - } + get { return _n; } + set { SetParameters(_p, value); } } /// @@ -230,10 +216,7 @@ namespace MathNet.Numerics.Distributions /// public Vector Mean { - get - { - return _n * (DenseVector)P; - } + get { return _n*(DenseVector) P; } } /// @@ -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; } diff --git a/src/Numerics/Distributions/Multivariate/NormalGamma.cs b/src/Numerics/Distributions/Multivariate/NormalGamma.cs index 61121993..87505126 100644 --- a/src/Numerics/Distributions/Multivariate/NormalGamma.cs +++ b/src/Numerics/Distributions/Multivariate/NormalGamma.cs @@ -39,12 +39,12 @@ namespace MathNet.Numerics.Distributions /// /// The mean value. /// - private double _mean; + double _mean; /// /// The precision value. /// - private double _precision; + double _precision; /// /// Initializes a new instance of the struct. @@ -62,15 +62,9 @@ namespace MathNet.Numerics.Distributions /// public double Mean { - get - { - return _mean; - } + get { return _mean; } - set - { - _mean = value; - } + set { _mean = value; } } /// @@ -78,15 +72,9 @@ namespace MathNet.Numerics.Distributions /// public double Precision { - get - { - return _precision; - } + get { return _precision; } - set - { - _precision = value; - } + set { _precision = value; } } } @@ -112,27 +100,27 @@ namespace MathNet.Numerics.Distributions /// /// The location of the mean. /// - private double _meanLocation; + double _meanLocation; /// /// The scale of the mean. /// - private double _meanScale; + double _meanScale; /// /// The shape of the precision. /// - private double _precisionShape; + double _precisionShape; /// /// The inverse scale of the precision. /// - private double _precisionInvScale; + double _precisionInvScale; /// /// The distribution's random number generator. /// - private Random _random; + Random _random; /// /// Initializes a new instance of the class. @@ -169,7 +157,7 @@ namespace MathNet.Numerics.Distributions /// The shape of the precision. /// The inverse scale of the precision. /// true when the parameters are valid, false otherwise. - 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 /// The shape of the precision. /// The inverse scale of the precision. /// When the parameters don't pass the function. - 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 + ")"; } /// @@ -217,15 +205,8 @@ namespace MathNet.Numerics.Distributions /// public double MeanLocation { - get - { - return _meanLocation; - } - - set - { - SetParameters(value, _meanScale, _precisionShape, _precisionInvScale); - } + get { return _meanLocation; } + set { SetParameters(value, _meanScale, _precisionShape, _precisionInvScale); } } /// @@ -233,15 +214,8 @@ namespace MathNet.Numerics.Distributions /// public double MeanScale { - get - { - return _meanScale; - } - - set - { - SetParameters(_meanLocation, value, _precisionShape, _precisionInvScale); - } + get { return _meanScale; } + set { SetParameters(_meanLocation, value, _precisionShape, _precisionInvScale); } } /// @@ -249,15 +223,8 @@ namespace MathNet.Numerics.Distributions /// public double PrecisionShape { - get - { - return _precisionShape; - } - - set - { - SetParameters(_meanLocation, _meanScale, value, _precisionInvScale); - } + get { return _precisionShape; } + set { SetParameters(_meanLocation, _meanScale, value, _precisionInvScale); } } /// @@ -265,15 +232,8 @@ namespace MathNet.Numerics.Distributions /// public double PrecisionInverseScale { - get - { - return _precisionInvScale; - } - - set - { - SetParameters(_meanLocation, _meanScale, _precisionShape, value); - } + get { return _precisionInvScale; } + set { SetParameters(_meanLocation, _meanScale, _precisionShape, value); } } /// @@ -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); } /// @@ -322,10 +282,7 @@ namespace MathNet.Numerics.Distributions /// The mean of the distribution. 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); } } /// @@ -334,10 +291,7 @@ namespace MathNet.Numerics.Distributions /// The mean of the distribution. 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)); } } /// @@ -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)); } /// @@ -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); } /// @@ -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; } diff --git a/src/Numerics/Distributions/Multivariate/Wishart.cs b/src/Numerics/Distributions/Multivariate/Wishart.cs index 5d6b553c..b08630ef 100644 --- a/src/Numerics/Distributions/Multivariate/Wishart.cs +++ b/src/Numerics/Distributions/Multivariate/Wishart.cs @@ -49,22 +49,22 @@ namespace MathNet.Numerics.Distributions /// /// The degrees of freedom for the Wishart distribution. /// - private double _nu; + double _nu; /// /// The scale matrix for the Wishart distribution. /// - private Matrix _s; + Matrix _s; /// /// Caches the Cholesky factorization of the scale matrix. /// - private Cholesky _chol; + Cholesky _chol; /// /// The distribution's random number generator. /// - private Random _random; + Random _random; /// /// Initializes a new instance of the class. @@ -95,7 +95,7 @@ namespace MathNet.Numerics.Distributions /// The degrees of freedom for the Wishart distribution. /// The scale matrix for the Wishart distribution. /// When the parameters don't pass the function. - private void SetParameters(double nu, Matrix s) + void SetParameters(double nu, Matrix s) { if (Control.CheckDistributionParameters && !IsValidParameterSet(nu, s)) { @@ -113,7 +113,7 @@ namespace MathNet.Numerics.Distributions /// The degrees of freedom for the Wishart distribution. /// The scale matrix for the Wishart distribution. /// true when the parameters are valid, false otherwise. - private static bool IsValidParameterSet(double nu, Matrix s) + static bool IsValidParameterSet(double nu, Matrix s) { if (s.RowCount != s.ColumnCount) { @@ -141,15 +141,8 @@ namespace MathNet.Numerics.Distributions /// public double Nu { - get - { - return _nu; - } - - set - { - SetParameters(value, _s); - } + get { return _nu; } + set { SetParameters(value, _s); } } /// @@ -157,15 +150,8 @@ namespace MathNet.Numerics.Distributions /// public Matrix S { - get - { - return _s; - } - - set - { - SetParameters(_nu, value); - } + get { return _s; } + set { SetParameters(_nu, value); } } /// @@ -200,10 +186,7 @@ namespace MathNet.Numerics.Distributions /// The mean of the distribution. public Matrix Mean { - get - { - return _nu * _s; - } + get { return _nu*_s; } } /// @@ -212,10 +195,7 @@ namespace MathNet.Numerics.Distributions /// The mode of the distribution. public Matrix Mode { - get - { - return (_nu - _s.RowCount - 1.0) * _s; - } + get { return (_nu - _s.RowCount - 1.0)*_s; } } /// @@ -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; } /// @@ -311,7 +291,7 @@ namespace MathNet.Numerics.Distributions /// The S parameter to use. /// The cholesky decomposition to use. /// a random number from the distribution. - private static Matrix DoSample(Random rnd, double nu, Matrix s, Cholesky chol) + static Matrix DoSample(Random rnd, double nu, Matrix s, Cholesky 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(); } } }