Download the Jupyter Notebook for this section: optimization.ipynb

Optimization Example

[1]:
import pyblp
import numpy as np

pyblp.__version__
[1]:
'0.10.1'

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

[2]:
optimization = pyblp.Optimization('l-bfgs-b', {'gtol': 1e-3})
optimization
[2]:
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.

[3]:
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
       iteration_callback()
   return best_values, True

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

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