@stdlib/stats-base-dists-normal-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
@@ -116,7 +116,7 @@ y = pdf( 8.0, 8.0, 0.0 );
116
116
 
117
117
  #### pdf.factory( mu, sigma )
118
118
 
119
- Partially apply `mu` and `sigma` to create a reusable `function` for evaluating the PDF.
119
+ Partially applies `mu` and `sigma` to create a reusable `function` for evaluating the PDF.
120
120
 
121
121
  ```javascript
122
122
  var mypdf = pdf.factory( 10.0, 2.0 );
@@ -139,21 +139,112 @@ y = mypdf( 5.0 );
139
139
  <!-- eslint no-undef: "error" -->
140
140
 
141
141
  ```javascript
142
- var randu = require( '@stdlib/random-base-randu' );
142
+ var uniform = require( '@stdlib/random-array-uniform' );
143
+ var logEachMap = require( '@stdlib/console-log-each-map' );
143
144
  var pdf = require( '@stdlib/stats-base-dists-normal-pdf' );
144
145
 
145
- var sigma;
146
- var mu;
147
- var x;
148
- var y;
149
- var i;
150
-
151
- for ( i = 0; i < 10; i++ ) {
152
- x = randu() * 10.0;
153
- mu = (randu() * 10.0) - 5.0;
154
- sigma = randu() * 20.0;
155
- y = pdf( x, mu, sigma );
156
- console.log( 'x: %d, µ: %d, σ: %d, f(x;µ,σ): %d', x, mu, sigma, y );
146
+ var opts = {
147
+ 'dtype': 'float64'
148
+ };
149
+ var sigma = uniform( 10, 0.0, 20.0, opts );
150
+ var mu = uniform( 10, -5.0, 5.0, opts );
151
+ var x = uniform( 10, 0.0, 10.0, opts );
152
+
153
+ logEachMap( 'x: %0.4f, µ: %0.4f, σ: %0.4f, f(x;µ,σ): %0.4f', x, mu, sigma, pdf );
154
+ ```
155
+
156
+ </section>
157
+
158
+ <!-- /.examples -->
159
+
160
+ <!-- C interface documentation. -->
161
+
162
+ * * *
163
+
164
+ <section class="c">
165
+
166
+ ## C APIs
167
+
168
+ <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
169
+
170
+ <section class="intro">
171
+
172
+ </section>
173
+
174
+ <!-- /.intro -->
175
+
176
+ <!-- C usage documentation. -->
177
+
178
+ <section class="usage">
179
+
180
+ ### Usage
181
+
182
+ ```c
183
+ #include "stdlib/stats/base/dists/normal/pdf.h"
184
+ ```
185
+
186
+ #### stdlib_base_dists_normal_pdf( x, mu, sigma )
187
+
188
+ Evaluates the [probability density function][pdf] (PDF) for a [normal][normal-distribution] distribution with parameters `mu` (mean) and `sigma` (standard deviation).
189
+
190
+ ```c
191
+ double y = stdlib_base_dists_normal_pdf( 2.0, 0.0, 1.0 );
192
+ // returns ~0.054
193
+ ```
194
+
195
+ The function accepts the following arguments:
196
+
197
+ - **x**: `[in] double` input value.
198
+ - **mu**: `[in] double` mean.
199
+ - **sigma**: `[in] double` standard deviation.
200
+
201
+ ```c
202
+ double stdlib_base_dists_normal_pdf( const double x, const double mu, const double sigma );
203
+ ```
204
+
205
+ </section>
206
+
207
+ <!-- /.usage -->
208
+
209
+ <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
210
+
211
+ <section class="notes">
212
+
213
+ </section>
214
+
215
+ <!-- /.notes -->
216
+
217
+ <!-- C API usage examples. -->
218
+
219
+ <section class="examples">
220
+
221
+ ### Examples
222
+
223
+ ```c
224
+ #include "stdlib/stats/base/dists/normal/pdf.h"
225
+ #include "stdlib/constants/float64/eps.h"
226
+ #include <stdlib.h>
227
+ #include <stdio.h>
228
+
229
+ static double random_uniform( const double min, const double max ) {
230
+ double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
231
+ return min + ( v*(max-min) );
232
+ }
233
+
234
+ int main( void ) {
235
+ double sigma;
236
+ double mu;
237
+ double x;
238
+ double y;
239
+ int i;
240
+
241
+ for ( i = 0; i < 10; i++ ) {
242
+ x = random_uniform( 0.0, 10.0 );
243
+ mu = random_uniform( -5.0, 5.0 );
244
+ sigma = random_uniform( STDLIB_CONSTANT_FLOAT64_EPS, 20.0 );
245
+ y = stdlib_base_dists_normal_pdf( x, mu, sigma );
246
+ printf( "x: %lf, µ: %lf, σ: %lf, f(x;µ,σ): %lf\n", x, mu, sigma, y );
247
+ }
157
248
  }
158
249
  ```
159
250
 
@@ -161,6 +252,18 @@ for ( i = 0; i < 10; i++ ) {
161
252
 
162
253
  <!-- /.examples -->
163
254
 
255
+ </section>
256
+
257
+ <!-- /.c -->
258
+
259
+ <!-- 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. -->
260
+
261
+ <section class="references">
262
+
263
+ </section>
264
+
265
+ <!-- /.references -->
266
+
164
267
  <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
165
268
 
166
269
  <section class="related">
@@ -195,7 +298,7 @@ See [LICENSE][stdlib-license].
195
298
 
196
299
  ## Copyright
197
300
 
198
- Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
301
+ Copyright &copy; 2016-2026. The Stdlib [Authors][stdlib-authors].
199
302
 
200
303
  </section>
201
304
 
@@ -208,8 +311,8 @@ Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
208
311
  [npm-image]: http://img.shields.io/npm/v/@stdlib/stats-base-dists-normal-pdf.svg
209
312
  [npm-url]: https://npmjs.org/package/@stdlib/stats-base-dists-normal-pdf
210
313
 
211
- [test-image]: https://github.com/stdlib-js/stats-base-dists-normal-pdf/actions/workflows/test.yml/badge.svg?branch=v0.2.2
212
- [test-url]: https://github.com/stdlib-js/stats-base-dists-normal-pdf/actions/workflows/test.yml?query=branch:v0.2.2
314
+ [test-image]: https://github.com/stdlib-js/stats-base-dists-normal-pdf/actions/workflows/test.yml/badge.svg?branch=v0.3.0
315
+ [test-url]: https://github.com/stdlib-js/stats-base-dists-normal-pdf/actions/workflows/test.yml?query=branch:v0.3.0
213
316
 
214
317
  [coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/stats-base-dists-normal-pdf/main.svg
215
318
  [coverage-url]: https://codecov.io/github/stdlib-js/stats-base-dists-normal-pdf?branch=main
@@ -221,8 +324,8 @@ Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
221
324
 
222
325
  -->
223
326
 
224
- [chat-image]: https://img.shields.io/gitter/room/stdlib-js/stdlib.svg
225
- [chat-url]: https://app.gitter.im/#/room/#stdlib-js_stdlib:gitter.im
327
+ [chat-image]: https://img.shields.io/badge/zulip-join_chat-brightgreen.svg
328
+ [chat-url]: https://stdlib.zulipchat.com
226
329
 
227
330
  [stdlib]: https://github.com/stdlib-js/stdlib
228
331
 
@@ -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_NORMAL_PDF_H
20
+ #define STDLIB_STATS_BASE_DISTS_NORMAL_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 normal distribution with mean `mu` and standard deviation `sigma` at a value `x`.
31
+ */
32
+ double stdlib_base_dists_normal_pdf( const double x, const double mu, const double sigma );
33
+
34
+ #ifdef __cplusplus
35
+ }
36
+ #endif
37
+
38
+ #endif // !STDLIB_STATS_BASE_DISTS_NORMAL_PDF_H
package/lib/native.js ADDED
@@ -0,0 +1,77 @@
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 normal distribution with mean `mu` and standard deviation `sigma` at a value `x`.
30
+ *
31
+ * @private
32
+ * @param {number} x - input value
33
+ * @param {number} mu - mean
34
+ * @param {NonNegativeNumber} sigma - standard deviation
35
+ * @returns {number} evaluated probability density function
36
+ *
37
+ * @example
38
+ * var y = pdf( 2.0, 0.0, 1.0 );
39
+ * // returns ~0.054
40
+ *
41
+ * @example
42
+ * var y = pdf( -1.0, 4.0, 2.0 );
43
+ * // returns ~0.009
44
+ *
45
+ * @example
46
+ * var y = pdf( NaN, 0.0, 1.0 );
47
+ * // returns NaN
48
+ *
49
+ * @example
50
+ * var y = pdf( 0.0, NaN, 1.0 );
51
+ * // returns NaN
52
+ *
53
+ * @example
54
+ * var y = pdf( 0.0, 0.0, NaN );
55
+ * // returns NaN
56
+ *
57
+ * @example
58
+ * // Negative standard deviation:
59
+ * var y = pdf( 2.0, 0.0, -1.0 );
60
+ * // returns NaN
61
+ *
62
+ * @example
63
+ * var y = pdf( 2.0, 8.0, 0.0 );
64
+ * // returns 0.0
65
+ *
66
+ * @example
67
+ * var y = pdf( 8.0, 8.0, 0.0 );
68
+ * // returns Infinity
69
+ */
70
+ function pdf( x, mu, sigma ) {
71
+ return addon( x, mu, sigma );
72
+ }
73
+
74
+
75
+ // EXPORTS //
76
+
77
+ 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/constants-float64-two-pi",
43
+ "@stdlib/math-base-assert-is-nan",
44
+ "@stdlib/math-base-special-sqrt",
45
+ "@stdlib/math-base-special-pow",
46
+ "@stdlib/math-base-special-exp",
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/constants-float64-two-pi",
63
+ "@stdlib/math-base-assert-is-nan",
64
+ "@stdlib/math-base-special-sqrt",
65
+ "@stdlib/math-base-special-pow",
66
+ "@stdlib/math-base-special-exp",
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/constants-float64-two-pi",
84
+ "@stdlib/math-base-assert-is-nan",
85
+ "@stdlib/math-base-special-sqrt",
86
+ "@stdlib/math-base-special-pow",
87
+ "@stdlib/math-base-special-exp",
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-normal-pdf",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "description": "Normal 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",
@@ -33,12 +36,14 @@
33
36
  "@stdlib/constants-float64-pinf": "^0.2.2",
34
37
  "@stdlib/constants-float64-two-pi": "^0.2.2",
35
38
  "@stdlib/math-base-assert-is-nan": "^0.2.2",
36
- "@stdlib/math-base-special-exp": "^0.2.3",
39
+ "@stdlib/math-base-napi-ternary": "^0.3.1",
40
+ "@stdlib/math-base-special-exp": "^0.2.4",
37
41
  "@stdlib/math-base-special-pow": "^0.3.0",
38
42
  "@stdlib/math-base-special-sqrt": "^0.2.2",
39
- "@stdlib/stats-base-dists-degenerate-pdf": "^0.2.2",
43
+ "@stdlib/stats-base-dists-degenerate-pdf": "^0.3.0",
40
44
  "@stdlib/utils-constant-function": "^0.2.2",
41
- "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2"
45
+ "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2",
46
+ "@stdlib/utils-library-manifest": "^0.2.3"
42
47
  },
43
48
  "devDependencies": {},
44
49
  "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/normal/pdf.h"
20
+ #include "stdlib/math/base/napi/ternary.h"
21
+
22
+ STDLIB_MATH_BASE_NAPI_MODULE_DDD_D( stdlib_base_dists_normal_pdf )
package/src/main.c ADDED
@@ -0,0 +1,58 @@
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/normal/pdf.h"
20
+ #include "stdlib/math/base/assert/is_nan.h"
21
+ #include "stdlib/math/base/special/sqrt.h"
22
+ #include "stdlib/math/base/special/pow.h"
23
+ #include "stdlib/math/base/special/exp.h"
24
+ #include "stdlib/constants/float64/two_pi.h"
25
+ #include "stdlib/constants/float64/pinf.h"
26
+
27
+ /**
28
+ * Evaluates the probability density function (PDF) for a normal distribution with mean `mu` and standard deviation `sigma` at a value `x`.
29
+ *
30
+ * @param x input value
31
+ * @param mu mean of the distribution
32
+ * @param sigma standard deviation of the distribution
33
+ * @return evaluated probability density function
34
+ *
35
+ * @example
36
+ * double y = stdlib_base_dists_normal_pdf( 2.0, 0.0, 1.0 );
37
+ * // returns ~0.054
38
+ */
39
+ double stdlib_base_dists_normal_pdf( const double x, const double mu, const double sigma ) {
40
+ double s2;
41
+ double A;
42
+ double B;
43
+ if (
44
+ stdlib_base_is_nan( x ) ||
45
+ stdlib_base_is_nan( mu ) ||
46
+ stdlib_base_is_nan( sigma ) ||
47
+ sigma < 0.0
48
+ ) {
49
+ return 0.0/0.0; // NaN
50
+ }
51
+ if ( sigma == 0.0 ) {
52
+ return ( x == mu ) ? STDLIB_CONSTANT_FLOAT64_PINF : 0.0;
53
+ }
54
+ s2 = stdlib_base_pow( sigma, 2.0 );
55
+ A = 1.0 / stdlib_base_sqrt( s2 * STDLIB_CONSTANT_FLOAT64_TWO_PI );
56
+ B = -1.0 / ( 2.0 * s2 );
57
+ return A * stdlib_base_exp( B * stdlib_base_pow( x - mu, 2.0 ) );
58
+ }