Download the Jupyter Notebook for this section: iteration.ipynb

Iteration Example

[1]:
import pyblp
import numpy as np

pyblp.__version__
[1]:
'1.1.0'

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

[2]:
iteration = pyblp.Iteration('squarem', {'norm': np.linalg.norm, 'scheme': 1})
iteration
[2]:
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.

[3]:
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:
           callback()
       if weights is None:
           difference = norm(x - x0)
       else:
           difference = norm(weights * (x - x0))
       if difference < tol:
           break
    return x, evaluations < max_evaluations

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

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