#NEWLINE# def __init__( self , prob_dict = None , log = False , normalize = False ) : #NEWLINE# #INDENT# '\n        Construct a new probability distribution from the given\n        dictionary, which maps values to probabilities (or to log\n        probabilities, if ``log`` is true).  If ``normalize`` is\n        true, then the probability values are scaled by a constant\n        factor such that they sum to 1.\n\n        If called without arguments, the resulting probability\n        distribution assigns zero probability to all values.\n        ' #NEWLINE# self . _prob_dict = ( prob_dict . copy ( ) if ( prob_dict is not None ) else { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) #NEWLINE# self . _log = log #NEWLINE# if normalize : #NEWLINE# #INDENT# if ( len ( prob_dict ) == 0 ) : #NEWLINE# #INDENT# raise ValueError ( ( 'A DictionaryProbDist must have at least one sample ' + 'before it can be normalized.' ) ) #NEWLINE# #UNINDENT# if log : #NEWLINE# #INDENT# value_sum = sum_logs ( list ( self . _prob_dict . values ( ) ) ) #NEWLINE# if ( value_sum <= _NINF ) : #NEWLINE# #INDENT# logp = math . log ( ( 1.0 / len ( prob_dict ) ) , 2 ) #NEWLINE# for x in prob_dict : #NEWLINE# #INDENT# self . _prob_dict [ x ] = logp #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# for ( x , p ) in self . _prob_dict . items ( ) : #NEWLINE# #INDENT# self . _prob_dict [ x ] -= value_sum #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# value_sum = sum ( self . _prob_dict . values ( ) ) #NEWLINE# if ( value_sum == 0 ) : #NEWLINE# #INDENT# p = ( 1.0 / len ( prob_dict ) ) #NEWLINE# for x in prob_dict : #NEWLINE# #INDENT# self . _prob_dict [ x ] = logp #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# norm_factor = ( 1.0 / value_sum ) #NEWLINE# for ( x , p ) in self . _prob_dict . items ( ) : #NEWLINE# #INDENT# self . _prob_dict [ x ] *= norm_factor