/// <summary> /// 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. /// </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> /// <param name="burnInterval">The number of iterations in between returning samples.</param> /// <param name="pSdv">The standard deviation of the normal distribution that is used to sample /// the momentum.</param> /// <param name="diff">The method used for numerical differentiation.</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 UnivariateHybridMC(double x0, DensityLn <double> 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); }
/// <summary> /// The default method used for computing the derivative. Uses a simple three point estimation. /// </summary> /// <param name="function">Function for which the derivative is to be evaluated.</param> /// <param name="x">The location where the derivative is to be evaluated.</param> /// <returns>The derivative of the function at the point x.</returns> static private double Grad(DensityLn <double> 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)); }
/// <summary> /// 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. /// This constructor will set the burn interval. /// </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> /// <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 DiffMethod(HybridMC.Grad)) { for (int i = 0; i < Length; i++) { mpSdv[i] = 1; } }
/// <summary> /// The default method used for computing the derivative. Uses a simple three point estimation. /// </summary> /// <param name="function">Function for which the derivative is to be evaluated.</param> /// <param name="x">The location where the derivative is to be evaluated.</param> /// <returns>The derivative of the function at the point x.</returns> static double Grad(DensityLn <double> 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)); }
/// <summary> /// 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. /// This constructor will set the burn interval. /// </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> /// <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 Random(), Grad) { for (int i = 0; i < _length; i++) { _mpSdv[i] = 1; } }
/// <summary> /// 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. /// This constructor will set the burn interval. /// </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> /// <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 = 0) : this(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, new double[x0.Length], SystemRandomSource.Default, Grad) { for (int i = 0; i < _length; i++) { _mpSdv[i] = 1; } }
/// <summary> /// Constructs a new Hybrid Monte Carlo sampler. /// </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> /// <param name="burnInterval">The number of iterations in between returning samples.</param> /// <param name="diff">The method used for differentiation.</param> /// <exception cref="ArgumentOutOfRangeException">When the number of burnInterval iteration is negative.</exception> /// <exception cref="ArgumentNullException">When either x0, pdfLnP or diff is null.</exception> public HybridMCGeneric(T x0, DensityLn <T> pdfLnP, int frogLeapSteps, double stepSize, int burnInterval, DiffMethod diff) { Energy = new DensityLn <T>(x => - pdfLnP(x)); FrogLeapSteps = frogLeapSteps; StepSize = stepSize; BurnInterval = burnInterval; mCurrent = x0; Diff = diff; }
/// <summary> /// 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 using the default <see cref="System.Random"/> random /// number generator. This constructor will set both the burn interval and the method used for /// numerical 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> /// <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 /// the components of 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, DiffMethod diff) : base(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, diff) { Length = x0.Count(); MomentumStdDev = pSdv; Initialize(x0); Burn(BurnInterval); }
/// <summary> /// 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 /// numerical differentiation and the random number generator. /// </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> /// <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 /// 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) : base(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, randomSource, diff) { _length = x0.Length; MomentumStdDev = pSdv; Initialize(x0); Burn(BurnInterval); }
/// <summary> /// Constructs a new slice sampler using the default <see cref="System.Random"/> random number generator. It /// will set the number of burnInterval iterations and run a burnInterval phase. /// </summary> /// <param name="x0">The initial sample.</param> /// <param name="pdfLnP">The density of the distribution we want to sample from.</param> /// <param name="burnInterval">The number of iterations in between returning samples.</param> /// <param name="scale">The scale factor of the slice sampler.</param> /// <exception cref="ArgumentOutOfRangeException">When the number of burnInterval iteration is negative.</exception> /// <exception cref="ArgumentOutOfRangeException">When the scale of the slice sampler is not positive.</exception> public UnivariateSliceSampler(double x0, DensityLn <double> pdfLnP, int burnInterval, double scale) { _current = x0; _currentDensityLn = pdfLnP(x0); _pdfLnP = pdfLnP; Scale = scale; BurnInterval = burnInterval; Burn(BurnInterval); }
/// <summary> /// Constructs a new Hybrid Monte Carlo sampler. /// </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> /// <param name="burnInterval">The number of iterations in between returning samples.</param> /// <param name="randomSource">Random number generator used for sampling the momentum.</param> /// <param name="diff">The method used for differentiation.</param> /// <exception cref="ArgumentOutOfRangeException">When the number of burnInterval iteration is negative.</exception> /// <exception cref="ArgumentNullException">When either x0, pdfLnP or diff is null.</exception> protected HybridMCGeneric(T x0, DensityLn <T> pdfLnP, int frogLeapSteps, double stepSize, int burnInterval, Random randomSource, DiffMethod diff) { _energy = x => - pdfLnP(x); FrogLeapSteps = frogLeapSteps; StepSize = stepSize; BurnInterval = burnInterval; Current = x0; _diff = diff; RandomSource = randomSource; }
/// <summary> /// Constructs a new Metropolis sampler using the default <see cref="System.Random"/> random number generator. /// </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="proposal">A method that samples from the symmetric proposal distribution.</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 MetropolisSampler(T x0, DensityLn <T> pdfLnP, LocalProposalSampler <T> proposal, int burnInterval = 0) { _current = x0; _currentDensityLn = pdfLnP(x0); _pdfLnP = pdfLnP; _proposal = proposal; BurnInterval = burnInterval; Burn(BurnInterval); }
/// <summary> /// Constructs a new Metropolis-Hastings sampler using the default <see cref="System.Random"/> random number generator. This /// constructor will set the burn interval. /// </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="krnlQ">The log transition probability for the proposal distribution.</param> /// <param name="proposal">A method that samples from the proposal distribution.</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 MetropolisHastingsSampler(T x0, DensityLn <T> pdfLnP, TransitionKernelLn <T> krnlQ, LocalProposalSampler <T> proposal, int burnInterval) { mCurrent = x0; mCurrentDensityLn = pdfLnP(x0); mPdfLnP = pdfLnP; mKrnlQ = krnlQ; mProposal = proposal; BurnInterval = burnInterval; Burn(BurnInterval); }
public void SampleTest() { //Variances and Means double[] Sdv = new double[2]; double[] Mean = new double[2]; for (int i = 0; i < 2; i++) { Sdv[i] = RandomVar(); Mean[i] = 5 * rnd.NextDouble(); } //Cross Variance double rho = 1.0 - RandomVar(); DensityLn <double[]> pdfLn = new DensityLn <double[]>(x => LogDen(x, Sdv, Mean, rho)); HybridMC Hybrid = new HybridMC(new double[2] { 0, 0 }, pdfLn, 10, 0.1, 1000); Hybrid.BurnInterval = 0; int SampleSize = 10000; double[][] Sample = Hybrid.Sample(SampleSize); double[][] NewSamples = ArrangeSamples(SampleSize, Sample); double[] Convergence = new double[2]; double[] SampleMean = new double[2]; double[] SampleSdv = new double[2]; for (int i = 0; i < 2; i++) { Convergence[i] = 1 / Math.Sqrt(MCMCDiagnostics.EffectiveSize(Sample, x => x[i])); DescriptiveStatistics Stats = new DescriptiveStatistics(NewSamples[i]); SampleMean[i] = Stats.Mean; SampleSdv[i] = Stats.StandardDeviation; } double SampleRho = Correlation.Pearson(NewSamples[0], NewSamples[1]); for (int i = 0; i < 2; i++) { string index = i.ToString(); Assert.AreEqual(Mean[i], SampleMean[i], 10 * Convergence[i], index + "Mean"); Assert.AreEqual(SampleSdv[i] * SampleSdv[i], Sdv[i] * Sdv[i], 10 * Convergence[i], index + "Standard Deviation"); } double ConvergenceRho = 1 / Math.Sqrt(MCMCDiagnostics.EffectiveSize(Sample, x => (x[0] - SampleMean[0]) * (x[1] - SampleMean[1]))); Assert.AreEqual(SampleRho * SampleSdv[0] * SampleSdv[1], rho * Sdv[0] * Sdv[1], 10 * ConvergenceRho, "Rho"); }
/// <summary> /// The default method used for computing the gradient. Uses a simple three point estimation. /// </summary> /// <param name="function">Function which the gradient is to be evaluated.</param> /// <param name="x">The location where the gradient is to be evaluated.</param> /// <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++) { 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; } return(ReturnValue); }
public void SampleTest(double[] sdv, double[] mean, double rho, int seed) { var pdfLn = new DensityLn <double[]>(x => LogDen(x, sdv, mean, rho)); var hybrid = new HybridMC(new double[2] { 0, 0 }, pdfLn, 10, 0.1, 1000, new double[] { 1, 1 }, new System.Random(seed)) { BurnInterval = 0 }; const int sampleSize = 10000; double[][] sample = hybrid.Sample(sampleSize); double[][] newSamples = ArrangeSamples(sampleSize, sample); var convergence = new double[2]; var sampleMean = new double[2]; var sampleSdv = new double[2]; for (int i = 0; i < 2; i++) { convergence[i] = 1 / Math.Sqrt(MCMCDiagnostics.EffectiveSize(sample, x => x[i])); var stats = new DescriptiveStatistics(newSamples[i]); sampleMean[i] = stats.Mean; sampleSdv[i] = stats.StandardDeviation; } double sampleRho = Correlation.Pearson(newSamples[0], newSamples[1]); for (int i = 0; i < 2; i++) { string index = i.ToString(); Assert.AreEqual(mean[i], sampleMean[i], 10 * convergence[i], index + "Mean"); Assert.AreEqual(sampleSdv[i] * sampleSdv[i], sdv[i] * sdv[i], 10 * convergence[i], index + "Standard Deviation"); } double convergenceRho = 1 / Math.Sqrt(MCMCDiagnostics.EffectiveSize(sample, x => (x[0] - sampleMean[0]) * (x[1] - sampleMean[1]))); Assert.AreEqual(sampleRho * sampleSdv[0] * sampleSdv[1], rho * sdv[0] * sdv[1], 10 * convergenceRho, "Rho"); }
/// <summary> /// The default method used for computing the gradient. Uses a simple three point estimation. /// </summary> /// <param name="function">Function which the gradient is to be evaluated.</param> /// <param name="x">The location where the gradient is to be evaluated.</param> /// <returns>The gradient of the function at the point x.</returns> static double[] Grad(DensityLn <double[]> function, double[] x) { int length = x.Length; var returnValue = new double[length]; var increment = new double[length]; var decrement = new double[length]; Array.Copy(x, 0, increment, 0, length); Array.Copy(x, 0, decrement, 0, 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; } return(returnValue); }
public void SampleTest() { //Variances and Means double[] Sdv = new double[2]; double[] Mean = new double[2]; for (int i = 0; i < 2; i++) { Sdv[i] = RandomVar(); Mean[i] = 5 * rnd.NextDouble(); } //Cross Variance double rho = 1.0 - RandomVar(); DensityLn<double[]> pdfLn = new DensityLn<double[]>(x => LogDen(x, Sdv, Mean, rho)); HybridMC Hybrid = new HybridMC(new double[2] { 0, 0 }, pdfLn, 10, 0.1, 1000); Hybrid.BurnInterval = 0; int SampleSize = 10000; double[][] Sample = Hybrid.Sample(SampleSize); double[][] NewSamples = ArrangeSamples(SampleSize, Sample); double[] Convergence = new double[2]; double[] SampleMean = new double[2]; double[] SampleSdv = new double[2]; for (int i = 0; i < 2; i++) { Convergence[i] = 1 / Math.Sqrt(MCMCDiagnostics.EffectiveSize(Sample,x=>x[i])); DescriptiveStatistics Stats = new DescriptiveStatistics(NewSamples[i]); SampleMean[i] = Stats.Mean; SampleSdv[i] = Stats.StandardDeviation; } double SampleRho = Correlation.Pearson(NewSamples[0], NewSamples[1]); for (int i = 0; i < 2; i++) { string index = i.ToString(); Assert.AreEqual(Mean[i], SampleMean[i], 10 * Convergence[i], index + "Mean"); Assert.AreEqual(SampleSdv[i] * SampleSdv[i], Sdv[i] * Sdv[i], 10 * Convergence[i], index + "Standard Deviation"); } double ConvergenceRho=1/Math.Sqrt(MCMCDiagnostics.EffectiveSize(Sample,x=>(x[0]-SampleMean[0])*(x[1]-SampleMean[1]))); Assert.AreEqual(SampleRho*SampleSdv[0]*SampleSdv[1], rho*Sdv[0]*Sdv[1], 10 * ConvergenceRho, "Rho"); }
/// <summary> /// Constructs a new Metropolis-Hastings sampler using the default <see cref="System.Random"/> random /// number generator. The burn interval will be set to 0. /// </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="krnlQ">The log transition probability for the proposal distribution.</param> /// <param name="proposal">A method that samples from the proposal distribution.</param> public MetropolisHastingsSampler(T x0, DensityLn <T> pdfLnP, TransitionKernelLn <T> krnlQ, LocalProposalSampler <T> proposal) : this(x0, pdfLnP, krnlQ, proposal, 0) { }
/// <summary> /// 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. /// This constructor will set the burn interval. /// </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> /// <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 /// 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 DiffMethod(HybridMC.Grad)) { }
/// <summary> /// 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. /// This constructor will set the burn interval. /// </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> /// <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 /// 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, MersenneTwister.Default) { }
/// <summary> /// Constructs a new Hybrid Monte Carlo sampler for a univariate probability distribution. /// The burn interval will be set to 0. /// 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. /// </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 UnivariateHybridMC(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 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. /// </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> /// <param name="burnInterval">The number of iterations in between returning samples.</param> /// <param name="pSdv">The standard deviation of the normal distribution that is used to sample /// the momentum.</param> /// <param name="randomSource">Random number generator used to sample the momentum.</param> /// <exception cref="ArgumentOutOfRangeException">When the number of burnInterval iteration is negative.</exception> public UnivariateHybridMC(double x0, DensityLn <double> pdfLnP, int frogLeapSteps, double stepSize, int burnInterval, double pSdv, Random randomSource) : this(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, pSdv, randomSource, new DiffMethod(UnivariateHybridMC.Grad)) { }
/// <summary> /// Constructs a new Hybrid Monte Carlo sampler for a univariate probability distribution. /// 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. /// This constructor will set the burn interval. /// </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> /// <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 UnivariateHybridMC(double x0, DensityLn <double> pdfLnP, int frogLeapSteps, double stepSize, int burnInterval) : this(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, 1) { }
/// <summary> /// Constructs a new Slice sampler using the default <see cref="System.Random"/> random /// number generator. The burn interval will be set to 0. /// </summary> /// <param name="x0">The initial sample.</param> /// <param name="pdfLnP">The density of the distribution we want to sample from.</param> /// <param name="scale">The scale factor of the slice sampler.</param> /// <exception cref="ArgumentOutOfRangeException">When the scale of the slice sampler is not positive.</exception> public UnivariateSliceSampler(double x0, DensityLn <double> pdfLnP, double scale) : this(x0, pdfLnP, 0, scale) { }
/// <summary> /// 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 <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> /// <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> /// <param name="burnInterval">The number of iterations in between returning samples.</param> /// <param name="pSdv">The standard deviation of the normal distribution that is used to sample /// the momentum.</param> /// <exception cref="ArgumentOutOfRangeException">When the number of burnInterval iteration is negative.</exception> public UnivariateHybridMC(double x0, DensityLn <double> pdfLnP, int frogLeapSteps, double stepSize, int burnInterval = 0, double pSdv = 1) : this(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, pSdv, SystemRandomSource.Default) { }
public void SampleTest(double[] sdv, double[] mean, double rho, int seed) { var pdfLn = new DensityLn<double[]>(x => LogDen(x, sdv, mean, rho)); var hybrid = new HybridMC(new double[] { 0, 0 }, pdfLn, 10, 0.1, 1000, new double[] { 1, 1 }, new System.Random(seed)) { BurnInterval = 0 }; const int sampleSize = 10000; double[][] sample = hybrid.Sample(sampleSize); double[][] newSamples = ArrangeSamples(sampleSize, sample); var convergence = new double[2]; var sampleMean = new double[2]; var sampleSdv = new double[2]; for (int i = 0; i < 2; i++) { // ReSharper disable once AccessToModifiedClosure convergence[i] = 1/Math.Sqrt(MCMCDiagnostics.EffectiveSize(sample, x => x[i])); var stats = new DescriptiveStatistics(newSamples[i]); sampleMean[i] = stats.Mean; sampleSdv[i] = stats.StandardDeviation; } double sampleRho = Correlation.Pearson(newSamples[0], newSamples[1]); for (int i = 0; i < 2; i++) { string index = i.ToString(CultureInfo.InvariantCulture); Assert.AreEqual(mean[i], sampleMean[i], 10*convergence[i], index + "Mean"); Assert.AreEqual(sampleSdv[i]*sampleSdv[i], sdv[i]*sdv[i], 10*convergence[i], index + "Standard Deviation"); } double convergenceRho = 1/Math.Sqrt(MCMCDiagnostics.EffectiveSize(sample, x => (x[0] - sampleMean[0])*(x[1] - sampleMean[1]))); Assert.AreEqual(sampleRho*sampleSdv[0]*sampleSdv[1], rho*sdv[0]*sdv[1], 10*convergenceRho, "Rho"); }
/// <summary> /// 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. /// This constructor will set the burn interval. /// </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> /// <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 /// 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, Grad) { }
/// <summary> /// 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. /// This constructor will set the burn interval. /// </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> /// <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 /// 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, SystemRandomSource.Default) { }
/// <summary> /// Constructs a new Metropolis sampler using the default <see cref="System.Random"/> random /// number generator. The burnInterval interval will be set to 0. /// </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="proposal">A method that samples from the symmetric proposal distribution.</param> public MetropolisSampler(T x0, DensityLn <T> pdfLnP, LocalProposalSampler <T> proposal) : this(x0, pdfLnP, proposal, 0) { }
/// <summary> /// 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 /// 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) { }
/// <summary> /// 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. /// This constructor will set the burn interval. /// </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> /// <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 /// 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 Random()) { }