program 4/naiveBayes.js
/**
  * Encapsulate all the probability/frequency distro fns
  */
Encapsulate all the probability/frequency distro fns
Encapsulate all the probability / frequency distro fns
function computeProbabilities(data){
      var rawCount = data.rawcount;
      var epsilon = 0.1;
      var PC = function(freq_c, totalCats){
        return ((freq_c ? freq_c:0)+epsilon) / (1+totalCats*epsilon);
      };
      var PWC = function(freq_wc){
        return (freq_wc + epsilon) / (1+2*epsilon);
      };
      var LC = function (prob_cats) {
        return -(Math.log2(prob_cats));
      }
      var LWC = function (pwc) {
        return -(Math.log2(pwc));
      }
      var freqDistro = function (totalWords, totalCats) {
        return (totalWords ? totalWords:0) / (totalCats ? totalCats:0);
      }
      Object.keys(rawCount.words).forEach(function(word){
        data.freq.words[word] = {};
        data.prob.words[word] = {};
        data.lg.words[word]   = {};
        Object.keys(rawCount.cats).forEach(function(cat){
          data.freq.cats[cat]         = freqDistro( rawCount.cats[cat] , Object.keys(rawCount.cats).length);
          data.freq.words[word][cat]  = freqDistro( rawCount.words[word][cat] , rawCount.cats[cat]);
          data.prob.cats[cat]         = PC(  data.freq.cats[cat], Object.keys(rawCount.cats).length);
          data.prob.words[word][cat]  = PWC( data.freq.words[word][cat] );
          data.lg.cats[cat]           = LC( data.prob.cats[cat] );
          data.lg.words[word][cat]    = LWC( data.prob.words[word][cat] );
        })
      })
      return;
    }
function computeProbabilities ( data ) { var rawCount = data . rawcount ; var epsilon = 0.1 ; var PC = function ( freq_c , totalCats ) { return ( ( freq_c ? freq_c : 0 ) + epsilon ) / ( 1 + totalCats * epsilon ) ; } ; var PWC = function ( freq_wc ) { return ( freq_wc + epsilon ) / ( 1 + 2 * epsilon ) ; } ; var LC = function ( prob_cats ) { return - ( Math . log2 ( prob_cats ) ) ; } var LWC = function ( pwc ) { return - ( Math . log2 ( pwc ) ) ; } var freqDistro = function ( totalWords , totalCats ) { return ( totalWords ? totalWords : 0 ) / ( totalCats ? totalCats : 0 ) ; } Object . keys ( rawCount . words ) . forEach ( function ( word ) { data . freq . words [ word ] = { } ; data . prob . words [ word ] = { } ; data . lg . words [ word ] = { } ; Object . keys ( rawCount . cats ) . forEach ( function ( cat ) { data . freq . cats [ cat ] = freqDistro ( rawCount . cats [ cat ] , Object . keys ( rawCount . cats ) . length ) ; data . freq . words [ word ] [ cat ] = freqDistro ( rawCount . words [ word ] [ cat ] , rawCount . cats [ cat ] ) ; data . prob . cats [ cat ] = PC ( data . freq . cats [ cat ] , Object . keys ( rawCount . cats ) . length ) ; data . prob . words [ word ] [ cat ] = PWC ( data . freq . words [ word ] [ cat ] ) ; data . lg . cats [ cat ] = LC ( data . prob . cats [ cat ] ) ; data . lg . words [ word ] [ cat ] = LWC ( data . prob . words [ word ] [ cat ] ) ; } ) } ) return ; }
Encapsulate all the probability/frequency distro fns
Encapsulate all the probability / frequency distro fns
// now that we've tabulated all the counts, start workingout all the probabilities //console.log(rawCount.cats) /* For each classification C, define FreqT (C) = OccT (C)/|T|, 
          the fraction of the biographies that are of category C.*/ // freq of words per cat  = total amount of words per cat / total amount of cats