'use strict';

// MODULES //

var sqrt = require( '@stdlib/math/base/special/sqrt' );
var pow = require( '@stdlib/math/base/special/pow' );


// INCRSKEWNESS //

/**
* Returns an accumulator function which incrementally computes a corrected sample skewness.
*
* @returns {Function} accumulator function
*
* @example
* var accumulator = incrskewness();
*/
function incrskewness() {
	var deltaN = 0;
	var delta = 0;
	var term1 = 0;
	var mean = 0;
	var M2 = 0;
	var M3 = 0;
	var N = 0;
	var g1;

	/**
	* If provided a value, the accumulator function returns an updated corrected sample skewness. If not provided a value, the accumulator function returns the current corrected sample skewness.
	*
	* #### Method
	*
	* This implementation computes the corrected sample skewness using the formula for `G_1` in
	*
	* > Jones and Gill (1998). "Comparing measures of sample skewness and kurtosis." *The Statistician*. [DOI: 10.1111/1467-9884.00122]{@link http://onlinelibrary.wiley.com/doi/10.1111/1467-9884.00122/}
	*
	*
	* @param {number} [x] - new value
	* @returns {number} corrected sample skewness
	*
	* @example
	* var skewness = accumulator();
	* // returns null
	*
	* skewness = accumulator( 2.0 );
	* // returns null
	*
	* skewness = accumulator( -5.0 );
	* // returns null
	*
	* skewness = accumulator( -10.0 );
	* // returns ~0.492
	*
	* skewness = accumulator();
	* // returns ~0.492
	*/
	return function accumulator( x ) {
		if ( arguments.length === 0 ) {
			if ( N < 3 ) {
				return null;
			}
			// Calculate the population skewness:
			g1 = sqrt( N )*M3 / pow( M2, 1.5 );
			// Return the corrected sample skewness:
			return sqrt( N*(N-1) )*g1 / (N-2);
		}
		N += 1;
		delta = x - mean;
		deltaN = delta / N;
		term1 = delta * deltaN * (N-1);
		M3 += term1*deltaN*(N-2) - 3.0*deltaN*M2;
		M2 += term1;
		mean += deltaN;
		if ( N < 3 ) {
			return null;
		}
		// Calculate the population skewness:
		g1 = sqrt( N )*M3 / pow( M2, 1.5 );

		// Return the corrected sample skewness:
		return sqrt( N*(N-1) )*g1 / (N-2);
	};
} // end FUNCTION incrskewness()


// EXPORTS //

module.exports = incrskewness;

'use strict';

// MODULES //

var sqrt = require( '@stdlib/math/base/special/sqrt' );
var pow = require( '@stdlib/math/base/special/pow' );


// INCRSKEWNESS //

/**
* Returns an accumulator function which incrementally computes a corrected sample skewness.
*
* #### Method
*
* The algorithm computes the corrected sample skewness using the formula for `G_1` in [Joanes and Gill 1998][@joanes:1998].
*
* #### References
*
* * Joanes, D. N., and C. A. Gill. 1998. "Comparing measures of sample skewness and kurtosis." *Journal of the Royal Statistical Society: Series D (The Statistician)* 47 (1). Blackwell Publishers Ltd: 183–89. doi:[10.1111/1467-9884.00122][@joanes:1998].
*
* [@joanes:1998]: http://dx.doi.org/10.1111/1467-9884.00122
*
*
* @returns {Function} accumulator function
*
* @example
* var accumulator = incrskewness();
*/
function incrskewness() {
	var deltaN = 0;
	var delta = 0;
	var term1 = 0;
	var mean = 0;
	var M2 = 0;
	var M3 = 0;
	var N = 0;
	var g1;

	/**
	* If provided a value, the accumulator function returns an updated corrected sample skewness. If not provided a value, the accumulator function returns the current corrected sample skewness.
	*
	* @param {number} [x] - new value
	* @returns {number} corrected sample skewness
	*
	* @example
	* var skewness = accumulator();
	* // returns null
	*
	* skewness = accumulator( 2.0 );
	* // returns null
	*
	* skewness = accumulator( -5.0 );
	* // returns null
	*
	* skewness = accumulator( -10.0 );
	* // returns ~0.492
	*
	* skewness = accumulator();
	* // returns ~0.492
	*/
	return function accumulator( x ) {
		if ( arguments.length === 0 ) {
			if ( N < 3 ) {
				return null;
			}
			// Calculate the population skewness:
			g1 = sqrt( N )*M3 / pow( M2, 1.5 );
			// Return the corrected sample skewness:
			return sqrt( N*(N-1) )*g1 / (N-2);
		}
		N += 1;
		delta = x - mean;
		deltaN = delta / N;
		term1 = delta * deltaN * (N-1);
		M3 += term1*deltaN*(N-2) - 3.0*deltaN*M2;
		M2 += term1;
		mean += deltaN;
		if ( N < 3 ) {
			return null;
		}
		// Calculate the population skewness:
		g1 = sqrt( N )*M3 / pow( M2, 1.5 );

		// Return the corrected sample skewness:
		return sqrt( N*(N-1) )*g1 / (N-2);
	};
} // end FUNCTION incrskewness()


// EXPORTS //

module.exports = incrskewness;

Fix reference and move to extended comment to parent function