<commit_before>'use strict';

// MODULES //

var tape = require( 'tape' );
var onlineSGDRegression = require( './../lib' );


// TESTS //

tape( 'main export is a function', function test( t ) {
	t.ok( true, __filename );
	t.equal( typeof onlineSGDRegression, 'function', 'main export is a function' );
	t.end();
});

tape( 'the function throws an error if the `options` object is not a simple object', function test( t ) {
	var values;
	var i;

	values = [
		'abc',
		5,
		null,
		true,
		undefined,
		NaN,
		[],
		function(){}
	];

	for ( i = 0; i < values.length; i++ ) {
		t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
	}
	t.end();

	function badValue( value ) {
		return function() {
			onlineSGDRegression( value );
		};
	}
});

tape( 'the `loss` option of the function specifies the used loss function', function test( t ) {
	var values;
	var model;
	var len;
	var i;

	values = [
		'epsilonInsensitive',
		'huber',
		'squaredError'
	];
	len = values.length;

	for ( i = 0; i < len; i++ ) {
		model = onlineSGDRegression({
			'loss': values[ i ]
		});
	}

	t.end();
});

tape( 'the function throws an error if supplied an unknown `loss` option', function test( t ) {
	t.throws( badValue, Error, 'throws an error when provided unknown loss' );
	t.end();

	function badValue() {
		onlineSGDRegression({
			'loss': 'hilbert'
		});
	}
});

tape( 'the `learningRate` option can be used to set the strength schedule of the gradient descent algorithm', function test( t ) {
	var values;
	var len;
	var i;

	values = [
		'basic',
		'constant',
		'pegasos'
	];
	for ( i = 0; i < len; i++ ) {
		onlineSGDRegression({
			'learningRate': values[ i ]
		});
	}
	t.end();
});

tape( 'the constructor throws an error if supplied an unknown `learningRate` option', function test( t ) {
	t.throws( badValue, Error, 'throws an error when provided unknown learning rate' );
	t.end();

	function badValue() {
		onlineSGDRegression({
			'learningRate': 'scaling'
		});
	}
});

tape( 'the function has an `intercept` option determining whether the regression model should include an implicit intercept / bias term', function test( t ) {
	var model;

	model = onlineSGDRegression({
		'intercept': true
	});
	model.update( [1.0,1.0,1.0], 2.0 );
	t.equal( model.coefs.length, 4, 'the model contains a regression coefficient for the intercept' );

	model = onlineSGDRegression({
		'intercept': false
	});
	model.update( [1.0,1.0,1.0], 2.0 );
	t.equal( model.coefs.length, 3, 'the model does not contain an intercept regression coefficient' );

	t.end();
});

tape( 'the `update` method throws an error if the `x` value is not an array', function test( t ) {
	var values;
	var model;
	var i;
	var y;

	model = onlineSGDRegression();
	values = [
		'abc',
		5,
		null,
		true,
		undefined,
		NaN,
		{},
		function(){}
	];
	y = 2.0;

	for ( i = 0; i < values.length; i++ ) {
		t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
	}
	t.end();

	function badValue( value ) {
		return function() {
			model.update( value, y );
		};
	}
});

tape( 'the `update` method throws an error if the `x` is an array with an invalid number of elements', function test( t ) {
	var model;
	var i;
	var y;
	var x;

	model = onlineSGDRegression();
	y = 2.0;
	x = [1.0,1.0];
	model.update( x, y );

	for ( i = 0; i < 10; i++ ) {
		x.push( 1.0 );
		t.throws( badValue, TypeError, 'throws an error when provided an array of any length other than two' );
	}
	t.end();

	function badValue() {
		model.update( x, y );
	}
});

tape( 'the `predict` method calculates the inner product of the weights and feature vector `x`', function test( t ) {
	var expected;
	var weights;
	var actual;
	var model;
	var i;
	var x;

	model = onlineSGDRegression();
	model.update( [0.0,0.0], 2.0 );
	weights = model.coefs;

	x = [ 2.0, 2.0 ];
	actual = model.predict( x );
	expected = 0;
	for ( i = 0; i < weights.length; i++ ) {
		expected += x[ i ] * weights[ i ];
	}

	t.end();
});

tape( 'the `predict` method throws an error if the `x` value is not an array', function test( t ) {
	var values;
	var model;
	var i;

	model = onlineSGDRegression();
	values = [
		'abc',
		5,
		null,
		true,
		undefined,
		NaN,
		{},
		function(){}
	];

	for ( i = 0; i < values.length; i++ ) {
		t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
	}
	t.end();

	function badValue( value ) {
		return function() {
			model.predict( value );
		};
	}
});

tape( 'the `predict` method throws an error if the `x` is an array with an invalid number of elements', function test( t ) {
	var model;
	var i;
	var x;

	model = onlineSGDRegression();
	x = [1.0,1.0];
	model.update( x, 2.0 );

	for ( i = 0; i < 10; i++ ) {
		x.push( 1.0 );
		t.throws( badValue, TypeError, 'throws an error when provided an array of any length other than two' );
	}
	t.end();

	function badValue() {
		model.predict( x );
	}
});
<commit_msg>Finish tests<commit_after>'use strict';

// MODULES //

var tape = require( 'tape' );
var onlineSGDRegression = require( './../lib' );


// TESTS //

tape( 'main export is a function', function test( t ) {
	t.ok( true, __filename );
	t.equal( typeof onlineSGDRegression, 'function', 'main export is a function' );
	t.end();
});

tape( 'the function throws an error if the `options` object is not a simple object', function test( t ) {
	var values;
	var i;

	values = [
		'abc',
		5,
		null,
		true,
		undefined,
		NaN,
		[],
		function(){}
	];

	for ( i = 0; i < values.length; i++ ) {
		t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
	}
	t.end();

	function badValue( value ) {
		return function() {
			onlineSGDRegression( value );
		};
	}
});

tape( 'the `loss` option of the function specifies the used loss function', function test( t ) {
	var values;
	var model;
	var len;
	var i;

	values = [
		'epsilonInsensitive',
		'huber',
		'squaredError'
	];
	len = values.length;

	for ( i = 0; i < len; i++ ) {
		model = onlineSGDRegression({
			'loss': values[ i ]
		});
	}

	t.end();
});

tape( 'the function throws an error if supplied an unknown `loss` option', function test( t ) {
	t.throws( badValue, Error, 'throws an error when provided unknown loss' );
	t.end();

	function badValue() {
		onlineSGDRegression({
			'loss': 'hilbert'
		});
	}
});

tape( 'the `learningRate` option can be used to set the strength schedule of the gradient descent algorithm', function test( t ) {
	var values;
	var len;
	var i;

	values = [
		'basic',
		'constant',
		'pegasos'
	];
	for ( i = 0; i < len; i++ ) {
		onlineSGDRegression({
			'learningRate': values[ i ]
		});
	}
	t.end();
});

tape( 'the constructor throws an error if supplied an unknown `learningRate` option', function test( t ) {
	t.throws( badValue, Error, 'throws an error when provided unknown learning rate' );
	t.end();

	function badValue() {
		onlineSGDRegression({
			'learningRate': 'scaling'
		});
	}
});

tape( 'the function has an `intercept` option determining whether the regression model should include an implicit intercept / bias term', function test( t ) {
	var model;

	model = onlineSGDRegression({
		'intercept': true
	});
	model.update( [1.0,1.0,1.0], 2.0 );
	t.equal( model.coefs.length, 4, 'the model contains a regression coefficient for the intercept' );

	model = onlineSGDRegression({
		'intercept': false
	});
	model.update( [1.0,1.0,1.0], 2.0 );
	t.equal( model.coefs.length, 3, 'the model does not contain an intercept regression coefficient' );

	t.end();
});

tape( 'the `update` method throws an error if the `x` value is not an array', function test( t ) {
	var values;
	var model;
	var i;
	var y;

	model = onlineSGDRegression();
	values = [
		'abc',
		5,
		null,
		true,
		undefined,
		NaN,
		{},
		function(){}
	];
	y = 2.0;

	for ( i = 0; i < values.length; i++ ) {
		t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
	}
	t.end();

	function badValue( value ) {
		return function() {
			model.update( value, y );
		};
	}
});

tape( 'the `update` method throws an error if the `x` is an array with an invalid number of elements', function test( t ) {
	var model;
	var i;
	var y;
	var x;

	model = onlineSGDRegression();
	y = 2.0;
	x = [1.0,1.0];
	model.update( x, y );

	for ( i = 0; i < 10; i++ ) {
		x.push( 1.0 );
		t.throws( badValue, TypeError, 'throws an error when provided an array of any length other than two' );
	}
	t.end();

	function badValue() {
		model.update( x, y );
	}
});

tape( 'the `predict` method calculates the inner product of the weights and feature vector `x`', function test( t ) {
	var expected;
	var weights;
	var actual;
	var model;
	var i;
	var x;

	model = onlineSGDRegression();
	model.update( [0.0,0.0], 2.0 );
	weights = model.coefs;

	x = [ 2.0, 2.0 ];
	actual = model.predict( x );

	// Add intercept...
	x.push( 1.0 );
	expected = 0;
	for ( i = 0; i < weights.length; i++ ) {
		expected += x[ i ] * weights[ i ];
	}
	t.deepEqual( actual, expected, 'deep equal' );

	t.end();
});

tape( 'the `predict` method throws an error if the `x` value is not an array', function test( t ) {
	var values;
	var model;
	var i;

	model = onlineSGDRegression();
	values = [
		'abc',
		5,
		null,
		true,
		undefined,
		NaN,
		{},
		function(){}
	];

	for ( i = 0; i < values.length; i++ ) {
		t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
	}
	t.end();

	function badValue( value ) {
		return function() {
			model.predict( value );
		};
	}
});

tape( 'the `predict` method throws an error if the `x` is an array with an invalid number of elements', function test( t ) {
	var model;
	var i;
	var x;

	model = onlineSGDRegression();
	x = [1.0,1.0];
	model.update( x, 2.0 );

	for ( i = 0; i < 10; i++ ) {
		x.push( 1.0 );
		t.throws( badValue, TypeError, 'throws an error when provided an array of any length other than two' );
	}
	t.end();

	function badValue() {
		model.predict( x );
	}
});

{'fraction_non_alphanumeric': 0.1595435892303073, 'fraction_numerical': 0.012611350215193674, 'mean_word_length': 3.233898305084746, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 3, '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': '1375634', 'n_tokens_mistral': 3906, 'n_tokens_neox': 3627, 'n_words': 1224}