time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

README.md (4480B)


      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 # itermmean
     22 
     23 > Create an [iterator][mdn-iterator-protocol] which iteratively computes a moving [arithmetic mean][arithmetic-mean].
     24 
     25 <section class="intro">
     26 
     27 For a window of size `W`, the [arithmetic mean][arithmetic-mean] is defined as
     28 
     29 <!-- <equation class="equation" label="eq:arithmetic_mean" align="center" raw="\bar{x} = \frac{1}{W} \sum_{i=0}^{W-1} x_i" alt="Equation for the arithmetic mean."> -->
     30 
     31 <div class="equation" align="center" data-raw-text="\bar{x} = \frac{1}{W} \sum_{i=0}^{W-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/mmean/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 itermmean = require( '@stdlib/stats/iter/mmean' );
     50 ```
     51 
     52 #### itermmean( iterator, W )
     53 
     54 Returns an [iterator][mdn-iterator-protocol] which iteratively computes a moving [arithmetic mean][arithmetic-mean]. The `W` parameter defines the number of iterated values over which to compute the moving 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 = itermmean( arr, 3 );
     61 
     62 // Fill the window...
     63 var m = it.next().value; // [2.0]
     64 // returns 2.0
     65 
     66 m = it.next().value; // [2.0, 1.0]
     67 // returns 1.5
     68 
     69 m = it.next().value; // [2.0, 1.0, 3.0]
     70 // returns 2.0
     71 
     72 // Window begins sliding...
     73 m = it.next().value; // [1.0, 3.0, -7.0]
     74 // returns -1.0
     75 
     76 m = it.next().value; // [3.0, -7.0, -5.0]
     77 // returns -3.0
     78 ```
     79 
     80 </section>
     81 
     82 <!-- /.usage -->
     83 
     84 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
     85 
     86 <section class="notes">
     87 
     88 ## Notes
     89 
     90 -   If an iterated value is non-numeric (including `NaN`), the function returns `NaN` for **at least** `W-1` future invocations. 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.
     91 -   As `W` values are needed to fill the window buffer, the first `W-1` returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all previously iterated values.
     92 
     93 </section>
     94 
     95 <!-- /.notes -->
     96 
     97 <!-- Package usage examples. -->
     98 
     99 <section class="examples">
    100 
    101 ## Examples
    102 
    103 <!-- eslint no-undef: "error" -->
    104 
    105 ```javascript
    106 var runif = require( '@stdlib/random/iter/uniform' );
    107 var itermmean = require( '@stdlib/stats/iter/mmean' );
    108 
    109 // Create an iterator for generating uniformly distributed pseudorandom numbers:
    110 var rand = runif( -10.0, 10.0, {
    111     'seed': 1234,
    112     'iter': 100
    113 });
    114 
    115 // Create an iterator for iteratively computing a moving arithmetic mean:
    116 var it = itermmean( rand, 3 );
    117 
    118 // Perform manual iteration...
    119 var v;
    120 while ( true ) {
    121     v = it.next();
    122     if ( typeof v.value === 'number' ) {
    123         console.log( 'Mean: %d', v.value );
    124     }
    125     if ( v.done ) {
    126         break;
    127     }
    128 }
    129 ```
    130 
    131 </section>
    132 
    133 <!-- /.examples -->
    134 
    135 <!-- 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. -->
    136 
    137 <section class="references">
    138 
    139 </section>
    140 
    141 <!-- /.references -->
    142 
    143 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    144 
    145 <section class="links">
    146 
    147 [arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean
    148 
    149 [mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
    150 
    151 </section>
    152 
    153 <!-- /.links -->