README.md (2961B)
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 # isUnityProbabilityArray 22 23 > Test if a value is an array of probabilities that sum to one. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var isUnityProbabilityArray = require( '@stdlib/assert/is-unity-probability-array' ); 31 ``` 32 33 #### isUnityProbabilityArray( value ) 34 35 Tests if a `value` is an array of probabilities that sum to one. 36 37 ```javascript 38 var Uint8Array = require( '@stdlib/array/uint8' ); 39 40 var bool = isUnityProbabilityArray( [ 0.25, 0.5, 0.25 ] ); 41 // returns true 42 43 bool = isUnityProbabilityArray( new Uint8Array( [ 0, 1 ] ) ); 44 // returns true 45 46 bool = isUnityProbabilityArray( [ 3.14, 0.0 ] ); 47 // returns false 48 ``` 49 50 </section> 51 52 <!-- /.usage --> 53 54 <section class="notes"> 55 56 ## Notes 57 58 - Summation of finite-precision floating-point numbers often has numerical error. For example, 59 60 ```javascript 61 var arr = [ 0.1, 0.2, 0.1, 0.1, 0.2, 0.2, 0.1 ]; // => 1.0 62 var sum = 0.0; 63 var i; 64 for ( i = 0; i < arr.length; i++ ) { 65 sum += arr[ i ]; 66 } 67 console.log( sum ); 68 // => 0.9999999999999999 69 ``` 70 71 To account for numerical error, the function tests if array elements sum to approximately one; specifically, 72 73 ```text 74 1.0 - sqrt(eps) <= sum(A) <= 1.0 + sqrt(eps) 75 ``` 76 77 where `eps` is [double-precision floating-point][ieee754] epsilon (`~2.22e-16`) and `sqrt(eps) ~ 1.49e-8`. The above comparison ensures equality for approximately half the significand bits. 78 79 </section> 80 81 <!-- /.notes --> 82 83 <section class="examples"> 84 85 ## Examples 86 87 <!-- eslint no-undef: "error" --> 88 89 ```javascript 90 var Uint8Array = require( '@stdlib/array/uint8' ); 91 var isUnityProbabilityArray = require( '@stdlib/assert/is-unity-probability-array' ); 92 93 var arr = [ 0.0, 1.0 ]; 94 var bool = isUnityProbabilityArray( arr ); 95 // returns true 96 97 arr = [ 0.5, 0.25, 0.25 ]; 98 bool = isUnityProbabilityArray( arr ); 99 // returns true 100 101 arr = new Uint8Array( [ 0, 0, 1, 0 ] ); 102 bool = isUnityProbabilityArray( arr ); 103 // returns true 104 105 arr = [ 0.4, 0.4, 0.4 ]; 106 bool = isUnityProbabilityArray( arr ); 107 // returns false 108 109 arr = [ 3.14, -1.0 ]; 110 bool = isUnityProbabilityArray( arr ); 111 // returns false 112 113 bool = isUnityProbabilityArray( [] ); 114 // returns false 115 116 bool = isUnityProbabilityArray( null ); 117 // returns false 118 ``` 119 120 </section> 121 122 <!-- /.examples --> 123 124 <section class="links"> 125 126 [ieee754]: https://en.wikipedia.org/wiki/IEEE_floating_point 127 128 </section> 129 130 <!-- /.links -->