@stdlib/stats-base-dists-cauchy-entropy 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
@@ -127,20 +127,109 @@ v = entropy( 1.0, 0.0 );
127
127
  <!-- eslint no-undef: "error" -->
128
128
 
129
129
  ```javascript
130
- var randu = require( '@stdlib/random-base-randu' );
130
+ var uniform = require( '@stdlib/random-array-uniform' );
131
+ var logEachMap = require( '@stdlib/console-log-each-map' );
131
132
  var EPS = require( '@stdlib/constants-float64-eps' );
132
133
  var entropy = require( '@stdlib/stats-base-dists-cauchy-entropy' );
133
134
 
134
- var gamma;
135
- var x0;
136
- var v;
137
- var i;
135
+ var opts = {
136
+ 'dtype': 'float64'
137
+ };
138
+ var gamma = uniform( 10, EPS, 10.0, opts );
139
+ var x0 = uniform( 10, 0.0, 100.0, opts );
138
140
 
139
- for ( i = 0; i < 10; i++ ) {
140
- x0 = randu() * 100.0;
141
- gamma = ( randu()*10.0 ) + EPS;
142
- v = entropy( x0, gamma );
143
- console.log( 'x0: %d, γ: %d, h(X;x0,γ): %d', x0.toFixed( 4 ), gamma.toFixed( 4 ), v.toFixed( 4 ) );
141
+ logEachMap( 'x0: %0.4f, γ: %0.4f, h(X;x0,γ): %0.4f', x0, gamma, entropy );
142
+ ```
143
+
144
+ </section>
145
+
146
+ <!-- /.examples -->
147
+
148
+ <!-- C interface documentation. -->
149
+
150
+ * * *
151
+
152
+ <section class="c">
153
+
154
+ ## C APIs
155
+
156
+ <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
157
+
158
+ <section class="intro">
159
+
160
+ </section>
161
+
162
+ <!-- /.intro -->
163
+
164
+ <!-- C usage documentation. -->
165
+
166
+ <section class="usage">
167
+
168
+ ### Usage
169
+
170
+ ```c
171
+ #include "stdlib/stats/base/dists/cauchy/entropy.h"
172
+ ```
173
+
174
+ #### stdlib_base_dists_cauchy_entropy( x0, gamma )
175
+
176
+ Evaluates the [differential entropy][entropy] of a [Cauchy][cauchy-distribution] distribution with location parameter `x0` and scale parameter `gamma` (in [nats][nats]).
177
+
178
+ ```c
179
+ double out = stdlib_base_dists_cauchy_entropy( 10.0, 5.0 );
180
+ // returns ~4.14
181
+ ```
182
+
183
+ The function accepts the following arguments:
184
+
185
+ - **x0**: `[in] double` location parameter.
186
+ - **gamma**: `[in] double` scale parameter.
187
+
188
+ ```c
189
+ double stdlib_base_dists_cauchy_entropy( const double x0, const double gamma );
190
+ ```
191
+
192
+ </section>
193
+
194
+ <!-- /.usage -->
195
+
196
+ <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
197
+
198
+ <section class="notes">
199
+
200
+ </section>
201
+
202
+ <!-- /.notes -->
203
+
204
+ <!-- C API usage examples. -->
205
+
206
+ <section class="examples">
207
+
208
+ ### Examples
209
+
210
+ ```c
211
+ #include "stdlib/stats/base/dists/cauchy/entropy.h"
212
+ #include "stdlib/constants/float64/eps.h"
213
+ #include <stdlib.h>
214
+ #include <stdio.h>
215
+
216
+ static double random_uniform( const double min, const double max ) {
217
+ double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
218
+ return min + ( v*(max-min) );
219
+ }
220
+
221
+ int main( void ) {
222
+ double gamma;
223
+ double x0;
224
+ double y;
225
+ int i;
226
+
227
+ for ( i = 0; i < 25; i++ ) {
228
+ x0 = random_uniform( 0.0, 100.0 );
229
+ gamma = random_uniform( STDLIB_CONSTANT_FLOAT64_EPS, 10.0 );
230
+ y = stdlib_base_dists_cauchy_entropy( x0, gamma );
231
+ printf( "x0: %lf, γ: %lf, h(X;x0,γ): %lf\n", x0, gamma, y );
232
+ }
144
233
  }
145
234
  ```
146
235
 
@@ -148,6 +237,10 @@ for ( i = 0; i < 10; i++ ) {
148
237
 
149
238
  <!-- /.examples -->
150
239
 
240
+ </section>
241
+
242
+ <!-- /.c -->
243
+
151
244
  <!-- 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. -->
152
245
 
153
246
  <section class="references">
@@ -190,7 +283,7 @@ See [LICENSE][stdlib-license].
190
283
 
191
284
  ## Copyright
192
285
 
193
- Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
286
+ Copyright &copy; 2016-2026. The Stdlib [Authors][stdlib-authors].
194
287
 
195
288
  </section>
196
289
 
@@ -203,8 +296,8 @@ Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
203
296
  [npm-image]: http://img.shields.io/npm/v/@stdlib/stats-base-dists-cauchy-entropy.svg
204
297
  [npm-url]: https://npmjs.org/package/@stdlib/stats-base-dists-cauchy-entropy
205
298
 
206
- [test-image]: https://github.com/stdlib-js/stats-base-dists-cauchy-entropy/actions/workflows/test.yml/badge.svg?branch=v0.2.1
207
- [test-url]: https://github.com/stdlib-js/stats-base-dists-cauchy-entropy/actions/workflows/test.yml?query=branch:v0.2.1
299
+ [test-image]: https://github.com/stdlib-js/stats-base-dists-cauchy-entropy/actions/workflows/test.yml/badge.svg?branch=v0.3.0
300
+ [test-url]: https://github.com/stdlib-js/stats-base-dists-cauchy-entropy/actions/workflows/test.yml?query=branch:v0.3.0
208
301
 
209
302
  [coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/stats-base-dists-cauchy-entropy/main.svg
210
303
  [coverage-url]: https://codecov.io/github/stdlib-js/stats-base-dists-cauchy-entropy?branch=main
@@ -216,8 +309,8 @@ Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
216
309
 
217
310
  -->
218
311
 
219
- [chat-image]: https://img.shields.io/gitter/room/stdlib-js/stdlib.svg
220
- [chat-url]: https://app.gitter.im/#/room/#stdlib-js_stdlib:gitter.im
312
+ [chat-image]: https://img.shields.io/badge/zulip-join_chat-brightgreen.svg
313
+ [chat-url]: https://stdlib.zulipchat.com
221
314
 
222
315
  [stdlib]: https://github.com/stdlib-js/stdlib
223
316
 
@@ -0,0 +1,38 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2024 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_CAUCHY_ENTROPY_H
20
+ #define STDLIB_STATS_BASE_DISTS_CAUCHY_ENTROPY_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
+ * Returns the differential entropy of a Cauchy distribution.
31
+ */
32
+ double stdlib_base_dists_cauchy_entropy( const double x0, const double gamma );
33
+
34
+ #ifdef __cplusplus
35
+ }
36
+ #endif
37
+
38
+ #endif // !STDLIB_STATS_BASE_DISTS_CAUCHY_ENTROPY_H
package/lib/native.js ADDED
@@ -0,0 +1,55 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2024 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
+ * Returns the differential entropy of a Cauchy distribution.
30
+ *
31
+ * @private
32
+ * @param {number} x0 - location parameter
33
+ * @param {PositiveNumber} gamma - scale parameter
34
+ * @returns {PositiveNumber} entropy
35
+ *
36
+ * @example
37
+ * var v = entropy( 10.0, 5.0 );
38
+ * // returns ~4.14
39
+ *
40
+ * @example
41
+ * var v = entropy( 7.0, 0.1 );
42
+ * // returns ~0.228
43
+ *
44
+ * @example
45
+ * var v = entropy( 10.0, -0.5 );
46
+ * // returns NaN
47
+ */
48
+ function entropy( x0, gamma ) {
49
+ return addon( x0, gamma );
50
+ }
51
+
52
+
53
+ // EXPORTS //
54
+
55
+ module.exports = entropy;
package/manifest.json ADDED
@@ -0,0 +1,84 @@
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-nan",
43
+ "@stdlib/math-base-special-ln",
44
+ "@stdlib/constants-float64-pi"
45
+ ]
46
+ },
47
+ {
48
+ "task": "benchmark",
49
+ "wasm": false,
50
+ "src": [
51
+ "./src/main.c"
52
+ ],
53
+ "include": [
54
+ "./include"
55
+ ],
56
+ "libraries": [],
57
+ "libpath": [],
58
+ "dependencies": [
59
+ "@stdlib/math-base-assert-is-nan",
60
+ "@stdlib/math-base-special-ln",
61
+ "@stdlib/constants-float64-eps",
62
+ "@stdlib/constants-float64-pi"
63
+ ]
64
+ },
65
+ {
66
+ "task": "examples",
67
+ "wasm": false,
68
+ "src": [
69
+ "./src/main.c"
70
+ ],
71
+ "include": [
72
+ "./include"
73
+ ],
74
+ "libraries": [],
75
+ "libpath": [],
76
+ "dependencies": [
77
+ "@stdlib/math-base-assert-is-nan",
78
+ "@stdlib/math-base-special-ln",
79
+ "@stdlib/constants-float64-eps",
80
+ "@stdlib/constants-float64-pi"
81
+ ]
82
+ }
83
+ ]
84
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stdlib/stats-base-dists-cauchy-entropy",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "Cauchy distribution differential entropy.",
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,9 +33,11 @@
30
33
  "url": "https://github.com/stdlib-js/stdlib/issues"
31
34
  },
32
35
  "dependencies": {
33
- "@stdlib/constants-float64-pi": "^0.2.1",
34
- "@stdlib/math-base-assert-is-nan": "^0.2.1",
35
- "@stdlib/math-base-special-ln": "^0.2.1"
36
+ "@stdlib/constants-float64-pi": "^0.2.2",
37
+ "@stdlib/math-base-assert-is-nan": "^0.2.2",
38
+ "@stdlib/math-base-napi-binary": "^0.3.1",
39
+ "@stdlib/math-base-special-ln": "^0.2.4",
40
+ "@stdlib/utils-library-manifest": "^0.2.3"
36
41
  },
37
42
  "devDependencies": {},
38
43
  "engines": {
package/src/addon.c ADDED
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2024 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/cauchy/entropy.h"
20
+ #include "stdlib/math/base/napi/binary.h"
21
+
22
+ STDLIB_MATH_BASE_NAPI_MODULE_DD_D( stdlib_base_dists_cauchy_entropy )
package/src/main.c ADDED
@@ -0,0 +1,44 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2024 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/cauchy/entropy.h"
20
+ #include "stdlib/math/base/assert/is_nan.h"
21
+ #include "stdlib/math/base/special/ln.h"
22
+ #include "stdlib/constants/float64/pi.h"
23
+
24
+ /**
25
+ * Returns the differential entropy of a Cauchy distribution.
26
+ *
27
+ * @param x0 location parameter
28
+ * @param gamma scale parameter
29
+ * @return entropy
30
+ *
31
+ * @example
32
+ * double y = stdlib_base_dists_cauchy_entropy( 10.0, 5.0 );
33
+ * // returns ~4.14
34
+ */
35
+ double stdlib_base_dists_cauchy_entropy( const double x0, const double gamma ) {
36
+ if (
37
+ stdlib_base_is_nan( gamma ) ||
38
+ stdlib_base_is_nan( x0 ) ||
39
+ gamma <= 0.0
40
+ ) {
41
+ return 0.0/0.0; // NaN
42
+ }
43
+ return stdlib_base_ln( gamma ) + stdlib_base_ln( 4.0*STDLIB_CONSTANT_FLOAT64_PI );
44
+ }