Module: Numo::GSL::Ran
- Included in:
- Rng
- Defined in:
- ext/numo/gsl/rng/gsl_rng.c,
ext/numo/gsl/ran/gsl_ran.c
Defined Under Namespace
Classes: Discrete
Instance Method Summary collapse
-
#bernoulli(p, [shape]) ⇒ Integer or UInt
This function returns either 0 or 1, the result of a Bernoulli trial with probability p.
-
#beta(a, b, [shape]) ⇒ Float or DFloat
This function returns a random variate from the beta distribution.
-
#binomial(p, n, [shape]) ⇒ Integer or UInt
This function returns a random integer from the binomial distribution, the number of successes in n independent trials with probability p.
-
#bivariate_gaussian(sigma_x, sigma_y, rho, [shape]) ⇒ Object
This function generates a pair of correlated Gaussian variates, with mean zero, correlation coefficient rho and standard deviations sigma_x and sigma_y in the x and y directions.
-
#cauchy(a, [shape]) ⇒ Float or DFloat
This function returns a random variate from the Cauchy distribution with scale parameter a.
-
#chisq(nu, [shape]) ⇒ Float or DFloat
This function returns a random variate from the chi-squared distribution with nu degrees of freedom.
-
#dir_2d([shape]) ⇒ Object
This function returns a random direction vector v = (x,y) in two dimensions.
-
#dir_2d_trig_method([shape]) ⇒ Object
This function returns a random direction vector v = (x,y) in two dimensions.
-
#dir_3d([shape]) ⇒ Array
This function returns a random direction vector v = (x,y,z) in three dimensions.
-
#dirichlet(alpha[]) ⇒ DFloat
This function returns an array of K random variates from a Dirichlet distribution of order K-1.
-
#exponential(mu, [shape]) ⇒ Float or DFloat
This function returns a random variate from the exponential distribution with mean mu.
-
#exppow(a, b, [shape]) ⇒ Float or DFloat
This function returns a random variate from the exponential power distribution with scale parameter a and exponent b.
-
#fdist(nu1, nu2, [shape]) ⇒ Float or DFloat
This function returns a random variate from the F-distribution with degrees of freedom nu1 and nu2.
-
#flat(a, b, [shape]) ⇒ Float or DFloat
This function returns a random variate from the flat (uniform) distribution from a to b.
-
#gamma(a, b, [shape]) ⇒ Float or DFloat
This function returns a random variate from the gamma distribution.
-
#gamma_knuth(a, b, [shape]) ⇒ Float or DFloat
This function returns a gamma variate using the algorithms from Knuth (vol 2).
-
#gaussian(sigma, [shape]) ⇒ Float or DFloat
This function returns a Gaussian random variate, with mean zero and standard deviation sigma.
-
#gaussian_ratio_method(sigma, [shape]) ⇒ Float or DFloat
This function computes a Gaussian random variate using the alternative Marsaglia-Tsang ziggurat and Kinderman-Monahan-Leva ratio methods.
-
#gaussian_tail(a, sigma, [shape]) ⇒ Float or DFloat
This function provides random variates from the upper tail of a Gaussian distribution with standard deviation sigma.
-
#gaussian_ziggurat(sigma, [shape]) ⇒ Float or DFloat
This function computes a Gaussian random variate using the alternative Marsaglia-Tsang ziggurat and Kinderman-Monahan-Leva ratio methods.
-
#geometric(p, [shape]) ⇒ Integer or UInt
This function returns a random integer from the geometric distribution, the number of independent trials with probability p until the first success.
-
#gumbel1(a, b, [shape]) ⇒ Float or DFloat
This function returns a random variate from the Type-1 Gumbel distribution.
-
#gumbel2(a, b, [shape]) ⇒ Float or DFloat
This function returns a random variate from the Type-2 Gumbel distribution.
-
#hypergeometric(n1, n2, t, [shape]) ⇒ Integer or UInt
This function returns a random integer from the hypergeometric distribution.
-
#landau([shape]) ⇒ Float or DFloat
This function returns a random variate from the Landau distribution.
-
#laplace(a, [shape]) ⇒ Float or DFloat
This function returns a random variate from the Laplace distribution with width a.
-
#levy(c, alpha, [shape]) ⇒ Float or DFloat
This function returns a random variate from the Levy symmetric stable distribution with scale c and exponent alpha.
-
#logarithmic(p, [shape]) ⇒ Integer or UInt
This function returns a random integer from the logarithmic distribution.
-
#logistic(a, [shape]) ⇒ Float or DFloat
This function returns a random variate from the logistic distribution.
-
#lognormal(zeta, sigma, [shape]) ⇒ Float or DFloat
This function returns a random variate from the lognormal distribution.
-
#multinomial(N, p[]) ⇒ UInt
This function computes a random sample n[] from the multinomial distribution formed by N trials from an underlying distribution p[K].
-
#pareto(a, b, [shape]) ⇒ Float or DFloat
This function returns a random variate from the Pareto distribution of order a.
-
#pascal(p, n, [shape]) ⇒ Integer or UInt
This function returns a random integer from the Pascal distribution.
-
#poisson(mu, [shape]) ⇒ Integer or UInt
This function returns a random integer from the Poisson distribution with mean mu.
-
#rayleigh(sigma, [shape]) ⇒ Float or DFloat
This function returns a random variate from the Rayleigh distribution with scale parameter sigma.
-
#rayleigh_tail(a, sigma, [shape]) ⇒ Float or DFloat
This function returns a random variate from the tail of the Rayleigh distribution with scale parameter sigma and a lower limit of a.
-
#tdist(nu, [shape]) ⇒ Float or DFloat
This function returns a random variate from the t-distribution.
-
#ugaussian([shape]) ⇒ Float or DFloat
These functions compute results for the unit Gaussian distribution.
-
#ugaussian_ratio_method([shape]) ⇒ Float or DFloat
These functions compute results for the unit Gaussian distribution.
-
#ugaussian_tail(a, [shape]) ⇒ Float or DFloat
These functions compute results for the tail of a unit Gaussian distribution.
-
#weibull(a, b, [shape]) ⇒ Float or DFloat
This function returns a random variate from the Weibull distribution.
Instance Method Details
#bernoulli(p, [shape]) ⇒ Integer or UInt
This function returns either 0 or 1, the result of a Bernoulli trial with probability p. The probability distribution for a Bernoulli trial is,
p(0) = 1 - p p(1) = p
4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 4352
static VALUE
ran_bernoulli(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
unsigned int *ptr;
VALUE v0;
double a0;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "11" , &v0, &vshape);
a0 = NUM2DBL(v0);
if (nargs == 1) {
return rb_float_new(gsl_ran_bernoulli(r , a0));
} else {
vna = create_new_narray(cUInt,vshape);
ptr = (unsigned int*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_bernoulli(r , a0);
}
return vna;
}
}
|
#beta(a, b, [shape]) ⇒ Float or DFloat
This function returns a random variate from the beta distribution. The distribution function is,
p(x) dx = [\Gamma(a+b) \over \Gamma(a) \Gamma(b)] x^[a-1] (1-x)^[b-1] dx
for $0 \le x \le 1$ 0 <= x <= 1.
3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 3776
static VALUE
ran_beta(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
VALUE v0;
VALUE v1;
double a0;
double a1;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "21" , &v0, &v1, &vshape);
a0 = NUM2DBL(v0);
a1 = NUM2DBL(v1);
if (nargs == 2) {
return rb_float_new(gsl_ran_beta(r , a0, a1));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_beta(r , a0, a1);
}
return vna;
}
}
|
#binomial(p, n, [shape]) ⇒ Integer or UInt
This function returns a random integer from the binomial distribution, the number of successes in n independent trials with probability p. The probability distribution for binomial variates is,
p(k) = [n! \over k! (n-k)! ] p^k (1-p)^[n-k]
for $0 \le k \le n$ 0 <= k <= n.
4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 4402
static VALUE
ran_binomial(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
unsigned int *ptr;
VALUE v0;
VALUE v1;
double a0;
unsigned int a1;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "21" , &v0, &v1, &vshape);
a0 = NUM2DBL(v0);
a1 = NUM2UINT(v1);
if (nargs == 2) {
return rb_float_new(gsl_ran_binomial(r , a0, a1));
} else {
vna = create_new_narray(cUInt,vshape);
ptr = (unsigned int*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_binomial(r , a0, a1);
}
return vna;
}
}
|
#bivariate_gaussian(sigma_x, sigma_y, rho, [shape]) ⇒ Object
This function generates a pair of correlated Gaussian variates, with mean zero, correlation coefficient rho and standard deviations sigma_x and sigma_y in the x and y directions. The probability distribution for bivariate Gaussian random variates is,
p(x,y) dx dy = [1 \over 2 \pi \sigma_x \sigma_y \sqrt[1-\rho^2]] \exp (-(x^2/\sigma_x^2 + y^2/\sigma_y^2 - 2 \rho x y/(\sigma_x\sigma_y))/2(1-\rho^2)) dx dy
for x,y in the range -\infty to +\infty. The correlation coefficient rho should lie between 1 and -1.
2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 2952
static VALUE
ran_bivariate_gaussian(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vx, vy;
size_t i, size;
int nargs;
double x, y;
double *px, *py;
VALUE v0;
VALUE v1;
VALUE v2;
double a0;
double a1;
double a2;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "31" , &v0, &v1, &v2, &vshape);
a0 = NUM2DBL(v0);
a1 = NUM2DBL(v1);
a2 = NUM2DBL(v2);
if (nargs == 3) {
gsl_ran_bivariate_gaussian(r , a0, a1, a2, &x, &y);
return rb_assoc_new(DBL2NUM(x),DBL2NUM(y));
} else {
vx = create_new_narray(cDF,vshape);
vy = create_new_narray(cDF,vshape);
px = (double*)na_get_pointer_for_write(vx);
py = (double*)na_get_pointer_for_write(vy);
size = RNARRAY_SIZE(vx);
for (i=0; i<size; i++) {
gsl_ran_bivariate_gaussian(r , a0, a1, a2, px, py);
px++; py++;
}
return rb_assoc_new(vx,vy);
}
}
|
#cauchy(a, [shape]) ⇒ Float or DFloat
This function returns a random variate from the Cauchy distribution with scale parameter a. The probability distribution for Cauchy random variates is,
p(x) dx = [1 \over a\pi (1 + (x/a)^2) ] dx
for x in the range -\infty to +\infty. The Cauchy distribution is also known as the Lorentz distribution.
3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 3162
static VALUE
ran_cauchy(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
VALUE v0;
double a0;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "11" , &v0, &vshape);
a0 = NUM2DBL(v0);
if (nargs == 1) {
return rb_float_new(gsl_ran_cauchy(r , a0));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_cauchy(r , a0);
}
return vna;
}
}
|
#chisq(nu, [shape]) ⇒ Float or DFloat
This function returns a random variate from the chi-squared distribution with nu degrees of freedom. The distribution function is,
p(x) dx = [1 \over 2 \Gamma(\nu/2) ] (x/2)^[\nu/2 - 1] \exp(-x/2) dx
for $x \ge 0$ x >= 0.
3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 3624
static VALUE
ran_chisq(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
VALUE v0;
double a0;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "11" , &v0, &vshape);
a0 = NUM2DBL(v0);
if (nargs == 1) {
return rb_float_new(gsl_ran_chisq(r , a0));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_chisq(r , a0);
}
return vna;
}
}
|
#dir_2d([shape]) ⇒ Object
This function returns a random direction vector v = (x,y) in two dimensions. The vector is normalized such that |v|^2 = x^2 + y^2 = 1. The obvious way to do this is to take a uniform random number between 0 and 2\pi and let x and y be the sine and cosine respectively. Two trig functions would have been expensive in the old days, but with modern hardware implementations, this is sometimes the fastest way to go. This is the case for the Pentium (but not the case for the Sun Sparcstation). One can avoid the trig evaluations by choosing x and y in the interior of a unit circle (choose them at random from the interior of the enclosing square, and then reject those that are outside the unit circle), and then dividing by $\sqrt+ y^2$ \sqrt[x^2 + y^2]. A much cleverer approach, attributed to von Neumann (See Knuth, v2, 3rd ed, p140, exercise 23), requires neither trig nor a square root. In this approach, u and v are chosen at random from the interior of a unit circle, and then x=(u^2-v^2)/(u^2+v^2) and y=2uv/(u^2+v^2).
3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 3936
static VALUE
ran_dir_2d(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vx, vy;
size_t i, size;
int nargs;
double x, y;
double *px, *py;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "01" , &vshape);
if (nargs == 0) {
gsl_ran_dir_2d(r , &x, &y);
return rb_assoc_new(DBL2NUM(x),DBL2NUM(y));
} else {
vx = create_new_narray(cDF,vshape);
vy = create_new_narray(cDF,vshape);
px = (double*)na_get_pointer_for_write(vx);
py = (double*)na_get_pointer_for_write(vy);
size = RNARRAY_SIZE(vx);
for (i=0; i<size; i++) {
gsl_ran_dir_2d(r , px, py);
px++; py++;
}
return rb_assoc_new(vx,vy);
}
}
|
#dir_2d_trig_method([shape]) ⇒ Object
This function returns a random direction vector v = (x,y) in two dimensions. The vector is normalized such that |v|^2 = x^2 + y^2 = 1. The obvious way to do this is to take a uniform random number between 0 and 2\pi and let x and y be the sine and cosine respectively. Two trig functions would have been expensive in the old days, but with modern hardware implementations, this is sometimes the fastest way to go. This is the case for the Pentium (but not the case for the Sun Sparcstation). One can avoid the trig evaluations by choosing x and y in the interior of a unit circle (choose them at random from the interior of the enclosing square, and then reject those that are outside the unit circle), and then dividing by $\sqrt+ y^2$ \sqrt[x^2 + y^2]. A much cleverer approach, attributed to von Neumann (See Knuth, v2, 3rd ed, p140, exercise 23), requires neither trig nor a square root. In this approach, u and v are chosen at random from the interior of a unit circle, and then x=(u^2-v^2)/(u^2+v^2) and y=2uv/(u^2+v^2).
3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 3996
static VALUE
ran_dir_2d_trig_method(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vx, vy;
size_t i, size;
int nargs;
double x, y;
double *px, *py;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "01" , &vshape);
if (nargs == 0) {
gsl_ran_dir_2d_trig_method(r , &x, &y);
return rb_assoc_new(DBL2NUM(x),DBL2NUM(y));
} else {
vx = create_new_narray(cDF,vshape);
vy = create_new_narray(cDF,vshape);
px = (double*)na_get_pointer_for_write(vx);
py = (double*)na_get_pointer_for_write(vy);
size = RNARRAY_SIZE(vx);
for (i=0; i<size; i++) {
gsl_ran_dir_2d_trig_method(r , px, py);
px++; py++;
}
return rb_assoc_new(vx,vy);
}
}
|
#dir_3d([shape]) ⇒ Array
This function returns a random direction vector v = (x,y,z) in three dimensions. The vector is normalized such that |v|^2 = x^2 + y^2 + z^2 = 1. The method employed is due to Robert E. Knop (CACM 13, 326 (1970)), and explained in Knuth, v2, 3rd ed, p136. It uses the surprising fact that the distribution projected along any axis is actually uniform (this is only true for 3 dimensions).
4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 4045
static VALUE
ran_dir_3d(int argc, VALUE *argv, VALUE self)
{
VALUE vshape;
VALUE v[3];
size_t i, size;
int nargs;
double x, y, z;
double *px, *py, *pz;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "01" , &vshape);
if (nargs == 0) {
gsl_ran_dir_3d(r , &x, &y, &z);
v[0] = DBL2NUM(x);
v[1] = DBL2NUM(y);
v[2] = DBL2NUM(z);
return rb_ary_new4(3,v);
} else {
v[0] = create_new_narray(cDF,vshape);
v[1] = create_new_narray(cDF,vshape);
v[2] = create_new_narray(cDF,vshape);
px = (double*)na_get_pointer_for_write(v[0]);
py = (double*)na_get_pointer_for_write(v[1]);
pz = (double*)na_get_pointer_for_write(v[2]);
size = RNARRAY_SIZE(v[0]);
for (i=0; i<size; i++) {
gsl_ran_dir_3d(r , px, py, pz);
px++; py++; pz++;
}
return rb_ary_new4(3,v);
}
}
|
#dirichlet(alpha[]) ⇒ DFloat
This function returns an array of K random variates from a Dirichlet distribution of order K-1. The distribution function is
p(\theta_1, …, \theta_K) d\theta_1 … d\theta_K = (1/Z) \prod_[i=1]^K \theta_i^[\alpha_i - 1] \delta(1 -\sum_[i=1]^K \theta_i) d\theta_1 … d\theta_K
for $\theta_i \ge 0$ theta_i >= 0 and $\alpha_i > 0$ alpha_i > 0. The delta function ensures that \sum \theta_i = 1. The normalization factor Z is
Z = [\prod_[i=1]^K \Gamma(\alpha_i)] / [\Gamma( \sum_[i=1]^K \alpha_i)]
The random variates are generated by sampling K values from gamma distributions with parameters $a=\alpha_i$, $b=1$ a=alpha_i, b=1, and renormalizing. See A.M. Law, W.D. Kelton, Simulation Modeling and Analysis (1991).
4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 4266
static VALUE
ran_dirichlet(VALUE self, VALUE valpha)
{
VALUE vtheta;
double *alpha, *theta;
narray_t *na;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
valpha = cast_1d_contiguous(valpha, cDF);
GetNArray(valpha,na);
vtheta = rb_narray_new(cDF,na->ndim,na->shape);
theta = (double*)na_get_pointer_for_write(vtheta);
alpha = (double*)na_get_pointer_for_read(valpha);
gsl_ran_dirichlet(r, na->size, alpha, theta);
RB_GC_GUARD(valpha);
return vtheta;
}
|
#exponential(mu, [shape]) ⇒ Float or DFloat
This function returns a random variate from the exponential distribution with mean mu. The distribution is,
p(x) dx = [1 \over \mu] \exp(-x/\mu) dx
for $x \ge 0$ x >= 0.
3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 3011
static VALUE
ran_exponential(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
VALUE v0;
double a0;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "11" , &v0, &vshape);
a0 = NUM2DBL(v0);
if (nargs == 1) {
return rb_float_new(gsl_ran_exponential(r , a0));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_exponential(r , a0);
}
return vna;
}
}
|
#exppow(a, b, [shape]) ⇒ Float or DFloat
This function returns a random variate from the exponential power distribution with scale parameter a and exponent b. The distribution is,
p(x) dx = [1 \over 2 a \Gamma(1+1/b)] \exp(- | x/a | ^b) dx |
for $x \ge 0$ x >= 0. For b = 1 this reduces to the Laplace distribution. For b = 2 it has the same form as a Gaussian distribution, but with $a = \sqrt2 \sigma$ a = \sqrt[2] \sigma.
3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 3110
static VALUE
ran_exppow(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
VALUE v0;
VALUE v1;
double a0;
double a1;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "21" , &v0, &v1, &vshape);
a0 = NUM2DBL(v0);
a1 = NUM2DBL(v1);
if (nargs == 2) {
return rb_float_new(gsl_ran_exppow(r , a0, a1));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_exppow(r , a0, a1);
}
return vna;
}
}
|
#fdist(nu1, nu2, [shape]) ⇒ Float or DFloat
This function returns a random variate from the F-distribution with degrees of freedom nu1 and nu2. The distribution function is,
p(x) dx = [ \Gamma((\nu_1 + \nu_2)/2) \over \Gamma(\nu_1/2) \Gamma(\nu_2/2) ] \nu_1^[\nu_1/2] \nu_2^[\nu_2/2] x^[\nu_1/2 - 1] (\nu_2 + \nu_1 x)^[-\nu_1/2 -\nu_2/2]
for $x \ge 0$ x >= 0.
3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 3676
static VALUE
ran_fdist(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
VALUE v0;
VALUE v1;
double a0;
double a1;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "21" , &v0, &v1, &vshape);
a0 = NUM2DBL(v0);
a1 = NUM2DBL(v1);
if (nargs == 2) {
return rb_float_new(gsl_ran_fdist(r , a0, a1));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_fdist(r , a0, a1);
}
return vna;
}
}
|
#flat(a, b, [shape]) ⇒ Float or DFloat
This function returns a random variate from the flat (uniform) distribution from a to b. The distribution is,
p(x) dx = [1 \over (b-a)] dx
if $a \le x < b$ a <= x < b and 0 otherwise.
3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 3522
static VALUE
ran_flat(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
VALUE v0;
VALUE v1;
double a0;
double a1;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "21" , &v0, &v1, &vshape);
a0 = NUM2DBL(v0);
a1 = NUM2DBL(v1);
if (nargs == 2) {
return rb_float_new(gsl_ran_flat(r , a0, a1));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_flat(r , a0, a1);
}
return vna;
}
}
|
#gamma(a, b, [shape]) ⇒ Float or DFloat
This function returns a random variate from the gamma distribution. The distribution function is,
p(x) dx = [1 \over \Gamma(a) b^a] x^[a-1] e^[-x/b] dx
for x > 0. If X and Y are independent gamma-distributed random variables of order a and b, then X+Y has a gamma distribution of order a+b.
The gamma distribution with an integer parameter a is known as the Erlang distribution.
The variates are computed using the Marsaglia-Tsang fast gamma method. This function for this method was previously called gsl_ran_gamma_mt and can still be accessed using this name.
3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 3424
static VALUE
ran_gamma(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
VALUE v0;
VALUE v1;
double a0;
double a1;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "21" , &v0, &v1, &vshape);
a0 = NUM2DBL(v0);
a1 = NUM2DBL(v1);
if (nargs == 2) {
return rb_float_new(gsl_ran_gamma(r , a0, a1));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_gamma(r , a0, a1);
}
return vna;
}
}
|
#gamma_knuth(a, b, [shape]) ⇒ Float or DFloat
This function returns a gamma variate using the algorithms from Knuth (vol 2).
3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 3470
static VALUE
ran_gamma_knuth(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
VALUE v0;
VALUE v1;
double a0;
double a1;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "21" , &v0, &v1, &vshape);
a0 = NUM2DBL(v0);
a1 = NUM2DBL(v1);
if (nargs == 2) {
return rb_float_new(gsl_ran_gamma_knuth(r , a0, a1));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_gamma_knuth(r , a0, a1);
}
return vna;
}
}
|
#gaussian(sigma, [shape]) ⇒ Float or DFloat
This function returns a Gaussian random variate, with mean zero and standard deviation sigma. The probability distribution for Gaussian random variates is,
p(x) dx = [1 \over \sqrt[2 \pi \sigma^2]] \exp (-x^2 / 2\sigma^2) dx
for x in the range -\infty to +\infty. Use the transformation z = \mu + x on the numbers returned by gsl_ran_gaussian to obtain a Gaussian distribution with mean \mu. This function uses the Box-Muller algorithm which requires two calls to the random number generator r.
2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 2627
static VALUE
ran_gaussian(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
VALUE v0;
double a0;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "11" , &v0, &vshape);
a0 = NUM2DBL(v0);
if (nargs == 1) {
return rb_float_new(gsl_ran_gaussian(r , a0));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_gaussian(r , a0);
}
return vna;
}
}
|
#gaussian_ratio_method(sigma, [shape]) ⇒ Float or DFloat
This function computes a Gaussian random variate using the alternative Marsaglia-Tsang ziggurat and Kinderman-Monahan-Leva ratio methods. The Ziggurat algorithm is the fastest available algorithm in most cases.
2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 2715
static VALUE
ran_gaussian_ratio_method(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
VALUE v0;
double a0;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "11" , &v0, &vshape);
a0 = NUM2DBL(v0);
if (nargs == 1) {
return rb_float_new(gsl_ran_gaussian_ratio_method(r , a0));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_gaussian_ratio_method(r , a0);
}
return vna;
}
}
|
#gaussian_tail(a, sigma, [shape]) ⇒ Float or DFloat
This function provides random variates from the upper tail of a Gaussian distribution with standard deviation sigma. The values returned are larger than the lower limit a, which must be positive. The method is based on Marsaglia’s famous rectangle-wedge-tail algorithm (Ann. Math. Stat. 32, 894–899 (1961)), with this aspect explained in Knuth, v2, 3rd ed, p139,586 (exercise 11).
The probability distribution for Gaussian tail random variates is,
p(x) dx = [1 \over N(a;\sigma) \sqrt[2 \pi \sigma^2]] \exp (- x^2/(2 \sigma^2)) dx
for x > a where N(a;\sigma) is the normalization constant,
N(a;\sigma) = (1/2) erfc(a / sqrt(2 sigma^2)).
2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 2852
static VALUE
ran_gaussian_tail(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
VALUE v0;
VALUE v1;
double a0;
double a1;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "21" , &v0, &v1, &vshape);
a0 = NUM2DBL(v0);
a1 = NUM2DBL(v1);
if (nargs == 2) {
return rb_float_new(gsl_ran_gaussian_tail(r , a0, a1));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_gaussian_tail(r , a0, a1);
}
return vna;
}
}
|
#gaussian_ziggurat(sigma, [shape]) ⇒ Float or DFloat
This function computes a Gaussian random variate using the alternative Marsaglia-Tsang ziggurat and Kinderman-Monahan-Leva ratio methods. The Ziggurat algorithm is the fastest available algorithm in most cases.
2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 2671
static VALUE
ran_gaussian_ziggurat(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
VALUE v0;
double a0;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "11" , &v0, &vshape);
a0 = NUM2DBL(v0);
if (nargs == 1) {
return rb_float_new(gsl_ran_gaussian_ziggurat(r , a0));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_gaussian_ziggurat(r , a0);
}
return vna;
}
}
|
#geometric(p, [shape]) ⇒ Integer or UInt
This function returns a random integer from the geometric distribution, the number of independent trials with probability p until the first success. The probability distribution for geometric variates is,
p(k) = p (1-p)^(k-1)
for $k \ge 1$ k >= 1. Note that the distribution begins with k=1 with this definition. There is another convention in which the exponent k-1 is replaced by k.
4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 4567
static VALUE
ran_geometric(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
unsigned int *ptr;
VALUE v0;
double a0;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "11" , &v0, &vshape);
a0 = NUM2DBL(v0);
if (nargs == 1) {
return rb_float_new(gsl_ran_geometric(r , a0));
} else {
vna = create_new_narray(cUInt,vshape);
ptr = (unsigned int*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_geometric(r , a0);
}
return vna;
}
}
|
#gumbel1(a, b, [shape]) ⇒ Float or DFloat
This function returns a random variate from the Type-1 Gumbel distribution. The Type-1 Gumbel distribution function is,
p(x) dx = a b \exp(-(b \exp(-ax) + ax)) dx
for -\infty < x < \infty.
4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 4153
static VALUE
ran_gumbel1(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
VALUE v0;
VALUE v1;
double a0;
double a1;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "21" , &v0, &v1, &vshape);
a0 = NUM2DBL(v0);
a1 = NUM2DBL(v1);
if (nargs == 2) {
return rb_float_new(gsl_ran_gumbel1(r , a0, a1));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_gumbel1(r , a0, a1);
}
return vna;
}
}
|
#gumbel2(a, b, [shape]) ⇒ Float or DFloat
This function returns a random variate from the Type-2 Gumbel distribution. The Type-2 Gumbel distribution function is,
p(x) dx = a b x^[-a-1] \exp(-b x^[-a]) dx
for 0 < x < \infty.
4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 4204
static VALUE
ran_gumbel2(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
VALUE v0;
VALUE v1;
double a0;
double a1;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "21" , &v0, &v1, &vshape);
a0 = NUM2DBL(v0);
a1 = NUM2DBL(v1);
if (nargs == 2) {
return rb_float_new(gsl_ran_gumbel2(r , a0, a1));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_gumbel2(r , a0, a1);
}
return vna;
}
}
|
#hypergeometric(n1, n2, t, [shape]) ⇒ Integer or UInt
This function returns a random integer from the hypergeometric distribution. The probability distribution for hypergeometric random variates is,
p(k) = C(n_1, k) C(n_2, t - k) / C(n_1 + n_2, t)
where C(a,b) = a!/(b!(a-b)!) and $t \leq n_1 + n_2$ t <= n_1 + n_2. The domain of k is $\hboxmax(0,t-n_2), \ldots, \hboxmin(t,n_1)$ max(0,t-n_2), …, min(t,n_1).
If a population contains n_1 elements of type 1'' and
n_2 elements of
type 2’’ then the hypergeometric
distribution gives the probability of obtaining k elements of
``type 1’’ in t samples from the population without
replacement.
4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 4627
static VALUE
ran_hypergeometric(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
unsigned int *ptr;
VALUE v0;
VALUE v1;
VALUE v2;
unsigned int a0;
unsigned int a1;
unsigned int a2;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "31" , &v0, &v1, &v2, &vshape);
a0 = NUM2UINT(v0);
a1 = NUM2UINT(v1);
a2 = NUM2UINT(v2);
if (nargs == 3) {
return rb_float_new(gsl_ran_hypergeometric(r , a0, a1, a2));
} else {
vna = create_new_narray(cUInt,vshape);
ptr = (unsigned int*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_hypergeometric(r , a0, a1, a2);
}
return vna;
}
}
|
#landau([shape]) ⇒ Float or DFloat
This function returns a random variate from the Landau distribution. The probability distribution for Landau random variates is defined analytically by the complex integral,
p(x) = (1/(2 \pi i)) \int_[c-i\infty]^[c+i\infty] ds exp(s log(s) + x s) For numerical purposes it is more convenient to use the following equivalent form of the integral,
p(x) = (1/\pi) \int_0^\infty dt \exp(-t \log(t) - x t) \sin(\pi t).
3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 3310
static VALUE
ran_landau(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "01" , &vshape);
if (nargs == 0) {
return rb_float_new(gsl_ran_landau(r ));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_landau(r );
}
return vna;
}
}
|
#laplace(a, [shape]) ⇒ Float or DFloat
This function returns a random variate from the Laplace distribution with width a. The distribution is,
p(x) dx = [1 \over 2 a] \exp(- | x/a | ) dx |
for -\infty < x < \infty.
3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 3058
static VALUE
ran_laplace(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
VALUE v0;
double a0;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "11" , &v0, &vshape);
a0 = NUM2DBL(v0);
if (nargs == 1) {
return rb_float_new(gsl_ran_laplace(r , a0));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_laplace(r , a0);
}
return vna;
}
}
|
#levy(c, alpha, [shape]) ⇒ Float or DFloat
This function returns a random variate from the Levy symmetric stable distribution with scale c and exponent alpha. The symmetric stable probability distribution is defined by a Fourier transform,
p(x) = [1 \over 2 \pi] \int_[-\infty]^[+\infty] dt \exp(-it x - | c t | ^alpha) |
There is no explicit solution for the form of p(x) and the library does not define a corresponding pdf function. For \alpha = 1 the distribution reduces to the Cauchy distribution. For \alpha = 2 it is a Gaussian distribution with $\sigma = \sqrt2 c$ \sigma = \sqrt[2] c. For \alpha < 1 the tails of the distribution become extremely wide.
The algorithm only works for $0 < \alpha \le 2$ 0 < alpha <= 2.
3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 3364
static VALUE
ran_levy(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
VALUE v0;
VALUE v1;
double a0;
double a1;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "21" , &v0, &v1, &vshape);
a0 = NUM2DBL(v0);
a1 = NUM2DBL(v1);
if (nargs == 2) {
return rb_float_new(gsl_ran_levy(r , a0, a1));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_levy(r , a0, a1);
}
return vna;
}
}
|
#logarithmic(p, [shape]) ⇒ Integer or UInt
This function returns a random integer from the logarithmic distribution. The probability distribution for logarithmic random variates is,
p(k) = [-1 \over \log(1-p)] [(p^k \over k)]
for $k \ge 1$ k >= 1.
4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 4682
static VALUE
ran_logarithmic(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
unsigned int *ptr;
VALUE v0;
double a0;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "11" , &v0, &vshape);
a0 = NUM2DBL(v0);
if (nargs == 1) {
return rb_float_new(gsl_ran_logarithmic(r , a0));
} else {
vna = create_new_narray(cUInt,vshape);
ptr = (unsigned int*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_logarithmic(r , a0);
}
return vna;
}
}
|
#logistic(a, [shape]) ⇒ Float or DFloat
This function returns a random variate from the logistic distribution. The distribution function is,
p(x) dx = [ \exp(-x/a) \over a (1 + \exp(-x/a))^2 ] dx
for -\infty < x < +\infty.
3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 3826
static VALUE
ran_logistic(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
VALUE v0;
double a0;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "11" , &v0, &vshape);
a0 = NUM2DBL(v0);
if (nargs == 1) {
return rb_float_new(gsl_ran_logistic(r , a0));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_logistic(r , a0);
}
return vna;
}
}
|
#lognormal(zeta, sigma, [shape]) ⇒ Float or DFloat
This function returns a random variate from the lognormal distribution. The distribution function is,
p(x) dx = [1 \over x \sqrt[2 \pi \sigma^2] ] \exp(-(\ln(x) - \zeta)^2/2 \sigma^2) dx
for x > 0.
3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 3573
static VALUE
ran_lognormal(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
VALUE v0;
VALUE v1;
double a0;
double a1;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "21" , &v0, &v1, &vshape);
a0 = NUM2DBL(v0);
a1 = NUM2DBL(v1);
if (nargs == 2) {
return rb_float_new(gsl_ran_lognormal(r , a0, a1));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_lognormal(r , a0, a1);
}
return vna;
}
}
|
#multinomial(N, p[]) ⇒ UInt
This function computes a random sample n[] from the multinomial distribution formed by N trials from an underlying distribution p[K]. The distribution function for n[] is,
P(n_1, n_2, …, n_K) = (N!/(n_1! n_2! … n_K!)) p_1^n_1 p_2^n_2 … p_K^n_K
where ($n_1$, $n_2$, $\ldots$, $n_K$) (n_1, n_2, …, n_K) are nonnegative integers with $\sum_k=1^K n_k =N$ sum_[k=1]^K n_k = N, and $(p_1, p_2, \ldots, p_K)$ (p_1, p_2, …, p_K) is a probability distribution with \sum p_i = 1. If the array p[K] is not normalized then its entries will be treated as weights and normalized appropriately. The arrays n[] and p[] must both be of length K.
Random variates are generated using the conditional binomial method (see C.S. Davis, The computer generation of multinomial random variates, Comp. Stat. Data Anal. 16 (1993) 205–217 for details).
4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 4469
static VALUE
ran_multinomial(VALUE self, VALUE vN, VALUE vp)
{
VALUE vn;
double *p;
unsigned int *n;
unsigned int N;
narray_t *na;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
N = NUM2UINT(vN);
vp = cast_1d_contiguous(vp, cDF);
GetNArray(vp,na);
p = (double*)na_get_pointer_for_read(vp);
vn = rb_narray_new(cUInt,na->ndim,na->shape);
n = (unsigned int*)na_get_pointer_for_write(vn);
gsl_ran_multinomial(r, na->size, N, p, n);
RB_GC_GUARD(vp);
return vn;
}
|
#pareto(a, b, [shape]) ⇒ Float or DFloat
This function returns a random variate from the Pareto distribution of order a. The distribution function is,
p(x) dx = (a/b) / (x/b)^[a+1] dx
for $x \ge b$ x >= b.
3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 3875
static VALUE
ran_pareto(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
VALUE v0;
VALUE v1;
double a0;
double a1;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "21" , &v0, &v1, &vshape);
a0 = NUM2DBL(v0);
a1 = NUM2DBL(v1);
if (nargs == 2) {
return rb_float_new(gsl_ran_pareto(r , a0, a1));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_pareto(r , a0, a1);
}
return vna;
}
}
|
#pascal(p, n, [shape]) ⇒ Integer or UInt
This function returns a random integer from the Pascal distribution. The Pascal distribution is simply a negative binomial distribution with an integer value of n.
p(k) = [(n + k - 1)! \over k! (n - 1)! ] p^n (1-p)^k
for $k \ge 0$ k >= 0
4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 4512
static VALUE
ran_pascal(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
unsigned int *ptr;
VALUE v0;
VALUE v1;
double a0;
unsigned int a1;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "21" , &v0, &v1, &vshape);
a0 = NUM2DBL(v0);
a1 = NUM2UINT(v1);
if (nargs == 2) {
return rb_float_new(gsl_ran_pascal(r , a0, a1));
} else {
vna = create_new_narray(cUInt,vshape);
ptr = (unsigned int*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_pascal(r , a0, a1);
}
return vna;
}
}
|
#poisson(mu, [shape]) ⇒ Integer or UInt
This function returns a random integer from the Poisson distribution with mean mu. The probability distribution for Poisson variates is,
p(k) = [\mu^k \over k!] \exp(-\mu)
for $k \ge 0$ k >= 0.
4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 4304
static VALUE
ran_poisson(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
unsigned int *ptr;
VALUE v0;
double a0;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "11" , &v0, &vshape);
a0 = NUM2DBL(v0);
if (nargs == 1) {
return rb_float_new(gsl_ran_poisson(r , a0));
} else {
vna = create_new_narray(cUInt,vshape);
ptr = (unsigned int*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_poisson(r , a0);
}
return vna;
}
}
|
#rayleigh(sigma, [shape]) ⇒ Float or DFloat
This function returns a random variate from the Rayleigh distribution with scale parameter sigma. The distribution is,
p(x) dx = [x \over \sigma^2] \exp(- x^2/(2 \sigma^2)) dx
for x > 0.
3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 3209
static VALUE
ran_rayleigh(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
VALUE v0;
double a0;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "11" , &v0, &vshape);
a0 = NUM2DBL(v0);
if (nargs == 1) {
return rb_float_new(gsl_ran_rayleigh(r , a0));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_rayleigh(r , a0);
}
return vna;
}
}
|
#rayleigh_tail(a, sigma, [shape]) ⇒ Float or DFloat
This function returns a random variate from the tail of the Rayleigh distribution with scale parameter sigma and a lower limit of a. The distribution is,
p(x) dx = [x \over \sigma^2] \exp ((a^2 - x^2) /(2 \sigma^2)) dx
for x > a.
3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 3258
static VALUE
ran_rayleigh_tail(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
VALUE v0;
VALUE v1;
double a0;
double a1;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "21" , &v0, &v1, &vshape);
a0 = NUM2DBL(v0);
a1 = NUM2DBL(v1);
if (nargs == 2) {
return rb_float_new(gsl_ran_rayleigh_tail(r , a0, a1));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_rayleigh_tail(r , a0, a1);
}
return vna;
}
}
|
#tdist(nu, [shape]) ⇒ Float or DFloat
This function returns a random variate from the t-distribution. The distribution function is,
p(x) dx = [\Gamma((\nu + 1)/2) \over \sqrt[\pi \nu] \Gamma(\nu/2)] (1 + x^2/\nu)^[-(\nu + 1)/2] dx
for -\infty < x < +\infty.
3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 3727
static VALUE
ran_tdist(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
VALUE v0;
double a0;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "11" , &v0, &vshape);
a0 = NUM2DBL(v0);
if (nargs == 1) {
return rb_float_new(gsl_ran_tdist(r , a0));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_tdist(r , a0);
}
return vna;
}
}
|
#ugaussian([shape]) ⇒ Float or DFloat
These functions compute results for the unit Gaussian distribution. They are equivalent to the functions above with a standard deviation of one, sigma = 1.
2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 2758
static VALUE
ran_ugaussian(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "01" , &vshape);
if (nargs == 0) {
return rb_float_new(gsl_ran_ugaussian(r ));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_ugaussian(r );
}
return vna;
}
}
|
#ugaussian_ratio_method([shape]) ⇒ Float or DFloat
These functions compute results for the unit Gaussian distribution. They are equivalent to the functions above with a standard deviation of one, sigma = 1.
2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 2798
static VALUE
ran_ugaussian_ratio_method(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "01" , &vshape);
if (nargs == 0) {
return rb_float_new(gsl_ran_ugaussian_ratio_method(r ));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_ugaussian_ratio_method(r );
}
return vna;
}
}
|
#ugaussian_tail(a, [shape]) ⇒ Float or DFloat
These functions compute results for the tail of a unit Gaussian distribution. They are equivalent to the functions above with a standard deviation of one, sigma = 1.
2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 2899
static VALUE
ran_ugaussian_tail(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
VALUE v0;
double a0;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "11" , &v0, &vshape);
a0 = NUM2DBL(v0);
if (nargs == 1) {
return rb_float_new(gsl_ran_ugaussian_tail(r , a0));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_ugaussian_tail(r , a0);
}
return vna;
}
}
|
#weibull(a, b, [shape]) ⇒ Float or DFloat
This function returns a random variate from the Weibull distribution. The distribution function is,
p(x) dx = [b \over a^b] x^[b-1] \exp(-(x/a)^b) dx
for $x \ge 0$ x >= 0.
4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 |
# File 'ext/numo/gsl/rng/gsl_rng.c', line 4102
static VALUE
ran_weibull(int argc, VALUE *argv, VALUE self)
{
VALUE vshape, vna;
size_t i, size;
int nargs;
double *ptr;
VALUE v0;
VALUE v1;
double a0;
double a1;
gsl_rng *r;
TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);
nargs = rb_scan_args(argc, argv, "21" , &v0, &v1, &vshape);
a0 = NUM2DBL(v0);
a1 = NUM2DBL(v1);
if (nargs == 2) {
return rb_float_new(gsl_ran_weibull(r , a0, a1));
} else {
vna = create_new_narray(cDF,vshape);
ptr = (double*)na_get_pointer_for_write(vna);
size = RNARRAY_SIZE(vna);
for (i=0; i<size; i++) {
ptr[i] = gsl_ran_weibull(r , a0, a1);
}
return vna;
}
}
|