Browse Source

Distributions: fix sampling wiring of new Skewed Generalized distributions

Related to #654
v4
Christoph Ruegg 6 years ago
parent
commit
7306226559
  1. 110
      src/Numerics/Distributions/SkewedGeneralizedError.cs
  2. 114
      src/Numerics/Distributions/SkewedGeneralizedT.cs

110
src/Numerics/Distributions/SkewedGeneralizedError.cs

@ -3,7 +3,7 @@
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2019 Math.NET
// Copyright (c) 2009-2020 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
@ -313,6 +313,43 @@ namespace MathNet.Numerics.Distributions
return PDFLn(Location, Scale, Skew, P, x);
}
public double Sample()
{
return SampleUnchecked(_random, Location, Scale, Skew, P);
}
public void Samples(double[] values)
{
SamplesUnchecked(_random, values, Location, Scale, Skew, P);
}
public IEnumerable<double> Samples()
{
return SamplesUnchecked(_random, Location, Scale, Skew, 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);
}
static void SamplesUnchecked(System.Random rnd, double[] values, double location, double scale, double skew, double p)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = SampleUnchecked(rnd, location, scale, skew, p);
}
}
static IEnumerable<double> SamplesUnchecked(System.Random rnd, double location, double scale, double skew, double p)
{
while (true)
{
yield return SampleUnchecked(rnd, location, scale, skew, p);
}
}
/// <summary>
/// Generates a sample from the Skew Generalized Error distribution.
/// </summary>
@ -348,22 +385,37 @@ namespace MathNet.Numerics.Distributions
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
while (true)
{
yield return SampleUnchecked(rnd, location, scale, skew, p);
}
return SamplesUnchecked(rnd, location, scale, skew, p);
}
public static IEnumerable<double> Samples(double location, double scale, double skew, double p)
/// <summary>
/// Fills an array with samples from the Skew Generalized Error distribution using inverse transform.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="values">The array to fill with the samples.</param>
/// <param name="location">The location (μ) of the distribution.</param>
/// <param name="scale">The scale (σ) of the distribution. Range: σ > 0.</param>
/// <param name="skew">The skew, 1 > λ > -1</param>
/// <param name="p">Parameter that controls kurtosis. Range: p > 0</param>
/// <returns>a sequence of samples from the distribution.</returns>
public static void Samples(System.Random rnd, double[] values, double location, double scale, double skew, double p)
{
if (!IsValidParameterSet(location, scale, skew, p))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
return Samples(SystemRandomSource.Default, location, scale, skew, p);
SamplesUnchecked(rnd, values, location, scale, skew, p);
}
/// <summary>
/// Generates a sample from the Skew Generalized Error distribution.
/// </summary>
/// <param name="location">The location (μ) of the distribution.</param>
/// <param name="scale">The scale (σ) of the distribution. Range: σ > 0.</param>
/// <param name="skew">The skew, 1 > λ > -1</param>
/// <param name="p">Parameter that controls kurtosis. Range: p > 0</param>
/// <returns>a sample from the distribution.</returns>
public static double Sample(double location, double scale, double skew, double p)
{
if (!IsValidParameterSet(location, scale, skew, p))
@ -374,31 +426,41 @@ namespace MathNet.Numerics.Distributions
return SampleUnchecked(SystemRandomSource.Default, location, scale, skew, p);
}
static double SampleUnchecked(System.Random rnd, double location, double scale, double skew, double p)
/// <summary>
/// Generates a sequence of samples from the Skew Generalized Error distribution using inverse transform.
/// </summary>
/// <param name="location">The location (μ) of the distribution.</param>
/// <param name="scale">The scale (σ) of the distribution. Range: σ > 0.</param>
/// <param name="skew">The skew, 1 > λ > -1</param>
/// <param name="p">Parameter that controls kurtosis. Range: p > 0</param>
/// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(double location, double scale, double skew, double p)
{
var u = ContinuousUniform.Sample(rnd, 0, 1);
return InvCDF(location, scale, skew, p, u);
}
if (!IsValidParameterSet(location, scale, skew, p))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
public double Sample()
{
return SampleUnchecked(SystemRandomSource.Default, Location, Scale, Skew, P);
return SamplesUnchecked(SystemRandomSource.Default, location, scale, skew, p);
}
public void Samples(double[] values)
/// <summary>
/// Fills an array with samples from the Skew Generalized Error distribution using inverse transform.
/// </summary>
/// <param name="values">The array to fill with the samples.</param>
/// <param name="location">The location (μ) of the distribution.</param>
/// <param name="scale">The scale (σ) of the distribution. Range: σ > 0.</param>
/// <param name="skew">The skew, 1 > λ > -1</param>
/// <param name="p">Parameter that controls kurtosis. Range: p > 0</param>
/// <returns>a sequence of samples from the distribution.</returns>
public static void Samples(double[] values, double location, double scale, double skew, double p)
{
if (values == null)
return;
for (int i = 0; i < values.Length; i++)
if (!IsValidParameterSet(location, scale, skew, p))
{
values[i] = Sample();
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
}
public IEnumerable<double> Samples()
{
return Samples();
SamplesUnchecked(SystemRandomSource.Default, values, location, scale, skew, p);
}
}
}

114
src/Numerics/Distributions/SkewedGeneralizedT.cs

@ -3,7 +3,7 @@
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2019 Math.NET
// Copyright (c) 2009-2020 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
@ -482,6 +482,43 @@ namespace MathNet.Numerics.Distributions
return _d?.DensityLn(x) ?? PDFLn(Location, Scale, Skew, P, Q, x);
}
public double Sample()
{
return SampleUnchecked(_random, Location, Scale, Skew, P, Q);
}
public void Samples(double[] values)
{
SamplesUnchecked(_random, values, Location, Scale, Skew, P, Q);
}
public IEnumerable<double> Samples()
{
return SamplesUnchecked(_random, Location, Scale, Skew, P, 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);
}
static void SamplesUnchecked(System.Random rnd, double[] values, double location, double scale, double skew, double p, double q)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = SampleUnchecked(rnd, location, scale, skew, p, q);
}
}
static IEnumerable<double> SamplesUnchecked(System.Random rnd, double location, double scale, double skew, double p, double q)
{
while (true)
{
yield return SampleUnchecked(rnd, location, scale, skew, p, q);
}
}
/// <summary>
/// Generates a sample from the Skew Generalized t-distribution.
/// </summary>
@ -519,22 +556,39 @@ namespace MathNet.Numerics.Distributions
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
while (true)
{
yield return SampleUnchecked(rnd, location, scale, skew, p, q);
}
return SamplesUnchecked(rnd, location, scale, skew, p, q);
}
public static IEnumerable<double> Samples(double location, double scale, double skew, double p, double q)
/// <summary>
/// Fills an array with samples from the Skew Generalized t-distribution using inverse transform.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="values">The array to fill with the samples.</param>
/// <param name="location">The location (μ) of the distribution.</param>
/// <param name="scale">The scale (σ) of the distribution. Range: σ > 0.</param>
/// <param name="skew">The skew, 1 > λ > -1</param>
/// <param name="p">First parameter that controls kurtosis. Range: p > 0</param>
/// <param name="q">Second parameter that controls kurtosis. Range: q > 0</param>
/// <returns>a sequence of samples from the distribution.</returns>
public static void Samples(System.Random rnd, double[] values, double location, double scale, double skew, double p, double q)
{
if (!IsValidParameterSet(location, scale, skew, p, q))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
return Samples(SystemRandomSource.Default, location, scale, skew, p, q);
SamplesUnchecked(rnd, values, location, scale, skew, p, q);
}
/// <summary>
/// Generates a sample from the Skew Generalized t-distribution.
/// </summary>
/// <param name="location">The location (μ) of the distribution.</param>
/// <param name="scale">The scale (σ) of the distribution. Range: σ > 0.</param>
/// <param name="skew">The skew, 1 > λ > -1</param>
/// <param name="p">First parameter that controls kurtosis. Range: p > 0</param>
/// <param name="q">Second parameter that controls kurtosis. Range: q > 0</param>
/// <returns>a sample from the distribution.</returns>
public static double Sample(double location, double scale, double skew, double p, double q)
{
if (!IsValidParameterSet(location, scale, skew, p, q))
@ -545,31 +599,43 @@ namespace MathNet.Numerics.Distributions
return SampleUnchecked(SystemRandomSource.Default, location, scale, skew, p, q);
}
static double SampleUnchecked(System.Random rnd, double location, double scale, double skew, double p, double q)
/// <summary>
/// Generates a sequence of samples from the Skew Generalized t-distribution using inverse transform.
/// </summary>
/// <param name="location">The location (μ) of the distribution.</param>
/// <param name="scale">The scale (σ) of the distribution. Range: σ > 0.</param>
/// <param name="skew">The skew, 1 > λ > -1</param>
/// <param name="p">First parameter that controls kurtosis. Range: p > 0</param>
/// <param name="q">Second parameter that controls kurtosis. Range: q > 0</param>
/// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(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);
}
if (!IsValidParameterSet(location, scale, skew, p, q))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
public double Sample()
{
return SampleUnchecked(SystemRandomSource.Default, Location, Scale, Skew, P, Q);
return SamplesUnchecked(SystemRandomSource.Default, location, scale, skew, p, q);
}
public void Samples(double[] values)
/// <summary>
/// Fills an array with samples from the Skew Generalized t-distribution using inverse transform.
/// </summary>
/// <param name="values">The array to fill with the samples.</param>
/// <param name="location">The location (μ) of the distribution.</param>
/// <param name="scale">The scale (σ) of the distribution. Range: σ > 0.</param>
/// <param name="skew">The skew, 1 > λ > -1</param>
/// <param name="p">First parameter that controls kurtosis. Range: p > 0</param>
/// <param name="q">Second parameter that controls kurtosis. Range: q > 0</param>
/// <returns>a sequence of samples from the distribution.</returns>
public static void Samples(double[] values, double location, double scale, double skew, double p, double q)
{
if (values == null)
return;
for (int i = 0; i < values.Length; i++)
if (!IsValidParameterSet(location, scale, skew, p, q))
{
values[i] = Sample();
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
}
public IEnumerable<double> Samples()
{
return Samples();
SamplesUnchecked(SystemRandomSource.Default, values, location, scale, skew, p, q);
}
}
}

Loading…
Cancel
Save