diff --git a/src/Numerics/Distributions/Bernoulli.cs b/src/Numerics/Distributions/Bernoulli.cs index a1707abd..2cf69852 100644 --- a/src/Numerics/Distributions/Bernoulli.cs +++ b/src/Numerics/Distributions/Bernoulli.cs @@ -87,7 +87,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "Bernoulli(p = " + _p + ")"; + return $"Bernoulli(p = {_p})"; } /// diff --git a/src/Numerics/Distributions/Beta.cs b/src/Numerics/Distributions/Beta.cs index 2677901a..e2c45631 100644 --- a/src/Numerics/Distributions/Beta.cs +++ b/src/Numerics/Distributions/Beta.cs @@ -97,7 +97,7 @@ namespace MathNet.Numerics.Distributions /// A string representation of the Beta distribution. public override string ToString() { - return "Beta(α = " + _shapeA + ", β = " + _shapeB + ")"; + return $"Beta(α = {_shapeA}, β = {_shapeB})"; } /// diff --git a/src/Numerics/Distributions/BetaScaled.cs b/src/Numerics/Distributions/BetaScaled.cs index b0dfa2b0..ef8fdd92 100644 --- a/src/Numerics/Distributions/BetaScaled.cs +++ b/src/Numerics/Distributions/BetaScaled.cs @@ -136,7 +136,7 @@ namespace MathNet.Numerics.Distributions /// A string representation of the BetaScaled distribution. public override string ToString() { - return "BetaScaled(α = " + _shapeA + ", β = " + _shapeB + ", μ = " + _location + ", σ = " + _scale + ")"; + return $"BetaScaled(α = {_shapeA}, β = {_shapeB}, μ = {_location}, σ = {_scale})"; } /// diff --git a/src/Numerics/Distributions/Binomial.cs b/src/Numerics/Distributions/Binomial.cs index fc6a797b..4a58f163 100644 --- a/src/Numerics/Distributions/Binomial.cs +++ b/src/Numerics/Distributions/Binomial.cs @@ -95,7 +95,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "Binomial(p = " + _p + ", n = " + _trials + ")"; + return $"Binomial(p = {_p}, n = {_trials})"; } /// diff --git a/src/Numerics/Distributions/Burr.cs b/src/Numerics/Distributions/Burr.cs index 6d81b07a..f485afaa 100644 --- a/src/Numerics/Distributions/Burr.cs +++ b/src/Numerics/Distributions/Burr.cs @@ -36,7 +36,7 @@ namespace MathNet.Numerics.Distributions { public class Burr : IContinuousDistribution { - private System.Random _random; + System.Random _random; /// /// Gets the scale (a) of the distribution. Range: a > 0. @@ -78,7 +78,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "Burr(a = " + a + ", c = " + c + ", k = " + k + ")"; + return $"Burr(a = {a}, c = {c}, k = {k})"; } /// diff --git a/src/Numerics/Distributions/Categorical.cs b/src/Numerics/Distributions/Categorical.cs index f686f0a9..70bf1152 100644 --- a/src/Numerics/Distributions/Categorical.cs +++ b/src/Numerics/Distributions/Categorical.cs @@ -154,7 +154,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "Categorical(Dimension = " + _pmfNormalized.Length + ")"; + return $"Categorical(Dimension = {_pmfNormalized.Length})"; } /// diff --git a/src/Numerics/Distributions/Cauchy.cs b/src/Numerics/Distributions/Cauchy.cs index 490b66b6..b129465a 100644 --- a/src/Numerics/Distributions/Cauchy.cs +++ b/src/Numerics/Distributions/Cauchy.cs @@ -95,7 +95,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "Cauchy(x0 = " + _location + ", γ = " + _scale + ")"; + return $"Cauchy(x0 = {_location}, γ = {_scale})"; } /// diff --git a/src/Numerics/Distributions/Chi.cs b/src/Numerics/Distributions/Chi.cs index f46e3087..131d366a 100644 --- a/src/Numerics/Distributions/Chi.cs +++ b/src/Numerics/Distributions/Chi.cs @@ -85,7 +85,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "Chi(k = " + _freedom + ")"; + return $"Chi(k = {_freedom})"; } /// diff --git a/src/Numerics/Distributions/ChiSquared.cs b/src/Numerics/Distributions/ChiSquared.cs index c58ff529..7ca908ba 100644 --- a/src/Numerics/Distributions/ChiSquared.cs +++ b/src/Numerics/Distributions/ChiSquared.cs @@ -83,7 +83,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "ChiSquared(k = " + _freedom + ")"; + return $"ChiSquared(k = {_freedom})"; } /// diff --git a/src/Numerics/Distributions/ContinuousUniform.cs b/src/Numerics/Distributions/ContinuousUniform.cs index b9bdf416..adf0c372 100644 --- a/src/Numerics/Distributions/ContinuousUniform.cs +++ b/src/Numerics/Distributions/ContinuousUniform.cs @@ -97,7 +97,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "ContinuousUniform(Lower = " + _lower + ", Upper = " + _upper + ")"; + return $"ContinuousUniform(Lower = {_lower}, Upper = {_upper})"; } /// diff --git a/src/Numerics/Distributions/ConwayMaxwellPoisson.cs b/src/Numerics/Distributions/ConwayMaxwellPoisson.cs index dc6c80a5..0c0540cf 100644 --- a/src/Numerics/Distributions/ConwayMaxwellPoisson.cs +++ b/src/Numerics/Distributions/ConwayMaxwellPoisson.cs @@ -116,7 +116,7 @@ namespace MathNet.Numerics.Distributions /// A that represents this instance. public override string ToString() { - return "ConwayMaxwellPoisson(λ = " + _lambda + ", ν = " + _nu + ")"; + return $"ConwayMaxwellPoisson(λ = {_lambda}, ν = {_nu})"; } /// diff --git a/src/Numerics/Distributions/Dirichlet.cs b/src/Numerics/Distributions/Dirichlet.cs index c9f4c36c..c8822253 100644 --- a/src/Numerics/Distributions/Dirichlet.cs +++ b/src/Numerics/Distributions/Dirichlet.cs @@ -132,7 +132,7 @@ namespace MathNet.Numerics.Distributions /// public override string ToString() { - return "Dirichlet(Dimension = " + Dimension + ")"; + return $"Dirichlet(Dimension = {Dimension})"; } /// diff --git a/src/Numerics/Distributions/DiscreteUniform.cs b/src/Numerics/Distributions/DiscreteUniform.cs index 0b1a5d29..d6921e47 100644 --- a/src/Numerics/Distributions/DiscreteUniform.cs +++ b/src/Numerics/Distributions/DiscreteUniform.cs @@ -90,7 +90,7 @@ namespace MathNet.Numerics.Distributions /// public override string ToString() { - return "DiscreteUniform(Lower = " + _lower + ", Upper = " + _upper + ")"; + return $"DiscreteUniform(Lower = {_lower}, Upper = {_upper})"; } /// diff --git a/src/Numerics/Distributions/Erlang.cs b/src/Numerics/Distributions/Erlang.cs index 5a9ee3a8..6ff68e1e 100644 --- a/src/Numerics/Distributions/Erlang.cs +++ b/src/Numerics/Distributions/Erlang.cs @@ -112,7 +112,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "Erlang(k = " + _shape + ", λ = " + _rate + ")"; + return $"Erlang(k = {_shape}, λ = {_rate})"; } /// diff --git a/src/Numerics/Distributions/Exponential.cs b/src/Numerics/Distributions/Exponential.cs index a7967d0a..d5d92fc6 100644 --- a/src/Numerics/Distributions/Exponential.cs +++ b/src/Numerics/Distributions/Exponential.cs @@ -84,7 +84,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "Exponential(λ = " + _rate + ")"; + return $"Exponential(λ = {_rate})"; } /// diff --git a/src/Numerics/Distributions/FisherSnedecor.cs b/src/Numerics/Distributions/FisherSnedecor.cs index d6277d39..88dd7e93 100644 --- a/src/Numerics/Distributions/FisherSnedecor.cs +++ b/src/Numerics/Distributions/FisherSnedecor.cs @@ -89,7 +89,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "FisherSnedecor(d1 = " + _freedom1 + ", d2 = " + _freedom2 + ")"; + return $"FisherSnedecor(d1 = {_freedom1}, d2 = {_freedom2})"; } /// diff --git a/src/Numerics/Distributions/Gamma.cs b/src/Numerics/Distributions/Gamma.cs index b7ada40b..cf3ec699 100644 --- a/src/Numerics/Distributions/Gamma.cs +++ b/src/Numerics/Distributions/Gamma.cs @@ -121,7 +121,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "Gamma(α = " + _shape + ", β = " + _rate + ")"; + return $"Gamma(α = {_shape}, β = {_rate})"; } /// diff --git a/src/Numerics/Distributions/Geometric.cs b/src/Numerics/Distributions/Geometric.cs index 6a3bfed9..4e5e7270 100644 --- a/src/Numerics/Distributions/Geometric.cs +++ b/src/Numerics/Distributions/Geometric.cs @@ -85,7 +85,7 @@ namespace MathNet.Numerics.Distributions /// A that represents this instance. public override string ToString() { - return "Geometric(p = " + _p + ")"; + return $"Geometric(p = {_p})"; } /// diff --git a/src/Numerics/Distributions/Hypergeometric.cs b/src/Numerics/Distributions/Hypergeometric.cs index 225a2681..b48125e0 100644 --- a/src/Numerics/Distributions/Hypergeometric.cs +++ b/src/Numerics/Distributions/Hypergeometric.cs @@ -96,7 +96,7 @@ namespace MathNet.Numerics.Distributions /// public override string ToString() { - return "Hypergeometric(N = " + _population + ", M = " + _success + ", n = " + _draws + ")"; + return $"Hypergeometric(N = {_population}, M = {_success}, n = {_draws})"; } /// diff --git a/src/Numerics/Distributions/InverseGamma.cs b/src/Numerics/Distributions/InverseGamma.cs index 90dbf58e..b47441cf 100644 --- a/src/Numerics/Distributions/InverseGamma.cs +++ b/src/Numerics/Distributions/InverseGamma.cs @@ -90,7 +90,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "InverseGamma(α = " + _shape + ", β = " + _scale + ")"; + return $"InverseGamma(α = {_shape}, β = {_scale})"; } /// diff --git a/src/Numerics/Distributions/InverseGaussian.cs b/src/Numerics/Distributions/InverseGaussian.cs index 9eb4541a..9b8d7f61 100644 --- a/src/Numerics/Distributions/InverseGaussian.cs +++ b/src/Numerics/Distributions/InverseGaussian.cs @@ -38,7 +38,7 @@ namespace MathNet.Numerics.Distributions { public class InverseGaussian : IContinuousDistribution { - private System.Random _random; + System.Random _random; /// /// Gets the mean (μ) of the distribution. Range: μ > 0. @@ -73,7 +73,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "InverseGaussian(μ = " + Mu + ", λ = " + Lambda + ")"; + return $"InverseGaussian(μ = {Mu}, λ = {Lambda})"; } /// diff --git a/src/Numerics/Distributions/InverseWishart.cs b/src/Numerics/Distributions/InverseWishart.cs index e0089332..07600ec7 100644 --- a/src/Numerics/Distributions/InverseWishart.cs +++ b/src/Numerics/Distributions/InverseWishart.cs @@ -96,7 +96,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "InverseWishart(ν = " + _freedom + ", Rows = " + _scale.RowCount + ", Columns = " + _scale.ColumnCount + ")"; + return $"InverseWishart(ν = {_freedom}, Rows = {_scale.RowCount}, Columns = {_scale.ColumnCount})"; } /// diff --git a/src/Numerics/Distributions/Laplace.cs b/src/Numerics/Distributions/Laplace.cs index 203777bf..b41e0089 100644 --- a/src/Numerics/Distributions/Laplace.cs +++ b/src/Numerics/Distributions/Laplace.cs @@ -100,7 +100,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "Laplace(μ = " + _location + ", b = " + _scale + ")"; + return $"Laplace(μ = {_location}, b = {_scale})"; } /// diff --git a/src/Numerics/Distributions/LogNormal.cs b/src/Numerics/Distributions/LogNormal.cs index b4916538..8471fee3 100644 --- a/src/Numerics/Distributions/LogNormal.cs +++ b/src/Numerics/Distributions/LogNormal.cs @@ -132,7 +132,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "LogNormal(μ = " + _mu + ", σ = " + _sigma + ")"; + return $"LogNormal(μ = {_mu}, σ = {_sigma})"; } /// diff --git a/src/Numerics/Distributions/MatrixNormal.cs b/src/Numerics/Distributions/MatrixNormal.cs index 5ffc2b0e..048b698a 100644 --- a/src/Numerics/Distributions/MatrixNormal.cs +++ b/src/Numerics/Distributions/MatrixNormal.cs @@ -109,7 +109,7 @@ namespace MathNet.Numerics.Distributions /// public override string ToString() { - return "MatrixNormal(Rows = " + _m.RowCount + ", Columns = " + _m.ColumnCount + ")"; + return $"MatrixNormal(Rows = {_m.RowCount}, Columns = {_m.ColumnCount})"; } /// diff --git a/src/Numerics/Distributions/Multinomial.cs b/src/Numerics/Distributions/Multinomial.cs index 3879e8ff..49cbbe02 100644 --- a/src/Numerics/Distributions/Multinomial.cs +++ b/src/Numerics/Distributions/Multinomial.cs @@ -142,7 +142,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "Multinomial(Dimension = " + _p.Length + ", Number of Trails = " + _trials + ")"; + return $"Multinomial(Dimension = {_p.Length}, Number of Trails = {_trials})"; } /// diff --git a/src/Numerics/Distributions/NegativeBinomial.cs b/src/Numerics/Distributions/NegativeBinomial.cs index d2b48eee..378d235d 100644 --- a/src/Numerics/Distributions/NegativeBinomial.cs +++ b/src/Numerics/Distributions/NegativeBinomial.cs @@ -91,7 +91,7 @@ namespace MathNet.Numerics.Distributions /// public override string ToString() { - return "NegativeBinomial(R = " + _r + ", P = " + _p + ")"; + return $"NegativeBinomial(R = {_r}, P = {_p})"; } /// diff --git a/src/Numerics/Distributions/Normal.cs b/src/Numerics/Distributions/Normal.cs index 1a465827..bbd14304 100644 --- a/src/Numerics/Distributions/Normal.cs +++ b/src/Numerics/Distributions/Normal.cs @@ -160,7 +160,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "Normal(μ = " + _mean + ", σ = " + _stdDev + ")"; + return $"Normal(μ = {_mean}, σ = {_stdDev})"; } /// diff --git a/src/Numerics/Distributions/NormalGamma.cs b/src/Numerics/Distributions/NormalGamma.cs index f345faee..8045c844 100644 --- a/src/Numerics/Distributions/NormalGamma.cs +++ b/src/Numerics/Distributions/NormalGamma.cs @@ -133,8 +133,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "NormalGamma(Mean Location = " + _meanLocation + ", Mean Scale = " + _meanScale + - ", Precision Shape = " + _precisionShape + ", Precision Inverse Scale = " + _precisionInvScale + ")"; + return $"NormalGamma(Mean Location = {_meanLocation}, Mean Scale = {_meanScale}, Precision Shape = {_precisionShape}, Precision Inverse Scale = {_precisionInvScale})"; } /// diff --git a/src/Numerics/Distributions/Pareto.cs b/src/Numerics/Distributions/Pareto.cs index 35093854..13c1c04f 100644 --- a/src/Numerics/Distributions/Pareto.cs +++ b/src/Numerics/Distributions/Pareto.cs @@ -93,7 +93,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "Pareto(xm = " + _scale + ", α = " + _shape + ")"; + return $"Pareto(xm = {_scale}, α = {_shape})"; } /// diff --git a/src/Numerics/Distributions/Poisson.cs b/src/Numerics/Distributions/Poisson.cs index 94dafbef..d030aa96 100644 --- a/src/Numerics/Distributions/Poisson.cs +++ b/src/Numerics/Distributions/Poisson.cs @@ -89,7 +89,7 @@ namespace MathNet.Numerics.Distributions /// public override string ToString() { - return "Poisson(λ = " + _lambda + ")"; + return $"Poisson(λ = {_lambda})"; } /// diff --git a/src/Numerics/Distributions/Rayleigh.cs b/src/Numerics/Distributions/Rayleigh.cs index 3c4e0bd8..28e0f5f9 100644 --- a/src/Numerics/Distributions/Rayleigh.cs +++ b/src/Numerics/Distributions/Rayleigh.cs @@ -89,7 +89,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "Rayleigh(σ = " + _scale + ")"; + return $"Rayleigh(σ = {_scale})"; } /// diff --git a/src/Numerics/Distributions/SkewedGeneralizedError.cs b/src/Numerics/Distributions/SkewedGeneralizedError.cs index baeb5167..33579d86 100644 --- a/src/Numerics/Distributions/SkewedGeneralizedError.cs +++ b/src/Numerics/Distributions/SkewedGeneralizedError.cs @@ -47,7 +47,7 @@ namespace MathNet.Numerics.Distributions /// https://cran.r-project.org/web/packages/sgt/vignettes/sgt.pdf. Compared to that /// implementation, the options for mean adjustment and variance adjustment are always true. /// The location (μ) is the mean of the distribution. - /// The scale (σ) squared is the variance of the distribution. + /// The scale (σ) squared is the variance of the distribution. /// /// The distribution will use the by /// default. Users can get/set the random number generator by using the @@ -56,9 +56,9 @@ namespace MathNet.Numerics.Distributions /// whether they are in the allowed range. public class SkewedGeneralizedError : IContinuousDistribution { - private System.Random _random; + System.Random _random; - private readonly double _skewness; + readonly double _skewness; /// /// Initializes a new instance of the SkewedGeneralizedError class. This is a generalized error distribution @@ -172,10 +172,12 @@ namespace MathNet.Numerics.Distributions public double Median => Skew == 0 ? Mean : InverseCumulativeDistribution(0.5); - private double CalculateSkewness() + double CalculateSkewness() { if (Skew == 0) + { return 0.0; + } var piPow = Math.Pow(Constants.Pi, 3.0 / 2.0); var g1 = SpecialFunctions.Gamma(1.0 / P); @@ -191,7 +193,7 @@ namespace MathNet.Numerics.Distributions return t1 * (t2 - t3 + t4); } - private static double AdjustScale(double scale, double skew, double p) + static double AdjustScale(double scale, double skew, double p) { var g1 = SpecialFunctions.Gamma(3.0 / p); var g2 = SpecialFunctions.Gamma(0.5 + 1.0 / p); @@ -203,12 +205,12 @@ namespace MathNet.Numerics.Distributions return scale / Math.Sqrt((n1 - n2) / d); } - private static double AdjustX(double x, double scale, double skew, double p) + static double AdjustX(double x, double scale, double skew, double p) { return x + AdjustAddend(scale, skew, p); } - private static double AdjustAddend(double scale, double skew, double p) + static double AdjustAddend(double scale, double skew, double p) { return (Math.Pow(2.0, 2.0 / p) * scale * skew * SpecialFunctions.Gamma(1.0 / 2.0 + 1.0 / p)) / Math.Sqrt(Constants.Pi); @@ -223,7 +225,7 @@ namespace MathNet.Numerics.Distributions scale = AdjustScale(scale, skew, p); x = AdjustX(x, scale, skew, p); - + // p / (2 * sigma * gamma(1 / p) * exp((abs(x - mu) / (sigma * (1 + lambda * sgn(x - mu)))) ^ p)) var d1 = Math.Abs(x - location); var d2 = scale * (1.0 + skew * Math.Sign(x - location)); @@ -372,7 +374,7 @@ namespace MathNet.Numerics.Distributions return SampleUnchecked(SystemRandomSource.Default, location, scale, skew, p); } - private static double SampleUnchecked(System.Random rnd, double location, double scale, double skew, double p) + static double SampleUnchecked(System.Random rnd, double location, double scale, double skew, double p) { var u = ContinuousUniform.Sample(rnd, 0, 1); return InvCDF(location, scale, skew, p, u); diff --git a/src/Numerics/Distributions/SkewedGeneralizedT.cs b/src/Numerics/Distributions/SkewedGeneralizedT.cs index 4fb35041..72724b4b 100644 --- a/src/Numerics/Distributions/SkewedGeneralizedT.cs +++ b/src/Numerics/Distributions/SkewedGeneralizedT.cs @@ -48,7 +48,7 @@ namespace MathNet.Numerics.Distributions /// https://cran.r-project.org/web/packages/sgt/vignettes/sgt.pdf. Compared to that /// implementation, the options for mean adjustment and variance adjustment are always true. /// The location (μ) is the mean of the distribution. - /// The scale (σ) squared is the variance of the distribution. + /// The scale (σ) squared is the variance of the distribution. /// /// The distribution will use the by /// default. Users can get/set the random number generator by using the @@ -57,14 +57,14 @@ namespace MathNet.Numerics.Distributions /// whether they are in the allowed range. public class SkewedGeneralizedT : IContinuousDistribution { - private System.Random _random; + System.Random _random; // If the given parameterization is one of the recognized special cases, then // this variable is non-null and the special case is used for all functions. // Else this value is null and the full formulation of the generalized distribution is used. - private IContinuousDistribution _d; + IContinuousDistribution _d; - private readonly double _skewness; + readonly double _skewness; /// /// Initializes a new instance of the SkewedGeneralizedT class. This is a skewed generalized t-distribution @@ -108,7 +108,9 @@ namespace MathNet.Numerics.Distributions _d = FindSpecializedDistribution(location, scale, skew, p, q); if (_d == null) + { _skewness = CalculateSkewness(); + } } /// @@ -139,8 +141,8 @@ namespace MathNet.Numerics.Distributions /// public System.Random RandomSource { - get { return _random; } - set { _random = value ?? SystemRandomSource.Default; } + get => _random; + set => _random = value ?? SystemRandomSource.Default; } /// @@ -149,7 +151,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return $"SkewedGeneralizedT(μ = {Location}, σ = {Scale}, λ = { Skew }, p = {P}, q = {Q})"; + return $"SkewedGeneralizedT(μ = {Location}, σ = {Scale}, λ = {Skew}, p = {P}, q = {Q})"; } /// @@ -168,61 +170,57 @@ namespace MathNet.Numerics.Distributions /// /// Gets the location (μ) of the Skewed Generalized t-distribution. /// - public double Location { get; private set; } + public double Location { get; } /// /// Gets the scale (σ) of the Skewed Generalized t-distribution. Range: σ > 0. /// - public double Scale { get; private set; } + public double Scale { get; } /// /// Gets the skew (λ) of the Skewed Generalized t-distribution. Range: 1 > λ > -1. /// - public double Skew { get; private set; } + public double Skew { get; } /// /// Gets the first parameter that controls the kurtosis of the distribution. Range: p > 0. /// - public double P { get; private set; } + public double P { get; } /// /// Gets the second parameter that controls the kurtosis of the distribution. Range: q > 0. /// - public double Q { get; private set; } + public double Q { get; } // No skew implies Median=Mode=Mean - public double Mode => _d == null ? - Skew == 0 ? Mean : Mean - AdjustAddend(AdjustScale(Scale, Skew, P, Q), Skew, P, Q) : - _d.Mode; + public double Mode => _d?.Mode ?? (Skew == 0 ? Mean : Mean - AdjustAddend(AdjustScale(Scale, Skew, P, Q), Skew, P, Q)); - public double Minimum => _d == null ? double.NegativeInfinity : _d.Minimum; + public double Minimum => _d?.Minimum ?? double.NegativeInfinity; - public double Maximum => _d == null ? double.PositiveInfinity : _d.Maximum; + public double Maximum => _d?.Maximum ?? double.PositiveInfinity; // Mean=Location due to our adjustments made - public double Mean => _d == null ? Location : _d.Mean; + public double Mean => _d?.Mean ?? Location; // Variance=Scale*Scale due to our adjustments made - public double Variance => _d == null ? Scale * Scale : _d.Variance; + public double Variance => _d?.Variance ?? Scale * Scale; - public double StdDev => _d == null ? Scale : _d.StdDev; + public double StdDev => _d?.StdDev ?? Scale; - public double Entropy => _d == null ? throw new NotImplementedException() : _d.Entropy; + public double Entropy => _d?.Entropy ?? throw new NotImplementedException(); - public double Skewness => _d == null ? - _skewness : - _d.Skewness; + public double Skewness => _d?.Skewness ?? _skewness; // No skew implies Median=Mode=Mean // Else find it via the point where CDF gives 0.5 - public double Median => _d == null ? - Skew == 0 ? Mean : InverseCumulativeDistribution(0.5) : - _d.Median; + public double Median => _d?.Median ?? (Skew == 0 ? Mean : InverseCumulativeDistribution(0.5)); - private double CalculateSkewness() + double CalculateSkewness() { if (P * Q <= 3 || Skew == 0) + { return 0.0; + } var scale = AdjustScale(Scale, Skew, P, Q); var b1 = SpecialFunctions.Beta(1.0 / P, Q); @@ -239,7 +237,7 @@ namespace MathNet.Numerics.Distributions return t1 * (t2 - t3 * t4 + t5); } - private static double AdjustScale(double scale, double skew, double p, double q) + static double AdjustScale(double scale, double skew, double p, double q) { var b1 = SpecialFunctions.Beta(3.0 / p, q - 2.0 / p); var b2 = SpecialFunctions.Beta(1.0 / p, q); @@ -250,13 +248,13 @@ namespace MathNet.Numerics.Distributions } // Note: Scale is assumed to be adjusted already when calling this function. - private static double AdjustX(double x, double scale, double skew, double p, double q) + static double AdjustX(double x, double scale, double skew, double p, double q) { return x + AdjustAddend(scale, skew, p, q); } // Note: Scale is assumed to be adjusted already when calling this function. - private static double AdjustAddend(double scale, double skew, double p, double q) + static double AdjustAddend(double scale, double skew, double p, double q) { var b1 = SpecialFunctions.Beta(2.0 / p, q - 1.0 / p); var b2 = SpecialFunctions.Beta(1.0 / p, q); @@ -308,7 +306,7 @@ namespace MathNet.Numerics.Distributions return fn(x); } - private static double PDFull(double location, double scale, double skew, double p, double q, double x) + static double PDFull(double location, double scale, double skew, double p, double q, double x) { scale = AdjustScale(scale, skew, p, q); x = AdjustX(x, scale, skew, p, q); @@ -322,7 +320,7 @@ namespace MathNet.Numerics.Distributions return p / denominator; } - private static double PDFullLn(double location, double scale, double skew, double p, double q, double x) + static double PDFullLn(double location, double scale, double skew, double p, double q, double x) { scale = AdjustScale(scale, skew, p, q); x = AdjustX(x, scale, skew, p, q); @@ -337,7 +335,7 @@ namespace MathNet.Numerics.Distributions // by Hansen, McDonald and Newey (2010). // Note that, for all cases where skew is required to be 0, if skew is non-zero, this // simply gives the corresponding skewed version of the distribution. - private static Func PDFunc(double location, double scale, double skew, double p, double q, bool ln) + static Func PDFunc(double location, double scale, double skew, double p, double q, bool ln) { if (p == double.PositiveInfinity) { @@ -414,10 +412,13 @@ namespace MathNet.Numerics.Distributions // InverseCumulativeDistribution is not a part of the interface, so resort to type-checking. if (d != null) { - if (d is SkewedGeneralizedError sge) - return sge.InverseCumulativeDistribution(pr); - if (d is ContinuousUniform u) - return u.InverseCumulativeDistribution(pr); + switch (d) + { + case SkewedGeneralizedError sge: + return sge.InverseCumulativeDistribution(pr); + case ContinuousUniform u: + return u.InverseCumulativeDistribution(pr); + } } // Note: Adapted from the R package, @@ -444,7 +445,7 @@ namespace MathNet.Numerics.Distributions public double CumulativeDistribution(double x) { - return _d == null ? CDF(Location, Scale, Skew, P, Q, x) : _d.CumulativeDistribution(x); + return _d?.CumulativeDistribution(x) ?? CDF(Location, Scale, Skew, P, Q, x); } /// @@ -459,10 +460,13 @@ namespace MathNet.Numerics.Distributions // InverseCumulativeDistribution is not a part of the interface, so resort to type-checking. if (_d != null) { - if (_d is SkewedGeneralizedError sge) - return sge.InverseCumulativeDistribution(p); - if (_d is ContinuousUniform u) - return u.InverseCumulativeDistribution(p); + switch (_d) + { + case SkewedGeneralizedError sge: + return sge.InverseCumulativeDistribution(p); + case ContinuousUniform u: + return u.InverseCumulativeDistribution(p); + } } return InvCDF(Location, Scale, Skew, P, Q, p); @@ -470,12 +474,12 @@ namespace MathNet.Numerics.Distributions public double Density(double x) { - return _d == null ? PDF(Location, Scale, Skew, P, Q, x) : _d.Density(x); + return _d?.Density(x) ?? PDF(Location, Scale, Skew, P, Q, x); } public double DensityLn(double x) { - return _d == null ? PDFLn(Location, Scale, Skew, P, Q, x) : _d.DensityLn(x); + return _d?.DensityLn(x) ?? PDFLn(Location, Scale, Skew, P, Q, x); } /// @@ -541,7 +545,7 @@ namespace MathNet.Numerics.Distributions return SampleUnchecked(SystemRandomSource.Default, location, scale, skew, p, q); } - private static double SampleUnchecked(System.Random rnd, double location, double scale, double skew, double p, double q) + static double SampleUnchecked(System.Random rnd, double location, double scale, double skew, double p, double q) { var u = ContinuousUniform.Sample(rnd, 0, 1); return InvCDF(location, scale, skew, p, q, u); diff --git a/src/Numerics/Distributions/Stable.cs b/src/Numerics/Distributions/Stable.cs index 13299e6b..ae77fcb0 100644 --- a/src/Numerics/Distributions/Stable.cs +++ b/src/Numerics/Distributions/Stable.cs @@ -100,7 +100,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "Stable(α = " + _alpha + ", β = " + _beta + ", c = " + _scale + ", μ = " + _location + ")"; + return $"Stable(α = {_alpha}, β = {_beta}, c = {_scale}, μ = {_location})"; } /// diff --git a/src/Numerics/Distributions/StudentT.cs b/src/Numerics/Distributions/StudentT.cs index b51b9650..e1e1f16d 100644 --- a/src/Numerics/Distributions/StudentT.cs +++ b/src/Numerics/Distributions/StudentT.cs @@ -123,7 +123,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "StudentT(μ = " + _location + ", σ = " + _scale + ", ν = " + _freedom + ")"; + return $"StudentT(μ = {_location}, σ = {_scale}, ν = {_freedom})"; } /// diff --git a/src/Numerics/Distributions/Triangular.cs b/src/Numerics/Distributions/Triangular.cs index 48be7c5c..cfb04fed 100644 --- a/src/Numerics/Distributions/Triangular.cs +++ b/src/Numerics/Distributions/Triangular.cs @@ -99,7 +99,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "Triangular(Lower = " + _lower + ", Upper = " + _upper + ", Mode = " + _mode + ")"; + return $"Triangular(Lower = {_lower}, Upper = {_upper}, Mode = {_mode})"; } /// diff --git a/src/Numerics/Distributions/TruncatedPareto.cs b/src/Numerics/Distributions/TruncatedPareto.cs index 1edcd190..155266c8 100644 --- a/src/Numerics/Distributions/TruncatedPareto.cs +++ b/src/Numerics/Distributions/TruncatedPareto.cs @@ -36,7 +36,7 @@ namespace MathNet.Numerics.Distributions { public class TruncatedPareto : IContinuousDistribution { - private System.Random _random; + System.Random _random; /// /// Initializes a new instance of the TruncatedPareto class. @@ -64,7 +64,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "Truncated Pareto(Scale = " + Scale + ", Shape = " + Shape + ", Truncation = " + Truncation + ")"; + return $"Truncated Pareto(Scale = {Scale}, Shape = {Shape}, Truncation = {Truncation})"; } /// diff --git a/src/Numerics/Distributions/Weibull.cs b/src/Numerics/Distributions/Weibull.cs index 9c377be0..c54c74a1 100644 --- a/src/Numerics/Distributions/Weibull.cs +++ b/src/Numerics/Distributions/Weibull.cs @@ -103,7 +103,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "Weibull(k = " + _shape + ", λ = " + _scale + ")"; + return $"Weibull(k = {_shape}, λ = {_scale})"; } /// diff --git a/src/Numerics/Distributions/Wishart.cs b/src/Numerics/Distributions/Wishart.cs index f2d0a5ba..1a446095 100644 --- a/src/Numerics/Distributions/Wishart.cs +++ b/src/Numerics/Distributions/Wishart.cs @@ -143,7 +143,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "Wishart(DegreesOfFreedom = " + _degreesOfFreedom + ", Rows = " + _scale.RowCount + ", Columns = " + _scale.ColumnCount + ")"; + return $"Wishart(DegreesOfFreedom = {_degreesOfFreedom}, Rows = {_scale.RowCount}, Columns = {_scale.ColumnCount})"; } /// diff --git a/src/Numerics/Distributions/Zipf.cs b/src/Numerics/Distributions/Zipf.cs index 5f59d9aa..d925c85c 100644 --- a/src/Numerics/Distributions/Zipf.cs +++ b/src/Numerics/Distributions/Zipf.cs @@ -97,7 +97,7 @@ namespace MathNet.Numerics.Distributions /// a string representation of the distribution. public override string ToString() { - return "Zipf(S = " + _s + ", N = " + _n + ")"; + return $"Zipf(S = {_s}, N = {_n})"; } ///