2. How to estimate calibration model parameters

After implementing a custom calibration model class, the next step is to estimate the parameters of the calibration model by maximum likelihood.

In addition to measurement results obtained from measurement standards, this optimization step requires an initial guess and bounds for the calibration model parameters.

The calibr8.optimization module implements convenience functions such as fit_scipy that use the common API of all calibration models.

 1cmodel = MyCustomCalibrationModel()
 2
 3cal_independent = ...
 4cal_dependent = ...
 5
 6theta_fit, history = calibr8.fit_scipy(
 7    cmodel,
 8    independent=cal_independent,
 9    dependent=cal_dependent,
10    theta_guess=[
11        # NOTE: this example assumes a linear model with
12        #       constant, normally distributed noise
13        0,    # intercept
14        1,    # slope
15        0.2,  # standard deviation
16    ],
17    theta_guess=[
18        (-1, 1),       # intercept
19        (0, 3),        # slope
20        (0.001, 0.5),  # standard deviation (must be >= 0)
21    ]
22)

In some cases finding a good initial guess and parameter bounds can be surprisingly hard. That’s why fit_scipy returns not only the estimated parameter vector theta_fitted, but also the history of parameter vectors that were tested during optimization. Looking into the path that the optimizer took through the parameter space sometimes helps to diagnose bad convergence.

If you can’t get the scipy.optimize.minimize-based optimization to work, you can take out one of the “big guns”:

  • You can set up your own optimization using the loglikelihood method.

  • Or you could run MCMC sampling by creating a PyMC model of your calibration model. For this you can simply pass a list of PyMC random variables (priors) as the theta argument of the loglikelihood method.

Good luck!