gsl_randist__levy.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* randist/levy.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 James Theiler, Brian Gough
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include "gsl__config.h"
  20. #include <math.h>
  21. #include "gsl_math.h"
  22. #include "gsl_rng.h"
  23. #include "gsl_randist.h"
  24. /* The stable Levy probability distributions have the form
  25. p(x) dx = (1/(2 pi)) \int dt exp(- it x - |c t|^alpha)
  26. with 0 < alpha <= 2.
  27. For alpha = 1, we get the Cauchy distribution
  28. For alpha = 2, we get the Gaussian distribution with sigma = sqrt(2) c.
  29. Fromn Chapter 5 of Bratley, Fox and Schrage "A Guide to
  30. Simulation". The original reference given there is,
  31. J.M. Chambers, C.L. Mallows and B. W. Stuck. "A method for
  32. simulating stable random variates". Journal of the American
  33. Statistical Association, JASA 71 340-344 (1976).
  34. */
  35. double
  36. gsl_ran_levy (const gsl_rng * r, const double c, const double alpha)
  37. {
  38. double u, v, t, s;
  39. u = M_PI * (gsl_rng_uniform_pos (r) - 0.5);
  40. if (alpha == 1) /* cauchy case */
  41. {
  42. t = tan (u);
  43. return c * t;
  44. }
  45. do
  46. {
  47. v = gsl_ran_exponential (r, 1.0);
  48. }
  49. while (v == 0);
  50. if (alpha == 2) /* gaussian case */
  51. {
  52. t = 2 * sin (u) * sqrt(v);
  53. return c * t;
  54. }
  55. /* general case */
  56. t = sin (alpha * u) / pow (cos (u), 1 / alpha);
  57. s = pow (cos ((1 - alpha) * u) / v, (1 - alpha) / alpha);
  58. return c * t * s;
  59. }
  60. /* The following routine for the skew-symmetric case was provided by
  61. Keith Briggs.
  62. The stable Levy probability distributions have the form
  63. 2*pi* p(x) dx
  64. = \int dt exp(mu*i*t-|sigma*t|^alpha*(1-i*beta*sign(t)*tan(pi*alpha/2))) for alpha!=1
  65. = \int dt exp(mu*i*t-|sigma*t|^alpha*(1+i*beta*sign(t)*2/pi*log(|t|))) for alpha==1
  66. with 0<alpha<=2, -1<=beta<=1, sigma>0.
  67. For beta=0, sigma=c, mu=0, we get gsl_ran_levy above.
  68. For alpha = 1, beta=0, we get the Lorentz distribution
  69. For alpha = 2, beta=0, we get the Gaussian distribution
  70. See A. Weron and R. Weron: Computer simulation of Lévy alpha-stable
  71. variables and processes, preprint Technical University of Wroclaw.
  72. http://www.im.pwr.wroc.pl/~hugo/Publications.html
  73. */
  74. double
  75. gsl_ran_levy_skew (const gsl_rng * r, const double c,
  76. const double alpha, const double beta)
  77. {
  78. double V, W, X;
  79. if (beta == 0) /* symmetric case */
  80. {
  81. return gsl_ran_levy (r, c, alpha);
  82. }
  83. V = M_PI * (gsl_rng_uniform_pos (r) - 0.5);
  84. do
  85. {
  86. W = gsl_ran_exponential (r, 1.0);
  87. }
  88. while (W == 0);
  89. if (alpha == 1)
  90. {
  91. X = ((M_PI_2 + beta * V) * tan (V) -
  92. beta * log (M_PI_2 * W * cos (V) / (M_PI_2 + beta * V))) / M_PI_2;
  93. return c * (X + beta * log (c) / M_PI_2);
  94. }
  95. else
  96. {
  97. double t = beta * tan (M_PI_2 * alpha);
  98. double B = atan (t) / alpha;
  99. double S = pow (1 + t * t, 1/(2 * alpha));
  100. X = S * sin (alpha * (V + B)) / pow (cos (V), 1 / alpha)
  101. * pow (cos (V - alpha * (V + B)) / W, (1 - alpha) / alpha);
  102. return c * X;
  103. }
  104. }