time-to-botec

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

README.md (6161B)


      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 # incrmpcorr2
     22 
     23 > Compute a moving squared sample [Pearson product-moment correlation coefficient][pearson-correlation] incrementally.
     24 
     25 <section class="intro">
     26 
     27 The [Pearson product-moment correlation coefficient][pearson-correlation] between random variables `X` and `Y` is defined as
     28 
     29 <!-- <equation class="equation" label="eq:pearson_correlation_coefficient" align="center" raw="\rho_{X,Y} = \frac{\operatorname{cov}(X,Y)}{\sigma_X \sigma_Y}" alt="Equation for the Pearson product-moment correlation coefficient."> -->
     30 
     31 <div class="equation" align="center" data-raw-text="\rho_{X,Y} = \frac{\operatorname{cov}(X,Y)}{\sigma_X \sigma_Y}" data-equation="eq:pearson_correlation_coefficient">
     32     <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@e6bc812ab63010afd0f25418c0c6954c3a680357/lib/node_modules/@stdlib/stats/incr/mpcorr2/docs/img/equation_pearson_correlation_coefficient.svg" alt="Equation for the Pearson product-moment correlation coefficient.">
     33     <br>
     34 </div>
     35 
     36 <!-- </equation> -->
     37 
     38 where the numerator is the [covariance][covariance] and the denominator is the product of the respective standard deviations.
     39 
     40 For a sample of size `W`, the sample [Pearson product-moment correlation coefficient][pearson-correlation] is defined as
     41 
     42 <!-- <equation class="equation" label="eq:sample_pearson_correlation_coefficient" align="center" raw="r = \frac{\sum_{i=0}^{n-1} (x_i - \bar{x})(y_i - \bar{y})}{\sqrt{\sum_{i=0}^{n-1} (x_i - \bar{x})^2} \sqrt{\sum_{i=0}^{n-1} (y_i - \bar{y})^2}}" alt="Equation for the sample Pearson product-moment correlation coefficient."> -->
     43 
     44 <div class="equation" align="center" data-raw-text="r = \frac{\sum_{i=0}^{n-1} (x_i - \bar{x})(y_i - \bar{y})}{\sqrt{\sum_{i=0}^{n-1} (x_i - \bar{x})^2} \sqrt{\sum_{i=0}^{n-1} (y_i - \bar{y})^2}}" data-equation="eq:sample_pearson_correlation_coefficient">
     45     <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@e6bc812ab63010afd0f25418c0c6954c3a680357/lib/node_modules/@stdlib/stats/incr/mpcorr2/docs/img/equation_sample_pearson_correlation_coefficient.svg" alt="Equation for the sample Pearson product-moment correlation coefficient.">
     46     <br>
     47 </div>
     48 
     49 <!-- </equation> -->
     50 
     51 The squared sample [Pearson product-moment correlation coefficient][pearson-correlation] is thus defined as the square of the sample [Pearson product-moment correlation coefficient][pearson-correlation].
     52 
     53 </section>
     54 
     55 <!-- /.intro -->
     56 
     57 <section class="usage">
     58 
     59 ## Usage
     60 
     61 ```javascript
     62 var incrmpcorr2 = require( '@stdlib/stats/incr/mpcorr2' );
     63 ```
     64 
     65 #### incrmpcorr2( window\[, mx, my] )
     66 
     67 Returns an accumulator `function` which incrementally computes a moving squared sample [Pearson product-moment correlation coefficient][pearson-correlation]. The `window` parameter defines the number of values over which to compute the moving squared sample [Pearson product-moment correlation coefficient][pearson-correlation].
     68 
     69 ```javascript
     70 var accumulator = incrmpcorr2( 3 );
     71 ```
     72 
     73 If means are already known, provide `mx` and `my` arguments.
     74 
     75 ```javascript
     76 var accumulator = incrmpcorr2( 3, 5.0, -3.14 );
     77 ```
     78 
     79 #### accumulator( \[x, y] )
     80 
     81 If provided input values `x` and `y`, the accumulator function returns an updated accumulated value. If not provided input values `x` and `y`, the accumulator function returns the current accumulated value.
     82 
     83 ```javascript
     84 var accumulator = incrmpcorr2( 3 );
     85 
     86 var r2 = accumulator();
     87 // returns null
     88 
     89 // Fill the window...
     90 r2 = accumulator( 2.0, 1.0 ); // [(2.0, 1.0)]
     91 // returns 0.0
     92 
     93 r2 = accumulator( -5.0, 3.14 ); // [(2.0, 1.0), (-5.0, 3.14)]
     94 // returns ~1.0
     95 
     96 r2 = accumulator( 3.0, -1.0 ); // [(2.0, 1.0), (-5.0, 3.14), (3.0, -1.0)]
     97 // returns ~0.86
     98 
     99 // Window begins sliding...
    100 r2 = accumulator( 5.0, -9.5 ); // [(-5.0, 3.14), (3.0, -1.0), (5.0, -9.5)]
    101 // returns ~0.74
    102 
    103 r2 = accumulator( -5.0, 1.5 ); // [(3.0, -1.0), (5.0, -9.5), (-5.0, 1.5)]
    104 // returns ~0.64
    105 
    106 r2 = accumulator();
    107 // returns ~0.64
    108 ```
    109 
    110 </section>
    111 
    112 <!-- /.usage -->
    113 
    114 <section class="notes">
    115 
    116 ## Notes
    117 
    118 -   Input values are **not** type checked. If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for **at least** `W-1` future invocations. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function.
    119 -   As `W` (x,y) pairs 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 provided values.
    120 -   In comparison to the sample [Pearson product-moment correlation coefficient][pearson-correlation], the squared sample [Pearson product-moment correlation coefficient][pearson-correlation] is useful for emphasizing strong correlations.
    121 
    122 </section>
    123 
    124 <!-- /.notes -->
    125 
    126 <section class="examples">
    127 
    128 ## Examples
    129 
    130 <!-- eslint no-undef: "error" -->
    131 
    132 ```javascript
    133 var randu = require( '@stdlib/random/base/randu' );
    134 var incrmpcorr2 = require( '@stdlib/stats/incr/mpcorr2' );
    135 
    136 var accumulator;
    137 var x;
    138 var y;
    139 var i;
    140 
    141 // Initialize an accumulator:
    142 accumulator = incrmpcorr2( 5 );
    143 
    144 // For each simulated datum, update the moving squared sample correlation coefficient...
    145 for ( i = 0; i < 100; i++ ) {
    146     x = randu() * 100.0;
    147     y = randu() * 100.0;
    148     accumulator( x, y );
    149 }
    150 console.log( accumulator() );
    151 ```
    152 
    153 </section>
    154 
    155 <!-- /.examples -->
    156 
    157 <section class="links">
    158 
    159 [pearson-correlation]: https://en.wikipedia.org/wiki/Pearson_correlation_coefficient
    160 
    161 [covariance]: https://en.wikipedia.org/wiki/Covariance
    162 
    163 </section>
    164 
    165 <!-- /.links -->