/// <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="Alpha"></param> /// <param name="Beta"></param> /// <returns></returns> public static double Sample(Generators.Generator Random, double Alpha, double Beta) { double betaVariate = BetaDistribution.Sample(Random, Alpha, Beta); double tmp = 1.0 - betaVariate; return(IsNearZero(tmp) ? double.PositiveInfinity : betaVariate / tmp); }
/// <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="Mu"></param> /// <param name="Sigma"></param> /// <returns></returns> public static double Sample(Generators.Generator Random, double Mu, double Sigma) { const double nm = 0; const double ns = 1; return(Math.Exp(NormalDistribution.Sample(Random, nm, ns) * Sigma + Mu)); }
/// <summary> /// Returns a sample from the distribution /// </summary> /// <param name="Random"></param> /// <param name="Sigma"></param> /// <returns></returns> public static double Sample(Generators.Generator Random, double Sigma) { const double mu = 0; double n1 = Square(NormalDistribution.Sample(Random, mu, Sigma)); double n2 = Square(NormalDistribution.Sample(Random, mu, Sigma)); return(Math.Sqrt(n1 + n2)); }
/// <summary> /// Returns a sample from the distribution /// </summary> /// <param name="Random"></param> /// <param name="Nu"></param> /// <returns></returns> public static double Sample(Generators.Generator Random, double Nu) { const double mu = 0; const double sigma = 0; double n = NormalDistribution.Sample(Random, mu, sigma); double c = ChiSquareDistribution.Sample(Random, Nu); return(n / Math.Sqrt(c / Nu)); }
/// <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> /// <param name="DefaultBeta"></param> /// <returns></returns> public static double Sample(Generators.Generator Random, double Alpha, double Beta, double DefaultBeta = 1) { double x = GammaDistribution.Sample(Random, Alpha, DefaultBeta); double t; do { t = (x + GammaDistribution.Sample(Random, Beta, DefaultBeta)); }while (IsNearZero(t)); return(x / t); }
/// <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) { double m = 0.0; double s = 1.0; double sum = 0.0; for (int i = 0; i < Alpha; i++) { sum += Square(NormalDistribution.Sample(Random, m, s)); } return(sum); }
/// <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="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="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="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> /// <returns></returns> public static double Sample(Generators.Generator Random, double Alpha) { throw new NotImplementedException(); }
/// <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(Alpha * Math.Pow(Math.E, ExponentialDistribution.Sample(Random, 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)); }
/// <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="Beta"></param> /// <returns></returns> public static int Sample(Generators.Generator Random, int Alpha, int Beta) { return(Random.NextInt(Alpha, Beta + 1)); }
/// <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) { 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> /// <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="Beta"></param> /// <returns></returns> public static double Sample(Generators.Generator Random, double Alpha, double Beta) { double x = BetaDistribution.Sample(Random, Alpha / 2, Beta / 2); return((Beta * x) / (Alpha * (1 - x))); }