<commit_before>/* logistical.js
 * A logistical regression classifier for Node.js
 *
 * Authors: <NAME> @baywes, <NAME> @sbyrnes
 * Copyright 2015
 *
 * Made available via the MIT License, text included in LICENSE.
 */

var Validator = require('./lib/linear_support.js');

/*
 * Main classifier entity.
 */
var Classifier = function() {};

/*
 * Intermediate function for calculating the partial sum for the likelihood
 *
 * w = vector coefficients with k elements
 * Xi = vector of training data with k elements
 */
Classifier.prototype.ZiPartialSum = function(w, Xi) {
  var validator = new Validator();

  validator.isVector(w, Xi).hasEqualElementCount(w, Xi);

  var sum = w.dot(Xi);

  // Need to try this as "failure" is a null return.  The challenge here is
  // that this doesn't halp the caller.
  if ( sum === null ) throw new Error('Unable to compute the dot product');

  return sum;
};

/*
 * Compute the likelihood for a set of coefficients, data and labels
 *
 * w = Vector coefficients
 * Y = array of labels for the data (Y1, Y2, ..., Yn)
 * X = Matrix of training data vecors (X1, X2, ..., Xn)
 */
Classifier.prototype.likelihood = function(w, Y, X) {
  var validator = new Validator();

  validator.isVector(w).isArray(Y).isMatrix(X);

  // Ensure the partial sum will not throw an error
  validator.hasEqualElementCount(w, X.row(1));

  var N = Y.length;

  var sum = 0;

  for (var i = 0; i < N; i++) {
    sum += Math.log( this.logistic(Y[i] * this.ZiPartialSum(w, X.row(i+1))) );
  }
<commit_msg>modifying the validator usage to a single instance<commit_after>/* logistical.js
 * A logistical regression classifier for Node.js
 *
 * Authors: <NAME> @baywes, <NAME> @sbyrnes
 * Copyright 2015
 *
 * Made available via the MIT License, text included in LICENSE.
 */

var LinearValidator = require('./lib/linear_support.js');

/*
 * Main classifier entity.
 */
var Classifier = function() {
  this.lv = new LinearValidator();
};

/*
 * Intermediate function for calculating the partial sum for the likelihood
 *
 * w = vector coefficients with k elements
 * Xi = vector of training data with k elements
 */
Classifier.prototype.ZiPartialSum = function(w, Xi) {
  this.lv.isVector(w, Xi).hasEqualElementCount(w, Xi);

  var sum = w.dot(Xi);

  // Need to try this as "failure" is a null return.  The challenge here is
  // that this doesn't halp the caller.
  if ( sum === null ) throw new Error('Unable to compute the dot product');

  return sum;
};

/*
 * Compute the likelihood for a set of coefficients, data and labels
 *
 * w = Vector coefficients
 * Y = array of labels for the data (Y1, Y2, ..., Yn)
 * X = Matrix of training data vecors (X1, X2, ..., Xn)
 */
Classifier.prototype.likelihood = function(w, Y, X) {
  this.lv.isVector(w).isArray(Y).isMatrix(X);

  // Ensure the partial sum will not throw an error
  this.lv.hasEqualElementCount(w, X.row(1));

  var N = Y.length;

  var sum = 0;

  for (var i = 0; i < N; i++) {
    sum += Math.log( this.logistic(Y[i] * this.ZiPartialSum(w, X.row(i+1))) );
  }

{'fraction_non_alphanumeric': 0.1316222441592629, 'fraction_numerical': 0.007897334649555774, 'mean_word_length': 3.3741007194244603, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '3894509', 'n_tokens_mistral': 1027, 'n_tokens_neox': 968, 'n_words': 352}