'use strict';

// MODULES //

var isNumberArray = require( '@stdlib/utils/is-number').isNumberArray;
var isTypedArrayLike = require( '@stdlib/utils/is-typed-array-like' );
var tCDF = require( '@stdlib/math/base/dist/t/cdf' );
var tQuantile = require( '@stdlib/math/base/dist/t/quantile' );
var sqrt = require( '@stdlib/math/base/special/sqrt' );
var abs = require( '@stdlib/math/base/special/abs' );
var pow = require( '@stdlib/math/base/special/pow' );
var NINF = require( '@stdlib/math/constants/float64-ninf' );
var PINF = require( '@stdlib/math/constants/float64-pinf' );
var validate = require( './validate.js' );
var variance = require( './variance.js' );
var print = require( './print.js' );
var mean = require( './mean.js' );


// TTEST2 //

/**
* Computes a two-sample Student's t test.
*
* @param {NumericArray} x - first data array
* @param {NumericArray} y - second data array
* @param {Object} [opts] - function options
* @param {number} [opts.alpha=0.05] - significance level
* @param {string} [opts.alternative='two-sided'] - alternative hypothesis (`two-sided`, `less` or `greater`)
* @param {number} [opts.difference=0] - difference in means under H0
* @param {string} [opts.variance='unequal'] - whether variances are `equal` or `unequal` under H0
* @throws {TypeError} x argument has to be a typed array or array of numbers
* @throws {TypeError} y argument has to be a typed array or array of numbers
* @throws {TypeError} options has to be simple object
* @throws {TypeError} alpha option has to be a number primitive
* @throws {RangeError} alpha option has to be a number in the interval `[0,1]`
* @throws {TypeError} alternative option has to be a string primitive
* @throws {Error} alternative option must be `two-sided`, `less` or `greater`
* @throws {TypeError} difference option has to be a number primitive
* @throws {TypeError} variance option has to be a string primitive
* @throws {Error} variance option must be `equal` or `unequal`
* @returns {Object} result object
*/
function ttest2( x, y, options ) {
	var stderr;
	var alpha;
	var xmean;
	var ymean;
	var vars;
	var cint;
	var diff;
	var opts;
	var pval;
	var xvar;
	var yvar;
	var sdx;
	var sdy;
	var stat;
	var alt;
	var err;
	var nx;
	var ny;
	var df;
	var v;

	if ( !isTypedArrayLike( x ) && !isNumberArray( x ) ) {
		throw new TypeError( 'invalid input argument. First argument `x` must be a numeric array. Value: `' + x + '`.' );
	}
	if ( !isTypedArrayLike( y ) && !isNumberArray( y ) ) {
		throw new TypeError( 'invalid input argument. Second argument `y` must be a numeric array. Value: `' + y + '`.' );
	}
	opts = {};
	if ( options ) {
		err = validate( opts, options );
		if ( err ) {
			throw err;
		}
	}

	diff = opts.difference || 0.0;
	alpha = opts.alpha !== void 0 ? opts.alpha : 0.05;
	if ( alpha < 0.0 || alpha > 1.0 ) {
		throw new RangeError( 'invalid input argument. Option `alpha` must be a number in the range 0 to 1. Value: `' + alpha + '`.' );
	}

	nx = x.length;
	ny = y.length;

	xvar = variance( x );
	yvar = variance( y );

	vars = opts.variance || 'unequal';
	if ( vars === 'equal' ) {
		df = nx + ny - 2;
		v = (nx-1) * xvar + (ny-1) * yvar;
		v /= df;
		stderr = sqrt( v * (1/nx+1/ny) );
	}
	else if ( vars === 'unequal' ) {
		sdx = sqrt( xvar/nx );
		sdy = sqrt( yvar/ny );
		stderr = sqrt( sdx*sdx + sdy*sdy );
		df = pow( stderr, 4 ) / ( pow(sdx,4) / (nx-1) + pow(sdy,4) / (ny-1) );
	}
	else {
		throw new Error( 'Invalid option. `variance` must be either `equal` or `unequal`. Value: `' + vars + '`' );
	}

	xmean = mean( x );
	ymean = mean( y );
	stat = ( xmean - ymean - diff ) / stderr;

	alt = opts.alternative || 'two-sided';
	switch ( alt ) {
	case 'two-sided':
		pval = 2.0 * tCDF( -abs(stat), df );
		cint = [
			stat - tQuantile( 1.0-alpha/2.0, df ),
			stat + tQuantile( 1.0-alpha/2.0, df )
		];
		cint[ 0 ] = diff + cint[ 0 ] * stderr;
		cint[ 1 ] = diff + cint[ 1 ] * stderr;
	break;
	case 'greater':
		pval = 1.0 - tCDF( stat, df );
		cint = [ stat - tQuantile( 1.0-alpha, df ), PINF ];
		cint[ 0 ] = diff + cint[ 0 ] * stderr;
	break;
	case 'less':
		pval = tCDF( stat, df );
		cint = [ NINF, stat + tQuantile( 1.0-alpha, df ) ];
		cint[ 1 ] = diff + cint[ 1 ] * stderr;
	break;
	default:
		throw new Error( 'Invalid option. `alternative` must be either `two-sided`, `less` or `greater`. Value: `' + alt + '`' );
	}

	return {
		'alpha': alpha,
		'rejected': pval <= alpha,
		'pValue': pval,
		'statistic': stat,
		'ci': cint,
		'alternative': alt,
		'method': vars === 'equal' ? 'Two-sample t-test' : 'Welch two-sample t-test',
		'nullValue': diff,
		'print': print
	};

} // end FUNCTION ttest2()


// EXPORTS //

module.exports = ttest2;

@@ -4476,16 +4476,28 @@
 ': alt,%0A
+%09%09'df': df,%0A
 %09%09'metho

Attach df property