parameters: (game, data, min_tau, max_tau, min_lam, max_lam,
                  grid_size=10, sample_cands=20, verbose=False)
return_statement: return results[0]
docstring: Compute maximum likelihood estimate of (tau, lambda) parameters
      for cognitive hierarchy model.  Operates with initial grid
      search over [ min_tau, max_tau ] x [ min_lam, max_lam ],
      with 'grid_size' points in each direction.  Then, uses
      Newton function minimization starting at the top 'sample_cands'
      points, to polish the maximizer.
docstring_summary: Compute maximum likelihood estimate of (tau, lambda) parameters
      for cognitive hierarchy model.  Operates with initial grid
      search over [ min_tau, max_tau ] x [ min_lam, max_lam ],
      with 'grid_size' points in each direction.  Then, uses
      Newton function minimization starting at the top 'sample_cands'
      points, to polish the maximizer.
docstring_tokens: Compute maximum likelihood estimate of ( tau lambda ) parameters for cognitive hierarchy model . Operates with initial grid search over [ min_tau max_tau ] x [ min_lam max_lam ] with grid_size points in each direction . Then uses Newton function minimization starting at the top sample_cands points to polish the maximizer .
function: def fit_coghier(game, data, min_tau, max_tau, min_lam, max_lam,
                  grid_size=10, sample_cands=20, verbose=False):
      """
      Compute maximum likelihood estimate of (tau, lambda) parameters
      for cognitive hierarchy model.  Operates with initial grid
      search over [ min_tau, max_tau ] x [ min_lam, max_lam ],
      with 'grid_size' points in each direction.  Then, uses
      Newton function minimization starting at the top 'sample_cands'
      points, to polish the maximizer.
      """
      def log_like(profile, data):
          return sum(math.log(p) * d for (p, d) in zip(profile, data))
  
      def objective(params, game, data):
          penalty = 0.0
          tau, lam = params
          if lam < 0.0:
              penalty += lam*lam
              lam = 0.0
          if tau < 0.0:
              penalty += tau*tau
              tau = 0.0
          profile = compute_coghier(game, tau, lam)
          logL = log_like(profile, data)
          return penalty - logL
  
      results = []
      for lam in scipy.linspace(min_lam, max_lam, grid_size):
          if verbose:
              print(f"Searching lambda={lam:.3f}")
          for tau in scipy.linspace(min_tau, max_tau, grid_size):
              profile = compute_coghier(game, tau, lam)
              profile.logL = log_like(profile, data)
              results.append(profile)
          results.sort(key=lambda x: x.logL)
          results = results[:sample_cands]
          if verbose:
              print("tau,lam,logL")
              for profile in results:
                  print(f"{profile.tau:f},{profile.lam:f},{profile.logL:f}")
              print()
  
      if verbose:
          print()
      for start in results[:sample_cands]:
          params = scipy.optimize.fmin(lambda x: objective(x, game, data),
                                       (start.tau, start.lam),
                                       disp=0)
          end_tau, end_lam = list(params)
          profile = compute_coghier(game, end_tau, end_lam)
          profile.logL = log_like(profile, data)
          results.append(profile)
          if verbose:
              print(f"{profile.tau:f},{profile.lam:f},{profile.logL:f}")
      results.sort(key=lambda x: x.logL)
      return results[0]
function_tokens: def fit_coghier ( game , data , min_tau , max_tau , min_lam , max_lam , grid_size = 10 , sample_cands = 20 , verbose = False ) : def log_like ( profile , data ) : return sum ( math . log ( p ) * d for ( p , d ) in zip ( profile , data ) ) def objective ( params , game , data ) : penalty = 0.0 tau , lam = params if lam < 0.0 : penalty += lam * lam lam = 0.0 if tau < 0.0 : penalty += tau * tau tau = 0.0 profile = compute_coghier ( game , tau , lam ) logL = log_like ( profile , data ) return penalty - logL results = [ ] for lam in scipy . linspace ( min_lam , max_lam , grid_size ) : if verbose : print ( f"Searching lambda={lam:.3f}" ) for tau in scipy . linspace ( min_tau , max_tau , grid_size ) : profile = compute_coghier ( game , tau , lam ) profile . logL = log_like ( profile , data ) results . append ( profile ) results . sort ( key = lambda x : x . logL ) results = results [ : sample_cands ] if verbose : print ( "tau,lam,logL" ) for profile in results : print ( f"{profile.tau:f},{profile.lam:f},{profile.logL:f}" ) print ( ) if verbose : print ( ) for start in results [ : sample_cands ] : params = scipy . optimize . fmin ( lambda x : objective ( x , game , data ) , ( start . tau , start . lam ) , disp = 0 ) end_tau , end_lam = list ( params ) profile = compute_coghier ( game , end_tau , end_lam ) profile . logL = log_like ( profile , data ) results . append ( profile ) if verbose : print ( f"{profile.tau:f},{profile.lam:f},{profile.logL:f}" ) results . sort ( key = lambda x : x . logL ) return results [ 0 ]