@stdlib/stats-base-dists-geometric-logpmf 0.2.1 → 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
@@ -141,20 +141,109 @@ y = mylogpmf( 1.0 );
141
141
  <!-- eslint no-undef: "error" -->
142
142
 
143
143
  ```javascript
144
- var randu = require( '@stdlib/random-base-randu' );
145
- var round = require( '@stdlib/math-base-special-round' );
144
+ var uniform = require( '@stdlib/random-array-uniform' );
145
+ var discreteUniform = require( '@stdlib/random-array-discrete-uniform' );
146
+ var logEachMap = require( '@stdlib/console-log-each-map' );
146
147
  var logpmf = require( '@stdlib/stats-base-dists-geometric-logpmf' );
147
148
 
148
- var p;
149
- var x;
150
- var y;
151
- var i;
149
+ var opts = {
150
+ 'dtype': 'float64'
151
+ };
152
+ var p = uniform( 10, 0.0, 1.0, opts );
153
+ var x = discreteUniform( 10, 0, 5, opts );
152
154
 
153
- for ( i = 0; i < 10; i++ ) {
154
- x = round( randu() * 5.0 );
155
- p = randu();
156
- y = logpmf( x, p );
157
- console.log( 'x: %d, p: %d, ln( P( X = x; p ) ): %d', x, p.toFixed( 4 ), y.toFixed( 4 ) );
155
+ logEachMap( 'x: %d, p: %0.4f, ln( P( X = x; p ) ): %0.4f', x, p, logpmf );
156
+ ```
157
+
158
+ </section>
159
+
160
+ <!-- /.examples -->
161
+
162
+ <!-- C interface documentation. -->
163
+
164
+ * * *
165
+
166
+ <section class="c">
167
+
168
+ ## C APIs
169
+
170
+ <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
171
+
172
+ <section class="intro">
173
+
174
+ </section>
175
+
176
+ <!-- /.intro -->
177
+
178
+ <!-- C usage documentation. -->
179
+
180
+ <section class="usage">
181
+
182
+ ### Usage
183
+
184
+ ```c
185
+ #include "stdlib/stats/base/dists/geometric/logpmf.h"
186
+ ```
187
+
188
+ #### stdlib_base_dists_geometric_logpmf( x, p )
189
+
190
+ Evaluates the logarithm of the [probability mass function][pmf] (PMF) of the [geometric][geometric-distribution] distribution with success probability `0 <= p <= 1`.
191
+
192
+ ```c
193
+ double out = stdlib_base_dists_geometric_logpmf( 4.0, 0.3 );
194
+ // returns ~-2.631
195
+ ```
196
+
197
+ The function accepts the following arguments:
198
+
199
+ - **x**: `[in] double` input value.
200
+ - **p**: `[in] double` probability of success.
201
+
202
+ ```c
203
+ double stdlib_base_dists_geometric_logpmf( const double x, const double p );
204
+ ```
205
+
206
+ </section>
207
+
208
+ <!-- /.usage -->
209
+
210
+ <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
211
+
212
+ <section class="notes">
213
+
214
+ </section>
215
+
216
+ <!-- /.notes -->
217
+
218
+ <!-- C API usage examples. -->
219
+
220
+ <section class="examples">
221
+
222
+ ### Examples
223
+
224
+ ```c
225
+ #include "stdlib/stats/base/dists/geometric/logpmf.h"
226
+ #include "stdlib/math/base/special/round.h"
227
+ #include <stdlib.h>
228
+ #include <stdio.h>
229
+
230
+ static double random_uniform( const double min, const double max ) {
231
+ double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
232
+ return min + ( v*(max-min) );
233
+ }
234
+
235
+ int main( void ) {
236
+ double x;
237
+ double p;
238
+ double y;
239
+ int i;
240
+
241
+ for ( i = 0; i < 25; i++ ) {
242
+ x = stdlib_base_round( random_uniform( 0.0, 40.0 ) );
243
+ p = random_uniform( 0.0, 1.0 );
244
+ y = stdlib_base_dists_geometric_logpmf( x, p );
245
+ printf( "x: %lf, p: %lf, ln(P(X=x;p)): %lf\n", x, p, y );
246
+ }
158
247
  }
159
248
  ```
160
249
 
@@ -162,6 +251,18 @@ for ( i = 0; i < 10; i++ ) {
162
251
 
163
252
  <!-- /.examples -->
164
253
 
254
+ </section>
255
+
256
+ <!-- /.c -->
257
+
258
+ <!-- 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. -->
259
+
260
+ <section class="references">
261
+
262
+ </section>
263
+
264
+ <!-- /.references -->
265
+
165
266
  <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
166
267
 
167
268
  <section class="related">
@@ -196,7 +297,7 @@ See [LICENSE][stdlib-license].
196
297
 
197
298
  ## Copyright
198
299
 
199
- Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
300
+ Copyright &copy; 2016-2026. The Stdlib [Authors][stdlib-authors].
200
301
 
201
302
  </section>
202
303
 
@@ -209,8 +310,8 @@ Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
209
310
  [npm-image]: http://img.shields.io/npm/v/@stdlib/stats-base-dists-geometric-logpmf.svg
210
311
  [npm-url]: https://npmjs.org/package/@stdlib/stats-base-dists-geometric-logpmf
211
312
 
212
- [test-image]: https://github.com/stdlib-js/stats-base-dists-geometric-logpmf/actions/workflows/test.yml/badge.svg?branch=v0.2.1
213
- [test-url]: https://github.com/stdlib-js/stats-base-dists-geometric-logpmf/actions/workflows/test.yml?query=branch:v0.2.1
313
+ [test-image]: https://github.com/stdlib-js/stats-base-dists-geometric-logpmf/actions/workflows/test.yml/badge.svg?branch=v0.3.0
314
+ [test-url]: https://github.com/stdlib-js/stats-base-dists-geometric-logpmf/actions/workflows/test.yml?query=branch:v0.3.0
214
315
 
215
316
  [coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/stats-base-dists-geometric-logpmf/main.svg
216
317
  [coverage-url]: https://codecov.io/github/stdlib-js/stats-base-dists-geometric-logpmf?branch=main
@@ -222,8 +323,8 @@ Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
222
323
 
223
324
  -->
224
325
 
225
- [chat-image]: https://img.shields.io/gitter/room/stdlib-js/stdlib.svg
226
- [chat-url]: https://app.gitter.im/#/room/#stdlib-js_stdlib:gitter.im
326
+ [chat-image]: https://img.shields.io/badge/zulip-join_chat-brightgreen.svg
327
+ [chat-url]: https://stdlib.zulipchat.com
227
328
 
228
329
  [stdlib]: https://github.com/stdlib-js/stdlib
229
330
 
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  /// <reference path="../docs/types/index.d.ts" />
2
- import logPMF from '../docs/types/index';
3
- export = logPMF;
2
+ import logpmf from '../docs/types/index';
3
+ export = logpmf;
@@ -106,9 +106,9 @@ interface LogPMF {
106
106
  * y = mylogpmf( 1.0 );
107
107
  * // returns ~-1.386
108
108
  */
109
- declare var logPMF: LogPMF;
109
+ declare var logpmf: LogPMF;
110
110
 
111
111
 
112
112
  // EXPORTS //
113
113
 
114
- export = logPMF;
114
+ export = logpmf;
@@ -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_GEOMETRIC_LOGPMF_H
20
+ #define STDLIB_STATS_BASE_DISTS_GEOMETRIC_LOGPMF_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 mass function (PMF) for the geometric distribution with success probability `p` at a value `x`.
31
+ */
32
+ double stdlib_base_dists_geometric_logpmf( const double x, const double p );
33
+
34
+ #ifdef __cplusplus
35
+ }
36
+ #endif
37
+
38
+ #endif // !STDLIB_STATS_BASE_DISTS_GEOMETRIC_LOGPMF_H
package/lib/native.js ADDED
@@ -0,0 +1,68 @@
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 mass function (PMF) for a geometric distribution with success probability `p` at a value `x`.
30
+ *
31
+ * @private
32
+ * @param {number} x - input value
33
+ * @param {Probability} p - success probability
34
+ * @returns {NonPositiveNumber} logarithm of PMF
35
+ *
36
+ * @example
37
+ * var y = logpmf( 4.0, 0.3 );
38
+ * // returns ~-2.631
39
+ *
40
+ * @example
41
+ * var y = logpmf( 2.0, 0.7 );
42
+ * // returns ~-2.765
43
+ *
44
+ * @example
45
+ * var y = logpmf( -1.0, 0.5 );
46
+ * // returns -Infinity
47
+ *
48
+ * @example
49
+ * var y = logpmf( 0.0, NaN );
50
+ * // returns NaN
51
+ *
52
+ * @example
53
+ * var y = logpmf( NaN, 0.5 );
54
+ * // returns NaN
55
+ *
56
+ * @example
57
+ * // Invalid success probability:
58
+ * var y = logpmf( 2.0, 1.5 );
59
+ * // returns NaN
60
+ */
61
+ function logpmf( x, p ) {
62
+ return addon( x, p );
63
+ }
64
+
65
+
66
+ // EXPORTS //
67
+
68
+ module.exports = logpmf;
package/manifest.json ADDED
@@ -0,0 +1,87 @@
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-binary",
42
+ "@stdlib/math-base-assert-is-nonnegative-integer",
43
+ "@stdlib/math-base-assert-is-nan",
44
+ "@stdlib/math-base-special-ln",
45
+ "@stdlib/constants-float64-ninf"
46
+ ]
47
+ },
48
+ {
49
+ "task": "benchmark",
50
+ "wasm": false,
51
+ "src": [
52
+ "./src/main.c"
53
+ ],
54
+ "include": [
55
+ "./include"
56
+ ],
57
+ "libraries": [],
58
+ "libpath": [],
59
+ "dependencies": [
60
+ "@stdlib/math-base-assert-is-nonnegative-integer",
61
+ "@stdlib/math-base-assert-is-nan",
62
+ "@stdlib/math-base-special-ln",
63
+ "@stdlib/math-base-special-round",
64
+ "@stdlib/constants-float64-ninf"
65
+ ]
66
+ },
67
+ {
68
+ "task": "examples",
69
+ "wasm": false,
70
+ "src": [
71
+ "./src/main.c"
72
+ ],
73
+ "include": [
74
+ "./include"
75
+ ],
76
+ "libraries": [],
77
+ "libpath": [],
78
+ "dependencies": [
79
+ "@stdlib/math-base-assert-is-nonnegative-integer",
80
+ "@stdlib/math-base-assert-is-nan",
81
+ "@stdlib/math-base-special-ln",
82
+ "@stdlib/math-base-special-round",
83
+ "@stdlib/constants-float64-ninf"
84
+ ]
85
+ }
86
+ ]
87
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stdlib/stats-base-dists-geometric-logpmf",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "Geometric distribution logarithm of probability mass function (PMF).",
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",
@@ -30,12 +33,14 @@
30
33
  "url": "https://github.com/stdlib-js/stdlib/issues"
31
34
  },
32
35
  "dependencies": {
33
- "@stdlib/constants-float64-ninf": "^0.2.1",
34
- "@stdlib/math-base-assert-is-nan": "^0.2.1",
35
- "@stdlib/math-base-assert-is-nonnegative-integer": "^0.1.2",
36
- "@stdlib/math-base-special-ln": "^0.2.1",
37
- "@stdlib/utils-constant-function": "^0.2.1",
38
- "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.1"
36
+ "@stdlib/constants-float64-ninf": "^0.2.2",
37
+ "@stdlib/math-base-assert-is-nan": "^0.2.2",
38
+ "@stdlib/math-base-assert-is-nonnegative-integer": "^0.2.2",
39
+ "@stdlib/math-base-napi-binary": "^0.3.1",
40
+ "@stdlib/math-base-special-ln": "^0.2.4",
41
+ "@stdlib/utils-constant-function": "^0.2.2",
42
+ "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2",
43
+ "@stdlib/utils-library-manifest": "^0.2.3"
39
44
  },
40
45
  "devDependencies": {},
41
46
  "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/geometric/logpmf.h"
20
+ #include "stdlib/math/base/napi/binary.h"
21
+
22
+ STDLIB_MATH_BASE_NAPI_MODULE_DD_D( stdlib_base_dists_geometric_logpmf )
package/src/main.c ADDED
@@ -0,0 +1,49 @@
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/geometric/logpmf.h"
20
+ #include "stdlib/math/base/assert/is_nonnegative_integer.h"
21
+ #include "stdlib/math/base/assert/is_nan.h"
22
+ #include "stdlib/math/base/special/ln.h"
23
+ #include "stdlib/constants/float64/ninf.h"
24
+
25
+ /**
26
+ * Evaluates the logarithm of the probability mass function (PMF) for a geometric distribution with success probability `p` at a value `x`.
27
+ *
28
+ * @param x input value
29
+ * @param p success probability
30
+ * @return logarithm of PMF
31
+ *
32
+ * @example
33
+ * double y = stdlib_base_dists_geometric_logpmf( 4.0, 0.3 );
34
+ * // returns ~-2.631
35
+ */
36
+ double stdlib_base_dists_geometric_logpmf( const double x, const double p ) {
37
+ if (
38
+ stdlib_base_is_nan( x ) ||
39
+ stdlib_base_is_nan( p ) ||
40
+ p < 0.0 ||
41
+ p > 1.0
42
+ ) {
43
+ return 0.0/0.0; // NaN
44
+ }
45
+ if ( stdlib_base_is_nonnegative_integer( x ) ) {
46
+ return stdlib_base_ln( p ) + ( x * stdlib_base_ln( 1.0 - p ) );
47
+ }
48
+ return STDLIB_CONSTANT_FLOAT64_NINF;
49
+ }