//
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2010 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
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// 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
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using IStation.Numerics.Distributions;
using IStation.Numerics.Random;
namespace IStation.Numerics.Statistics.Mcmc
{
///
/// A hybrid Monte Carlo sampler for univariate distributions.
///
public class UnivariateHybridMC : HybridMCGeneric
{
///
/// Distribution to sample momentum from.
///
private readonly Normal _distribution;
///
/// Standard deviations used in the sampling of the
/// momentum.
///
private double _sdv;
///
/// Gets or sets the standard deviation used in the sampling of the
/// momentum.
///
/// When standard deviation is negative.
public double MomentumStdDev
{
get => _sdv;
set
{
if (_sdv != value)
{
_sdv = SetPositive(value);
}
}
}
///
/// Constructs a new Hybrid Monte Carlo sampler for a univariate probability distribution.
/// The momentum will be sampled from a normal distribution with standard deviation
/// specified by pSdv using the default random
/// number generator. A three point estimation will be used for differentiation.
/// This constructor will set the burn interval.
///
/// The initial sample.
/// The log density of the distribution we want to sample from.
/// Number frog leap simulation steps.
/// Size of the frog leap simulation steps.
/// The number of iterations in between returning samples.
/// The standard deviation of the normal distribution that is used to sample
/// the momentum.
/// When the number of burnInterval iteration is negative.
public UnivariateHybridMC(double x0, DensityLn pdfLnP, int frogLeapSteps, double stepSize, int burnInterval = 0, double pSdv = 1)
: this(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, pSdv, SystemRandomSource.Default)
{
}
///
/// Constructs a new Hybrid Monte Carlo sampler for a univariate probability distribution.
/// The momentum will be sampled from a normal distribution with standard deviation
/// specified by pSdv using a random
/// number generator provided by the user. A three point estimation will be used for differentiation.
/// This constructor will set the burn interval.
///
/// The initial sample.
/// The log density of the distribution we want to sample from.
/// Number frog leap simulation steps.
/// Size of the frog leap simulation steps.
/// The number of iterations in between returning samples.
/// The standard deviation of the normal distribution that is used to sample
/// the momentum.
/// Random number generator used to sample the momentum.
/// When the number of burnInterval iteration is negative.
public UnivariateHybridMC(double x0, DensityLn pdfLnP, int frogLeapSteps, double stepSize, int burnInterval, double pSdv, System.Random randomSource)
: this(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, pSdv, randomSource, Grad)
{
}
///
/// Constructs a new Hybrid Monte Carlo sampler for a multivariate probability distribution.
/// The momentum will be sampled from a normal distribution with standard deviation
/// given by pSdv using a random
/// number generator provided by the user. This constructor will set both the burn interval and the method used for
/// numerical differentiation.
///
/// The initial sample.
/// The log density of the distribution we want to sample from.
/// Number frog leap simulation steps.
/// Size of the frog leap simulation steps.
/// The number of iterations in between returning samples.
/// The standard deviation of the normal distribution that is used to sample
/// the momentum.
/// The method used for numerical differentiation.
/// Random number generator used for sampling the momentum.
/// When the number of burnInterval iteration is negative.
public UnivariateHybridMC(double x0, DensityLn pdfLnP, int frogLeapSteps, double stepSize, int burnInterval, double pSdv, System.Random randomSource, DiffMethod diff)
: base(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, randomSource, diff)
{
MomentumStdDev = pSdv;
_distribution = new Normal(0.0, MomentumStdDev, RandomSource);
Burn(BurnInterval);
}
///
/// Use for copying objects in the Burn method.
///
/// The source of copying.
/// A copy of the source object.
protected override double Copy(double source)
{
return source;
}
///
/// Use for creating temporary objects in the Burn method.
///
/// An object of type T.
protected override double Create()
{
return 0;
}
///
protected override void DoAdd(ref double first, double factor, double second)
{
first += factor * second;
}
///
protected override double DoProduct(double first, double second)
{
return first * second;
}
///
protected override void DoSubtract(ref double first, double factor, double second)
{
first -= factor * second;
}
///
/// Samples the momentum from a normal distribution.
///
/// The momentum to be randomized.
protected override void RandomizeMomentum(ref double p)
{
p = _distribution.Sample();
}
///
/// The default method used for computing the derivative. Uses a simple three point estimation.
///
/// Function for which the derivative is to be evaluated.
/// The location where the derivative is to be evaluated.
/// The derivative of the function at the point x.
static double Grad(DensityLn function, double x)
{
double h = Math.Max(10e-4, (10e-7) * x);
double increment = x + h;
double decrement = x - h;
return (function(increment) - function(decrement)) / (2 * h);
}
}
}