time-to-botec

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

README.md (6659B)


      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 # Kolmogorov-Smirnov Goodness-of-Fit Test
     22 
     23 > One-sample Kolmogorov-Smirnov goodness-of-fit test.
     24 
     25 <section class="usage">
     26 
     27 ## Usage
     28 
     29 ```javascript
     30 var kstest = require( '@stdlib/stats/kstest' );
     31 ```
     32 
     33 #### kstest( x, y\[, ...params]\[, opts] )
     34 
     35 For a numeric [array][mdn-array] or [typed array][mdn-typed-array]
     36 `x`, a Kolmogorov-Smirnov goodness-of-fit is computed for the null hypothesis that the values of `x` come from the distribution specified by `y`. `y` can be either a [string][mdn-string] with the name of the distribution to test against, or a [function][mdn-function]. In the latter case, `y` is expected to be the cumulative distribution function (CDF) of the distribution to test against, with its first parameter being the value at which to evaluate the CDF and the remaining parameters constituting the parameters of the distribution. The parameters of the distribution are passed as additional arguments after `y` from `kstest` to the chosen CDF. The function returns an object holding the calculated test statistic `statistic` and the `pValue` of the test.
     37 
     38 ```javascript
     39 var factory = require( '@stdlib/random/base/uniform' ).factory;
     40 var runif;
     41 var out;
     42 var x;
     43 var i;
     44 
     45 runif = factory( 0.0, 1.0, {
     46     'seed': 8798
     47 });
     48 
     49 x = new Array( 100 );
     50 for ( i = 0; i < x.length; i++ ) {
     51     x[ i ] = runif();
     52 }
     53 out = kstest( x, 'uniform', 0.0, 1.0 );
     54 // returns { 'pValue': ~0.703, 'statistic': ~0.069, ... }
     55 ```
     56 
     57 The returned object comes with a `.print()` method which when invoked will print a formatted output of the hypothesis test results. `print` accepts a `digits` option that controls the number of decimal digits displayed for the outputs and a `decision` option, which when set to `false` will hide the test decision.
     58 
     59 <!-- run-disable -->
     60 
     61 ```javascript
     62 console.log( out.print() );
     63 /* e.g., =>
     64     Kolmogorov-Smirnov goodness-of-fit test.
     65 
     66     Null hypothesis: the CDF of `x` is equal equal to the reference CDF.
     67 
     68         pValue: 0.7039
     69         statistic: 0.0689
     70 
     71     Test Decision: Fail to reject null in favor of alternative at 5% significance level
     72 */
     73 ```
     74 
     75 The function accepts the following `options`:
     76 
     77 -   **alpha**: `number` in the interval `[0,1]` giving the significance level of the hypothesis test. Default: `0.05`.
     78 -   **alternative**: Either `two-sided`, `less` or `greater`. Indicates whether the alternative hypothesis is that the true distribution of `x` is not equal to the reference distribution specified by `y` (`two-sided`), whether it is `less` than the reference distribution or `greater` than the reference distribution. Default: `two-sided`.
     79 -   **sorted**: `boolean` indicating if the `x` [array][mdn-array] is in sorted order (ascending). Default: `false`.
     80 
     81 By default, the test is carried out at a significance level of `0.05`. To choose a custom significance level, set the `alpha` option.
     82 
     83 <!-- run-disable -->
     84 
     85 ```javascript
     86 out = kstest( x, 'uniform', 0.0, 1.0, {
     87     'alpha': 0.1
     88 });
     89 console.log( out.print() );
     90 /* e.g., =>
     91     Kolmogorov-Smirnov goodness-of-fit test.
     92 
     93     Null hypothesis: the CDF of `x` is equal equal to the reference CDF.
     94 
     95         pValue: 0.7039
     96         statistic: 0.0689
     97 
     98     Test Decision: Fail to reject null in favor of alternative at 10% significance level
     99 */
    100 ```
    101 
    102 By default, the function tests the null hypothesis that the true distribution of `x` and the reference distribution `y` are equal to each other against the alternative that they are not equal. To carry out a one-sided hypothesis test, set the `alternative` option to either `less` or `greater`.
    103 
    104 ```javascript
    105 var factory = require( '@stdlib/random/base/uniform' ).factory;
    106 var runif;
    107 var out;
    108 var x;
    109 var i;
    110 
    111 runif = factory( 0.0, 1.0, {
    112     'seed': 8798
    113 });
    114 
    115 x = new Array( 100 );
    116 for ( i = 0; i < x.length; i++ ) {
    117     x[ i ] = runif();
    118 }
    119 
    120 out = kstest( x, 'uniform', 0.0, 1.0, {
    121     'alternative': 'less'
    122 });
    123 // returns { 'pValue': ~0.358, 'statistic': ~0.07, ... }
    124 
    125 out = kstest( x, 'uniform', 0.0, 1.0, {
    126     'alternative': 'greater'
    127 });
    128 // returns { 'pValue': ~0.907, 'statistic': ~0.02, ... }
    129 ```
    130 
    131 To perform the Kolmogorov-Smirnov test, the data has to be sorted in ascending order. If the data in `x` are already sorted, set the `sorted` option to `true` to speed up computation.
    132 
    133 ```javascript
    134 x = [ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9 ];
    135 
    136 out = kstest( x, 'uniform', 0.0, 1.0, {
    137     'sorted': true
    138 });
    139 // returns { 'pValue': ~1, 'statistic': 0.1, ... }
    140 ```
    141 
    142 </section>
    143 
    144 <!-- /.usage -->
    145 
    146 <section class="examples">
    147 
    148 ## Examples
    149 
    150 <!-- eslint no-undef: "error" -->
    151 
    152 ```javascript
    153 var kstest = require( '@stdlib/stats/kstest' );
    154 var factory = require( '@stdlib/random/base/normal' ).factory;
    155 
    156 var rnorm;
    157 var table;
    158 var out;
    159 var i;
    160 var x;
    161 
    162 rnorm = factory({
    163     'seed': 4839
    164 });
    165 
    166 // Values drawn from a Normal(3,1) distribution
    167 x = new Array( 100 );
    168 for ( i = 0; i < 100; i++ ) {
    169     x[ i ] = rnorm( 3.0, 1.0 );
    170 }
    171 
    172 // Test against N(0,1)
    173 out = kstest( x, 'normal', 0.0, 1.0 );
    174 table = out.print();
    175 /* e.g., returns
    176     Kolmogorov-Smirnov goodness-of-fit test.
    177 
    178     Null hypothesis: the CDF of `x` is equal to the reference CDF.
    179 
    180         statistic: 0.847
    181         pValue: 0
    182 
    183     Test Decision: Reject null in favor of alternative at 5% significance level
    184 */
    185 
    186 // Test against N(3,1)
    187 out = kstest( x, 'normal', 3.0, 1.0 );
    188 table = out.print();
    189 /* e.g., returns
    190     Kolmogorov-Smirnov goodness-of-fit test.
    191 
    192     Null hypothesis: the CDF of `x` is equal to the reference CDF.
    193 
    194         statistic: 0.0733
    195         pValue: 0.6282
    196 
    197     Test Decision: Fail to reject null in favor of alternative at 5% significance level
    198 */
    199 ```
    200 
    201 </section>
    202 
    203 <!-- /.examples -->
    204 
    205 <section class="links">
    206 
    207 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
    208 
    209 [mdn-function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function
    210 
    211 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays
    212 
    213 [mdn-string]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
    214 
    215 </section>
    216 
    217 <!-- /.links -->