README.md (4151B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 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 > [Discrete uniform][discrete-uniform-distribution] distribution [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 [entropy][entropy] (in [nats][nats]) for a [discrete uniform][discrete-uniform-distribution] random variable is 30 31 <!-- <equation class="equation" label="eq:discrete_uniform_entropy" align="center" raw="h\left( X \right) = \ln(b-a+1)" alt="Entropy for a discrete uniform distribution."> --> 32 33 <div class="equation" align="center" data-raw-text="h\left( X \right) = \ln(b-a+1)" data-equation="eq:discrete_uniform_entropy"> 34 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@591cf9d5c3a0cd3c1ceec961e5c49d73a68374cb/lib/node_modules/@stdlib/stats/base/dists/discrete-uniform/entropy/docs/img/equation_discrete_uniform_entropy.svg" alt="Entropy for a discrete uniform distribution."> 35 <br> 36 </div> 37 38 <!-- </equation> --> 39 40 where `a` is the minimum support and `b` is the maximum support. The parameters must satisfy `a < b`. 41 42 </section> 43 44 <!-- /.intro --> 45 46 <!-- Package usage documentation. --> 47 48 <section class="usage"> 49 50 ## Usage 51 52 ```javascript 53 var entropy = require( '@stdlib/stats/base/dists/discrete-uniform/entropy' ); 54 ``` 55 56 #### entropy( a, b ) 57 58 Returns the [entropy][entropy] of a [discrete uniform][discrete-uniform-distribution] distribution with minimum support `a` and maximum support `b` (in [nats][nats]). 59 60 ```javascript 61 var v = entropy( 0, 1 ); 62 // returns ~0.693 63 64 v = entropy( 4, 12 ); 65 // returns ~2.197 66 67 v = entropy( 2, 8 ); 68 // returns ~1.946 69 ``` 70 71 If `a` or `b` is not an integer value, the function returns `NaN`. 72 73 ```javascript 74 var v = entropy( 0.1, 2 ); 75 // returns NaN 76 77 v = entropy( 0, 2.2 ); 78 // returns NaN 79 80 v = entropy( NaN, 2 ); 81 // returns NaN 82 83 v = entropy( 2, NaN ); 84 // returns NaN 85 ``` 86 87 If provided `a > b`, the function returns `NaN`. 88 89 ```javascript 90 var v = entropy( 3, 2 ); 91 // returns NaN 92 93 v = entropy( -1, -2 ); 94 // returns NaN 95 ``` 96 97 </section> 98 99 <!-- /.usage --> 100 101 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 102 103 <section class="notes"> 104 105 </section> 106 107 <!-- /.notes --> 108 109 <!-- Package usage examples. --> 110 111 <section class="examples"> 112 113 ## Examples 114 115 <!-- eslint no-undef: "error" --> 116 117 ```javascript 118 var randint = require( '@stdlib/random/base/discrete-uniform' ); 119 var entropy = require( '@stdlib/stats/base/dists/discrete-uniform/entropy' ); 120 121 var randa = randint.factory( 0, 10 ); 122 var randb = randint.factory(); 123 var a; 124 var b; 125 var v; 126 var i; 127 128 for ( i = 0; i < 10; i++ ) { 129 a = randa(); 130 b = randb( a, a+randa() ); 131 v = entropy( a, b ); 132 console.log( 'a: %d, b: %d, H(X;a,b): %d', a.toFixed( 4 ), b.toFixed( 4 ), v.toFixed( 4 ) ); 133 } 134 ``` 135 136 </section> 137 138 <!-- /.examples --> 139 140 <!-- 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. --> 141 142 <section class="references"> 143 144 </section> 145 146 <!-- /.references --> 147 148 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 149 150 <section class="links"> 151 152 [discrete-uniform-distribution]: https://en.wikipedia.org/wiki/Discrete_uniform_distribution 153 154 [entropy]: https://en.wikipedia.org/wiki/Entropy_%28information_theory%29 155 156 [nats]: https://en.wikipedia.org/wiki/Nat_%28unit%29 157 158 </section> 159 160 <!-- /.links -->