GNU Octave 10.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-2025 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: Marsaglia G and Tsang W (2000), "A simple method for generating
37gamma variables", ACM Transactions on Mathematical Software 26(3) 363-372
38
39Needs the following defines:
40* NAN: value to return for Not-A-Number
41* RUNI: uniform generator on (0,1)
42* RNOR: normal generator
43* REXP: exponential generator, or -log(RUNI) if one isn't available
44* INFINITE: function to test whether a value is infinite
45
46Test using:
47 mean = a
48 variance = a
49 skewness = 2/sqrt(a)
50 kurtosis = 3 + 6/sqrt(a)
51
52Note that randg can be used to generate many distributions:
53
54gamma(a,b) for a>0, b>0 (from R)
55 r = b*randg(a)
56beta(a,b) for a>0, b>0
57 r1 = randg(a,1)
58 r = r1 / (r1 + randg(b,1))
59Erlang(a,n)
60 r = a*randg(n)
61chisq(df) for df>0
62 r = 2*randg(df/2)
63t(df) for 0<df<inf (use randn if df is infinite)
64 r = randn () / sqrt(2*randg(df/2)/df)
65F(n1,n2) for 0<n1, 0<n2
66 r1 = 2*randg(n1/2)/n1 or 1 if n1 is infinite
67 r2 = 2*randg(n2/2)/n2 or 1 if n2 is infinite
68 r = r1 / r2
69negative binonial (n, p) for n>0, 0<p<=1
70 r = randp((1-p)/p * randg(n))
71 (from R, citing Devroye(1986), Non-Uniform Random Variate Generation)
72non-central chisq(df,L), for df>=0 and L>0 (use chisq if L=0)
73 r = randp(L/2)
74 r(r>0) = 2*randg(r(r>0))
75 r(df>0) += 2*randg(df(df>0)/2)
76 (from R, citing formula 29.5b-c in Johnson, Kotz, Balkrishnan(1995))
77Dirichlet(a1,...,ak) for ai > 0
78 r = (randg(a1),...,randg(ak))
79 r = r / sum(r)
80 (from GSL, citing Law & Kelton(1991), Simulation Modeling and Analysis)
81*/
82
83#if defined (HAVE_CONFIG_H)
84# include "config.h"
85#endif
86
87#include <cmath>
88
89#include "lo-ieee.h"
90#include "lo-mappers.h"
91#include "randgamma.h"
92#include "randmtzig.h"
93
95
96template <typename T> void rand_gamma (T a, octave_idx_type n, T *r)
97{
99 /* If a < 1, start by generating gamma (1+a) */
100 const T d = (a < 1. ? 1.+a : a) - 1./3.;
101 const T c = 1./std::sqrt (9.*d);
102
103 /* Handle invalid cases */
104 if (a <= 0 || math::isinf (a))
105 {
106 for (i=0; i < n; i++)
107 r[i] = numeric_limits<T>::NaN ();
108 return;
109 }
110
111 for (i=0; i < n; i++)
112 {
113 T x, xsq, v, u;
114 restart:
115 x = rand_normal<T> ();
116 v = (1+c*x);
117 v *= (v*v);
118 if (v <= 0)
119 goto restart; /* rare, so don't bother moving up */
120 u = rand_uniform<T> ();
121 xsq = x*x;
122 if (u >= 1.-0.0331*xsq*xsq && std::log (u) >= 0.5*xsq + d*(1-v+std::log (v)))
123 goto restart;
124 r[i] = d*v;
125 }
126 if (a < 1)
127 {
128 /* Use gamma(a) = gamma(1+a)*U^(1/a) */
129 /* Given REXP = -log(U) then U^(1/a) = exp(-REXP/a) */
130 for (i = 0; i < n; i++)
131 r[i] *= exp (-rand_exponential<T> () / a);
132 }
133}
134
135template OCTAVE_API void rand_gamma (double, octave_idx_type, double *);
136template OCTAVE_API void rand_gamma (float, octave_idx_type, float *);
137
138OCTAVE_END_NAMESPACE(octave)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
F77_RET_T const F77_DBLE * x
#define OCTAVE_API
Definition main.in.cc:55
void rand_gamma(T a, octave_idx_type n, T *r)
Definition randgamma.cc:96