@stdlib/stats-base-dists-beta-pdf 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
@@ -155,22 +155,114 @@ y = mypdf( 0.3 );
155
155
  <!-- eslint no-undef: "error" -->
156
156
 
157
157
  ```javascript
158
- var randu = require( '@stdlib/random-base-randu' );
158
+ var uniform = require( '@stdlib/random-array-uniform' );
159
+ var logEachMap = require( '@stdlib/console-log-each-map' );
159
160
  var EPS = require( '@stdlib/constants-float64-eps' );
160
161
  var pdf = require( '@stdlib/stats-base-dists-beta-pdf' );
161
162
 
162
- var alpha;
163
- var beta;
164
- var x;
165
- var y;
166
- var i;
167
-
168
- for ( i = 0; i < 10; i++ ) {
169
- x = randu();
170
- alpha = ( randu()*5.0 ) + EPS;
171
- beta = ( randu()*5.0 ) + EPS;
172
- y = pdf( x, alpha, beta );
173
- console.log( 'x: %d, α: %d, β: %d, f(x;α,β): %d', x.toFixed( 4 ), alpha.toFixed( 4 ), beta.toFixed( 4 ), y.toFixed( 4 ) );
163
+ var opts = {
164
+ 'dtype': 'float64'
165
+ };
166
+ var alpha = uniform( 10, EPS, 5.0, opts );
167
+ var beta = uniform( 10, EPS, 5.0, opts );
168
+ var x = uniform( 10, 0.0, 1.0, opts );
169
+
170
+ logEachMap( 'x: %0.4f, α: %0.4f, β: %0.4f, f(x;α,β): %0.4f', x, alpha, beta, pdf );
171
+ ```
172
+
173
+ </section>
174
+
175
+ <!-- /.examples -->
176
+
177
+ <!-- C interface documentation. -->
178
+
179
+ * * *
180
+
181
+ <section class="c">
182
+
183
+ ## C APIs
184
+
185
+ <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
186
+
187
+ <section class="intro">
188
+
189
+ </section>
190
+
191
+ <!-- /.intro -->
192
+
193
+ <!-- C usage documentation. -->
194
+
195
+ <section class="usage">
196
+
197
+ ### Usage
198
+
199
+ ```c
200
+ #include "stdlib/stats/base/dists/beta/pdf.h"
201
+ ```
202
+
203
+ #### stdlib_base_dists_beta_pdf( x, alpha, beta )
204
+
205
+ Evaluates the probability density function (PDF) for a beta distribution with first shape parameter `alpha` and second shape parameter `beta`.
206
+
207
+ ```c
208
+ double y = stdlib_base_dists_beta_pdf( 0.5, 1.0, 1.0 );
209
+ // returns 1.0
210
+ ```
211
+
212
+ The function accepts the following arguments:
213
+
214
+ - **x**: `[in] double` input value.
215
+ - **alpha**: `[in] double` first shape parameter.
216
+ - **beta**: `[in] double` second shape parameter.
217
+
218
+ ```c
219
+ double stdlib_base_dists_beta_pdf( const double x, double alpha, const double beta );
220
+ ```
221
+
222
+ </section>
223
+
224
+ <!-- /.usage -->
225
+
226
+ <!-- C API usage notes. Make sure to keep an empty line after the `section`
227
+ element and another before the `/section` close. -->
228
+
229
+ <section class="notes">
230
+
231
+ </section>
232
+
233
+ <!-- /.notes -->
234
+
235
+ <!-- C API usage examples. -->
236
+
237
+ <section class="examples">
238
+
239
+ ### Examples
240
+
241
+ ```c
242
+ #include "stdlib/stats/base/dists/beta/pdf.h"
243
+ #include "stdlib/constants/float64/eps.h"
244
+ #include <stdlib.h>
245
+ #include <stdio.h>
246
+
247
+ static double random_uniform( const double min, const double max ) {
248
+ double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
249
+ return min + ( v*(max-min) );
250
+ }
251
+
252
+ int main( void ) {
253
+ double alpha;
254
+ double beta;
255
+ double x;
256
+ double y;
257
+ int i;
258
+
259
+ for ( i = 0; i < 10; i++ ) {
260
+ x = random_uniform( 0.0, 1.0 );
261
+ alpha = random_uniform( STDLIB_CONSTANT_FLOAT64_EPS, 5.0 );
262
+ beta = random_uniform( STDLIB_CONSTANT_FLOAT64_EPS, 5.0 );
263
+ y = stdlib_base_dists_beta_pdf( x, alpha, beta );
264
+ printf( "x: %lf, α: %lf, β: %lf, f(x;α,β): %lf\n", x, alpha, beta, y );
265
+ }
174
266
  }
175
267
  ```
176
268
 
@@ -178,6 +270,18 @@ for ( i = 0; i < 10; i++ ) {
178
270
 
179
271
  <!-- /.examples -->
180
272
 
273
+ </section>
274
+
275
+ <!-- /.c -->
276
+
277
+ <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
278
+
279
+ <section class="references">
280
+
281
+ </section>
282
+
283
+ <!-- /.references -->
284
+
181
285
  <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
182
286
 
183
287
  <section class="related">
@@ -212,7 +316,7 @@ See [LICENSE][stdlib-license].
212
316
 
213
317
  ## Copyright
214
318
 
215
- Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
319
+ Copyright &copy; 2016-2026. The Stdlib [Authors][stdlib-authors].
216
320
 
217
321
  </section>
218
322
 
@@ -225,8 +329,8 @@ Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
225
329
  [npm-image]: http://img.shields.io/npm/v/@stdlib/stats-base-dists-beta-pdf.svg
226
330
  [npm-url]: https://npmjs.org/package/@stdlib/stats-base-dists-beta-pdf
227
331
 
228
- [test-image]: https://github.com/stdlib-js/stats-base-dists-beta-pdf/actions/workflows/test.yml/badge.svg?branch=v0.2.2
229
- [test-url]: https://github.com/stdlib-js/stats-base-dists-beta-pdf/actions/workflows/test.yml?query=branch:v0.2.2
332
+ [test-image]: https://github.com/stdlib-js/stats-base-dists-beta-pdf/actions/workflows/test.yml/badge.svg?branch=v0.3.0
333
+ [test-url]: https://github.com/stdlib-js/stats-base-dists-beta-pdf/actions/workflows/test.yml?query=branch:v0.3.0
230
334
 
231
335
  [coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/stats-base-dists-beta-pdf/main.svg
232
336
  [coverage-url]: https://codecov.io/github/stdlib-js/stats-base-dists-beta-pdf?branch=main
@@ -238,8 +342,8 @@ Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
238
342
 
239
343
  -->
240
344
 
241
- [chat-image]: https://img.shields.io/gitter/room/stdlib-js/stdlib.svg
242
- [chat-url]: https://app.gitter.im/#/room/#stdlib-js_stdlib:gitter.im
345
+ [chat-image]: https://img.shields.io/badge/zulip-join_chat-brightgreen.svg
346
+ [chat-url]: https://stdlib.zulipchat.com
243
347
 
244
348
  [stdlib]: https://github.com/stdlib-js/stdlib
245
349
 
@@ -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_BETA_PDF_H
20
+ #define STDLIB_STATS_BASE_DISTS_BETA_PDF_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 probability density function (PDF) for a beta distribution with first shape parameter `alpha` and second shape parameter `beta`.
31
+ */
32
+ double stdlib_base_dists_beta_pdf( const double x, const double alpha, const double beta );
33
+
34
+ #ifdef __cplusplus
35
+ }
36
+ #endif
37
+
38
+ #endif // !STDLIB_STATS_BASE_DISTS_BETA_PDF_H
package/lib/native.js ADDED
@@ -0,0 +1,88 @@
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 probability density function (PDF) for a beta distribution with first shape parameter `alpha` and second shape parameter `beta` at a value `x`.
30
+ *
31
+ * @private
32
+ * @param {number} x - input value
33
+ * @param {PositiveNumber} alpha - first shape parameter
34
+ * @param {PositiveNumber} beta - second shape parameter
35
+ * @returns {number} evaluated PDF
36
+ *
37
+ * @example
38
+ * var y = pdf( 0.5, 1.0, 1.0 );
39
+ * // returns 1.0
40
+ *
41
+ * @example
42
+ * var y = pdf( 0.5, 2.0, 4.0 );
43
+ * // returns 1.25
44
+ *
45
+ * @example
46
+ * var y = pdf( 0.2, 2.0, 2.0 );
47
+ * // returns ~0.96
48
+ *
49
+ * @example
50
+ * var y = pdf( 0.8, 4.0, 4.0 );
51
+ * // returns ~0.573
52
+ *
53
+ * @example
54
+ * var y = pdf( -0.5, 4.0, 2.0 );
55
+ * // returns 0.0
56
+ *
57
+ * @example
58
+ * var y = pdf( 1.5, 4.0, 2.0 );
59
+ * // returns 0.0
60
+ *
61
+ * @example
62
+ * var y = pdf( 0.5, -1.0, 0.5 );
63
+ * // returns NaN
64
+ *
65
+ * @example
66
+ * var y = pdf( 0.5, 0.5, -1.0 );
67
+ * // returns NaN
68
+ *
69
+ * @example
70
+ * var y = pdf( NaN, 1.0, 1.0 );
71
+ * // returns NaN
72
+ *
73
+ * @example
74
+ * var y = pdf( 0.5, NaN, 1.0 );
75
+ * // returns NaN
76
+ *
77
+ * @example
78
+ * var y = pdf( 0.5, 1.0, NaN );
79
+ * // returns NaN
80
+ */
81
+ function pdf( x, alpha, beta ) {
82
+ return addon( x, alpha, beta );
83
+ }
84
+
85
+
86
+ // EXPORTS //
87
+
88
+ module.exports = pdf;
package/manifest.json ADDED
@@ -0,0 +1,93 @@
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-betaln",
44
+ "@stdlib/math-base-special-log1p",
45
+ "@stdlib/math-base-special-exp",
46
+ "@stdlib/math-base-special-ln",
47
+ "@stdlib/constants-float64-pinf"
48
+ ]
49
+ },
50
+ {
51
+ "task": "benchmark",
52
+ "wasm": false,
53
+ "src": [
54
+ "./src/main.c"
55
+ ],
56
+ "include": [
57
+ "./include"
58
+ ],
59
+ "libraries": [],
60
+ "libpath": [],
61
+ "dependencies": [
62
+ "@stdlib/math-base-assert-is-nan",
63
+ "@stdlib/math-base-special-betaln",
64
+ "@stdlib/math-base-special-log1p",
65
+ "@stdlib/math-base-special-exp",
66
+ "@stdlib/math-base-special-ln",
67
+ "@stdlib/constants-float64-pinf",
68
+ "@stdlib/constants-float64-eps"
69
+ ]
70
+ },
71
+ {
72
+ "task": "examples",
73
+ "wasm": false,
74
+ "src": [
75
+ "./src/main.c"
76
+ ],
77
+ "include": [
78
+ "./include"
79
+ ],
80
+ "libraries": [],
81
+ "libpath": [],
82
+ "dependencies": [
83
+ "@stdlib/math-base-assert-is-nan",
84
+ "@stdlib/math-base-special-betaln",
85
+ "@stdlib/math-base-special-log1p",
86
+ "@stdlib/math-base-special-exp",
87
+ "@stdlib/math-base-special-ln",
88
+ "@stdlib/constants-float64-pinf",
89
+ "@stdlib/constants-float64-eps"
90
+ ]
91
+ }
92
+ ]
93
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stdlib/stats-base-dists-beta-pdf",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "description": "Beta distribution 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",
@@ -32,12 +35,14 @@
32
35
  "dependencies": {
33
36
  "@stdlib/constants-float64-pinf": "^0.2.2",
34
37
  "@stdlib/math-base-assert-is-nan": "^0.2.2",
35
- "@stdlib/math-base-special-betaln": "^0.2.1",
38
+ "@stdlib/math-base-napi-ternary": "^0.3.1",
39
+ "@stdlib/math-base-special-betaln": "^0.2.2",
36
40
  "@stdlib/math-base-special-exp": "^0.2.4",
37
41
  "@stdlib/math-base-special-ln": "^0.2.4",
38
42
  "@stdlib/math-base-special-log1p": "^0.2.3",
39
43
  "@stdlib/utils-constant-function": "^0.2.2",
40
- "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2"
44
+ "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2",
45
+ "@stdlib/utils-library-manifest": "^0.2.3"
41
46
  },
42
47
  "devDependencies": {},
43
48
  "engines": {
package/src/addon.c ADDED
@@ -0,0 +1,22 @@
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/beta/pdf.h"
20
+ #include "stdlib/math/base/napi/ternary.h"
21
+
22
+ STDLIB_MATH_BASE_NAPI_MODULE_DDD_D( stdlib_base_dists_beta_pdf )
package/src/main.c ADDED
@@ -0,0 +1,76 @@
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/beta/pdf.h"
20
+ #include "stdlib/math/base/assert/is_nan.h"
21
+ #include "stdlib/math/base/special/betaln.h"
22
+ #include "stdlib/math/base/special/log1p.h"
23
+ #include "stdlib/math/base/special/exp.h"
24
+ #include "stdlib/math/base/special/ln.h"
25
+ #include "stdlib/constants/float64/pinf.h"
26
+
27
+ /**
28
+ * Evaluates the probability density function (PDF) for a beta distribution with first shape parameter `alpha` and second shape parameter `beta`.
29
+ *
30
+ * @param x input value
31
+ * @param alpha first shape parameter
32
+ * @param beta second shape parameter
33
+ * @return evaluated PDF
34
+ *
35
+ * @example
36
+ * double y = stdlib_base_dists_beta_pdf( 0.5, 1.0, 1.0 );
37
+ * // returns 1.0
38
+ */
39
+ double stdlib_base_dists_beta_pdf( const double x, const double alpha, const double beta ) {
40
+ double out;
41
+ if (
42
+ stdlib_base_is_nan( x ) ||
43
+ stdlib_base_is_nan( alpha ) ||
44
+ stdlib_base_is_nan( beta ) ||
45
+ alpha <= 0.0 ||
46
+ beta <= 0.0
47
+ ) {
48
+ return 0.0 / 0.0;
49
+ }
50
+ if ( x < 0.0 || x > 1.0 ) {
51
+ // Support of the Beta distribution: [0,1]
52
+ return 0.0;
53
+ }
54
+ if ( x == 0.0 ) {
55
+ if ( alpha < 1.0 ) {
56
+ return STDLIB_CONSTANT_FLOAT64_PINF;
57
+ }
58
+ if ( alpha > 1.0 ) {
59
+ return 0.0;
60
+ }
61
+ return beta;
62
+ }
63
+ if ( x == 1.0 ) {
64
+ if ( beta < 1.0 ) {
65
+ return STDLIB_CONSTANT_FLOAT64_PINF;
66
+ }
67
+ if ( beta > 1.0 ) {
68
+ return 0.0;
69
+ }
70
+ return alpha;
71
+ }
72
+ out = ( alpha-1.0 ) * stdlib_base_ln( x );
73
+ out += ( beta-1.0 ) * stdlib_base_log1p( -x );
74
+ out -= stdlib_base_betaln( alpha, beta );
75
+ return stdlib_base_exp( out );
76
+ }