internal static void SamplesUnchecked(System.Random rnd, double[] values, double freedom) { // Use the simple method if the degrees of freedom is an integer anyway if (System.Math.Floor(freedom) == freedom && freedom < int.MaxValue) { var n = (int)freedom; var standard = new double[values.Length * n]; Normal.SamplesUnchecked(rnd, standard, 0.0, 1.0); CommonParallel.For(0, values.Length, 4096, (a, b) => { for (int i = a; i < b; i++) { int k = i * n; double sum = 0; for (int j = 0; j < n; j++) { sum += standard[k + j] * standard[k + j]; } values[i] = System.Math.Sqrt(sum); } }); return; } // Call the gamma function (see http://en.wikipedia.org/wiki/Gamma_distribution#Specializations // for a justification) Gamma.SamplesUnchecked(rnd, values, freedom / 2.0, .5); }
static void SamplesUnchecked(System.Random rnd, double[] values, double location, double scale, double freedom) { Gamma.SamplesUnchecked(rnd, values, 0.5 * freedom, 0.5); for (int i = 0; i < values.Length; i++) { values[i] = Normal.Sample(rnd, location, scale * System.Math.Sqrt(freedom / values[i])); } }
static void SamplesUnchecked(System.Random rnd, double[] values, double shape, double scale) { Gamma.SamplesUnchecked(rnd, values, shape, scale); CommonParallel.For(0, values.Length, 4096, (a, b) => { for (int i = a; i < b; i++) { values[i] = 1.0 / values[i]; } }); }
internal static void SamplesUnchecked(System.Random rnd, double[] values, double a, double b) { var y = new double[values.Length]; Gamma.SamplesUnchecked(rnd, values, a, 1.0); Gamma.SamplesUnchecked(rnd, y, b, 1.0); CommonParallel.For(0, values.Length, 4096, (aa, bb) => { for (int i = aa; i < bb; i++) { values[i] = values[i] / (values[i] + y[i]); } }); }
static IEnumerable <double> SamplesUnchecked(System.Random rnd, double shape, double scale) { return(Gamma.SamplesUnchecked(rnd, shape, scale).Select(z => 1.0 / z)); }
/// <summary> /// Fills an array with samples generated from the distribution. /// </summary> public void Samples(double[] values) { Gamma.SamplesUnchecked(_random, values, _shape, _rate); }