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

@@ -253,16 +253,498 @@
 ness.%0A*%0A
+* #### Method%0A*%0A* The algorithm computes the corrected sample skewness using the formula for %60G_1%60 in %5BJoanes and Gill 1998%5D%5B@joanes:1998%5D.%0A*%0A* #### References%0A*%0A* * Joanes, D. N., and C. A. Gill. 1998. %22Comparing measures of sample skewness and kurtosis.%22 *Journal of the Royal Statistical Society: Series D (The Statistician)* 47 (1). Blackwell Publishers Ltd: 183%E2%80%9389. doi:%5B10.1111/1467-9884.00122%5D%5B@joanes:1998%5D.%0A*%0A* %5B@joanes:1998%5D: http://dx.doi.org/10.1111/1467-9884.00122%0A*%0A*%0A
 * @retur
@@ -1172,328 +1172,8 @@
 %0A%09*%0A
-%09* #### Method%0A%09*%0A%09* This implementation computes the corrected sample skewness using the formula for %60G_1%60 in%0A%09*%0A%09* %3E Jones and Gill (1998). %22Comparing measures of sample skewness and kurtosis.%22 *The Statistician*. %5BDOI: 10.1111/1467-9884.00122%5D%7B@link http://onlinelibrary.wiley.com/doi/10.1111/1467-9884.00122/%7D%0A%09*%0A%09*%0A
 %09* @

Fix reference and move to extended comment to parent function