|
|
|
@ -3,7 +3,9 @@ |
|
|
|
// http://numerics.mathdotnet.com
|
|
|
|
// http://github.com/mathnet/mathnet-numerics
|
|
|
|
// http://mathnetnumerics.codeplex.com
|
|
|
|
// Copyright (c) 2009-2010 Math.NET
|
|
|
|
//
|
|
|
|
// Copyright (c) 2009-2013 Math.NET
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person
|
|
|
|
// obtaining a copy of this software and associated documentation
|
|
|
|
// files (the "Software"), to deal in the Software without
|
|
|
|
@ -12,8 +14,10 @@ |
|
|
|
// copies of the Software, and to permit persons to whom the
|
|
|
|
// Software is furnished to do so, subject to the following
|
|
|
|
// conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be
|
|
|
|
// included in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
|
|
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
@ -24,13 +28,14 @@ |
|
|
|
// OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
// </copyright>
|
|
|
|
|
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using MathNet.Numerics.Properties; |
|
|
|
|
|
|
|
namespace MathNet.Numerics.Distributions |
|
|
|
{ |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using Properties; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Continuous Univariate Stable distribution.
|
|
|
|
/// A random variable is said to be stable (or to have a stable distribution) if it has
|
|
|
|
/// the property that a linear combination of two independent copies of the variable has
|
|
|
|
/// the same distribution, up to location and scale parameters.
|
|
|
|
@ -67,7 +72,7 @@ namespace MathNet.Numerics.Distributions |
|
|
|
/// <summary>
|
|
|
|
/// The distribution's random number generator.
|
|
|
|
/// </summary>
|
|
|
|
Random _random; |
|
|
|
System.Random _random; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="Stable"/> class.
|
|
|
|
@ -78,7 +83,7 @@ namespace MathNet.Numerics.Distributions |
|
|
|
/// <param name="location">The location parameter of the distribution.</param>
|
|
|
|
public Stable(double alpha, double beta, double scale, double location) |
|
|
|
{ |
|
|
|
_random = new Random(); |
|
|
|
_random = new System.Random(); |
|
|
|
SetParameters(alpha, beta, scale, location); |
|
|
|
} |
|
|
|
|
|
|
|
@ -90,9 +95,9 @@ namespace MathNet.Numerics.Distributions |
|
|
|
/// <param name="scale">The scale parameter of the distribution.</param>
|
|
|
|
/// <param name="location">The location parameter of the distribution.</param>
|
|
|
|
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
|
|
|
|
public Stable(double alpha, double beta, double scale, double location, Random randomSource) |
|
|
|
public Stable(double alpha, double beta, double scale, double location, System.Random randomSource) |
|
|
|
{ |
|
|
|
_random = randomSource ?? new Random(); |
|
|
|
_random = randomSource ?? new System.Random(); |
|
|
|
SetParameters(alpha, beta, scale, location); |
|
|
|
} |
|
|
|
|
|
|
|
@ -174,12 +179,10 @@ namespace MathNet.Numerics.Distributions |
|
|
|
return "Stable(" + "Stability = " + _alpha + ", Skewness = " + _beta + ", Scale = " + _scale + ", Location = " + _location + ")"; |
|
|
|
} |
|
|
|
|
|
|
|
#region IDistribution Members
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the random number generator which is used to draw random samples.
|
|
|
|
/// </summary>
|
|
|
|
public Random RandomSource |
|
|
|
public System.Random RandomSource |
|
|
|
{ |
|
|
|
get { return _random; } |
|
|
|
set |
|
|
|
@ -267,51 +270,6 @@ namespace MathNet.Numerics.Distributions |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Computes the cumulative distribution function of the distribution.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The location at which to compute the cumulative density.</param>
|
|
|
|
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
|
|
|
|
/// <remarks>Throws a not supported exception if <c>Alpha != 2</c>, <c>(Alpha != 1 and Beta !=0)</c>, or <c>(Alpha != 0.5 and Beta != 1)</c></remarks>
|
|
|
|
public double CumulativeDistribution(double x) |
|
|
|
{ |
|
|
|
if (_alpha == 2) |
|
|
|
{ |
|
|
|
return (new Normal(_location, StdDev)).CumulativeDistribution(x); |
|
|
|
} |
|
|
|
|
|
|
|
if (_alpha == 1 && _beta == 0) |
|
|
|
{ |
|
|
|
return (new Cauchy(_location, _scale)).CumulativeDistribution(x); |
|
|
|
} |
|
|
|
|
|
|
|
if (_alpha == 0.5 && _beta == 1) |
|
|
|
{ |
|
|
|
return LevyCumulativeDistribution(_scale, _location, x); |
|
|
|
} |
|
|
|
|
|
|
|
throw new NotSupportedException(); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Computes the cumulative distribution function of the Levy distribution.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="scale">The scale parameter.</param>
|
|
|
|
/// <param name="location">The location parameter.</param>
|
|
|
|
/// <param name="x">The location at which to compute the cumulative density.</param>
|
|
|
|
/// <returns>
|
|
|
|
/// the cumulative density at <paramref name="x"/>.
|
|
|
|
/// </returns>
|
|
|
|
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)))); |
|
|
|
} |
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region IContinuousDistribution Members
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the mode of the distribution.
|
|
|
|
/// </summary>
|
|
|
|
@ -371,7 +329,7 @@ namespace MathNet.Numerics.Distributions |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Computes the density of the distribution.
|
|
|
|
/// Computes the density of the distribution (PDF), i.e. dP(X <= x)/dx.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The location at which to compute the density.</param>
|
|
|
|
/// <returns>the density at <paramref name="x"/>.</returns>
|
|
|
|
@ -414,7 +372,7 @@ namespace MathNet.Numerics.Distributions |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Computes the log density of the distribution.
|
|
|
|
/// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X <= x)/dx).
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The location at which to compute the log density.</param>
|
|
|
|
/// <returns>the log density at <paramref name="x"/>.</returns>
|
|
|
|
@ -423,7 +381,44 @@ namespace MathNet.Numerics.Distributions |
|
|
|
return Math.Log(Density(x)); |
|
|
|
} |
|
|
|
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
|
|
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X <= x).
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
|
|
|
|
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
|
|
|
|
/// <remarks>Throws a not supported exception if <c>Alpha != 2</c>, <c>(Alpha != 1 and Beta !=0)</c>, or <c>(Alpha != 0.5 and Beta != 1)</c></remarks>
|
|
|
|
public double CumulativeDistribution(double x) |
|
|
|
{ |
|
|
|
if (_alpha == 2) |
|
|
|
{ |
|
|
|
return (new Normal(_location, StdDev)).CumulativeDistribution(x); |
|
|
|
} |
|
|
|
|
|
|
|
if (_alpha == 1 && _beta == 0) |
|
|
|
{ |
|
|
|
return (new Cauchy(_location, _scale)).CumulativeDistribution(x); |
|
|
|
} |
|
|
|
|
|
|
|
if (_alpha == 0.5 && _beta == 1) |
|
|
|
{ |
|
|
|
return LevyCumulativeDistribution(_scale, _location, x); |
|
|
|
} |
|
|
|
|
|
|
|
throw new NotSupportedException(); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Computes the cumulative distribution function of the Levy distribution.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="scale">The scale parameter.</param>
|
|
|
|
/// <param name="location">The location parameter.</param>
|
|
|
|
/// <param name="x">The location at which to compute the cumulative density.</param>
|
|
|
|
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
|
|
|
|
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)))); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Samples the distribution.
|
|
|
|
@ -434,7 +429,7 @@ namespace MathNet.Numerics.Distributions |
|
|
|
/// <param name="scale">The scale parameter of the distribution.</param>
|
|
|
|
/// <param name="location">The location parameter of the distribution.</param>
|
|
|
|
/// <returns>a random number from the distribution.</returns>
|
|
|
|
internal static double SampleUnchecked(Random rnd, double alpha, double beta, double scale, double location) |
|
|
|
internal static double SampleUnchecked(System.Random rnd, double alpha, double beta, double scale, double location) |
|
|
|
{ |
|
|
|
var randTheta = ContinuousUniform.Sample(rnd, -Constants.PiOver2, Constants.PiOver2); |
|
|
|
var randW = Exponential.Sample(rnd, 1.0); |
|
|
|
@ -492,7 +487,7 @@ namespace MathNet.Numerics.Distributions |
|
|
|
/// <param name="scale">The scale parameter of the distribution.</param>
|
|
|
|
/// <param name="location">The location parameter of the distribution.</param>
|
|
|
|
/// <returns>a sample from the distribution.</returns>
|
|
|
|
public static double Sample(Random rnd, double alpha, double beta, double scale, double location) |
|
|
|
public static double Sample(System.Random rnd, double alpha, double beta, double scale, double location) |
|
|
|
{ |
|
|
|
if (Control.CheckDistributionParameters && !IsValidParameterSet(alpha, beta, scale, location)) |
|
|
|
{ |
|
|
|
@ -511,7 +506,7 @@ namespace MathNet.Numerics.Distributions |
|
|
|
/// <param name="scale">The scale parameter of the distribution.</param>
|
|
|
|
/// <param name="location">The location parameter of the distribution.</param>
|
|
|
|
/// <returns>a sequence of samples from the distribution.</returns>
|
|
|
|
public static IEnumerable<double> Samples(Random rnd, double alpha, double beta, double scale, double location) |
|
|
|
public static IEnumerable<double> Samples(System.Random rnd, double alpha, double beta, double scale, double location) |
|
|
|
{ |
|
|
|
if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale, scale, location)) |
|
|
|
{ |
|
|
|
@ -524,4 +519,4 @@ namespace MathNet.Numerics.Distributions |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |