private static double IntegrateCdf(ISampler <double> sampler, double x, int trials) { var success = 0; for (var i = 0; i < trials; i++) { if (sampler.GetNext() <= x) { ++success; } } return((double)success / trials); }
public static double IntegrateMultivariateCdf(ISampler <double[]> sampler, double[] x, int trials) { var success = 0; for (var i = 0; i < trials; i++) { var l = sampler.GetNext(); if (LessThan(l, x)) { ++success; } } return((double)success / trials);
/// <summary> /// Draws <paramref name="n"/> values from the sampler and returns them as an array. /// </summary> /// <param name="sampler"> /// The sampler to draw observations from. /// </param> /// <param name="n"> /// The number of observations to draw. /// </param> /// <typeparam name="T"> /// The type of the values that are sampled. /// </typeparam> /// <returns> /// An array containing <paramref name="n"/> observations drawn from sampler. /// </returns> /// <exception cref="ArgumentNullException"> /// If <paramref name="sampler"/> is null. /// </exception> /// <exception cref="ArgumentException"> /// If <paramref name="n"/> is less than zero. /// </exception> public static T[] GetNext <T>(this ISampler <T> sampler, int n) { if (sampler is null) { throw new ArgumentNullException(nameof(sampler)); } if (n < 0) { throw new ArgumentException("value must be positive", nameof(n)); } var result = new T[n]; for (var i = 0; i < result.Length; i++) { result[i] = sampler.GetNext(); } return(result); }
public void TestGetNextArrayNull(ISampler <double> nullSampler) { Assert.Throws <ArgumentNullException>(() => _ = nullSampler.GetNext(1)); }