GNU Octave 11.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
randgamma.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2006-2026 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26/* Original version written by Paul Kienzle distributed as free
27 software in the in the public domain. */
28
29/*
30
31double randg (a)
32void fill_randg (a,n,x)
33
34Generate a series of standard gamma distributions.
35
36See: G. Marsaglia and W. Tsang, "A simple method for generating gamma
37variables", ACM Transactions on Mathematical Software, 26(3), pp. 363-372,
382000.
39
40Needs the following defines:
41* NAN: value to return for Not-A-Number
42* RUNI: uniform generator on (0,1)
43* RNOR: normal generator
44* REXP: exponential generator, or -log(RUNI) if one isn't available
45* INFINITE: function to test whether a value is infinite
46
47Test using:
48 mean = a
49 variance = a
50 skewness = 2/sqrt(a)
51 kurtosis = 3 + 6/sqrt(a)
52
53Note that randg can be used to generate many distributions:
54
55gamma(a,b) for a>0, b>0 (from R)
56 r = b*randg(a)
57beta(a,b) for a>0, b>0
58 r1 = randg(a,1)
59 r = r1 / (r1 + randg(b,1))
60Erlang(a,n)
61 r = a*randg(n)
62chisq(df) for df>0
63 r = 2*randg(df/2)
64t(df) for 0<df<inf (use randn if df is infinite)
65 r = randn () / sqrt(2*randg(df/2)/df)
66F(n1,n2) for 0<n1, 0<n2
67 r1 = 2*randg(n1/2)/n1 or 1 if n1 is infinite
68 r2 = 2*randg(n2/2)/n2 or 1 if n2 is infinite
69 r = r1 / r2
70negative binonial (n, p) for n>0, 0<p<=1
71 r = randp((1-p)/p * randg(n))
72 (from R, citing Devroye(1986), Non-Uniform Random Variate Generation)
73non-central chisq(df,L), for df>=0 and L>0 (use chisq if L=0)
74 r = randp(L/2)
75 r(r>0) = 2*randg(r(r>0))
76 r(df>0) += 2*randg(df(df>0)/2)
77 (from R, citing formula 29.5b-c in Johnson, Kotz, Balkrishnan(1995))
78Dirichlet(a1,...,ak) for ai > 0
79 r = (randg(a1),...,randg(ak))
80 r = r / sum(r)
81 (from GSL, citing Law & Kelton(1991), Simulation Modeling and Analysis)
82*/
83
84#if defined (HAVE_CONFIG_H)
85# include "config.h"
86#endif
87
88#include <cmath>
89
90#include "lo-ieee.h"
91#include "mappers.h"
92#include "randgamma.h"
93#include "randmtzig.h"
94
96
97template <typename T> void rand_gamma (T a, octave_idx_type n, T *r)
98{
100 /* If a < 1, start by generating gamma (1+a) */
101 const T d = (a < 1. ? 1.+a : a) - 1./3.;
102 const T c = 1./std::sqrt (9.*d);
103
104 /* Handle invalid cases */
105 if (a <= 0 || math::isinf (a))
106 {
107 for (i=0; i < n; i++)
108 r[i] = numeric_limits<T>::NaN ();
109 return;
110 }
111
112 for (i=0; i < n; i++)
113 {
114 T x, xsq, v, u;
115 restart:
116 x = rand_normal<T> ();
117 v = (1+c*x);
118 v *= (v*v);
119 if (v <= 0)
120 goto restart; /* rare, so don't bother moving up */
121 u = rand_uniform<T> ();
122 xsq = x*x;
123 if (u >= 1.-0.0331*xsq*xsq && std::log (u) >= 0.5*xsq + d*(1-v+std::log (v)))
124 goto restart;
125 r[i] = d*v;
126 }
127 if (a < 1)
128 {
129 /* Use gamma(a) = gamma(1+a)*U^(1/a) */
130 /* Given REXP = -log(U) then U^(1/a) = exp(-REXP/a) */
131 for (i = 0; i < n; i++)
132 r[i] *= exp (-rand_exponential<T> () / a);
133 }
134}
135
136template OCTAVE_API void rand_gamma (double, octave_idx_type, double *);
137template OCTAVE_API void rand_gamma (float, octave_idx_type, float *);
138
139OCTAVE_END_NAMESPACE(octave)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define OCTAVE_API
Definition main.in.cc:55
void rand_gamma(T a, octave_idx_type n, T *r)
Definition randgamma.cc:97
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
F77_RET_T const F77_DBLE * x