<commit_before>(function(define) {'use strict';
define(function(require) {
   /*
    * bratioLog -- see Algorithm 708
    *
    * Return the logs of the tails of the incomplete beta function
    * of x, a, b.
    *
    * Based on "Significant Digit Computation of the Incomplete Beta Function
    * Ratios", by DiDonato and Morris
    */
   var C, stirlerr, log1p, gam1, algdiv, series, contFrac, logspaceAdd,
       phi, lbeta, gratioc, erfc, grat_r;

   C = require('../constants');
   stirlerr = require('./stirlerr').stirlerr;
   log1p = require('./log1p').log1p;
   gam1 = require('./gam1').gam1;
   phi = require('./phi').phi;
   erfc = require('./erf').erfc;
   lbeta = require('./lbeta').lbeta;
   series = require('../utils').series;
   contFrac = require('../utils').contFrac;
   logspaceAdd = require('../utils').logspaceAdd;
   gratioc = require('./gratio').gratioc;
   grat_r = require('./gratio').grat_r;

   // Page 371 of DiDonato/Morris
   // algdiv(a, b) = lgamma(b) - lgamma(a + b)
   algdiv = (function(){
      var ds;
      ds = [
         0.0833333333333333,
        -0.00277777777760991,
         0.793650666825390e-3,
        -0.595202931351870e-3,
         0.837308034031215e-3,
        -0.125322962780713e-2
      ];
      return function algdiv(a, b) {
         var w, p, q, s;
         p = a / (a + b);
         q = b / (a + b);
         s = 1;
         w = p / b * series(function(i) {
            if (i === 0) { return ds[0]; }
            s += Math.pow(q, 2 * i - 1) + Math.pow(q, 2 * i);
            return ds[i] * s / Math.pow(b, 2 * i);
         }, 6);
         // (26)
         return w - (a + b - 0.5) * Math.log(1 + a / b) - a * (Math.log(b) - 1);
      };
   }());

   // bcorr
   // Computes stirlerr(a) + stirlerr(b) - stirlerr(a + b)
   // see DiDonato (23)
   function bcorr(a, b) {
      if (a > b) { return bcorr(b, a); }
      // a <= b
      return stirlerr(a) + algdiv(a, b) +
               (a + b - 0.5) * Math.log(1 + a / b) +
               a * (Math.log(b) - 1);
   }

   // brcomp -- see p. 370 - 372
   // Computes log(x^a * y^b / B(a, b))
   function brcomp(a, b, x, y) {
      var lambda;

      lambda = a - (a + b) * x;
      if (Math.min(a, b) < 8) {
         return a * Math.log(x) + b * Math.log(y) - lbeta(a, b);
      }
      return 0.5 * (Math.log(a) + Math.log(b) - C.log2pi - Math.log(a + b)) -
             a * phi(1 - lambda / a) - b * phi(1 + lambda / b) -
             bcorr(a, b);
   }

   // Assumes lp is the log of the lower prob
   // Returns a pair lower/upper in log space
   // fromUpper assumes given upper prob in log space
   function fromLower(lp) {
      return { lower: lp, upper: log1p(-Math.exp(lp)) };
   }
   function fromUpper(lq) {
      return { lower: log1p(-Math.exp(lq)), upper: lq };
   }

   // tailFlip exchanges roles of "lower" and "upper"
   function tailFlip(o) {
      return {
         lower: o.upper,
         upper: o.lower
      };
   }

   // bpser (7) Returns the log of the lower probability
   function bpser(a, b, x) {
      var c = 1;

      return -lbeta(a, b) + a * Math.log(x) - Math.log(a) +
         log1p(a * series(function(i) {
            if (i === 0) { return 0; }
            c = c * (1 - b / i) * x;
            return c / (a + i);
         }));
   }<commit_msg>Check if series > -1 before taking log1p. Ref #42<commit_after>(function(define) {'use strict';
define(function(require) {
   /*
    * bratioLog -- see Algorithm 708
    *
    * Return the logs of the tails of the incomplete beta function
    * of x, a, b.
    *
    * Based on "Significant Digit Computation of the Incomplete Beta Function
    * Ratios", by DiDonato and Morris
    */
   var C, stirlerr, log1p, gam1, algdiv, series, contFrac, logspaceAdd,
       phi, lbeta, gratioc, erfc, grat_r;

   C = require('../constants');
   stirlerr = require('./stirlerr').stirlerr;
   log1p = require('./log1p').log1p;
   gam1 = require('./gam1').gam1;
   phi = require('./phi').phi;
   erfc = require('./erf').erfc;
   lbeta = require('./lbeta').lbeta;
   series = require('../utils').series;
   contFrac = require('../utils').contFrac;
   logspaceAdd = require('../utils').logspaceAdd;
   gratioc = require('./gratio').gratioc;
   grat_r = require('./gratio').grat_r;

   // Page 371 of DiDonato/Morris
   // algdiv(a, b) = lgamma(b) - lgamma(a + b)
   algdiv = (function(){
      var ds;
      ds = [
         0.0833333333333333,
        -0.00277777777760991,
         0.793650666825390e-3,
        -0.595202931351870e-3,
         0.837308034031215e-3,
        -0.125322962780713e-2
      ];
      return function algdiv(a, b) {
         var w, p, q, s;
         p = a / (a + b);
         q = b / (a + b);
         s = 1;
         w = p / b * series(function(i) {
            if (i === 0) { return ds[0]; }
            s += Math.pow(q, 2 * i - 1) + Math.pow(q, 2 * i);
            return ds[i] * s / Math.pow(b, 2 * i);
         }, 6);
         // (26)
         return w - (a + b - 0.5) * Math.log(1 + a / b) - a * (Math.log(b) - 1);
      };
   }());

   // bcorr
   // Computes stirlerr(a) + stirlerr(b) - stirlerr(a + b)
   // see DiDonato (23)
   function bcorr(a, b) {
      if (a > b) { return bcorr(b, a); }
      // a <= b
      return stirlerr(a) + algdiv(a, b) +
               (a + b - 0.5) * Math.log(1 + a / b) +
               a * (Math.log(b) - 1);
   }

   // brcomp -- see p. 370 - 372
   // Computes log(x^a * y^b / B(a, b))
   function brcomp(a, b, x, y) {
      var lambda;

      lambda = a - (a + b) * x;
      if (Math.min(a, b) < 8) {
         return a * Math.log(x) + b * Math.log(y) - lbeta(a, b);
      }
      return 0.5 * (Math.log(a) + Math.log(b) - C.log2pi - Math.log(a + b)) -
             a * phi(1 - lambda / a) - b * phi(1 + lambda / b) -
             bcorr(a, b);
   }

   // Assumes lp is the log of the lower prob
   // Returns a pair lower/upper in log space
   // fromUpper assumes given upper prob in log space
   function fromLower(lp) {
      return { lower: lp, upper: log1p(-Math.exp(lp)) };
   }
   function fromUpper(lq) {
      return { lower: log1p(-Math.exp(lq)), upper: lq };
   }

   // tailFlip exchanges roles of "lower" and "upper"
   function tailFlip(o) {
      return {
         lower: o.upper,
         upper: o.lower
      };
   }

   // bpser (7) Returns the log of the lower probability
   function bpser(a, b, x) {
      var c, ser;
      c = 1;
      ser = a * series(function(i) {
         if (i === 0) { return 0; }
         c = c * (1 - b / i) * x;
         return c / (a + i);
      });
      ser = ser > -1 ? log1p(ser) : -Infinity;
      return -lbeta(a, b) + a * Math.log(x) - Math.log(a) + ser;
   }
{'fraction_non_alphanumeric': 0.1722480852980928, 'fraction_numerical': 0.047905090854482656, 'mean_word_length': 1.7250409165302782, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, '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': '6250217', 'n_tokens_mistral': 2733, 'n_tokens_neox': 2522, 'n_words': 667}