Download the Jupyter Notebook for this section: optimization.ipynb

Optimization Example

import pyblp
import numpy as np


In this example, we’ll build a L-BFGS-B configuration with a non-default tolerance.

optimization = pyblp.Optimization('l-bfgs-b', {'gtol': 1e-3})
Configured to optimize using the L-BFGS-B algorithm implemented in SciPy with analytic gradients and options {gtol: +1.000000E-03}.

Next, instead of using a non-custom routine, we’ll create a custom method that implements a grid search over parameter values between specified bounds.

from itertools import product
def custom_method(initial, bounds, objective_function, iteration_callback):
   best_values = initial
   best_objective = np.inf
   for values in product(*(np.linspace(l, u, 10) for l, u in bounds)):
       objective, _ = objective_function(values)
       if objective < best_objective:
           best_values = values
           best_objective = objective
   return best_values, True

We can then use this custom method to build an optimization configuration.

optimization = pyblp.Optimization(custom_method, compute_gradient=False)
Configured to optimize using a custom method without analytic gradients and options {}.