npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@stdlib/stats-incr-mgrubbs

v0.2.2

Published

Moving Grubbs' test for outliers.

Downloads

247

Readme

incrmgrubbs

NPM version Build Status Coverage Status

Moving Grubbs' test for outliers.

Grubbs' test (also known as the maximum normalized residual test or extreme studentized deviate test) is a statistical test used to detect outliers in a univariate dataset assumed to come from a normally distributed population. Grubbs' test is defined for the hypothesis:

  • H_0: the dataset does not contain outliers.
  • H_1: the dataset contains exactly one outlier.

For a window of size W, the Grubbs' test statistic for a two-sided alternative hypothesis is defined as

where s is the sample standard deviation. The Grubbs test statistic is thus the largest absolute deviation from the sample mean in units of the sample standard deviation.

The Grubbs' test statistic for the alternative hypothesis that the minimum value is an outlier is defined as

The 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 W-2 degrees of freedom and a significance level of α/(2W).

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 W-2 degrees of freedom and a significance level of α/W.

Installation

npm install @stdlib/stats-incr-mgrubbs

Usage

var incrmgrubbs = require( '@stdlib/stats-incr-mgrubbs' );

incrmgrubbs( window[, options] )

Returns an accumulator function which incrementally performs Grubbs' test for outliers. The window parameter defines the number of values over which to perform Grubbs' test.

var accumulator = incrmgrubbs( 20 );

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'.

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.

var rnorm = require( '@stdlib/random-base-normal' );

var accumulator = incrmgrubbs( 3 );

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.

Notes

  • Grubbs' test assumes that data is normally distributed. Accordingly, one should first verify that the data can be reasonably approximated by a normal distribution before applying the Grubbs' test.
  • The minimum window size is 3. In general, the larger the window, the more robust outlier detection will be. However, larger windows entail increased memory consumption.
  • Until window values have been provided, the accumulator returns null.
  • Input values are not type checked. If provided NaN or a value which, when used in computations, results in NaN, the accumulated test statistic 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.

Examples

var sensorData = require( '@stdlib/datasets-suthaharan-single-hop-sensor-network' );
var incrmgrubbs = require( '@stdlib/stats-incr-mgrubbs' );

var data;
var opts;
var acc;
var N;
var r;
var i;

// Get a test dataset:
data = sensorData();
N = 0;
for ( i = 0; i < data.length; i++ ) {
    if ( data[ i ].mote_id === 1 ) {
        N += 1;
        data[ i ] = data[ i ].temperature;
    }
}
data.length = N;

// Create a new accumulator which analyzes the last 5 minutes of data:
opts = {
    'alternative': 'two-sided'
};
acc = incrmgrubbs( 60, opts );

// Update the accumulator:
for ( i = 0; i < data.length; i++ ) {
    r = acc( data[ i ] );
    if ( r && r.rejected ) {
        console.log( 'Index: %d', i );
        console.log( '' );
        console.log( r.print() );
    }
}

References

  • Grubbs, Frank E. 1950. "Sample Criteria for Testing Outlying Observations." The Annals of Mathematical Statistics 21 (1). The Institute of Mathematical Statistics: 27–58. doi:10.1214/aoms/1177729885.
  • Grubbs, Frank E. 1969. "Procedures for Detecting Outlying Observations in Samples." Technometrics 11 (1). Taylor & Francis: 1–21. doi:10.1080/00401706.1969.10490657.

See Also


Notice

This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.

For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.

Community

Chat


License

See LICENSE.

Copyright

Copyright © 2016-2024. The Stdlib Authors.