<commit_before>'use strict';

// MODULES //

var isNonNegativeIntegerArray = require( '@stdlib/utils/is-nonnegative-integer-array' );
var isNonNegativeNumberArray = require( '@stdlib/utils/is-nonnegative-number-array' );
var isUnityProbabilityArray = require( '@stdlib/utils/is-unity-probability-array' );
var setReadOnly = require( '@stdlib/utils/define-read-only-property' );
var incrspace = require( '@stdlib/math/generics/utils/incrspace' );
var chisqCDF = require( '@stdlib/math/base/dist/chisquare/cdf' );
var isNumber = require( '@stdlib/utils/is-number' ).isPrimitive;
var isString = require( '@stdlib/utils/is-string' ).isPrimitive;
var sample = require( '@stdlib/math/generics/random/sample' );
var isnan = require( '@stdlib/utils/is-nan' );
var copy = require( '@stdlib/utils/copy' );
var testStatistic = require( './statistic.js' );
var prettyPrint = require( './print.js' );
var defaults = require( './defaults.json' );
var tabulate = require( './tabulate.js' );
var getPMF = require( './get_pmf.js' );
var mult = require( './mult.js' );
var sum = require( './sum.js' );
var div = require( './div.js' );
var validate = require( './validate.js' );


// MAIN //

/**
* Performs a chi-square goodness-of-fit test.
*
* @param {NonNegativeIntegerArray} x - observation frequencies
* @param {(NonNegativeNumericArray|ProbabilityArray|string)} y - array of expected values / probabilities or a string denoting the name of a distribution
* @param {...number} [params] - distribution parameters passed to mean function
* @param {Options} [options] - function options
* @param {number} [options.alpha=0.05] - significance level
* @param {NonNegativeInteger} [options.ddof=0] - degrees of freedom adjustment
* @param {boolean} [options.simulate=false] - boolean indicating whether to compute p-values by Monte Carlo simulation
* @param {PositiveInteger} [options.iterations=500] - number of Monte Carlo iterations
* @throws {TypeError} first argument must be an array of nonnegative integers
* @throws {TypeError} second argument must be an array of nonnegative numbers (when rescale = true)
* @throws {TypeError} second argument must be an array of probabilities summing to one (when rescale = false)
* @throws {TypeError} options argument must be an object
* @throws {TypeError} must provide valid options
* @throws {Error} `x` and `p` must have the same length
* @throws {Error} at least one element of `x` must be positive
* @throws {RangeError} alpha option must be a number in the interval `[0,1]`
* @returns {Object} test results
*
* @example
* var x = [ 89, 37, 30, 28, 2 ];
* var p = [ 0.40, 0.20, 0.20, 0.15, 0.05 ];
*
* var out = chi2gof( x, p );
* // returns { 'pValue': ~0.0406, 'statistic': ~9.9901, ... }
*/
function chi2gof() {
	var nDistParams = 0;
	var distParams;
	var distArgs;
	var expected;
	var options;
	var sfreq;
	var sstat;
	var args;
	var draw;
	var opts;
	var psum;
	var pval;
	var stat;
	var err;
	var len;
	var pmf;
	var seq;
	var geq;
	var out;
	var val;
	var df;
	var i;
	var n;
	var p;
	var x;

	args = arguments;
	x = args[ 0 ];
	if ( !isNonNegativeIntegerArray( x ) ) {
		throw new TypeError( 'invalid input argument. First argument must be an array of nonnegative integers. Value: `' + x + '`.' );
	}
	len = x.length;
	n = sum( x );
	if ( n === 0.0 ) {
		throw new Error( 'invalid input argument. At least one element of `x` must be positive. Value: `' + x + '`.' );
	}

	if ( isUnityProbabilityArray( args[ 1 ] ) ) {
		p = args[ 1 ];
		if ( len !== p.length ) {
			throw new Error( 'invalid input arguments. First and second parameter must be arrays of the same length.' );
		}
		expected = mult( new Array( len ), p, n );
	}
	else if ( isString( args[ 1 ] ) ) {
		pmf = getPMF( args[ 1 ] );
		nDistParams = pmf.length - 1;
		distParams = new Array( nDistParams );
		for ( i = 0; i < nDistParams; i++ ) {
			val = args[ i + 2 ];
			if ( !isNumber( val ) || isnan( val ) ) {
				throw new TypeError( 'invalid input argument. Distribution parameter must be a number primitive. Value: `' + val + '`.' );
			}
			distParams[ i ] = val;
		}
		distArgs = [ null ].concat( distParams );
		expected = new Array( len );
		for ( i = 0; i < len; i++ ) {
			distArgs[ 0 ] = i;
			val = pmf.apply( null, distArgs );
			expected[ i ] = val * n;<commit_msg>Add category to make probabilities sum to one Random variables of discrete distributions have often a countably infinite number of possible values. Since the user-supplied observed frequency table will only contain frequencies up to a certain number, we append another category for which the observed count is zero and the expected count is n * P(X >= len).<commit_after>'use strict';

// MODULES //

var isNonNegativeIntegerArray = require( '@stdlib/utils/is-nonnegative-integer-array' );
var isNonNegativeNumberArray = require( '@stdlib/utils/is-nonnegative-number-array' );
var isUnityProbabilityArray = require( '@stdlib/utils/is-unity-probability-array' );
var setReadOnly = require( '@stdlib/utils/define-read-only-property' );
var incrspace = require( '@stdlib/math/generics/utils/incrspace' );
var chisqCDF = require( '@stdlib/math/base/dist/chisquare/cdf' );
var isNumber = require( '@stdlib/utils/is-number' ).isPrimitive;
var isString = require( '@stdlib/utils/is-string' ).isPrimitive;
var sample = require( '@stdlib/math/generics/random/sample' );
var isnan = require( '@stdlib/utils/is-nan' );
var copy = require( '@stdlib/utils/copy' );
var testStatistic = require( './statistic.js' );
var prettyPrint = require( './print.js' );
var defaults = require( './defaults.json' );
var tabulate = require( './tabulate.js' );
var getPMF = require( './get_pmf.js' );
var mult = require( './mult.js' );
var sum = require( './sum.js' );
var div = require( './div.js' );
var validate = require( './validate.js' );


// MAIN //

/**
* Performs a chi-square goodness-of-fit test.
*
* @param {NonNegativeIntegerArray} x - observation frequencies
* @param {(NonNegativeNumericArray|ProbabilityArray|string)} y - array of expected values / probabilities or a string denoting the name of a distribution
* @param {...number} [params] - distribution parameters passed to mean function
* @param {Options} [options] - function options
* @param {number} [options.alpha=0.05] - significance level
* @param {NonNegativeInteger} [options.ddof=0] - degrees of freedom adjustment
* @param {boolean} [options.simulate=false] - boolean indicating whether to compute p-values by Monte Carlo simulation
* @param {PositiveInteger} [options.iterations=500] - number of Monte Carlo iterations
* @throws {TypeError} first argument must be an array of nonnegative integers
* @throws {TypeError} second argument must be an array of nonnegative numbers (when rescale = true)
* @throws {TypeError} second argument must be an array of probabilities summing to one (when rescale = false)
* @throws {TypeError} options argument must be an object
* @throws {TypeError} must provide valid options
* @throws {Error} `x` and `p` must have the same length
* @throws {Error} at least one element of `x` must be positive
* @throws {RangeError} alpha option must be a number in the interval `[0,1]`
* @returns {Object} test results
*
* @example
* var x = [ 89, 37, 30, 28, 2 ];
* var p = [ 0.40, 0.20, 0.20, 0.15, 0.05 ];
*
* var out = chi2gof( x, p );
* // returns { 'pValue': ~0.0406, 'statistic': ~9.9901, ... }
*/
function chi2gof() {
	var nDistParams = 0;
	var distParams;
	var distArgs;
	var expected;
	var options;
	var sfreq;
	var sstat;
	var args;
	var draw;
	var opts;
	var psum;
	var pval;
	var stat;
	var err;
	var len;
	var pmf;
	var seq;
	var geq;
	var out;
	var val;
	var df;
	var i;
	var n;
	var p;
	var x;

	args = arguments;
	x = args[ 0 ];
	if ( !isNonNegativeIntegerArray( x ) ) {
		throw new TypeError( 'invalid input argument. First argument must be an array of nonnegative integers. Value: `' + x + '`.' );
	}
	len = x.length;
	n = sum( x );
	if ( n === 0.0 ) {
		throw new Error( 'invalid input argument. At least one element of `x` must be positive. Value: `' + x + '`.' );
	}

	if ( isUnityProbabilityArray( args[ 1 ] ) ) {
		p = args[ 1 ];
		if ( len !== p.length ) {
			throw new Error( 'invalid input arguments. First and second parameter must be arrays of the same length.' );
		}
		expected = mult( new Array( len ), p, n );
	}
	else if ( isString( args[ 1 ] ) ) {
		pmf = getPMF( args[ 1 ] );
		nDistParams = pmf.length - 1;
		distParams = new Array( nDistParams );
		for ( i = 0; i < nDistParams; i++ ) {
			val = args[ i + 2 ];
			if ( !isNumber( val ) || isnan( val ) ) {
				throw new TypeError( 'invalid input argument. Distribution parameter must be a number primitive. Value: `' + val + '`.' );
			}
			distParams[ i ] = val;
		}
		distArgs = [ null ].concat( distParams );
		expected = new Array( len );
		psum = 0.0;
		for ( i = 0; i < len; i++ ) {
			distArgs[ 0 ] = i;
			val = pmf.apply( null, distArgs );
			psum += val;
			expected[ i ] = val * n;
		}
		if ( psum < 1.0 ) {
			// Add remaining category for all values greater or equal to `len`:
			val = 1.0 - psum;
			expected.push( val * n );
			x = Array.prototype.slice.call( x );
			x.push( 0 );
			len += 1;
{'fraction_non_alphanumeric': 0.1429975966790474, 'fraction_numerical': 0.013545990823683636, 'mean_word_length': 3.651930894308943, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2016984', 'n_tokens_mistral': 3207, 'n_tokens_neox': 2973, 'n_words': 1061}