/// <summary> /// Generates a random vector of observations from the /// Inverse Gaussian distribution with the given parameters. /// </summary> /// /// <param name="mean">The mean parameter mu.</param> /// <param name="shape">The shape parameter lambda.</param> /// <param name="samples">The number of samples to generate.</param> /// <param name="result">The location where to store the samples.</param> /// /// <returns>An array of double values sampled from the inverse Gaussian distribution.</returns> /// public static double[] Random(double mean, double shape, int samples, double[] result) { var u = Accord.Math.Random.Generator.Random; NormalDistribution.Random(samples, result); for (int i = 0; i < result.Length; i++) { double v = result[i]; double y = v * v; double x = mean + (mean * mean * y) / (2 * shape) - (mean / (2 * shape)) * Math.Sqrt(4 * mean * shape * y + mean * mean * y * y); double t = u.NextDouble(); if (t <= (mean) / (mean + x)) { result[i] = x; } else { result[i] = (mean * mean) / x; } } return(result); }
/// <summary> /// Generates a random vector of observations from the /// Lognormal distribution with the given parameters. /// </summary> /// /// <param name="location">The distribution's location value.</param> /// <param name="shape">The distribution's shape deviation.</param> /// <param name="samples">The number of samples to generate.</param> /// <param name="result">The location where to store the samples.</param> /// <param name="source">The random number generator to use as a source of randomness. /// Default is to use <see cref="Accord.Math.Random.Generator.Random"/>.</param> /// /// <returns>An array of double values sampled from the specified Lognormal distribution.</returns> /// public static double[] Random(double location, double shape, int samples, double[] result, Random source) { NormalDistribution.Random(samples, result, source); for (int i = 0; i < result.Length; i++) { result[i] = Math.Exp(location + shape * result[i]); } return(result); }
/// <summary> /// Generates a random observation from the /// Inverse Gaussian distribution with the given parameters. /// </summary> /// /// <param name="mean">The mean parameter mu.</param> /// <param name="shape">The shape parameter lambda.</param> /// <param name="source">The random number generator to use as a source of randomness. /// Default is to use <see cref="Accord.Math.Random.Generator.Random"/>.</param> /// /// <returns>A random double value sampled from the specified Uniform distribution.</returns> /// public static double Random(double mean, double shape, Random source) { double v = NormalDistribution.Random(source); double y = v * v; double x = mean + (mean * mean * y) / (2 * shape) - (mean / (2 * shape)) * Math.Sqrt(4 * mean * shape * y + mean * mean * y * y); double t = source.NextDouble(); if (t <= (mean) / (mean + x)) { return(x); } return((mean * mean) / x); }
/// <summary> /// Generates a random observation from the /// Inverse Gaussian distribution with the given parameters. /// </summary> /// /// <param name="mean">The mean parameter mu.</param> /// <param name="shape">The shape parameter lambda.</param> /// /// <returns>A random double value sampled from the specified Uniform distribution.</returns> /// public static double Random(double mean, double shape) { var u = Accord.Math.Random.Generator.Random; double v = NormalDistribution.Random(); double y = v * v; double x = mean + (mean * mean * y) / (2 * shape) - (mean / (2 * shape)) * Math.Sqrt(4 * mean * shape * y + mean * mean * y * y); double t = u.NextDouble(); if (t <= (mean) / (mean + x)) { return(x); } return((mean * mean) / x); }
/// <summary> /// Random Gamma-distribution number generation /// based on Marsaglia's Simple Method (2000). /// </summary> /// public static double Marsaglia(double d, double c) { var rand = Accord.Math.Random.Generator.Random; // References: // // - Marsaglia, G. A Simple Method for Generating Gamma Variables, 2000 // while (true) { // 2. Generate v = (1+cx)^3 with x normal double x, t, v; do { x = NormalDistribution.Random(); t = (1.0 + c * x); v = t * t * t; } while (v <= 0); // 3. Generate uniform U double U = rand.NextDouble(); // 4. If U < 1-0.0331*x^4 return d*v. double x2 = x * x; if (U < 1 - 0.0331 * x2 * x2) { return(d * v); } // 5. If log(U) < 0.5*x^2 + d*(1-v+log(v)) return d*v. if (Math.Log(U) < 0.5 * x2 + d * (1.0 - v + Math.Log(v))) { return(d * v); } // 6. Goto step 2 } }
/// <summary> /// Generates a random observation from the /// Lognormal distribution with the given parameters. /// </summary> /// /// <param name="location">The distribution's location value.</param> /// <param name="shape">The distribution's shape deviation.</param> /// <param name="source">The random number generator to use as a source of randomness. /// Default is to use <see cref="Accord.Math.Random.Generator.Random"/>.</param> /// /// <returns>A random double value sampled from the specified Lognormal distribution.</returns> /// public static double Random(double location, double shape, Random source) { return(Math.Exp(location + shape * NormalDistribution.Random(source))); }