<commit_before>import numpy
import itertools
import iminuit
import warnings
import matplotlib.pyplot as plt


class Minimizer(object):
  def __init__(self,function,parameters,ftol=1e-3,verbosity=1):
    
    self.function             = function
    self.parameters           = parameters
    self.Npar                 = len(self.parameters.keys())
    self.ftol                 = ftol
    self.verbosity            = verbosity
  
  def minimize(self):
    raise NotImplemented("This is the method of the base class. Must be implemented by the actual minimizer")

class iMinuitMinimizer(Minimizer):
  def __init__(self,function,parameters,ftol=1,verbosity=0):
    
    super(iMinuitMinimizer, self).__init__(function,parameters,ftol,verbosity)
    
    #Prepare the parameter dictionary for iminuit
    pars                      = {}
    varnames                  = []
    
    for k,par in parameters.iteritems():
      
      curName                 = "%s of %s" %(k[1],k[0])
      
      varnames.append(curName)
      
      #Initial value
      pars['%s' % curName]       = par.value
      
      #Initial delta
      pars['error_%s' % curName] = par.delta
      
      #Limits
      pars['limit_%s' % curName] = [par.minValue,par.maxValue]
      
      #This is useless, since all parameters here are free,
      #but do it anyway for clarity
      pars['fix_%s' % curName]   = False
    
    
    #Since the number and names of the parameters cannot be extracted
    #from the signature of the likelihood function in input,
    #we use the forced_parameters facility which allows for their
    #explicit specification (see iMinuit manual for details)
    
    pars['forced_parameters'] = varnames 
    
    #This is to tell iMinuit that we are dealing with likelihoods,
    #not chi square
    pars['errordef']          = 0.5
        
    pars['print_level']       = verbosity
    
    #Finally we can instance the Minuit class
    self.minuit               = iminuit.Minuit(self.function, **pars)
    
    self.minuit.tol           = ftol
    #self.minuit.strategy      = 2 #More accurate
        
  def minimize(self):
    
    #Repeat Migrad up to 10 times, until it converges
    for i in range(10):
      
      self.minuit.migrad()
      
      if(self.minuit.migrad_ok()):
        <commit_msg>Increased the default tolerance to ftol = 1000. Minuit converges when the expected distance from the minimum value of the logL is 1e-4 x tol x 0.5 . We don't need a precision of 1e-4 on the likelihood value, 0.05 is more than enough.<commit_after>import numpy
import itertools
import iminuit
import warnings
import matplotlib.pyplot as plt


class Minimizer(object):
  def __init__(self,function,parameters,ftol=1e-3,verbosity=1):
    
    self.function             = function
    self.parameters           = parameters
    self.Npar                 = len(self.parameters.keys())
    self.ftol                 = ftol
    self.verbosity            = verbosity
  
  def minimize(self):
    raise NotImplemented("This is the method of the base class. Must be implemented by the actual minimizer")

class iMinuitMinimizer(Minimizer):
  def __init__(self,function,parameters,ftol=1000,verbosity=0):
    
    super(iMinuitMinimizer, self).__init__(function,parameters,ftol,verbosity)
    
    #Prepare the parameter dictionary for iminuit
    pars                      = {}
    varnames                  = []
    
    for k,par in parameters.iteritems():
      
      curName                 = "%s of %s" %(k[1],k[0])
      
      varnames.append(curName)
      
      #Initial value
      pars['%s' % curName]       = par.value
      
      #Initial delta
      pars['error_%s' % curName] = par.delta
      
      #Limits
      pars['limit_%s' % curName] = [par.minValue,par.maxValue]
      
      #This is useless, since all parameters here are free,
      #but do it anyway for clarity
      pars['fix_%s' % curName]   = False
    
    
    #Since the number and names of the parameters cannot be extracted
    #from the signature of the likelihood function in input,
    #we use the forced_parameters facility which allows for their
    #explicit specification (see iMinuit manual for details)
    
    pars['forced_parameters'] = varnames 
    
    #This is to tell iMinuit that we are dealing with likelihoods,
    #not chi square
    pars['errordef']          = 0.5
        
    pars['print_level']       = verbosity
    
    #Finally we can instance the Minuit class
    self.minuit               = iminuit.Minuit(self.function, **pars)
    
    self.minuit.tol           = ftol
    self.minuit.strategy      = 1 #More accurate
        
  def minimize(self):
    
    #Repeat Migrad up to 10 times, until it converges
    for i in range(10):
      
      self.minuit.migrad()
      
      if(self.minuit.migrad_ok()):
        
{'fraction_non_alphanumeric': 0.08254033926354985, 'fraction_numerical': 0.009102192800992967, 'mean_word_length': 2.129449838187702, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '5088492', 'n_tokens_mistral': 1460, 'n_tokens_neox': 1410, 'n_words': 421}