/// <summary> /// Returns a sample from the distribution /// </summary> /// <param name="Random"></param> /// <param name="Alpha"></param> /// <param name="Lambda"></param> /// <returns></returns> public static double Sample(Generators.Generator Random, double Alpha, double Lambda) { if (double.IsPositiveInfinity(Lambda)) { return(Alpha); } const double Mu = 0; const double Sigma = 1; double a = Alpha; double alphaFix = 1; if (Alpha < 1.0) { a = Alpha + 1; alphaFix = Math.Pow(Random.NextDouble(), 1.0 / Alpha); } double d = a - (1.0 / 3.0); double c = 1.0 / Math.Sqrt(9 * d); double x, v, u; while (true) { x = NormalDistribution.Sample(Random, Mu, Sigma); v = 1 + (c * x); while (v <= 0) { x = NormalDistribution.Sample(Random, Mu, Sigma); v = 1 + (c * x); } v = v * v * v; u = Random.NextDouble(); x = x * x; if (u < 1.0 - (.0331 * x * x)) { return(alphaFix * d * v / Lambda); } if (Math.Log(u) < (.5 * x) + (d * (1.0 - v + Math.Log(v)))) { return(alphaFix * d * v / Lambda); } } }
/// <summary> /// Returns a sample from the distribution /// </summary> /// <param name="Random"></param> /// <param name="Alpha"></param> /// <param name="Mu"></param> /// <returns></returns> public static double Sample(Generators.Generator Random, double Alpha, double Mu) { double rand = .5 - Random.NextDouble(); double tmp = IsNearZero(rand) ? double.NegativeInfinity : Math.Log(2.0 * Math.Abs(rand)); return(Mu - Alpha * Math.Sign(rand) * tmp); }
/// <summary> /// Returns a sample from the distribution /// </summary> /// <param name="Random"></param> /// <param name="cdf"></param> /// <returns></returns> public static double Sample(Generators.Generator Random, double[] cdf) { double u = Random.NextDouble(); int minIndex = 0; int MaxIndex = cdf.Length - 1; int Index; double c; while (minIndex < MaxIndex) { Index = (MaxIndex - minIndex) / 2 + minIndex; c = cdf[Index]; if (AreEqual(u, c)) { minIndex = Index; break; } if (u < c) { MaxIndex = Index; } else { minIndex = Index + 1; } } return(minIndex); }
/// <summary> /// Returns a sample from the distribution /// </summary> /// <param name="Random"></param> /// <param name="Mu"></param> /// <param name="Sigma"></param> /// <returns></returns> public static double Sample(Generators.Generator Random, double Mu, double Sigma) { double u; do { u = Random.NextDouble(); }while (IsNearZero(u * (1 - u))); return(Mu + Sigma * Math.Log(u / (1 - u))); }
/// <summary> /// Returns a sample from the distribution /// </summary> /// <param name="Random"></param> /// <param name="Lambda"></param> /// <returns></returns> public static double Sample(Generators.Generator Random, double Lambda) { double u; do { u = Random.NextDouble(); }while (IsNearZero(u)); return(-Math.Log(u) / Lambda); }
/// <summary> /// Returns a sample from the distribution /// </summary> /// <param name="Random"></param> /// <param name="Alpha"></param> /// <param name="Beta"></param> /// <returns></returns> public static double Sample(Generators.Generator Random, double Alpha, double Beta) { int successes = 0; for (int i = 0; i < Beta; i++) { if (Random.NextDouble() < Alpha) { successes++; } } return(successes); }
/// <summary> /// Returns a sample from the distribution /// </summary> /// <param name="Random"></param> /// <param name="Alpha"></param> /// <param name="Beta"></param> /// <param name="Gamma"></param> /// <returns></returns> public static double Sample(Generators.Generator Random, double Alpha, double Beta, double Gamma) { double helper1 = Gamma - Alpha; double helper2 = Beta - Alpha; double helper3 = Math.Sqrt(helper1 * helper2); double helper4 = Math.Sqrt(Beta - Gamma); double genNum = Random.NextDouble(); if (genNum <= helper1 / helper2) { return(Alpha + Math.Sqrt(genNum) * helper3); } return(Beta - Math.Sqrt(genNum * helper2 - helper1) * helper4); }
/// <summary> /// Returns a sample from the distribution /// </summary> /// <param name="Random"></param> /// <param name="Lambda"></param> /// <returns></returns> public static double Sample(Generators.Generator Random, double Lambda) { const int step = 500; double k = 0; double p = 1; double r; do { k++; while (IsNearZero(r = Random.NextDouble())) { } p = p * r; if (p < Math.E && Lambda > 0) { p = p * Math.Exp(Lambda > step ? step : Lambda); Lambda -= step; } }while (p > 1); return(k - 1); }
/// <summary> /// Returns a sample from the distribution /// </summary> /// <param name="Random"></param> /// <param name="Alpha"></param> /// <param name="Lambda"></param> /// <returns></returns> public static double Sample(Generators.Generator Random, double Alpha, double Lambda) { double helper1 = 1 / Alpha; return(Lambda * Math.Pow(-Math.Log(1.0 - Random.NextDouble()), helper1)); }
/// <summary> /// Returns a sample from the distribution /// </summary> /// <param name="Random"></param> /// <param name="Alpha"></param> /// <param name="Mu"></param> /// <returns></returns> public static double Sample(Generators.Generator Random, double Alpha, double Mu) { return(Mu - Alpha * Math.Log(-Math.Log(1.0 - Random.NextDouble()))); }
/// <summary> /// Returns a sample from the distribution /// </summary> /// <param name="Random"></param> /// <param name="Alpha"></param> /// <param name="Beta"></param> /// <returns></returns> public static double Sample(Generators.Generator Random, double Alpha, double Beta) { double helper1 = 1 / Alpha; return(Math.Pow(Random.NextDouble(), helper1) / Beta); }
/// <summary> /// Returns a sample from the distribution /// </summary> /// <param name="Random"></param> /// <param name="Alpha"></param> /// <returns></returns> public static double Sample(Generators.Generator Random, double Alpha) { return(Random.NextDouble() < Alpha ? 1 : 0); }
/// <summary> /// Returns a sample from the distribution /// </summary> /// <param name="Random"></param> /// <param name="Alpha"></param> /// <param name="Beta"></param> /// <returns></returns> public static double Sample(Generators.Generator Random, double Alpha, double Beta) { return(Random.NextDouble(Alpha, Beta)); }
/// <summary> /// Returns a sample from the distribution /// </summary> /// <param name="Random"></param> /// <param name="Alpha"></param> /// <param name="Gamma"></param> /// <returns></returns> public static double Sample(Generators.Generator Random, double Alpha, double Gamma) { return Alpha + Gamma * Math.Tan(Math.PI * (Random.NextDouble() - .5)); }