README.md (3548B)
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 # itermean 22 23 > Compute the [arithmetic mean][arithmetic-mean] over all [iterated][mdn-iterator-protocol] values. 24 25 <section class="intro"> 26 27 The [arithmetic mean][arithmetic-mean] is defined as 28 29 <!-- <equation class="equation" label="eq:arithmetic_mean" align="center" raw="\mu = \frac{1}{n} \sum_{i=0}^{n-1} x_i" alt="Equation for the arithmetic mean."> --> 30 31 <div class="equation" align="center" data-raw-text="\mu = \frac{1}{n} \sum_{i=0}^{n-1} x_i" data-equation="eq:arithmetic_mean"> 32 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@e7ac225deb396ee6d2b4d5fc1a2faa645582349f/lib/node_modules/@stdlib/stats/iter/mean/docs/img/equation_arithmetic_mean.svg" alt="Equation for the arithmetic mean."> 33 <br> 34 </div> 35 36 <!-- </equation> --> 37 38 </section> 39 40 <!-- /.intro --> 41 42 <!-- Package usage documentation. --> 43 44 <section class="usage"> 45 46 ## Usage 47 48 ```javascript 49 var itermean = require( '@stdlib/stats/iter/mean' ); 50 ``` 51 52 #### itermean( iterator ) 53 54 Computes the [arithmetic mean][arithmetic-mean] over all [iterated][mdn-iterator-protocol] values. 55 56 ```javascript 57 var array2iterator = require( '@stdlib/array/to-iterator' ); 58 59 var arr = array2iterator( [ 1.0, 2.0, 3.0, 4.0 ] ); 60 61 var m = itermean( arr ); 62 // returns 2.5 63 ``` 64 65 </section> 66 67 <!-- /.usage --> 68 69 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 70 71 <section class="notes"> 72 73 ## Notes 74 75 - If an iterated value is non-numeric (including `NaN`), the returned [`iterator`][mdn-iterator-protocol] returns `NaN`. If non-numeric iterated values are possible, you are advised to provide an [`iterator`][mdn-iterator-protocol] which type checks and handles non-numeric values accordingly. 76 77 </section> 78 79 <!-- /.notes --> 80 81 <!-- Package usage examples. --> 82 83 <section class="examples"> 84 85 ## Examples 86 87 <!-- eslint no-undef: "error" --> 88 89 ```javascript 90 var runif = require( '@stdlib/random/iter/uniform' ); 91 var itermean = require( '@stdlib/stats/iter/mean' ); 92 93 // Create an iterator for generating uniformly distributed pseudorandom numbers: 94 var rand = runif( -10.0, 10.0, { 95 'seed': 1234, 96 'iter': 100 97 }); 98 99 // Compute the arithmetic mean: 100 var m = itermean( rand ); 101 // returns <number> 102 103 console.log( 'Mean: %d.', m ); 104 ``` 105 106 </section> 107 108 <!-- /.examples --> 109 110 <!-- 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. --> 111 112 <section class="references"> 113 114 </section> 115 116 <!-- /.references --> 117 118 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 119 120 <section class="links"> 121 122 [arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean 123 124 [mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol 125 126 </section> 127 128 <!-- /.links -->