Download the Jupyter Notebook for this section: iteration.ipynb

Iteration Example

import pyblp
import numpy as np


In this example, we’ll build a SQUAREM configuration with a \(\ell^2\)-norm and use scheme S1 from Varadhan and Roland (2008).

iteration = pyblp.Iteration('squarem', {'norm': np.linalg.norm, 'scheme': 1})
Configured to iterate using the SQUAREM acceleration method without analytic Jacobians with options {atol: +1.000000E-14, rtol: 0, max_evaluations: 5000, norm: numpy.linalg.norm, scheme: 1, step_min: +1.000000E+00, step_max: +1.000000E+00, step_factor: +4.000000E+00}.

Next, instead of using a built-in routine, we’ll create a custom method that implements a version of simple iteration, which, for the sake of having a nontrivial example, arbitrarily identifies a major iteration with three objective evaluations.

def custom_method(initial, contraction, callback, max_evaluations, tol, norm):
    x = initial
    evaluations = 0
    while evaluations < max_evaluations:
       x0, (x, weights, _) = x, contraction(x)
       evaluations += 1
       if evaluations % 3 == 0:
       if weights is None:
           difference = norm(x - x0)
           difference = norm(weights * (x - x0))
       if difference < tol:
    return x, evaluations < max_evaluations

We can then use this custom method to build a custom iteration configuration.

iteration = pyblp.Iteration(custom_method)
Configured to iterate using a custom method without analytic Jacobians with options {}.