Calculate the Loglikelihood coefficient
@param array $ngram Array of ngrams with frequencies
@return float Return the calculated value
778193
static public function ll(array $ngram, int $totalNgrams) : float
    {
        $var = self::setStatVariables($ngram, $totalNgrams);

        $logLikelihood = 0;

        if($var['jointFrequency']) {
            $logLikelihood += $var['jointFrequency'] * self::computePMI ( $var['jointFrequency'], $var['m11'] );
        }

        if($var['LminusJ']) {
            $logLikelihood += $var['LminusJ'] * self::computePMI( $var['LminusJ'], $var['m12'] );
        }

        if($var['RminusJ']) {
            $logLikelihood += $var['RminusJ']  * self::computePMI( $var['RminusJ'], $var['m21'] );
        }

        if($var['n22']) {
            $logLikelihood += $var['n22'] * self::computePMI( $var['n22'], $var['m22'] );
        }

        return $logLikelihood * 2;
    }
1153714
def to_ngrams(self, terms):
        ''' 
        '''
        if len(terms) <= self.n:
            return terms
        if self.n == 1:
            n_grams = [[term] for term in terms]
        else:
            n_grams = []
            for i in range(0,len(terms)-self.n+1):
                n_grams.append(terms[i:i+self.n])
        return n_grams
239832
protected function calcRelativeValues()
    {
        foreach ($this->stackLog as $key => $value) {
            foreach ($this->metrics as $metric) {
                foreach (array('own', 'sub', 'total') as $column) {
                    if (!isset($this->stackLog[$key][$metric . '_' . $column])) {
                        continue;
                    }
                    $this->stackLog[$key][$metric . '_rel_' . $column] = $this->stackLog[$key][$metric . '_' . $column] / $this->stackLog['timetracker_0'][$metric . '_total'];
                }
                $this->stackLog[$key][$metric . '_rel_offset'] = $this->stackLog[$key][$metric . '_start_relative'] / $this->stackLog['timetracker_0'][$metric . '_total'];
            }
        }
    }
942766
private void normalizeLogArr(double[] arr) {
    // Find the log-scale normalization value
    double max = Double.NEGATIVE_INFINITY;
    for (double d : arr) {
      if (d > max) max = d;
    }
    double expSum = 0.0;
    for (double d : arr) {
      expSum += Math.exp(d - max);
    }
    double logSumExp = max + Math.log(expSum);

    if (Double.isInfinite(logSumExp)) {
      // Just put in uniform probabilities if we are normalizing all 0s
      for (int i = 0; i < arr.length; i++) {
        arr[i] = 1.0 / arr.length;
      }
    } else {
      // Normalize in log-scale before exponentiation, to help with stability
      for (int i = 0; i < arr.length; i++) {
        arr[i] = Math.exp(arr[i] - logSumExp);
      }
    }
  }
1254275
def grad_local_log_likelihood(self, x):
        
        T, D = self.T, self.D_latent
        assert x.shape == (T, D)
        gfun = grad(self.local_log_likelihood)

        g = np.zeros((T, D))
        for t in range(T):
            g[t] += gfun(x[t], self.data[t], self.inputs[t])
        return g
623509
public double[] conditionalLogProbsGivenPrevious(int[] given) {
    if (given.length != windowSize - 1) {
      throw new IllegalArgumentException("conditionalLogProbsGivenPrevious requires given one less than clique size (" +
          windowSize + ") but was " + Arrays.toString(given));
    }
    double[] result = new double[numClasses];
    for (int i = 0; i < numClasses; i++) {
      int index = indexOf(given, i);
      result[i] = table[index];
    }
    ArrayMath.logNormalize(result);
    return result;
  }
951970
def ngram(n, iter_tokens):
    
    z = len(iter_tokens)
    return (iter_tokens[i:i+n] for i in range(z-n+1))
293426
def logp_plus_loglike(self):
        '''
        
        '''
        sum = logp_of_set(self.markov_blanket)
        if self.verbose > 2:
            print_('\t' + self._id +
                   ' Current log-likelihood plus current log-probability', sum)
        return sum
650805
static private function ngramsAsArray(string $sep, array $ngrams) : array {
        $ngramsArray = array();
        foreach($ngrams as $key => $ngram) {
            $ngramsArray[] = explode($sep, $ngram);
        }
        return $ngramsArray;
    }
155914
static public function getFreq(array $ngrams, string $sep = ' ') : array
    {
        //getting the frequencies of the ngrams array and an array with no repetition
        $ngramsUnique = array_count_values($ngrams);

        //array to be the product of this function
        $ngramsFinal = array();

        //creates an array of tokens per ngram
        $ngramsArray = self::ngramsAsArray($sep, $ngrams);

        $ngramSize = count($ngramsArray[0]);

        $tokens_frequencies = self::readFreq($sep, $ngrams);
        $combo_frequencies  = self::readCombFreq($sep, $ngrams);

        //interate the array with no repeated ngrams
        foreach ($ngramsUnique as $ngramString => $ngramFrequency) {
            $ngramsFinal[$ngramString] = array($ngramFrequency); //putting into the final array an array of frequencies (first, the ngram frequency)

            $ngramArray = explode($sep, $ngramString); //getting an array of tokens of the ngram
            foreach ($ngramArray as $kToken => $token) { //iterating the array of tokens of the ngram
                $ngramsFinal[$ngramString][$kToken+1] = $tokens_frequencies[$token][$kToken]; //getting the frequency of the token

                if($ngramSize > 2) {
                    //getting the combined frequency of the tokens
                    for ($i = $kToken+1; $i < $ngramSize; $i++) {
                        $ngramsFinal[$ngramString][$ngramSize+$kToken+$i] = $combo_frequencies[$token.$sep.$ngramArray[$i]][$kToken][$i];
                    }
                }
            }

        }

        return $ngramsFinal;
    }
970449
private function round($n, $sigfigs)
    {
        $new_log = (int)floor(log(abs($n), 10)); // Number of digits left of decimal - 1
        $rp = $sigfigs - $new_log - 1; // Number of decimal places needed
        $neg = $n < 0 ? '-' : ''; // Negative sign
        if ($this->bcmath) {
            if ($rp >= 0) {
                $n = bcadd($n, $neg . '0.' . str_repeat('0', $rp) . '5', $rp + 1);
                $n = bcdiv($n, '1', $rp);
            } else {
                // This algorithm partially depends on the standardized
                // form of numbers that comes out of bcmath.
                $n = bcadd($n, $neg . '5' . str_repeat('0', $new_log - $sigfigs), 0);
                $n = substr($n, 0, $sigfigs + strlen($neg)) . str_repeat('0', $new_log - $sigfigs + 1);
            }
            return $n;
        } else {
            return $this->scale(round($n, $sigfigs - $new_log - 1), $rp + 1);
        }
    }
93847
public static DoubleAD logSum(DoubleAD[] logInputs, int fromIndex, int toIndex) {
    if (logInputs.length == 0)
      throw new IllegalArgumentException();
    if(fromIndex >= 0 && toIndex < logInputs.length && fromIndex >= toIndex)
      return new DoubleAD(Double.NEGATIVE_INFINITY,Double.NEGATIVE_INFINITY);
    int maxIdx = fromIndex;
    double max = logInputs[fromIndex].getval();
    double maxdot = logInputs[fromIndex].getdot();

    for (int i = fromIndex+1; i < toIndex; i++) {
      if (logInputs[i].getval() > max) {
        maxIdx = i;
        maxdot = logInputs[i].getdot();
        max = logInputs[i].getval();
      }
    }

    DoubleAD ret = new DoubleAD();
    boolean haveTerms = false;
    double intermediate = 0.0;
    double intermediateDot = 0.0;
    double curEXP;
    double cutoff = max - SloppyMath.LOGTOLERANCE;
    // we avoid rearranging the array and so test indices each time!
    for (int i = fromIndex; i < toIndex; i++) {
      if (i != maxIdx && logInputs[i].getval() > cutoff) {
        haveTerms = true;
        curEXP = Math.exp(logInputs[i].getval() - max);
        intermediate += curEXP;
        intermediateDot += curEXP*logInputs[i].getdot();
      }
    }
    if (haveTerms) {
      ret.setval(max + Math.log(1.0 + intermediate));
      ret.setdot((maxdot + intermediateDot)/(1.0 + intermediate));
    } else {
      ret.setval(max);
      ret.setdot(maxdot);
    }
      return ret;
  }
1323186
static public function tmi(array $ngram, int $totalNgrams) : float
    {
        $var = self::setStatVariables($ngram, $totalNgrams);

        $tmi = 0;

        if($var['jointFrequency']) {
            $tmi += $var['jointFrequency']/$totalNgrams * self::computePMI( $var['jointFrequency'], $var['m11'] )/ log(2);
        }

        if($var['LminusJ']) {
            $tmi += $var['LminusJ']/$totalNgrams * self::computePMI( $var['LminusJ'], $var['m12'] )/ log(2);
        }

        if($var['RminusJ']) {
            $tmi += $var['RminusJ']/$totalNgrams * self::computePMI( $var['RminusJ'], $var['m21'] )/ log(2);
        }

        if($var['n22']) {
            $tmi += $var['n22']/$totalNgrams * self::computePMI( $var['n22'], $var['m22'] )/ log(2);
        }

        return $tmi;
    }
286399
public function createLM( $text, $maxNgrams ) {
		$ngram = [];
		foreach ( preg_split( "/[{$this->wordSeparator}]+/u", $text ) as $word ) {
			if ( empty( $word ) ) {
				continue;
			}
			$word = "_" . $word . "_";
			$len = mb_strlen( $word, "UTF-8" );
			for ( $i = 0; $i < $len; $i++ ) {
				$rlen = $len - $i;
				if ( $rlen > 4 ) {
					@$ngram[mb_substr( $word, $i, 5, "UTF-8" )]++;
				}
				if ( $rlen > 3 ) {
					@$ngram[mb_substr( $word, $i, 4, "UTF-8" )]++;
				}
				if ( $rlen > 2 ) {
					@$ngram[mb_substr( $word, $i, 3, "UTF-8" )]++;
				}
				if ( $rlen > 1 ) {
					@$ngram[mb_substr( $word, $i, 2, "UTF-8" )]++;
				}
				@$ngram[mb_substr( $word, $i, 1, "UTF-8" )]++;
			}
		}
		if ( $this->minFreq ) {
			$min = $this->minFreq;
			$ngram = array_filter( $ngram, function ( $v ) use ( $min ) {
				return $v > $min;
			} );
		}
		uksort( $ngram, function ( $k1, $k2 ) use ( $ngram ) {
				if ( $ngram[$k1] == $ngram[$k2] ) {
					return strcmp( $k1, $k2 );
				}
				return $ngram[$k2] - $ngram[$k1];
		} );
		if ( count( $ngram ) > $maxNgrams ) {
			array_splice( $ngram, $maxNgrams );
		}
		return $ngram;
	}
1293271
static public function fisher(array $ngram, int $totalNgrams, string $side) : float
    {
        $var = self::setStatVariables($ngram, $totalNgrams);

        # we shall have two	arrays one for the numerator and one for the
        # denominator. the arrays will contain the factorial upper limits. we
        # shall be arrange these two arrays	in descending order. while doing the
        # actual calculation, we shall take	a numerator/denominator	pair, and
        # go from the lower	value to the higher value, in effect doing a
        # "cancellation" of	sorts.

        # first create the numerator
        $numerator = array($var['leftFrequency'], $var['rightFrequency'], $var['TminusL'], $var['TminusR']);
        arsort($numerator);

        # now to the real calculation!!!
        $probability = 0;
        $i = 0;
        $j = 0;
        # we shall calculate for n11 = 0. thereafter we shall just multiply	and
        # divide the result	for 0 with correct numbers to obtain result for	i,
        # i>0, i<=n11!! :o)

        ########### this part by Nitin O Verma

        if($side == 'left') {
            $finalLimit = $var['jointFrequency'];
            $var['jointFrequency'] = 0;
            $var['LminusJ'] = $var['leftFrequency'];
            $var['RminusJ'] = $var['rightFrequency'];
            $var['n22'] = $var['TminusL'] - $var['RminusJ'];

            while($var['n22'] < 0) {
                $var['jointFrequency']++;
                $var['LminusJ'] = $var['leftFrequency'] - $var['jointFrequency'];
                $var['RminusJ'] = $var['rightFrequency'] - $var['jointFrequency'];
                $var['n22'] = $var['TminusL'] - $var['RminusJ'];
            }

        } else {
            $finalLimit = ($var['leftFrequency'] < $var['rightFrequency']) ? $var['leftFrequency'] : $var['rightFrequency'];
        }

        ########### end of part by Nitin O Verma

        $denominator = array($totalNgrams, $var['n22'], $var['LminusJ'], $var['RminusJ'], $var['jointFrequency']);
        arsort($denominator);

        # now that we have our two arrays all nicely sorted	and in place,
        # lets do the calculations!

        $dLimits  = array();
        $nLimits  = array();
        $dIndex   = 0;
        $nIndex   = 0;

        for($j = 0; $j < 4; $j ++) {
            if ( $numerator[$j] > $denominator[$j] ) {
                $nLimits[$nIndex] =	$denominator[$j] + 1;
                $nLimits[$nIndex+1]	= $numerator[$j];
                $nIndex += 2;
            } elseif ($denominator[$j] > $numerator[$j]) {
                $dLimits[$dIndex] =	$numerator[$j] + 1;
                $dLimits[$dIndex+1]	= $denominator[$j];
                $dIndex += 2;
            }
        }

        $dLimits[$dIndex] =	1;
        $dLimits[$dIndex+1]	= $denominator[4];

        $product = 1;

        while(isset($nLimits[0])) {

            while(($product < 10000) && (isset($nLimits[0]))) {
                $product *=	$nLimits[0];
                $nLimits[0]++;
                if ( $nLimits[0] > $nLimits[1] ) {
                    array_shift($nLimits);
                    array_shift($nLimits);
                }
            }

            while($product > 1) {
                $product /=	$dLimits[0];
                $dLimits[0]++;
                if ( $dLimits[0] > $dLimits[1] ) {
                    array_shift($dLimits);
                    array_shift($dLimits);
                }
            }
        }

        while(isset($dLimits[0])) {
            $product /= $dLimits[0];
            $dLimits[0]++;
            if($dLimits[0] > $dLimits[1]) {
                array_shift($dLimits);
                array_shift($dLimits);
            }
        }

        # $product now has the hypergeometric probability for n11 =	0. add it to
        # the cumulative probability
        $probability += $product;

        # Bridget Thomson McInnes October 15 2003
        # I set i <= final_Limit rather than n11 because we want to sum the
        # hypergeometric probabilities where the count in n11 is less and or
        # equal to the observed value.

        # now for the rest of n11's	!!

        $i = ($side == 'left') ? 1 : $var['jointFrequency']+1;

        for($i; $i <= $finalLimit; $i++ ) {
            $product *= $var['LminusJ'];

            $var['n22']++;
            if($var['n22'] <= 0) {
                continue;
            }
            $product /= $var['n22'];
            $product *= $var['RminusJ'];
            $var['LminusJ']--;
            $var['RminusJ']--;
            $product /= $i;

            # thats	our new	probability for	n11 = i! :o)) cool eh? ;o))
            # add it to the	main probability! :o))
            $probability +=	$product; # !! :o)
        }

        return $probability;
    }
1217913
function countNgrams(ngram) {
  nrOfNgrams++;
  var key = arrayToKey(ngram);
  if (!frequencies[key]) {
    frequencies[key] = 0;
  }
  frequencies[key]++;
}
379327
public static function getReadableSize($bytes)
    {
        $unit = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
        $exp = floor(log($bytes, 1024)) | 0;

        return round($bytes / (pow(1024, $exp)), 2) . " $unit[$exp]";
    }