@stdlib/stats-base-dists-gamma-logpdf 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/NOTICE CHANGED
@@ -1 +1 @@
1
- Copyright (c) 2016-2024 The Stdlib Authors.
1
+ Copyright (c) 2016-2026 The Stdlib Authors.
package/README.md CHANGED
@@ -152,21 +152,111 @@ y = mylogpdf( 4.0 );
152
152
  <!-- eslint no-undef: "error" -->
153
153
 
154
154
  ```javascript
155
- var randu = require( '@stdlib/random-base-randu' );
155
+ var uniform = require( '@stdlib/random-array-uniform' );
156
+ var logEachMap = require( '@stdlib/console-log-each-map' );
156
157
  var logpdf = require( '@stdlib/stats-base-dists-gamma-logpdf' );
157
158
 
158
- var alpha;
159
- var beta;
160
- var x;
161
- var y;
162
- var i;
163
-
164
- for ( i = 0; i < 10; i++ ) {
165
- x = randu() * 3.0;
166
- alpha = randu() * 5.0;
167
- beta = randu() * 5.0;
168
- y = logpdf( x, alpha, beta );
169
- console.log( 'x: %d, α: %d, β: %d, ln(f(x;α,β)): %d', x.toFixed( 4 ), alpha.toFixed( 4 ), beta.toFixed( 4 ), y.toFixed( 4 ) );
159
+ var opts = {
160
+ 'dtype': 'float64'
161
+ };
162
+ var x = uniform( 10, 0.0, 3.0, opts );
163
+ var alpha = uniform( 10, 0.0, 5.0, opts );
164
+ var beta = uniform( 10, 0.0, 5.0, opts );
165
+
166
+ logEachMap( 'x: %0.4f, α: %0.4f, β: %0.4f, ln(f(x;α,β)): %0.4f', x, alpha, beta, logpdf );
167
+ ```
168
+
169
+ </section>
170
+
171
+ <!-- /.examples -->
172
+
173
+ <!-- C interface documentation. -->
174
+
175
+ * * *
176
+
177
+ <section class="c">
178
+
179
+ ## C APIs
180
+
181
+ <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
182
+
183
+ <section class="intro">
184
+
185
+ </section>
186
+
187
+ <!-- /.intro -->
188
+
189
+ <!-- C usage documentation. -->
190
+
191
+ <section class="usage">
192
+
193
+ ### Usage
194
+
195
+ ```c
196
+ #include "stdlib/stats/base/dists/gamma/logpdf.h"
197
+ ```
198
+
199
+ #### stdlib_base_dists_gamma_logpdf( x, alpha, beta )
200
+
201
+ Evaluates the logarithm of the probability density function (PDF) for a gamma distribution with shape parameter `alpha` and rate parameter `beta` at a value `x`.
202
+
203
+ ```c
204
+ double out = stdlib_base_dists_gamma_logpdf( 2.0, 0.5, 1.0 );
205
+ // returns ~-2.919
206
+ ```
207
+
208
+ The function accepts the following arguments:
209
+
210
+ - **x**: `[in] double` input value.
211
+ - **mu**: `[in] double` shape parameter.
212
+ - **b**: `[in] double` rate parameter.
213
+
214
+ ```c
215
+ double stdlib_base_dists_gamma_logpdf( const double x, const double alpha, const double beta );
216
+ ```
217
+
218
+ </section>
219
+
220
+ <!-- /.usage -->
221
+
222
+ <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
223
+
224
+ <section class="notes">
225
+
226
+ </section>
227
+
228
+ <!-- /.notes -->
229
+
230
+ <!-- C API usage examples. -->
231
+
232
+ <section class="examples">
233
+
234
+ ### Examples
235
+
236
+ ```c
237
+ #include "stdlib/stats/base/dists/gamma/logpdf.h"
238
+ #include <stdlib.h>
239
+ #include <stdio.h>
240
+
241
+ static double random_uniform( const double min, const double max ) {
242
+ double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
243
+ return min + ( v*(max-min) );
244
+ }
245
+
246
+ int main( void ) {
247
+ double alpha;
248
+ double beta;
249
+ double x;
250
+ double y;
251
+ int i;
252
+
253
+ for ( i = 0; i < 25; i++ ) {
254
+ x = random_uniform( 0.0, 10.0 ) - 5.0;
255
+ alpha = random_uniform( 0.0, 20.0 );
256
+ beta = random_uniform( 0.0, 20.0 );
257
+ y = stdlib_base_dists_gamma_logpdf( x, alpha, beta );
258
+ printf( "x: %lf, α: %lf, β: %lf, ln(f(x;α,β)): %lf\n", x, alpha, beta, y );
259
+ }
170
260
  }
171
261
  ```
172
262
 
@@ -174,6 +264,10 @@ for ( i = 0; i < 10; i++ ) {
174
264
 
175
265
  <!-- /.examples -->
176
266
 
267
+ </section>
268
+
269
+ <!-- /.c -->
270
+
177
271
  <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
178
272
 
179
273
  <section class="related">
@@ -208,7 +302,7 @@ See [LICENSE][stdlib-license].
208
302
 
209
303
  ## Copyright
210
304
 
211
- Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
305
+ Copyright &copy; 2016-2026. The Stdlib [Authors][stdlib-authors].
212
306
 
213
307
  </section>
214
308
 
@@ -221,8 +315,8 @@ Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
221
315
  [npm-image]: http://img.shields.io/npm/v/@stdlib/stats-base-dists-gamma-logpdf.svg
222
316
  [npm-url]: https://npmjs.org/package/@stdlib/stats-base-dists-gamma-logpdf
223
317
 
224
- [test-image]: https://github.com/stdlib-js/stats-base-dists-gamma-logpdf/actions/workflows/test.yml/badge.svg?branch=v0.2.2
225
- [test-url]: https://github.com/stdlib-js/stats-base-dists-gamma-logpdf/actions/workflows/test.yml?query=branch:v0.2.2
318
+ [test-image]: https://github.com/stdlib-js/stats-base-dists-gamma-logpdf/actions/workflows/test.yml/badge.svg?branch=v0.3.0
319
+ [test-url]: https://github.com/stdlib-js/stats-base-dists-gamma-logpdf/actions/workflows/test.yml?query=branch:v0.3.0
226
320
 
227
321
  [coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/stats-base-dists-gamma-logpdf/main.svg
228
322
  [coverage-url]: https://codecov.io/github/stdlib-js/stats-base-dists-gamma-logpdf?branch=main
@@ -234,8 +328,8 @@ Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
234
328
 
235
329
  -->
236
330
 
237
- [chat-image]: https://img.shields.io/gitter/room/stdlib-js/stdlib.svg
238
- [chat-url]: https://app.gitter.im/#/room/#stdlib-js_stdlib:gitter.im
331
+ [chat-image]: https://img.shields.io/badge/zulip-join_chat-brightgreen.svg
332
+ [chat-url]: https://stdlib.zulipchat.com
239
333
 
240
334
  [stdlib]: https://github.com/stdlib-js/stdlib
241
335
 
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  /// <reference path="../docs/types/index.d.ts" />
2
- import logPDF from '../docs/types/index';
3
- export = logPDF;
2
+ import logpdf from '../docs/types/index';
3
+ export = logpdf;
@@ -113,9 +113,9 @@ interface LogPDF {
113
113
  * var y = mylogpdf( 2.0 );
114
114
  * // returns ~-3.646
115
115
  */
116
- declare var logPDF: LogPDF;
116
+ declare var logpdf: LogPDF;
117
117
 
118
118
 
119
119
  // EXPORTS //
120
120
 
121
- export = logPDF;
121
+ export = logpdf;
@@ -0,0 +1,38 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2025 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ #ifndef STDLIB_STATS_BASE_DISTS_GAMMA_LOGPDF_H
20
+ #define STDLIB_STATS_BASE_DISTS_GAMMA_LOGPDF_H
21
+
22
+ /*
23
+ * If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
24
+ */
25
+ #ifdef __cplusplus
26
+ extern "C" {
27
+ #endif
28
+
29
+ /**
30
+ * Evaluates the logarithm of the probability density function (PDF) for a gamma distribution with shape parameter `alpha` and rate parameter `beta` at a value `x`.
31
+ */
32
+ double stdlib_base_dists_gamma_logpdf( const double x, const double alpha, const double beta );
33
+
34
+ #ifdef __cplusplus
35
+ }
36
+ #endif
37
+
38
+ #endif // !STDLIB_STATS_BASE_DISTS_GAMMA_LOGPDF_H
package/lib/native.js ADDED
@@ -0,0 +1,78 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2025 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ 'use strict';
20
+
21
+ // MODULES //
22
+
23
+ var addon = require( './../src/addon.node' );
24
+
25
+
26
+ // MAIN //
27
+
28
+ /**
29
+ * Evaluates the logarithm of the probability density function (PDF) for a gamma distribution with shape parameter `alpha` and rate parameter `beta` at a value `x`.
30
+ *
31
+ * @private
32
+ * @param {number} x - input value
33
+ * @param {NonNegativeNumber} alpha - shape parameter
34
+ * @param {PositiveNumber} beta - rate parameter
35
+ * @returns {number} evaluated logPDF
36
+ *
37
+ * @example
38
+ * var y = logpdf( 2.0, 0.5, 1.0 );
39
+ * // returns ~-2.919
40
+ *
41
+ * @example
42
+ * var y = logpdf( 0.1, 1.0, 1.0 );
43
+ * // returns ~-0.1
44
+ *
45
+ * @example
46
+ * var y = logpdf( -1.0, 4.0, 2.0 );
47
+ * // returns -Infinity
48
+ *
49
+ * @example
50
+ * var y = logpdf( NaN, 0.6, 1.0 );
51
+ * // returns NaN
52
+ *
53
+ * @example
54
+ * var y = logpdf( 0.0, NaN, 1.0 );
55
+ * // returns NaN
56
+ *
57
+ * @example
58
+ * var y = logpdf( 0.0, 1.0, NaN );
59
+ * // returns NaN
60
+ *
61
+ * @example
62
+ * // Negative shape parameter:
63
+ * var y = logpdf( 2.0, -1.0, 1.0 );
64
+ * // returns NaN
65
+ *
66
+ * @example
67
+ * // Negative rate parameter:
68
+ * var y = logpdf( 2.0, 1.0, -1.0 );
69
+ * // returns NaN
70
+ */
71
+ function logpdf( x, alpha, beta ) {
72
+ return addon( x, alpha, beta );
73
+ }
74
+
75
+
76
+ // EXPORTS //
77
+
78
+ module.exports = logpdf;
package/manifest.json ADDED
@@ -0,0 +1,134 @@
1
+ {
2
+ "options": {
3
+ "task": "build",
4
+ "wasm": false
5
+ },
6
+ "fields": [
7
+ {
8
+ "field": "src",
9
+ "resolve": true,
10
+ "relative": true
11
+ },
12
+ {
13
+ "field": "include",
14
+ "resolve": true,
15
+ "relative": true
16
+ },
17
+ {
18
+ "field": "libraries",
19
+ "resolve": false,
20
+ "relative": false
21
+ },
22
+ {
23
+ "field": "libpath",
24
+ "resolve": true,
25
+ "relative": false
26
+ }
27
+ ],
28
+ "confs": [
29
+ {
30
+ "task": "build",
31
+ "wasm": false,
32
+ "src": [
33
+ "./src/main.c"
34
+ ],
35
+ "include": [
36
+ "./include"
37
+ ],
38
+ "libraries": [],
39
+ "libpath": [],
40
+ "dependencies": [
41
+ "@stdlib/math-base-napi-ternary",
42
+ "@stdlib/math-base-assert-is-nan",
43
+ "@stdlib/math-base-special-ln",
44
+ "@stdlib/constants-float64-pinf",
45
+ "@stdlib/constants-float64-ninf",
46
+ "@stdlib/math-base-special-gammaln",
47
+ "@stdlib/math-base-special-exp",
48
+ "@stdlib/constants-float64-max",
49
+ "@stdlib/math-base-special-gamma-lanczos-sum-expg-scaled",
50
+ "@stdlib/math-base-special-gamma",
51
+ "@stdlib/math-base-special-log1p",
52
+ "@stdlib/math-base-special-sqrt",
53
+ "@stdlib/math-base-special-abs",
54
+ "@stdlib/math-base-special-pow",
55
+ "@stdlib/math-base-special-max",
56
+ "@stdlib/math-base-special-min",
57
+ "@stdlib/math-base-special-ln",
58
+ "@stdlib/constants-float64-max-ln",
59
+ "@stdlib/constants-float64-min-ln",
60
+ "@stdlib/constants-float64-gamma-lanczos-g",
61
+ "@stdlib/constants-float64-e"
62
+ ]
63
+ },
64
+ {
65
+ "task": "benchmark",
66
+ "wasm": false,
67
+ "src": [
68
+ "./src/main.c"
69
+ ],
70
+ "include": [
71
+ "./include"
72
+ ],
73
+ "libraries": [],
74
+ "libpath": [],
75
+ "dependencies": [
76
+ "@stdlib/math-base-assert-is-nan",
77
+ "@stdlib/math-base-special-ln",
78
+ "@stdlib/constants-float64-pinf",
79
+ "@stdlib/constants-float64-ninf",
80
+ "@stdlib/math-base-special-gammaln",
81
+ "@stdlib/math-base-special-exp",
82
+ "@stdlib/constants-float64-max",
83
+ "@stdlib/math-base-special-gamma-lanczos-sum-expg-scaled",
84
+ "@stdlib/math-base-special-gamma",
85
+ "@stdlib/math-base-special-log1p",
86
+ "@stdlib/math-base-special-sqrt",
87
+ "@stdlib/math-base-special-abs",
88
+ "@stdlib/math-base-special-pow",
89
+ "@stdlib/math-base-special-max",
90
+ "@stdlib/math-base-special-min",
91
+ "@stdlib/math-base-special-ln",
92
+ "@stdlib/constants-float64-max-ln",
93
+ "@stdlib/constants-float64-min-ln",
94
+ "@stdlib/constants-float64-gamma-lanczos-g",
95
+ "@stdlib/constants-float64-e",
96
+ "@stdlib/constants-float64-eps"
97
+ ]
98
+ },
99
+ {
100
+ "task": "examples",
101
+ "wasm": false,
102
+ "src": [
103
+ "./src/main.c"
104
+ ],
105
+ "include": [
106
+ "./include"
107
+ ],
108
+ "libraries": [],
109
+ "libpath": [],
110
+ "dependencies": [
111
+ "@stdlib/math-base-assert-is-nan",
112
+ "@stdlib/math-base-special-ln",
113
+ "@stdlib/constants-float64-pinf",
114
+ "@stdlib/constants-float64-ninf",
115
+ "@stdlib/math-base-special-gammaln",
116
+ "@stdlib/math-base-special-exp",
117
+ "@stdlib/constants-float64-max",
118
+ "@stdlib/math-base-special-gamma-lanczos-sum-expg-scaled",
119
+ "@stdlib/math-base-special-gamma",
120
+ "@stdlib/math-base-special-log1p",
121
+ "@stdlib/math-base-special-sqrt",
122
+ "@stdlib/math-base-special-abs",
123
+ "@stdlib/math-base-special-pow",
124
+ "@stdlib/math-base-special-max",
125
+ "@stdlib/math-base-special-min",
126
+ "@stdlib/math-base-special-ln",
127
+ "@stdlib/constants-float64-max-ln",
128
+ "@stdlib/constants-float64-min-ln",
129
+ "@stdlib/constants-float64-gamma-lanczos-g",
130
+ "@stdlib/constants-float64-e"
131
+ ]
132
+ }
133
+ ]
134
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stdlib/stats-base-dists-gamma-logpdf",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "description": "Gamma distribution logarithm of probability density function (PDF).",
5
5
  "license": "Apache-2.0",
6
6
  "author": {
@@ -14,9 +14,12 @@
14
14
  }
15
15
  ],
16
16
  "main": "./lib",
17
+ "gypfile": false,
17
18
  "directories": {
18
19
  "doc": "./docs",
20
+ "include": "./include",
19
21
  "lib": "./lib",
22
+ "src": "./src",
20
23
  "dist": "./dist"
21
24
  },
22
25
  "types": "./docs/types",
@@ -38,20 +41,22 @@
38
41
  "@stdlib/constants-float64-ninf": "^0.2.2",
39
42
  "@stdlib/constants-float64-pinf": "^0.2.2",
40
43
  "@stdlib/math-base-assert-is-nan": "^0.2.2",
44
+ "@stdlib/math-base-napi-ternary": "^0.3.1",
41
45
  "@stdlib/math-base-special-abs": "^0.2.2",
42
46
  "@stdlib/math-base-special-exp": "^0.2.4",
43
47
  "@stdlib/math-base-special-gamma": "^0.3.0",
44
48
  "@stdlib/math-base-special-gamma-lanczos-sum-expg-scaled": "^0.3.0",
45
- "@stdlib/math-base-special-gammaln": "^0.2.2",
49
+ "@stdlib/math-base-special-gammaln": "^0.3.0",
46
50
  "@stdlib/math-base-special-ln": "^0.2.4",
47
51
  "@stdlib/math-base-special-log1p": "^0.2.3",
48
52
  "@stdlib/math-base-special-max": "^0.3.0",
49
53
  "@stdlib/math-base-special-min": "^0.2.3",
50
54
  "@stdlib/math-base-special-pow": "^0.3.0",
51
55
  "@stdlib/math-base-special-sqrt": "^0.2.2",
52
- "@stdlib/stats-base-dists-degenerate-logpdf": "^0.2.1",
56
+ "@stdlib/stats-base-dists-degenerate-logpdf": "^0.3.0",
53
57
  "@stdlib/utils-constant-function": "^0.2.2",
54
- "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2"
58
+ "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2",
59
+ "@stdlib/utils-library-manifest": "^0.2.3"
55
60
  },
56
61
  "devDependencies": {},
57
62
  "engines": {
package/src/addon.c ADDED
@@ -0,0 +1,23 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2025 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ #include "stdlib/stats/base/dists/gamma/logpdf.h"
20
+ #include "stdlib/math/base/napi/ternary.h"
21
+
22
+ // cppcheck-suppress shadowFunction
23
+ STDLIB_MATH_BASE_NAPI_MODULE_DDD_D( stdlib_base_dists_gamma_logpdf )
package/src/main.c ADDED
@@ -0,0 +1,192 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2025 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ #include "stdlib/math/base/assert/is_nan.h"
20
+ #include "stdlib/math/base/special/ln.h"
21
+ #include "stdlib/constants/float64/pinf.h"
22
+ #include "stdlib/constants/float64/ninf.h"
23
+ #include "stdlib/math/base/special/gammaln.h"
24
+ #include "stdlib/math/base/special/exp.h"
25
+ #include "stdlib/constants/float64/max.h"
26
+ #include "stdlib/math/base/special/gamma_lanczos_sum_expg_scaled.h"
27
+ #include "stdlib/math/base/special/gamma.h"
28
+ #include "stdlib/math/base/special/log1p.h"
29
+ #include "stdlib/math/base/special/sqrt.h"
30
+ #include "stdlib/math/base/special/abs.h"
31
+ #include "stdlib/math/base/special/pow.h"
32
+ #include "stdlib/math/base/special/max.h"
33
+ #include "stdlib/math/base/special/min.h"
34
+ #include "stdlib/math/base/special/ln.h"
35
+ #include "stdlib/constants/float64/max_ln.h"
36
+ #include "stdlib/constants/float64/min_ln.h"
37
+ #include "stdlib/constants/float64/gamma_lanczos_g.h"
38
+ #include "stdlib/constants/float64/e.h"
39
+
40
+ // BEGIN: regularised_gamma_prefix
41
+
42
+ /**
43
+ * Computes `(z^a)*(e^-z) / gamma(a)`.
44
+ *
45
+ * @param a input value
46
+ * @param z input value
47
+ * @return function value
48
+ */
49
+ static double regularised_gamma_prefix( const double a, const double z ) {
50
+ double prefix;
51
+ double amza;
52
+ double agh;
53
+ double alz;
54
+ double amz;
55
+ double sq;
56
+ double d;
57
+
58
+ agh = a + STDLIB_CONSTANT_FLOAT64_GAMMA_LANCZOS_G - 0.5;
59
+ d = ( (z - a) - STDLIB_CONSTANT_FLOAT64_GAMMA_LANCZOS_G + 0.5 ) / agh;
60
+ if ( a < 1.0 ) {
61
+ // Treat a < 1 as a special case because our Lanczos approximations are optimized against the factorials with a > 1, and for high precision types very small values of `a` can give rather erroneous results for gamma:
62
+ if ( z <= STDLIB_CONSTANT_FLOAT64_MIN_LN ) {
63
+ // Use logs, so should be free of cancellation errors:
64
+ return stdlib_base_exp( ( a * stdlib_base_ln(z) ) - z - stdlib_base_gammaln( a ) );
65
+ }
66
+ // No danger of overflow as gamma(a) < 1/a for small a, so direct calculation:
67
+ return stdlib_base_pow( z, a ) * stdlib_base_exp( -z ) / stdlib_base_gamma( a );
68
+ }
69
+ if ( stdlib_base_abs(d*d*a) <= 100.0 && a > 150.0 ) {
70
+ // Special case for large a and a ~ z:
71
+ prefix = ( a * ( stdlib_base_log1p( d ) - d ) ) + ( z * ( 0.5-STDLIB_CONSTANT_FLOAT64_GAMMA_LANCZOS_G ) / agh );
72
+ prefix = stdlib_base_exp( prefix );
73
+ }
74
+ else {
75
+ // General case. Direct computation is most accurate, but use various fallbacks for different parts of the problem domain:
76
+ alz = a * stdlib_base_ln(z / agh);
77
+ amz = a - z;
78
+ if (
79
+ stdlib_base_min(alz, amz) <= STDLIB_CONSTANT_FLOAT64_MIN_LN ||
80
+ stdlib_base_max(alz, amz) >= STDLIB_CONSTANT_FLOAT64_MAX_LN
81
+ ) {
82
+ amza = amz / a;
83
+ if (
84
+ stdlib_base_min(alz, amz)/2.0 > STDLIB_CONSTANT_FLOAT64_MIN_LN &&
85
+ stdlib_base_max(alz, amz)/2.0 < STDLIB_CONSTANT_FLOAT64_MAX_LN
86
+ ) {
87
+ // Compute square root of the result and then square it:
88
+ sq = stdlib_base_pow( z / agh, a / 2.0 ) * stdlib_base_exp( amz / 2.0 );
89
+ prefix = sq * sq;
90
+ }
91
+ else if (
92
+ stdlib_base_min(alz, amz)/4.0 > STDLIB_CONSTANT_FLOAT64_MIN_LN &&
93
+ stdlib_base_max(alz, amz)/4.0 < STDLIB_CONSTANT_FLOAT64_MAX_LN &&
94
+ z > a
95
+ ) {
96
+ // Compute the 4th root of the result then square it twice:
97
+ sq = stdlib_base_pow( z / agh, a / 4.0 ) * stdlib_base_exp( amz / 4.0 );
98
+ prefix = sq * sq;
99
+ prefix *= prefix;
100
+ }
101
+ else if (
102
+ amza > STDLIB_CONSTANT_FLOAT64_MIN_LN &&
103
+ amza < STDLIB_CONSTANT_FLOAT64_MAX_LN
104
+ ) {
105
+ prefix = stdlib_base_pow( (z * stdlib_base_exp(amza)) / agh, a );
106
+ }
107
+ else {
108
+ prefix = stdlib_base_exp( alz + amz );
109
+ }
110
+ }
111
+ else
112
+ {
113
+ prefix = stdlib_base_pow( z / agh, a ) * stdlib_base_exp( amz );
114
+ }
115
+ }
116
+ prefix *= stdlib_base_sqrt( agh / STDLIB_CONSTANT_FLOAT64_E ) / stdlib_base_gamma_lanczos_sum_expg_scaled( a );
117
+ return prefix;
118
+ }
119
+
120
+ // END: regularised_gamma_prefix
121
+
122
+ // BEGIN: gammma_p_derivative
123
+
124
+ /**
125
+ * Calculates the partial derivative with respect to x of the incomplete gamma function.
126
+ *
127
+ * @param a function parameter
128
+ * @param x function parameter
129
+ * @return function value
130
+ */
131
+ static double gamma_p_derivative( const double a, const double x ) {
132
+ double f1;
133
+ if ( a <= 0.0 ) {
134
+ return 0.0/0.0; // NaN
135
+ }
136
+ if ( x < 0.0 ) {
137
+ return 0.0/0.0; // NaN
138
+ }
139
+ if ( x == 0.0 ) {
140
+ if ( a > 1.0 ) {
141
+ return 0.0;
142
+ }
143
+ return ( a == 1.0 ) ? 1.0 : STDLIB_CONSTANT_FLOAT64_PINF;
144
+ }
145
+ f1 = regularised_gamma_prefix( a, x );
146
+ if ( x < 1.0 && ( STDLIB_CONSTANT_FLOAT64_MAX * x < f1 ) ) {
147
+ return STDLIB_CONSTANT_FLOAT64_PINF;
148
+ }
149
+ if ( f1 == 0.0 ) {
150
+ // Underflow in calculation, use logs instead:
151
+ f1 = (a * stdlib_base_ln( x )) - x - stdlib_base_gammaln( a ) - stdlib_base_ln( x );
152
+ f1 = stdlib_base_exp( f1 );
153
+ } else {
154
+ f1 /= x;
155
+ }
156
+ return f1;
157
+ }
158
+
159
+ // END: gammma_p_derivative
160
+
161
+ /**
162
+ * Evaluates the logarithm of the probability density function (PDF) for a gamma distribution with shape parameter `alpha` and rate parameter `beta` at a value `x`.
163
+ *
164
+ * @param x input value
165
+ * @param alpha shape parameter
166
+ * @param beta rate parameter
167
+ * @return evaluated logPDF
168
+ *
169
+ * @example
170
+ * double y = stdlib_base_dists_gamma_logpdf( 2.0, 0.5, 1.0 );
171
+ * // returns ~-2.919
172
+ */
173
+ double stdlib_base_dists_gamma_logpdf( const double x, const double alpha, const double beta ) {
174
+ if (
175
+ stdlib_base_is_nan( x ) ||
176
+ stdlib_base_is_nan( alpha ) ||
177
+ stdlib_base_is_nan( beta ) ||
178
+ alpha < 0.0 ||
179
+ beta <= 0.0
180
+ ) {
181
+ return 0.0/0.0; // NaN
182
+ }
183
+ if ( x < 0.0 || x == STDLIB_CONSTANT_FLOAT64_PINF ) {
184
+ return STDLIB_CONSTANT_FLOAT64_NINF;
185
+ }
186
+ if ( alpha == 0.0 ) {
187
+ // Point mass at 0...
188
+ return ( x == 0.0 ) ? STDLIB_CONSTANT_FLOAT64_PINF : STDLIB_CONSTANT_FLOAT64_NINF;
189
+ }
190
+ return stdlib_base_ln( gamma_p_derivative( alpha, x * beta ) ) + stdlib_base_ln( beta );
191
+ }
192
+