@stdlib/stats-base-dists-chi-entropy 0.2.2 → 0.3.1
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 +1 -1
- package/README.md +103 -14
- package/include/stdlib/stats/base/dists/chi/entropy.h +38 -0
- package/lib/native.js +58 -0
- package/manifest.json +86 -0
- package/package.json +9 -4
- package/src/addon.c +22 -0
- package/src/main.c +41 -0
package/NOTICE
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Copyright (c) 2016-
|
|
1
|
+
Copyright (c) 2016-2026 The Stdlib Authors.
|
package/README.md
CHANGED
|
@@ -116,18 +116,103 @@ var v = entropy( -1.0 );
|
|
|
116
116
|
<!-- eslint no-undef: "error" -->
|
|
117
117
|
|
|
118
118
|
```javascript
|
|
119
|
-
var
|
|
120
|
-
var
|
|
119
|
+
var uniform = require( '@stdlib/random-array-uniform' );
|
|
120
|
+
var logEachMap = require( '@stdlib/console-log-each-map' );
|
|
121
121
|
var entropy = require( '@stdlib/stats-base-dists-chi-entropy' );
|
|
122
122
|
|
|
123
|
-
var
|
|
124
|
-
|
|
125
|
-
|
|
123
|
+
var opts = {
|
|
124
|
+
'dtype': 'float64'
|
|
125
|
+
};
|
|
126
|
+
var k = uniform( 10, 0.0, 20.0, opts );
|
|
126
127
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
128
|
+
logEachMap( 'k: %0.4f, h(X;k): %0.4f', k, entropy );
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
</section>
|
|
132
|
+
|
|
133
|
+
<!-- /.examples -->
|
|
134
|
+
|
|
135
|
+
<!-- C interface documentation. -->
|
|
136
|
+
|
|
137
|
+
* * *
|
|
138
|
+
|
|
139
|
+
<section class="c">
|
|
140
|
+
|
|
141
|
+
## C APIs
|
|
142
|
+
|
|
143
|
+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
|
|
144
|
+
|
|
145
|
+
<section class="intro">
|
|
146
|
+
|
|
147
|
+
</section>
|
|
148
|
+
|
|
149
|
+
<!-- /.intro -->
|
|
150
|
+
|
|
151
|
+
<!-- C usage documentation. -->
|
|
152
|
+
|
|
153
|
+
<section class="usage">
|
|
154
|
+
|
|
155
|
+
### Usage
|
|
156
|
+
|
|
157
|
+
```c
|
|
158
|
+
#include "stdlib/stats/base/dists/chi/entropy.h"
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
#### stdlib_base_dists_chi_entropy( k )
|
|
162
|
+
|
|
163
|
+
Returns the [differential entropy][entropy] of a [chi][chi-distribution] distribution with degrees of freedom `k` (in [nats][nats]).
|
|
164
|
+
|
|
165
|
+
```c
|
|
166
|
+
double out = stdlib_base_dists_chi_entropy( 9.0 );
|
|
167
|
+
// returns ~1.052
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
The function accepts the following arguments:
|
|
171
|
+
|
|
172
|
+
- **k**: `[in] double` degrees of freedom (must be positive).
|
|
173
|
+
|
|
174
|
+
```c
|
|
175
|
+
double stdlib_base_dists_chi_entropy( const double k );
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
</section>
|
|
179
|
+
|
|
180
|
+
<!-- /.usage -->
|
|
181
|
+
|
|
182
|
+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
183
|
+
|
|
184
|
+
<section class="notes">
|
|
185
|
+
|
|
186
|
+
</section>
|
|
187
|
+
|
|
188
|
+
<!-- /.notes -->
|
|
189
|
+
|
|
190
|
+
<!-- C API usage examples. -->
|
|
191
|
+
|
|
192
|
+
<section class="examples">
|
|
193
|
+
|
|
194
|
+
### Examples
|
|
195
|
+
|
|
196
|
+
```c
|
|
197
|
+
#include "stdlib/stats/base/dists/chi/entropy.h"
|
|
198
|
+
#include <stdlib.h>
|
|
199
|
+
#include <stdio.h>
|
|
200
|
+
|
|
201
|
+
static double random_uniform( const double min, const double max ) {
|
|
202
|
+
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
|
|
203
|
+
return min + ( v * ( max - min ) );
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
int main( void ) {
|
|
207
|
+
double result;
|
|
208
|
+
double k;
|
|
209
|
+
int i;
|
|
210
|
+
|
|
211
|
+
for ( i = 0; i < 10; i++ ) {
|
|
212
|
+
k = random_uniform( 0.0, 10.0 );
|
|
213
|
+
result = stdlib_base_dists_chi_entropy( k );
|
|
214
|
+
printf( "k: %lf, h(X,k): %lf \n", k, result );
|
|
215
|
+
}
|
|
131
216
|
}
|
|
132
217
|
```
|
|
133
218
|
|
|
@@ -135,6 +220,10 @@ for ( i = 0; i < 10; i++ ) {
|
|
|
135
220
|
|
|
136
221
|
<!-- /.examples -->
|
|
137
222
|
|
|
223
|
+
</section>
|
|
224
|
+
|
|
225
|
+
<!-- /.c -->
|
|
226
|
+
|
|
138
227
|
<!-- 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. -->
|
|
139
228
|
|
|
140
229
|
<section class="references">
|
|
@@ -177,7 +266,7 @@ See [LICENSE][stdlib-license].
|
|
|
177
266
|
|
|
178
267
|
## Copyright
|
|
179
268
|
|
|
180
|
-
Copyright © 2016-
|
|
269
|
+
Copyright © 2016-2026. The Stdlib [Authors][stdlib-authors].
|
|
181
270
|
|
|
182
271
|
</section>
|
|
183
272
|
|
|
@@ -190,8 +279,8 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors].
|
|
|
190
279
|
[npm-image]: http://img.shields.io/npm/v/@stdlib/stats-base-dists-chi-entropy.svg
|
|
191
280
|
[npm-url]: https://npmjs.org/package/@stdlib/stats-base-dists-chi-entropy
|
|
192
281
|
|
|
193
|
-
[test-image]: https://github.com/stdlib-js/stats-base-dists-chi-entropy/actions/workflows/test.yml/badge.svg?branch=v0.
|
|
194
|
-
[test-url]: https://github.com/stdlib-js/stats-base-dists-chi-entropy/actions/workflows/test.yml?query=branch:v0.
|
|
282
|
+
[test-image]: https://github.com/stdlib-js/stats-base-dists-chi-entropy/actions/workflows/test.yml/badge.svg?branch=v0.3.1
|
|
283
|
+
[test-url]: https://github.com/stdlib-js/stats-base-dists-chi-entropy/actions/workflows/test.yml?query=branch:v0.3.1
|
|
195
284
|
|
|
196
285
|
[coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/stats-base-dists-chi-entropy/main.svg
|
|
197
286
|
[coverage-url]: https://codecov.io/github/stdlib-js/stats-base-dists-chi-entropy?branch=main
|
|
@@ -203,8 +292,8 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors].
|
|
|
203
292
|
|
|
204
293
|
-->
|
|
205
294
|
|
|
206
|
-
[chat-image]: https://img.shields.io/
|
|
207
|
-
[chat-url]: https://
|
|
295
|
+
[chat-image]: https://img.shields.io/badge/zulip-join_chat-brightgreen.svg
|
|
296
|
+
[chat-url]: https://stdlib.zulipchat.com
|
|
208
297
|
|
|
209
298
|
[stdlib]: https://github.com/stdlib-js/stdlib
|
|
210
299
|
|
|
@@ -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_CHI_ENTROPY_H
|
|
20
|
+
#define STDLIB_STATS_BASE_DISTS_CHI_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 chi distribution.
|
|
31
|
+
*/
|
|
32
|
+
double stdlib_base_dists_chi_entropy( const double k );
|
|
33
|
+
|
|
34
|
+
#ifdef __cplusplus
|
|
35
|
+
}
|
|
36
|
+
#endif
|
|
37
|
+
|
|
38
|
+
#endif // !STDLIB_STATS_BASE_DISTS_CHI_ENTROPY_H
|
package/lib/native.js
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
|
+
'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 chi distribution.
|
|
30
|
+
*
|
|
31
|
+
* @private
|
|
32
|
+
* @param {PositiveNumber} k - degrees of freedom
|
|
33
|
+
* @returns {number} entropy
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* var v = entropy( 9.0 );
|
|
37
|
+
* // returns ~1.052
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* var v = entropy( 1.0 );
|
|
41
|
+
* // returns ~0.726
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* var v = entropy( -0.2 );
|
|
45
|
+
* // returns NaN
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* var v = entropy( NaN );
|
|
49
|
+
* // returns NaN
|
|
50
|
+
*/
|
|
51
|
+
function entropy( k ) {
|
|
52
|
+
return addon( k );
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
// EXPORTS //
|
|
57
|
+
|
|
58
|
+
module.exports = entropy;
|
package/manifest.json
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
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-unary",
|
|
42
|
+
"@stdlib/math-base-assert-is-nan",
|
|
43
|
+
"@stdlib/math-base-special-digamma",
|
|
44
|
+
"@stdlib/math-base-special-gammaln",
|
|
45
|
+
"@stdlib/constants-float64-ln-two"
|
|
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-nan",
|
|
61
|
+
"@stdlib/math-base-special-digamma",
|
|
62
|
+
"@stdlib/math-base-special-gammaln",
|
|
63
|
+
"@stdlib/constants-float64-ln-two",
|
|
64
|
+
"@stdlib/constants-float64-eps"
|
|
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-nan",
|
|
80
|
+
"@stdlib/math-base-special-digamma",
|
|
81
|
+
"@stdlib/math-base-special-gammaln",
|
|
82
|
+
"@stdlib/constants-float64-ln-two"
|
|
83
|
+
]
|
|
84
|
+
}
|
|
85
|
+
]
|
|
86
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stdlib/stats-base-dists-chi-entropy",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Chi distribution 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,10 +33,12 @@
|
|
|
30
33
|
"url": "https://github.com/stdlib-js/stdlib/issues"
|
|
31
34
|
},
|
|
32
35
|
"dependencies": {
|
|
33
|
-
"@stdlib/constants-float64-ln-two": "^0.2.
|
|
34
|
-
"@stdlib/math-base-assert-is-nan": "^0.2.
|
|
36
|
+
"@stdlib/constants-float64-ln-two": "^0.2.3",
|
|
37
|
+
"@stdlib/math-base-assert-is-nan": "^0.2.3",
|
|
38
|
+
"@stdlib/math-base-napi-unary": "^0.2.7",
|
|
35
39
|
"@stdlib/math-base-special-digamma": "^0.3.0",
|
|
36
|
-
"@stdlib/math-base-special-gammaln": "^0.
|
|
40
|
+
"@stdlib/math-base-special-gammaln": "^0.3.0",
|
|
41
|
+
"@stdlib/utils-library-manifest": "^0.2.4"
|
|
37
42
|
},
|
|
38
43
|
"devDependencies": {},
|
|
39
44
|
"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/chi/entropy.h"
|
|
20
|
+
#include "stdlib/math/base/napi/unary.h"
|
|
21
|
+
|
|
22
|
+
STDLIB_MATH_BASE_NAPI_MODULE_D_D( stdlib_base_dists_chi_entropy )
|
package/src/main.c
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
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/chi/entropy.h"
|
|
20
|
+
#include "stdlib/math/base/special/digamma.h"
|
|
21
|
+
#include "stdlib/math/base/special/gammaln.h"
|
|
22
|
+
#include "stdlib/constants/float64/ln_two.h"
|
|
23
|
+
#include "stdlib/math/base/assert/is_nan.h"
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Returns the differential entropy of a chi distribution.
|
|
27
|
+
*
|
|
28
|
+
* @param k degrees of freedom
|
|
29
|
+
* @return entropy
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* double v = stdlib_base_dists_chi_entropy( 9.0 );
|
|
33
|
+
* // returns ~1.052
|
|
34
|
+
*/
|
|
35
|
+
double stdlib_base_dists_chi_entropy( const double k ) {
|
|
36
|
+
if ( stdlib_base_is_nan( k ) || k <= 0.0 ) {
|
|
37
|
+
return 0.0 / 0.0; // NaN
|
|
38
|
+
}
|
|
39
|
+
double kh = k / 2.0;
|
|
40
|
+
return stdlib_base_gammaln( kh ) + ( 0.5 * ( k - STDLIB_CONSTANT_FLOAT64_LN2 - ( ( k - 1.0 ) * stdlib_base_digamma( kh ) ) ) );
|
|
41
|
+
}
|