README.md (7414B)
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 # Sample 22 23 > Sample elements from an array-like object. 24 25 <section class="intro"> 26 27 </section> 28 29 <!-- /.intro --> 30 31 <section class="usage"> 32 33 ## Usage 34 35 ```javascript 36 var sample = require( '@stdlib/random/sample' ); 37 ``` 38 39 #### sample( x\[, options] ) 40 41 Samples elements from an `array`-like object. By default, elements are drawn with replacement from `x` to create an output `array` having the same length as `x`. 42 43 ```javascript 44 var out = sample( [ 'a', 'b', 'c' ] ); 45 // e.g., returns [ 'a', 'a', 'b' ] 46 47 out = sample( [ 3, 6, 9 ] ); 48 // e.g., returns [ 3, 9, 6 ] 49 50 var bool = ( out.length === 3 ); 51 // returns true 52 ``` 53 54 The function accepts the following `options`: 55 56 - **size**: sample size. Default: `N = x.length`. 57 - **probs**: a probability `array`. Default: `[1/N,...,1/N]`. 58 - **replace**: `boolean` indicating whether to sample from `x` with replacement. Default: `true`. 59 60 By default, the function returns an `array` having the same length as `x`. To generate a sample of a different size, set the `size` option. 61 62 ```javascript 63 var out = sample( [ 3, 6, 9 ], { 64 'size': 10 65 }); 66 // e.g., returns [ 6, 3, 9, 9, 9, 6, 9, 6, 9, 3 ] 67 68 out = sample( [ 0, 1 ], { 69 'size': 20 70 }); 71 // e.g., returns [ 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0 ] 72 ``` 73 74 To draw a sample _without_ replacement, set the `replace` option to `false`. In this case, the `size` option cannot be an integer larger than the number of elements in `x`. 75 76 ```javascript 77 var out = sample( [ 1, 2, 3, 4, 5, 6 ], { 78 'replace': false, 79 'size': 3 80 }); 81 // e.g., returns [ 6, 1, 5 ] 82 83 out = sample( [ 0, 1 ], { 84 'replace': false 85 }); 86 // e.g., returns [ 0, 1 ] 87 ``` 88 89 By default, the probability of sampling an element is the same for all elements. To assign elements different probabilities, set the `probs` option. 90 91 ```javascript 92 var x = [ 1, 2, 3, 4, 5, 6 ]; 93 var out = sample( x, { 94 'probs': [ 0.1, 0.1, 0.1, 0.1, 0.1, 0.5 ] 95 }); 96 // e.g., returns [ 5, 6, 6, 5, 6, 4 ] 97 98 x = [ 1, 2, 3, 4, 5, 6 ]; 99 out = sample( x, { 100 'probs': [ 0.1, 0.1, 0.1, 0.1, 0.1, 0.5 ], 101 'size': 3, 102 'replace': false 103 }); 104 // e.g., returns [ 6, 4, 1 ] 105 ``` 106 107 The `probs` option **must** be a numeric array consisting of nonnegative values which sum to one. When sampling _without_ replacement, note that the `probs` option denotes the initial element probabilities which are then updated after each draw. 108 109 #### sample.factory( \[pool, ]\[options] ) 110 111 Returns a `function` to sample elements from an `array`-like object. 112 113 ```javascript 114 var mysample = sample.factory(); 115 116 var out = mysample( [ 0, 1, 2, 3, 4 ] ); 117 // e.g., returns [ 4, 3, 4, 4 ] 118 ``` 119 120 If provided an array-like object `pool`, the returned function will always sample from the supplied object. 121 122 ```javascript 123 var mysample = sample.factory( [ 1, 2, 3, 4, 5, 6 ] ); 124 125 var out = mysample(); 126 // e.g., returns [ 2, 4, 1, 6, 5, 1 ] 127 128 out = mysample(); 129 // e.g., returns [ 5, 2, 3, 6, 1, 4 ] 130 ``` 131 132 The function accepts the following `options`: 133 134 - **seed**: pseudorandom number generator seed. 135 - **size**: sample size. 136 - **mutate**: `boolean` indicating whether to mutate the `pool` when sampling without replacement. Default: `false`. 137 - **replace**: `boolean` indicating whether to sample with replacement. Default: `true`. 138 139 To seed the pseudorandom number generator, set the `seed` option. 140 141 ```javascript 142 var mysample = sample.factory({ 143 'seed': 430 144 }); 145 146 var out = mysample( [ 1, 2, 3, 4, 5, 6 ] ); 147 // e.g., returns [ 1, 1, 1, 5, 4, 4 ] 148 149 mysample = sample.factory( [ 1, 2, 3, 4, 5, 6 ], { 150 'seed': 430 151 }); 152 153 out = mysample(); 154 // e.g., returns [ 1, 1, 1, 5, 4, 4 ] 155 ``` 156 157 To specify a sample size and/or override the default sample size, set the `size` option. 158 159 ```javascript 160 var mysample = sample.factory({ 161 'size': 4 162 }); 163 164 var out = mysample( [ 0, 1 ] ); 165 // e.g., returns [ 0, 0, 0, 1 ] 166 167 // Override the size option... 168 out = mysample( [ 0, 1 ], { 169 'size': 1 170 }); 171 // e.g., returns [ 1 ] 172 ``` 173 174 By default, the returned function draws samples _with_ replacement. To override the default `replace` strategy, set the `replace` option. 175 176 ```javascript 177 var mysample = sample.factory({ 178 'replace': false 179 }); 180 181 var out = mysample( [ 1, 2, 3 ] ); 182 // e.g., returns [ 3, 1, 2 ] 183 ``` 184 185 If a population from which to sample is provided, the underlying `pool` remains constant for each function invocation. To mutate the `pool` by permanently removing observations when sampling _without_ replacement, set the `mutate` option. 186 187 ```javascript 188 var mysample = sample.factory( [ 1, 2, 3, 4, 5, 6 ], { 189 'mutate': true, 190 'replace': false, 191 'size': 3, 192 'seed': 342 193 }); 194 195 var out = mysample(); 196 // e.g., returns [ 6, 5, 3 ] 197 198 // Override the mutate option... 199 out = mysample({ 200 'mutate': false 201 }); 202 // e.g., returns [ 1, 2, 4 ] 203 204 out = mysample(); 205 // e.g., returns [ 1, 2, 4 ] 206 ``` 207 208 The returned function returns `null` after all population units are exhausted. 209 210 ```javascript 211 var mysample = sample.factory( [ 1, 2, 3, 4, 5, 6 ], { 212 'mutate': true, 213 'replace': false 214 }); 215 216 var out = mysample(); 217 // e.g., returns [ 3, 2, 1, 6, 5, 4 ] 218 219 out = mysample(); 220 // returns null 221 ``` 222 223 </section> 224 225 <!-- /.usage --> 226 227 <section class="examples"> 228 229 ## Examples 230 231 <!-- eslint no-undef: "error" --> 232 233 ```javascript 234 var sample = require( '@stdlib/random/sample' ); 235 236 var out; 237 var x; 238 239 // By default, sample uniformly with replacement: 240 x = [ 'a', 'b', 'c', 'd' ]; 241 out = sample( x, { 242 'size': 10 243 }); 244 // e.g., returns [ 'd', 'c', 'b', 'b', 'b', 'd', 'c', 'c', 'b', 'd' ] 245 246 // Sample with replacement with custom probabilities: 247 x = [ 'a', 'b', 'c', 'd' ]; 248 out = sample( x, { 249 'probs': [ 0.1, 0.1, 0.2, 0.6 ], 250 'size': 10 251 }); 252 // e.g., returns [ 'b', 'a', 'c', 'd', 'd', 'd', 'd', 'c', 'd', 'd' ] 253 254 // Sample without replacement: 255 x = [ 'a', 'b', 'c', 'd' ]; 256 out = sample( x, { 257 'size': 3, 258 'replace': false 259 }); 260 // e.g., returns [ 'd', 'c', 'a' ] 261 262 // Sample without replacement when (initial) probabilities are nonuniform: 263 x = [ 1, 2, 3, 4, 5, 6 ]; 264 out = sample( x, { 265 'probs': [ 0.1, 0.1, 0.1, 0.1, 0.1, 0.5 ], 266 'size': 3, 267 'replace': false 268 }); 269 // e.g., returns [ 2, 3, 6 ] 270 ``` 271 272 </section> 273 274 <!-- /.examples --> 275 276 <!-- Section to include cited references. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 277 278 <section class="references"> 279 280 ### References 281 282 - Knuth, Donald E. 1997. _The Art of Computer Programming, Volume 2 (3rd Ed.): Seminumerical Algorithms_. Boston, MA, USA: Addison-Wesley Longman Publishing Co., Inc. 283 - Vose, Michael D. 1991. "A linear algorithm for generating random numbers with a given distribution." _IEEE Transactions on Software Engineering_ 17 (9): 972–75. doi:[10.1109/32.92917][@vose:1991]. 284 285 </section> 286 287 <!-- /.references --> 288 289 <section class="links"> 290 291 [@vose:1991]: https://doi.org/10.1109/32.92917 292 293 </section> 294 295 <!-- /.links -->