public static < E > Distribution < E > dynamicCounterWithDirichletPrior ( Counter < E > c , Distribution < E > prior , double weight ) { double totalWeight = c . totalCount ( ) + weight ; Distribution < E > norm = new DynamicDistribution < E > ( prior , weight / totalWeight ) ; norm . counter = new ClassicCounter < E > ( ) ; // this might be done more efficiently with entrySet but there isn't a way to get // the entrySet from a Counter now. In most cases c will be small(-ish) anyway for ( E key : c . keySet ( ) ) { double count = c . getCount ( key ) / totalWeight ; prior . addToKeySet ( key ) ; norm . counter . setCount ( key , count ) ; } norm . numberOfKeys = prior . numberOfKeys ; return norm ; }
Like normalizedCounterWithDirichletPrior except probabilities are computed dynamically from the counter and prior instead of all at once up front . The main advantage of this is if you are making many distributions from relatively sparse counters using the same relatively dense prior the prior is only represented once for major memory savings .