|
|
|
@ -28,84 +28,69 @@ |
|
|
|
// OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
// </copyright>
|
|
|
|
|
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using System.Text; |
|
|
|
|
|
|
|
|
|
|
|
namespace MathNet.Numerics.Statistics.Mcmc |
|
|
|
{ |
|
|
|
using System; |
|
|
|
using System.Linq; |
|
|
|
using Distributions; |
|
|
|
using Properties; |
|
|
|
using Numerics.Random; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// A hybrid Monte Carlo sampler for multivariate distributions.
|
|
|
|
/// </summary>
|
|
|
|
public class HybridMC : HybridMCGeneric<double[]> |
|
|
|
{ |
|
|
|
|
|
|
|
#region Variables for internal use.
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Number of parameters in the density function.
|
|
|
|
/// </summary>
|
|
|
|
private int Length; |
|
|
|
private readonly int _length; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Distribution to sample momentum from.
|
|
|
|
/// </summary>
|
|
|
|
private Normal pDistribution; |
|
|
|
|
|
|
|
#endregion
|
|
|
|
private Normal _pDistribution; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Standard deviations used in the sampling of different components of the
|
|
|
|
/// Standard deviations used in the sampling of different components of the
|
|
|
|
/// momentum.
|
|
|
|
/// </summary>
|
|
|
|
private double[] mpSdv; |
|
|
|
private double[] _mpSdv; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the standard deviations used in the sampling of different components of the
|
|
|
|
/// Gets or sets the standard deviations used in the sampling of different components of the
|
|
|
|
/// momentum.
|
|
|
|
/// </summary>
|
|
|
|
/// <exception cref="ArgumentOutOfRangeException">When the length of pSdv is not the same as Length.</exception>
|
|
|
|
public double[] MomentumStdDev |
|
|
|
{ |
|
|
|
get { return (double[])mpSdv.Clone(); } |
|
|
|
get { return (double[])_mpSdv.Clone(); } |
|
|
|
set |
|
|
|
{ |
|
|
|
CheckVariance(value); |
|
|
|
mpSdv = (double[])value.Clone(); |
|
|
|
|
|
|
|
|
|
|
|
_mpSdv = (double[])value.Clone(); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#region Ctor
|
|
|
|
/// <summary>
|
|
|
|
/// Constructs a new Hybrid Monte Carlo sampler for a multivariate probability distribution.
|
|
|
|
/// The burn interval will be set to 0.
|
|
|
|
/// Constructs a new Hybrid Monte Carlo sampler for a multivariate probability distribution.
|
|
|
|
/// The burn interval will be set to 0.
|
|
|
|
/// The components of the momentum will be sampled from a normal distribution with standard deviation
|
|
|
|
/// 1 using the default <see cref="System.Random"/> random
|
|
|
|
/// 1 using the default <see cref="System.Random"/> random
|
|
|
|
/// number generator. A three point estimation will be used for differentiation.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x0">The initial sample.</param>
|
|
|
|
/// <param name="pdfLnP">The log density of the distribution we want to sample from.</param>
|
|
|
|
/// <param name="frogLeapSteps">Number frogleap simulation steps.</param>
|
|
|
|
/// <param name="stepSize">Size of the frogleap simulation steps.</param>
|
|
|
|
public HybridMC(double[] x0, DensityLn<double[]> pdfLnP, int frogLeapSteps, double stepSize) : |
|
|
|
this(x0, pdfLnP, frogLeapSteps, stepSize, 0) |
|
|
|
{ } |
|
|
|
public HybridMC(double[] x0, DensityLn<double[]> pdfLnP, int frogLeapSteps, double stepSize) |
|
|
|
: this(x0, pdfLnP, frogLeapSteps, stepSize, 0) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Constructs a new Hybrid Monte Carlo sampler for a multivariate probability distribution.
|
|
|
|
/// Constructs a new Hybrid Monte Carlo sampler for a multivariate probability distribution.
|
|
|
|
/// The components of the momentum will be sampled from a normal distribution with standard deviation
|
|
|
|
/// 1 using the default <see cref="System.Random"/> random
|
|
|
|
/// number generator. A three point estimation will be used for differentiation.
|
|
|
|
/// 1 using the default <see cref="System.Random"/> random
|
|
|
|
/// number generator. A three point estimation will be used for differentiation.
|
|
|
|
/// This constructor will set the burn interval.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x0">The initial sample.</param>
|
|
|
|
@ -114,18 +99,20 @@ namespace MathNet.Numerics.Statistics.Mcmc |
|
|
|
/// <param name="stepSize">Size of the frogleap simulation steps.</param>
|
|
|
|
/// <param name="burnInterval">The number of iterations in between returning samples.</param>
|
|
|
|
/// <exception cref="ArgumentOutOfRangeException">When the number of burnInterval iteration is negative.</exception>
|
|
|
|
public HybridMC(double[] x0, DensityLn<double[]> pdfLnP, int frogLeapSteps, double stepSize, int burnInterval) : |
|
|
|
this(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, new double[x0.Count()], new System.Random(), new DiffMethod(HybridMC.Grad)) |
|
|
|
public HybridMC(double[] x0, DensityLn<double[]> pdfLnP, int frogLeapSteps, double stepSize, int burnInterval) |
|
|
|
: this(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, new double[x0.Count()], new Random(), Grad) |
|
|
|
{ |
|
|
|
for (int i = 0; i < Length; i++) |
|
|
|
{ mpSdv[i] = 1; } |
|
|
|
for (int i = 0; i < _length; i++) |
|
|
|
{ |
|
|
|
_mpSdv[i] = 1; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Constructs a new Hybrid Monte Carlo sampler for a multivariate probability distribution.
|
|
|
|
/// Constructs a new Hybrid Monte Carlo sampler for a multivariate probability distribution.
|
|
|
|
/// The components of the momentum will be sampled from a normal distribution with standard deviation
|
|
|
|
/// specified by pSdv using the default <see cref="System.Random"/> random
|
|
|
|
/// number generator. A three point estimation will be used for differentiation.
|
|
|
|
/// specified by pSdv using the default <see cref="System.Random"/> random
|
|
|
|
/// number generator. A three point estimation will be used for differentiation.
|
|
|
|
/// This constructor will set the burn interval.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x0">The initial sample.</param>
|
|
|
|
@ -133,18 +120,19 @@ namespace MathNet.Numerics.Statistics.Mcmc |
|
|
|
/// <param name="frogLeapSteps">Number frogleap simulation steps.</param>
|
|
|
|
/// <param name="stepSize">Size of the frogleap simulation steps.</param>
|
|
|
|
/// <param name="burnInterval">The number of iterations in between returning samples.</param>
|
|
|
|
/// <param name="pSdv">The standard deviations of the normal distributions that are used to sample
|
|
|
|
/// <param name="pSdv">The standard deviations of the normal distributions that are used to sample
|
|
|
|
/// the components of the momentum.</param>
|
|
|
|
/// <exception cref="ArgumentOutOfRangeException">When the number of burnInterval iteration is negative.</exception>
|
|
|
|
public HybridMC(double[] x0, DensityLn<double[]> pdfLnP, int frogLeapSteps, double stepSize, int burnInterval, double[] pSdv) : |
|
|
|
this(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, pSdv, new System.Random()) |
|
|
|
{ } |
|
|
|
public HybridMC(double[] x0, DensityLn<double[]> pdfLnP, int frogLeapSteps, double stepSize, int burnInterval, double[] pSdv) |
|
|
|
: this(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, pSdv, new Random()) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Constructs a new Hybrid Monte Carlo sampler for a multivariate probability distribution.
|
|
|
|
/// Constructs a new Hybrid Monte Carlo sampler for a multivariate probability distribution.
|
|
|
|
/// The components of the momentum will be sampled from a normal distribution with standard deviation
|
|
|
|
/// specified by pSdv using the a random number generator provided by the user.
|
|
|
|
/// A three point estimation will be used for differentiation.
|
|
|
|
/// specified by pSdv using the a random number generator provided by the user.
|
|
|
|
/// A three point estimation will be used for differentiation.
|
|
|
|
/// This constructor will set the burn interval.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x0">The initial sample.</param>
|
|
|
|
@ -152,20 +140,19 @@ namespace MathNet.Numerics.Statistics.Mcmc |
|
|
|
/// <param name="frogLeapSteps">Number frogleap simulation steps.</param>
|
|
|
|
/// <param name="stepSize">Size of the frogleap simulation steps.</param>
|
|
|
|
/// <param name="burnInterval">The number of iterations in between returning samples.</param>
|
|
|
|
/// <param name="pSdv">The standard deviations of the normal distributions that are used to sample
|
|
|
|
/// <param name="pSdv">The standard deviations of the normal distributions that are used to sample
|
|
|
|
/// the components of the momentum.</param>
|
|
|
|
/// <param name="randomSource">Random number generator used for sampling the momentum.</param>
|
|
|
|
/// <exception cref="ArgumentOutOfRangeException">When the number of burnInterval iteration is negative.</exception>
|
|
|
|
public HybridMC(double[] x0, DensityLn<double[]> pdfLnP, int frogLeapSteps, double stepSize, int burnInterval, double[] pSdv, System.Random randomSource) : |
|
|
|
this(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, pSdv, randomSource, new DiffMethod(HybridMC.Grad)) |
|
|
|
{ } |
|
|
|
|
|
|
|
|
|
|
|
public HybridMC(double[] x0, DensityLn<double[]> pdfLnP, int frogLeapSteps, double stepSize, int burnInterval, double[] pSdv, Random randomSource) |
|
|
|
: this(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, pSdv, randomSource, Grad) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Constructs a new Hybrid Monte Carlo sampler for a multivariate probability distribution.
|
|
|
|
/// Constructs a new Hybrid Monte Carlo sampler for a multivariate probability distribution.
|
|
|
|
/// The components of the momentum will be sampled from a normal distribution with standard deviations
|
|
|
|
/// given by pSdv. This constructor will set the burn interval, the method used for
|
|
|
|
/// given by pSdv. This constructor will set the burn interval, the method used for
|
|
|
|
/// numerical differentiation and the random number generator.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x0">The initial sample.</param>
|
|
|
|
@ -173,62 +160,59 @@ namespace MathNet.Numerics.Statistics.Mcmc |
|
|
|
/// <param name="frogLeapSteps">Number frogleap simulation steps.</param>
|
|
|
|
/// <param name="stepSize">Size of the frogleap simulation steps.</param>
|
|
|
|
/// <param name="burnInterval">The number of iterations in between returning samples.</param>
|
|
|
|
/// <param name="pSdv">The standard deviations of the normal distributions that are used to sample
|
|
|
|
/// <param name="pSdv">The standard deviations of the normal distributions that are used to sample
|
|
|
|
/// the components of the momentum.</param>
|
|
|
|
/// <param name="randomSource">Random number generator used for sampling the momentum.</param>
|
|
|
|
/// <param name="diff">The method used for numerical differentiation.</param>
|
|
|
|
/// <exception cref="ArgumentOutOfRangeException">When the number of burnInterval iteration is negative.</exception>
|
|
|
|
/// <exception cref="ArgumentOutOfRangeException">When the length of pSdv is not the same as x0.</exception>
|
|
|
|
public HybridMC(double[] x0, DensityLn<double[]> pdfLnP, int frogLeapSteps, double stepSize, int burnInterval, double[] pSdv, System.Random randomSource, DiffMethod diff) |
|
|
|
public HybridMC(double[] x0, DensityLn<double[]> pdfLnP, int frogLeapSteps, double stepSize, int burnInterval, double[] pSdv, Random randomSource, DiffMethod diff) |
|
|
|
: base(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, randomSource, diff) |
|
|
|
{ |
|
|
|
Length = x0.Count(); |
|
|
|
_length = x0.Count(); |
|
|
|
MomentumStdDev = pSdv; |
|
|
|
|
|
|
|
Initialize(x0); |
|
|
|
|
|
|
|
Burn(BurnInterval); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initialize parameters.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x0">The current location of the sampler.</param>
|
|
|
|
private void Initialize(double[] x0) |
|
|
|
{ |
|
|
|
mCurrent = (double[])x0.Clone(); |
|
|
|
pDistribution = new Normal(0, 1); |
|
|
|
pDistribution.RandomSource = RandomSource; |
|
|
|
Current = (double[])x0.Clone(); |
|
|
|
_pDistribution = new Normal(0, 1) |
|
|
|
{ |
|
|
|
RandomSource = RandomSource |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Checking that the location and the momentum are of the same dimension and that each component is positive.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="pSdv">The standard deviations used for sampling the momentum.</param>
|
|
|
|
/// <exception cref="ArgumentOutOfRangeException">When the length of pSdv is not the same as Length or if any
|
|
|
|
/// <exception cref="ArgumentOutOfRangeException">When the length of pSdv is not the same as Length or if any
|
|
|
|
/// component is negative.</exception>
|
|
|
|
/// <exception cref="ArgumentNullException">When pSdv is null.</exception>
|
|
|
|
private void CheckVariance(double[] pSdv) |
|
|
|
{ |
|
|
|
if (pSdv == null) |
|
|
|
{ |
|
|
|
throw new ArgumentNullException("Standard deviation cannot be null."); |
|
|
|
if (pSdv.Count() != Length) |
|
|
|
} |
|
|
|
if (pSdv.Count() != _length) |
|
|
|
{ |
|
|
|
throw new ArgumentOutOfRangeException("Standard deviation of momentum must have same length as sample."); |
|
|
|
foreach (double sdv in pSdv) |
|
|
|
} |
|
|
|
if (pSdv.Any(sdv => sdv < 0)) |
|
|
|
{ |
|
|
|
if (sdv < 0) |
|
|
|
{ throw new ArgumentOutOfRangeException("Standard deviation must be positive."); } |
|
|
|
throw new ArgumentOutOfRangeException("Standard deviation must be positive."); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
#region Inherited from HybridMCGeneric
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Use for copying objects in the Burn method.
|
|
|
|
/// </summary>
|
|
|
|
@ -236,8 +220,8 @@ namespace MathNet.Numerics.Statistics.Mcmc |
|
|
|
/// <returns>A copy of the source object.</returns>
|
|
|
|
protected override double[] Copy(double[] source) |
|
|
|
{ |
|
|
|
double[] destination = new double[Length]; |
|
|
|
Array.Copy(source, destination, Length); |
|
|
|
var destination = new double[_length]; |
|
|
|
Array.Copy(source, destination, _length); |
|
|
|
return destination; |
|
|
|
} |
|
|
|
|
|
|
|
@ -247,42 +231,49 @@ namespace MathNet.Numerics.Statistics.Mcmc |
|
|
|
/// <returns>An object of type T.</returns>
|
|
|
|
protected override double[] Create() |
|
|
|
{ |
|
|
|
return new double[Length]; |
|
|
|
return new double[_length]; |
|
|
|
} |
|
|
|
|
|
|
|
///<inheritdoc/>
|
|
|
|
protected override void DoAdd(ref double[] first, double factor, double[] second) |
|
|
|
{ |
|
|
|
for (int i = 0; i < Length; i++) |
|
|
|
{ first[i] += factor * second[i]; } |
|
|
|
for (int i = 0; i < _length; i++) |
|
|
|
{ |
|
|
|
first[i] += factor * second[i]; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
protected override void DoSubtract(ref double[] first, double factor, double[] second) |
|
|
|
{ |
|
|
|
for (int i = 0; i < Length; i++) |
|
|
|
{ first[i] -= factor * second[i]; } |
|
|
|
for (int i = 0; i < _length; i++) |
|
|
|
{ |
|
|
|
first[i] -= factor * second[i]; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
protected override double DoProduct(double[] first, double[] second) |
|
|
|
{ |
|
|
|
double prod = 0; |
|
|
|
for (int i = 0; i < Length; i++) |
|
|
|
{ prod += first[i] * second[i]; } |
|
|
|
for (int i = 0; i < _length; i++) |
|
|
|
{ |
|
|
|
prod += first[i] * second[i]; |
|
|
|
} |
|
|
|
return prod; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Samples the momentum from a normal distribution.
|
|
|
|
/// Samples the momentum from a normal distribution.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="p">The momentum to be randomized.</param>
|
|
|
|
protected override void RandomizeMomentum(ref double[] p) |
|
|
|
{ |
|
|
|
for (int j = 0; j < Length; j++) |
|
|
|
{ p[j] = mpSdv[j] * pDistribution.Sample(); } |
|
|
|
for (int j = 0; j < _length; j++) |
|
|
|
{ |
|
|
|
p[j] = _mpSdv[j] * _pDistribution.Sample(); |
|
|
|
} |
|
|
|
} |
|
|
|
#endregion
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The default method used for computing the gradient. Uses a simple three point estimation.
|
|
|
|
@ -292,25 +283,26 @@ namespace MathNet.Numerics.Statistics.Mcmc |
|
|
|
/// <returns>The gradient of the function at the point x.</returns>
|
|
|
|
static private double[] Grad(DensityLn<double[]> function, double[] x) |
|
|
|
{ |
|
|
|
int Length = x.Length; |
|
|
|
double[] ReturnValue = new double[Length]; |
|
|
|
double[] Increment = new double[Length]; |
|
|
|
Array.Copy(x, Increment, Length); |
|
|
|
double[] Decrement = new double[Length]; |
|
|
|
Array.Copy(x, Decrement, Length); |
|
|
|
for (int i = 0; i < Length; i++) |
|
|
|
int length = x.Length; |
|
|
|
var returnValue = new double[length]; |
|
|
|
var increment = new double[length]; |
|
|
|
var decrement = new double[length]; |
|
|
|
|
|
|
|
Array.Copy(x, increment, length); |
|
|
|
Array.Copy(x, decrement, length); |
|
|
|
|
|
|
|
for (int i = 0; i < length; i++) |
|
|
|
{ |
|
|
|
double y = x[i]; |
|
|
|
double h = Math.Max(10e-4, (10e-7) * y); |
|
|
|
Increment[i] += h; |
|
|
|
Decrement[i] -= h; |
|
|
|
ReturnValue[i] = (function(Increment) - function(Decrement)) / (2 * h); |
|
|
|
Increment[i] = y; |
|
|
|
Decrement[i] = y; |
|
|
|
increment[i] += h; |
|
|
|
decrement[i] -= h; |
|
|
|
returnValue[i] = (function(increment) - function(decrement)) / (2 * h); |
|
|
|
increment[i] = y; |
|
|
|
decrement[i] = y; |
|
|
|
} |
|
|
|
return ReturnValue; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return returnValue; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|