README.md (4051B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2019 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 # itercumean 22 23 > Create an [iterator][mdn-iterator-protocol] which iteratively computes a cumulative [arithmetic mean][arithmetic-mean]. 24 25 <section class="intro"> 26 27 The cumulative [arithmetic mean][arithmetic-mean] is defined as 28 29 <!-- <equation class="equation" label="eq:cumulative_arithmetic_mean" align="center" raw="\bar{x}_n = \frac{1}{n} \sum_{i=0}^n x_i" alt="Equation for the cumulative arithmetic mean."> --> 30 31 <div class="equation" align="center" data-raw-text="\bar{x}_n = \frac{1}{n} \sum_{i=0}^n x_i" data-equation="eq:cumulative_arithmetic_mean"> 32 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@409c465a46090d66ce8ea7980f14e8f28a95e9be/lib/node_modules/@stdlib/stats/iter/cumean/docs/img/equation_cumulative_arithmetic_mean.svg" alt="Equation for the cumulative 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 itercumean = require( '@stdlib/stats/iter/cumean' ); 50 ``` 51 52 #### itercumean( iterator ) 53 54 Returns an [iterator][mdn-iterator-protocol] which iteratively computes a cumulative [arithmetic mean][arithmetic-mean]. 55 56 ```javascript 57 var array2iterator = require( '@stdlib/array/to-iterator' ); 58 59 var arr = array2iterator( [ 2.0, 1.0, 3.0, -7.0, -5.0 ] ); 60 var it = itercumean( arr ); 61 62 var m = it.next().value; 63 // returns 2.0 64 65 m = it.next().value; 66 // returns 1.5 67 68 m = it.next().value; 69 // returns 2.0 70 71 m = it.next().value; 72 // returns -0.25 73 74 m = it.next().value; 75 // returns -1.2 76 ``` 77 78 </section> 79 80 <!-- /.usage --> 81 82 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 83 84 <section class="notes"> 85 86 ## Notes 87 88 - If an iterated value is non-numeric (including `NaN`), the function returns `NaN` for **all** future iterations. 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. 89 90 </section> 91 92 <!-- /.notes --> 93 94 <!-- Package usage examples. --> 95 96 <section class="examples"> 97 98 ## Examples 99 100 <!-- eslint no-undef: "error" --> 101 102 ```javascript 103 var runif = require( '@stdlib/random/iter/uniform' ); 104 var itercumean = require( '@stdlib/stats/iter/cumean' ); 105 106 // Create an iterator for generating uniformly distributed pseudorandom numbers: 107 var rand = runif( -10.0, 10.0, { 108 'seed': 1234, 109 'iter': 100 110 }); 111 112 // Create an iterator for iteratively computing a cumulative mean: 113 var it = itercumean( rand ); 114 115 // Perform manual iteration... 116 var v; 117 while ( true ) { 118 v = it.next(); 119 if ( typeof v.value === 'number' ) { 120 console.log( 'mean: %d', v.value ); 121 } 122 if ( v.done ) { 123 break; 124 } 125 } 126 ``` 127 128 </section> 129 130 <!-- /.examples --> 131 132 <!-- 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. --> 133 134 <section class="references"> 135 136 </section> 137 138 <!-- /.references --> 139 140 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 141 142 <section class="links"> 143 144 [arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean 145 146 [mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol 147 148 </section> 149 150 <!-- /.links -->