|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2026 The Stdlib Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +
|
| 19 | +--> |
| 20 | + |
| 21 | +# Entropy |
| 22 | + |
| 23 | +> [Half-normal][half-normal-distribution] distribution [differential entropy][entropy]. |
| 24 | +
|
| 25 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 26 | + |
| 27 | +<section class="intro"> |
| 28 | + |
| 29 | +The [differential entropy][entropy] (in [nats][nats]) for a [half-normal][half-normal-distribution] random variable is |
| 30 | + |
| 31 | +<!-- <equation class="equation" label="eq:halfnormal_entropy" align="center" raw="h\left( X \right) = \frac{1}{2}+\ln(\sigma)+\ln\left(\sqrt{\frac{\pi}{2}}\right)+\frac{\gamma}{2}" alt="Differential entropy for a half-normal distribution."> --> |
| 32 | + |
| 33 | +```math |
| 34 | +h\left( X \right) = \frac{1}{2}+\ln(\sigma)+\ln\left(\sqrt{\frac{\pi}{2}}\right)+\frac{\gamma}{2} |
| 35 | +``` |
| 36 | + |
| 37 | +<!-- </equation> --> |
| 38 | + |
| 39 | +where `σ > 0` is the scale parameter. |
| 40 | + |
| 41 | +</section> |
| 42 | + |
| 43 | +<!-- /.intro --> |
| 44 | + |
| 45 | +<!-- Package usage documentation. --> |
| 46 | + |
| 47 | +<section class="usage"> |
| 48 | + |
| 49 | +## Usage |
| 50 | + |
| 51 | +```javascript |
| 52 | +var entropy = require( '@stdlib/stats/base/dists/halfnormal/entropy' ); |
| 53 | +``` |
| 54 | + |
| 55 | +#### entropy( sigma ) |
| 56 | + |
| 57 | +Returns the [differential entropy][entropy] of a [half-normal][half-normal-distribution] distribution with scale `sigma` (in [nats][nats]). |
| 58 | + |
| 59 | +```javascript |
| 60 | +var y = entropy( 1.0 ); |
| 61 | +// returns ~1.014 |
| 62 | + |
| 63 | +y = entropy( 5.0 ); |
| 64 | +// returns ~2.624 |
| 65 | +``` |
| 66 | + |
| 67 | +If provided `sigma ≤ 0`, the function returns `NaN`. |
| 68 | + |
| 69 | +```javascript |
| 70 | +var y = entropy( -1.0 ); |
| 71 | +// returns NaN |
| 72 | +``` |
| 73 | + |
| 74 | +</section> |
| 75 | + |
| 76 | +<!-- /.usage --> |
| 77 | + |
| 78 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 79 | + |
| 80 | +<section class="notes"> |
| 81 | + |
| 82 | +</section> |
| 83 | + |
| 84 | +<!-- /.notes --> |
| 85 | + |
| 86 | +<!-- Package usage examples. --> |
| 87 | + |
| 88 | +<section class="examples"> |
| 89 | + |
| 90 | +## Examples |
| 91 | + |
| 92 | +<!-- eslint no-undef: "error" --> |
| 93 | + |
| 94 | +```javascript |
| 95 | +var uniform = require( '@stdlib/random/array/uniform' ); |
| 96 | +var logEachMap = require( '@stdlib/console/log-each-map' ); |
| 97 | +var entropy = require( '@stdlib/stats/base/dists/halfnormal/entropy' ); |
| 98 | + |
| 99 | +var opts = { |
| 100 | + 'dtype': 'float64' |
| 101 | +}; |
| 102 | +var sigma = uniform( 10, 0.1, 20.0, opts ); |
| 103 | + |
| 104 | +logEachMap( 'σ: %0.4f, h(X;σ): %0.4f', sigma, entropy ); |
| 105 | +``` |
| 106 | + |
| 107 | +</section> |
| 108 | + |
| 109 | +<!-- /.examples --> |
| 110 | + |
| 111 | +<!-- C interface documentation. --> |
| 112 | + |
| 113 | +* * * |
| 114 | + |
| 115 | +<section class="c"> |
| 116 | + |
| 117 | +## C APIs |
| 118 | + |
| 119 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 120 | + |
| 121 | +<section class="intro"> |
| 122 | + |
| 123 | +</section> |
| 124 | + |
| 125 | +<!-- /.intro --> |
| 126 | + |
| 127 | +<!-- C usage documentation. --> |
| 128 | + |
| 129 | +<section class="usage"> |
| 130 | + |
| 131 | +### Usage |
| 132 | + |
| 133 | +```c |
| 134 | +#include "stdlib/stats/base/dists/halfnormal/entropy.h" |
| 135 | +``` |
| 136 | + |
| 137 | +#### stdlib_base_dists_halfnormal_entropy( sigma ) |
| 138 | + |
| 139 | +Returns the differential entropy of a half-normal distribution. |
| 140 | + |
| 141 | +```c |
| 142 | +double out = stdlib_base_dists_halfnormal_entropy( 1.0 ); |
| 143 | +// returns ~1.014 |
| 144 | +``` |
| 145 | + |
| 146 | +The function accepts the following arguments: |
| 147 | + |
| 148 | +- **sigma**: `[in] double` scale parameter. |
| 149 | + |
| 150 | +```c |
| 151 | +double stdlib_base_dists_halfnormal_entropy( const double sigma ); |
| 152 | +``` |
| 153 | +
|
| 154 | +</section> |
| 155 | +
|
| 156 | +<!-- /.usage --> |
| 157 | +
|
| 158 | +<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 159 | +
|
| 160 | +<section class="notes"> |
| 161 | +
|
| 162 | +</section> |
| 163 | +
|
| 164 | +<!-- /.notes --> |
| 165 | +
|
| 166 | +<!-- C API usage examples. --> |
| 167 | +
|
| 168 | +<section class="examples"> |
| 169 | +
|
| 170 | +### Examples |
| 171 | +
|
| 172 | +```c |
| 173 | +#include "stdlib/stats/base/dists/halfnormal/entropy.h" |
| 174 | +#include <stdlib.h> |
| 175 | +#include <stdio.h> |
| 176 | +
|
| 177 | +static double random_uniform( const double min, const double max ) { |
| 178 | + double v = (double)rand() / ( (double)RAND_MAX + 1.0 ); |
| 179 | + return min + ( v*(max-min) ); |
| 180 | +} |
| 181 | +
|
| 182 | +int main( void ) { |
| 183 | + double sigma; |
| 184 | + double y; |
| 185 | + int i; |
| 186 | +
|
| 187 | + for ( i = 0; i < 25; i++ ) { |
| 188 | + sigma = random_uniform( 0.1, 20.0 ); |
| 189 | + y = stdlib_base_dists_halfnormal_entropy( sigma ); |
| 190 | + printf( "σ: %lf, h(σ): %lf\n", sigma, y ); |
| 191 | + } |
| 192 | +} |
| 193 | +``` |
| 194 | + |
| 195 | +</section> |
| 196 | + |
| 197 | +<!-- /.examples --> |
| 198 | + |
| 199 | +</section> |
| 200 | + |
| 201 | +<!-- /.c --> |
| 202 | + |
| 203 | +<!-- 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. --> |
| 204 | + |
| 205 | +<section class="references"> |
| 206 | + |
| 207 | +</section> |
| 208 | + |
| 209 | +<!-- /.references --> |
| 210 | + |
| 211 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 212 | + |
| 213 | +<section class="related"> |
| 214 | + |
| 215 | +</section> |
| 216 | + |
| 217 | +<!-- /.related --> |
| 218 | + |
| 219 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 220 | + |
| 221 | +<section class="links"> |
| 222 | + |
| 223 | +[half-normal-distribution]: https://en.wikipedia.org/wiki/Half-normal_distribution |
| 224 | + |
| 225 | +[entropy]: https://en.wikipedia.org/wiki/Entropy_%28information_theory%29 |
| 226 | + |
| 227 | +[nats]: https://en.wikipedia.org/wiki/Nat_%28unit%29 |
| 228 | + |
| 229 | +</section> |
| 230 | + |
| 231 | +<!-- /.links --> |
0 commit comments