Browse Source

Distributions: minor code maintenance

v4
Christoph Ruegg 6 years ago
parent
commit
da99060a8e
  1. 2
      src/Numerics/Distributions/Bernoulli.cs
  2. 2
      src/Numerics/Distributions/Beta.cs
  3. 2
      src/Numerics/Distributions/BetaScaled.cs
  4. 2
      src/Numerics/Distributions/Binomial.cs
  5. 4
      src/Numerics/Distributions/Burr.cs
  6. 2
      src/Numerics/Distributions/Categorical.cs
  7. 2
      src/Numerics/Distributions/Cauchy.cs
  8. 2
      src/Numerics/Distributions/Chi.cs
  9. 2
      src/Numerics/Distributions/ChiSquared.cs
  10. 2
      src/Numerics/Distributions/ContinuousUniform.cs
  11. 2
      src/Numerics/Distributions/ConwayMaxwellPoisson.cs
  12. 2
      src/Numerics/Distributions/Dirichlet.cs
  13. 2
      src/Numerics/Distributions/DiscreteUniform.cs
  14. 2
      src/Numerics/Distributions/Erlang.cs
  15. 2
      src/Numerics/Distributions/Exponential.cs
  16. 2
      src/Numerics/Distributions/FisherSnedecor.cs
  17. 2
      src/Numerics/Distributions/Gamma.cs
  18. 2
      src/Numerics/Distributions/Geometric.cs
  19. 2
      src/Numerics/Distributions/Hypergeometric.cs
  20. 2
      src/Numerics/Distributions/InverseGamma.cs
  21. 4
      src/Numerics/Distributions/InverseGaussian.cs
  22. 2
      src/Numerics/Distributions/InverseWishart.cs
  23. 2
      src/Numerics/Distributions/Laplace.cs
  24. 2
      src/Numerics/Distributions/LogNormal.cs
  25. 2
      src/Numerics/Distributions/MatrixNormal.cs
  26. 2
      src/Numerics/Distributions/Multinomial.cs
  27. 2
      src/Numerics/Distributions/NegativeBinomial.cs
  28. 2
      src/Numerics/Distributions/Normal.cs
  29. 3
      src/Numerics/Distributions/NormalGamma.cs
  30. 2
      src/Numerics/Distributions/Pareto.cs
  31. 2
      src/Numerics/Distributions/Poisson.cs
  32. 2
      src/Numerics/Distributions/Rayleigh.cs
  33. 20
      src/Numerics/Distributions/SkewedGeneralizedError.cs
  34. 96
      src/Numerics/Distributions/SkewedGeneralizedT.cs
  35. 2
      src/Numerics/Distributions/Stable.cs
  36. 2
      src/Numerics/Distributions/StudentT.cs
  37. 2
      src/Numerics/Distributions/Triangular.cs
  38. 4
      src/Numerics/Distributions/TruncatedPareto.cs
  39. 2
      src/Numerics/Distributions/Weibull.cs
  40. 2
      src/Numerics/Distributions/Wishart.cs
  41. 2
      src/Numerics/Distributions/Zipf.cs

2
src/Numerics/Distributions/Bernoulli.cs

@ -87,7 +87,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Bernoulli(p = " + _p + ")";
return $"Bernoulli(p = {_p})";
}
/// <summary>

2
src/Numerics/Distributions/Beta.cs

@ -97,7 +97,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>A string representation of the Beta distribution.</returns>
public override string ToString()
{
return "Beta(α = " + _shapeA + ", β = " + _shapeB + ")";
return $"Beta(α = {_shapeA}, β = {_shapeB})";
}
/// <summary>

2
src/Numerics/Distributions/BetaScaled.cs

@ -136,7 +136,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>A string representation of the BetaScaled distribution.</returns>
public override string ToString()
{
return "BetaScaled(α = " + _shapeA + ", β = " + _shapeB + ", μ = " + _location + ", σ = " + _scale + ")";
return $"BetaScaled(α = {_shapeA}, β = {_shapeB}, μ = {_location}, σ = {_scale})";
}
/// <summary>

2
src/Numerics/Distributions/Binomial.cs

@ -95,7 +95,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Binomial(p = " + _p + ", n = " + _trials + ")";
return $"Binomial(p = {_p}, n = {_trials})";
}
/// <summary>

4
src/Numerics/Distributions/Burr.cs

@ -36,7 +36,7 @@ namespace MathNet.Numerics.Distributions
{
public class Burr : IContinuousDistribution
{
private System.Random _random;
System.Random _random;
/// <summary>
/// Gets the scale (a) of the distribution. Range: a > 0.
@ -78,7 +78,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Burr(a = " + a + ", c = " + c + ", k = " + k + ")";
return $"Burr(a = {a}, c = {c}, k = {k})";
}
/// <summary>

2
src/Numerics/Distributions/Categorical.cs

@ -154,7 +154,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Categorical(Dimension = " + _pmfNormalized.Length + ")";
return $"Categorical(Dimension = {_pmfNormalized.Length})";
}
/// <summary>

2
src/Numerics/Distributions/Cauchy.cs

@ -95,7 +95,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Cauchy(x0 = " + _location + ", γ = " + _scale + ")";
return $"Cauchy(x0 = {_location}, γ = {_scale})";
}
/// <summary>

2
src/Numerics/Distributions/Chi.cs

@ -85,7 +85,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Chi(k = " + _freedom + ")";
return $"Chi(k = {_freedom})";
}
/// <summary>

2
src/Numerics/Distributions/ChiSquared.cs

@ -83,7 +83,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "ChiSquared(k = " + _freedom + ")";
return $"ChiSquared(k = {_freedom})";
}
/// <summary>

2
src/Numerics/Distributions/ContinuousUniform.cs

@ -97,7 +97,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "ContinuousUniform(Lower = " + _lower + ", Upper = " + _upper + ")";
return $"ContinuousUniform(Lower = {_lower}, Upper = {_upper})";
}
/// <summary>

2
src/Numerics/Distributions/ConwayMaxwellPoisson.cs

@ -116,7 +116,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>A <see cref="System.String"/> that represents this instance.</returns>
public override string ToString()
{
return "ConwayMaxwellPoisson(λ = " + _lambda + ", ν = " + _nu + ")";
return $"ConwayMaxwellPoisson(λ = {_lambda}, ν = {_nu})";
}
/// <summary>

2
src/Numerics/Distributions/Dirichlet.cs

@ -132,7 +132,7 @@ namespace MathNet.Numerics.Distributions
/// </returns>
public override string ToString()
{
return "Dirichlet(Dimension = " + Dimension + ")";
return $"Dirichlet(Dimension = {Dimension})";
}
/// <summary>

2
src/Numerics/Distributions/DiscreteUniform.cs

@ -90,7 +90,7 @@ namespace MathNet.Numerics.Distributions
/// </returns>
public override string ToString()
{
return "DiscreteUniform(Lower = " + _lower + ", Upper = " + _upper + ")";
return $"DiscreteUniform(Lower = {_lower}, Upper = {_upper})";
}
/// <summary>

2
src/Numerics/Distributions/Erlang.cs

@ -112,7 +112,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Erlang(k = " + _shape + ", λ = " + _rate + ")";
return $"Erlang(k = {_shape}, λ = {_rate})";
}
/// <summary>

2
src/Numerics/Distributions/Exponential.cs

@ -84,7 +84,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Exponential(λ = " + _rate + ")";
return $"Exponential(λ = {_rate})";
}
/// <summary>

2
src/Numerics/Distributions/FisherSnedecor.cs

@ -89,7 +89,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "FisherSnedecor(d1 = " + _freedom1 + ", d2 = " + _freedom2 + ")";
return $"FisherSnedecor(d1 = {_freedom1}, d2 = {_freedom2})";
}
/// <summary>

2
src/Numerics/Distributions/Gamma.cs

@ -121,7 +121,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Gamma(α = " + _shape + ", β = " + _rate + ")";
return $"Gamma(α = {_shape}, β = {_rate})";
}
/// <summary>

2
src/Numerics/Distributions/Geometric.cs

@ -85,7 +85,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>A <see cref="System.String"/> that represents this instance.</returns>
public override string ToString()
{
return "Geometric(p = " + _p + ")";
return $"Geometric(p = {_p})";
}
/// <summary>

2
src/Numerics/Distributions/Hypergeometric.cs

@ -96,7 +96,7 @@ namespace MathNet.Numerics.Distributions
/// </returns>
public override string ToString()
{
return "Hypergeometric(N = " + _population + ", M = " + _success + ", n = " + _draws + ")";
return $"Hypergeometric(N = {_population}, M = {_success}, n = {_draws})";
}
/// <summary>

2
src/Numerics/Distributions/InverseGamma.cs

@ -90,7 +90,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "InverseGamma(α = " + _shape + ", β = " + _scale + ")";
return $"InverseGamma(α = {_shape}, β = {_scale})";
}
/// <summary>

4
src/Numerics/Distributions/InverseGaussian.cs

@ -38,7 +38,7 @@ namespace MathNet.Numerics.Distributions
{
public class InverseGaussian : IContinuousDistribution
{
private System.Random _random;
System.Random _random;
/// <summary>
/// Gets the mean (μ) of the distribution. Range: μ > 0.
@ -73,7 +73,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "InverseGaussian(μ = " + Mu + ", λ = " + Lambda + ")";
return $"InverseGaussian(μ = {Mu}, λ = {Lambda})";
}
/// <summary>

2
src/Numerics/Distributions/InverseWishart.cs

@ -96,7 +96,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "InverseWishart(ν = " + _freedom + ", Rows = " + _scale.RowCount + ", Columns = " + _scale.ColumnCount + ")";
return $"InverseWishart(ν = {_freedom}, Rows = {_scale.RowCount}, Columns = {_scale.ColumnCount})";
}
/// <summary>

2
src/Numerics/Distributions/Laplace.cs

@ -100,7 +100,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Laplace(μ = " + _location + ", b = " + _scale + ")";
return $"Laplace(μ = {_location}, b = {_scale})";
}
/// <summary>

2
src/Numerics/Distributions/LogNormal.cs

@ -132,7 +132,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "LogNormal(μ = " + _mu + ", σ = " + _sigma + ")";
return $"LogNormal(μ = {_mu}, σ = {_sigma})";
}
/// <summary>

2
src/Numerics/Distributions/MatrixNormal.cs

@ -109,7 +109,7 @@ namespace MathNet.Numerics.Distributions
/// </returns>
public override string ToString()
{
return "MatrixNormal(Rows = " + _m.RowCount + ", Columns = " + _m.ColumnCount + ")";
return $"MatrixNormal(Rows = {_m.RowCount}, Columns = {_m.ColumnCount})";
}
/// <summary>

2
src/Numerics/Distributions/Multinomial.cs

@ -142,7 +142,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Multinomial(Dimension = " + _p.Length + ", Number of Trails = " + _trials + ")";
return $"Multinomial(Dimension = {_p.Length}, Number of Trails = {_trials})";
}
/// <summary>

2
src/Numerics/Distributions/NegativeBinomial.cs

@ -91,7 +91,7 @@ namespace MathNet.Numerics.Distributions
/// </returns>
public override string ToString()
{
return "NegativeBinomial(R = " + _r + ", P = " + _p + ")";
return $"NegativeBinomial(R = {_r}, P = {_p})";
}
/// <summary>

2
src/Numerics/Distributions/Normal.cs

@ -160,7 +160,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Normal(μ = " + _mean + ", σ = " + _stdDev + ")";
return $"Normal(μ = {_mean}, σ = {_stdDev})";
}
/// <summary>

3
src/Numerics/Distributions/NormalGamma.cs

@ -133,8 +133,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
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})";
}
/// <summary>

2
src/Numerics/Distributions/Pareto.cs

@ -93,7 +93,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Pareto(xm = " + _scale + ", α = " + _shape + ")";
return $"Pareto(xm = {_scale}, α = {_shape})";
}
/// <summary>

2
src/Numerics/Distributions/Poisson.cs

@ -89,7 +89,7 @@ namespace MathNet.Numerics.Distributions
/// </returns>
public override string ToString()
{
return "Poisson(λ = " + _lambda + ")";
return $"Poisson(λ = {_lambda})";
}
/// <summary>

2
src/Numerics/Distributions/Rayleigh.cs

@ -89,7 +89,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Rayleigh(σ = " + _scale + ")";
return $"Rayleigh(σ = {_scale})";
}
/// <summary>

20
src/Numerics/Distributions/SkewedGeneralizedError.cs

@ -47,7 +47,7 @@ namespace MathNet.Numerics.Distributions
/// <a href="">https://cran.r-project.org/web/packages/sgt/vignettes/sgt.pdf</a>. 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.
/// </para>
/// <para>The distribution will use the <see cref="System.Random"/> 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.</para></remarks>
public class SkewedGeneralizedError : IContinuousDistribution
{
private System.Random _random;
System.Random _random;
private readonly double _skewness;
readonly double _skewness;
/// <summary>
/// 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);

96
src/Numerics/Distributions/SkewedGeneralizedT.cs

@ -48,7 +48,7 @@ namespace MathNet.Numerics.Distributions
/// <a href="">https://cran.r-project.org/web/packages/sgt/vignettes/sgt.pdf</a>. 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.
/// </para>
/// <para>The distribution will use the <see cref="System.Random"/> 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.</para></remarks>
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;
/// <summary>
/// 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();
}
}
/// <summary>
@ -139,8 +141,8 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? SystemRandomSource.Default; }
get => _random;
set => _random = value ?? SystemRandomSource.Default;
}
/// <summary>
@ -149,7 +151,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return $"SkewedGeneralizedT(μ = {Location}, σ = {Scale}, λ = { Skew }, p = {P}, q = {Q})";
return $"SkewedGeneralizedT(μ = {Location}, σ = {Scale}, λ = {Skew}, p = {P}, q = {Q})";
}
/// <summary>
@ -168,61 +170,57 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Gets the location (μ) of the Skewed Generalized t-distribution.
/// </summary>
public double Location { get; private set; }
public double Location { get; }
/// <summary>
/// Gets the scale (σ) of the Skewed Generalized t-distribution. Range: σ > 0.
/// </summary>
public double Scale { get; private set; }
public double Scale { get; }
/// <summary>
/// Gets the skew (λ) of the Skewed Generalized t-distribution. Range: 1 > λ > -1.
/// </summary>
public double Skew { get; private set; }
public double Skew { get; }
/// <summary>
/// Gets the first parameter that controls the kurtosis of the distribution. Range: p > 0.
/// </summary>
public double P { get; private set; }
public double P { get; }
/// <summary>
/// Gets the second parameter that controls the kurtosis of the distribution. Range: q > 0.
/// </summary>
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<double, double> PDFunc(double location, double scale, double skew, double p, double q, bool ln)
static Func<double, double> 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);
}
/// <summary>
@ -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);
}
/// <summary>
@ -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);

2
src/Numerics/Distributions/Stable.cs

@ -100,7 +100,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Stable(α = " + _alpha + ", β = " + _beta + ", c = " + _scale + ", μ = " + _location + ")";
return $"Stable(α = {_alpha}, β = {_beta}, c = {_scale}, μ = {_location})";
}
/// <summary>

2
src/Numerics/Distributions/StudentT.cs

@ -123,7 +123,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "StudentT(μ = " + _location + ", σ = " + _scale + ", ν = " + _freedom + ")";
return $"StudentT(μ = {_location}, σ = {_scale}, ν = {_freedom})";
}
/// <summary>

2
src/Numerics/Distributions/Triangular.cs

@ -99,7 +99,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Triangular(Lower = " + _lower + ", Upper = " + _upper + ", Mode = " + _mode + ")";
return $"Triangular(Lower = {_lower}, Upper = {_upper}, Mode = {_mode})";
}
/// <summary>

4
src/Numerics/Distributions/TruncatedPareto.cs

@ -36,7 +36,7 @@ namespace MathNet.Numerics.Distributions
{
public class TruncatedPareto : IContinuousDistribution
{
private System.Random _random;
System.Random _random;
/// <summary>
/// Initializes a new instance of the TruncatedPareto class.
@ -64,7 +64,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Truncated Pareto(Scale = " + Scale + ", Shape = " + Shape + ", Truncation = " + Truncation + ")";
return $"Truncated Pareto(Scale = {Scale}, Shape = {Shape}, Truncation = {Truncation})";
}
/// <summary>

2
src/Numerics/Distributions/Weibull.cs

@ -103,7 +103,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Weibull(k = " + _shape + ", λ = " + _scale + ")";
return $"Weibull(k = {_shape}, λ = {_scale})";
}
/// <summary>

2
src/Numerics/Distributions/Wishart.cs

@ -143,7 +143,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Wishart(DegreesOfFreedom = " + _degreesOfFreedom + ", Rows = " + _scale.RowCount + ", Columns = " + _scale.ColumnCount + ")";
return $"Wishart(DegreesOfFreedom = {_degreesOfFreedom}, Rows = {_scale.RowCount}, Columns = {_scale.ColumnCount})";
}
/// <summary>

2
src/Numerics/Distributions/Zipf.cs

@ -97,7 +97,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Zipf(S = " + _s + ", N = " + _n + ")";
return $"Zipf(S = {_s}, N = {_n})";
}
/// <summary>

Loading…
Cancel
Save