We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.
The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.
When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.
To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!
incrgrubbs
[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url]
[Grubbs' test][grubbs-test] for outliers.
s
is the sample standard deviation. The [Grubbs test][grubbs-test] statistic is thus the largest absolute deviation from the sample mean in units of the sample standard deviation.
The [Grubbs' test][grubbs-test] statistic for the alternative hypothesis that the minimum value is an outlier is defined as
The [Grubbs' test][grubbs-test] statistic for the alternative hypothesis that the maximum value is an outlier is defined as
For a two-sided test, the hypothesis that a dataset does not contain an outlier is rejected at significance level α if
where t
denotes the upper critical value of the t-distribution with N-2
degrees of freedom and a significance level of α/(2N)
.
For a one-sided test, the hypothesis that a dataset does not contain an outlier is rejected at significance level α if
where t
denotes the upper critical value of the t-distribution with N-2
degrees of freedom and a significance level of α/N
.
bash
npm install @stdlib/stats-incr-grubbs
javascript
var incrgrubbs = require( '@stdlib/stats-incr-grubbs' );
#### incrgrubbs( [options] )
Returns an accumulator function
which incrementally performs [Grubbs' test][grubbs-test] for outliers.
javascript
var accumulator = incrgrubbs();
The function accepts the following options
:
- alpha: significance level. Default: 0.05
.
- alternative: alternative hypothesis. The option may be one of the following values:
- 'two-sided'
: test whether the minimum or maximum value is an outlier.
- 'min'
: test whether the minimum value is an outlier.
- 'max'
: test whether the maximum value is an outlier.
Default: 'two-sided'
.
- init: number of data points the accumulator should use to compute initial statistics before testing for an outlier. Until the accumulator is provided the number of data points specified by this option, the accumulator returns null
. Default: 100
.
#### accumulator( [x] )
If provided an input value x
, the accumulator function returns updated test results. If not provided an input value x
, the accumulator function returns the current test results.
javascript
var rnorm = require( '@stdlib/random-base-normal' );
var opts = {
'init': 0
};
var accumulator = incrgrubbs( opts );
var results = accumulator( rnorm( 10.0, 5.0 ) );
// returns null
results = accumulator( rnorm( 10.0, 5.0 ) );
// returns null
results = accumulator( rnorm( 10.0, 5.0 ) );
// returns <Object>
results = accumulator();
// returns <Object>
The accumulator function returns an object
having the following fields:
- rejected: boolean indicating whether the null hypothesis should be rejected.
- alpha: significance level.
- criticalValue: critical value.
- statistic: test statistic.
- df: degrees of freedom.
- mean: sample mean.
- sd: corrected sample standard deviation.
- min: minimum value.
- max: maximum value.
- alt: alternative hypothesis.
- method: method name.
- print: method for pretty-printing test output.
The print
method accepts the following options:
- digits: number of digits after the decimal point. Default: 4
.
- decision: boolean
indicating whether to print the test decision. Default: true
.
null
.
- Input values are not type checked. If provided NaN
or a value which, when used in computations, results in NaN
, the test statistic is NaN
for all 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.
javascript
var incrgrubbs = require( '@stdlib/stats-incr-grubbs' );
var data;
var opts;
var acc;
var i;
// Define a data set (8 mass spectrometer measurements of a uranium isotope; see Tietjen and Moore. 1972. "Some Grubbs-Type Statistics for the Detection of Several Outliers".)
data = [ 199.31, 199.53, 200.19, 200.82, 201.92, 201.95, 202.18, 245.57 ];
// Create a new accumulator:
opts = {
'init': data.length,
'alternative': 'two-sided'
};
acc = incrgrubbs( opts );
// Update the accumulator:
for ( i = 0; i < data.length; i++ ) {
acc( data[ i ] );
}
// Print the test results:
console.log( acc().print() );
/* e.g., =>
Grubbs' Test
Alternative hypothesis: The maximum value (245.57) is an outlier
criticalValue: 2.1266
statistic: 2.4688
df: 6
Test Decision: Reject null in favor of alternative at 5% significance level
*/
@stdlib/stats-incr/mgrubbs
][@stdlib/stats/incr/mgrubbs]: moving Grubbs' test for outliers.