Fitting Methods#

Here we will explore the various fitting methods in AstroPhot. You have already encountered some of the methods, but here we will take a more systematic approach and discuss their strengths/weaknesses. Each method will be applied to the same problem with the same initial conditions so you can see how they operate.

%load_ext autoreload
%autoreload 2
%matplotlib inline

import torch
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
from scipy.stats import gaussian_kde as kde
from scipy.stats import norm
from tqdm import tqdm
from corner import corner

import astrophot as ap

Hide code cell source

# Setup a fitting problem. You can ignore this cell to start, it just makes some test data to fit


def true_params():

    # just some random parameters to use for fitting. Feel free to play around with these to see what happens!
    sky_param = np.array([10**1.5])
    sersic_params = np.array(
        [
            [
                58.44035491,
                55.58516735,
                0.54945988,
                37.19794926 * np.pi / 180,
                2.14513004,
                22.05219055,
                10**2.45583024,
            ],
            [
                44.00353786,
                31.54430634,
                0.40203928,
                172.03862521 * np.pi / 180,
                2.88613347,
                12.095631,
                10**2.76711163,
            ],
        ]
    )

    return sersic_params, sky_param


def init_params():

    sky_param = np.array([10**1.4])
    sersic_params = np.array(
        [
            [57.0, 56.0, 0.6, 40.0 * np.pi / 180, 1.5, 25.0, 10**2.0],
            [45.0, 30.0, 0.5, 170.0 * np.pi / 180, 2.0, 10.0, 10**3.0],
        ]
    )

    return sersic_params, sky_param


def initialize_model(target, use_true_params=True):

    # Pick parameters to start the model with
    if use_true_params:
        sersic_params, sky_param = true_params()
    else:
        sersic_params, sky_param = init_params()

    # List of models, starting with the sky
    model_list = [
        ap.Model(
            name="sky",
            model_type="flat sky model",
            target=target,
            I0=sky_param[0],
        )
    ]
    # Add models to the list
    for i, params in enumerate(sersic_params):
        model_list.append(
            ap.Model(
                name=f"sersic_{i}",
                model_type="sersic galaxy model",
                target=target,
                center=[params[0], params[1]],
                q=params[2],
                PA=params[3],
                n=params[4],
                Re=params[5],
                Ie=params[6],
                # psf_convolve = True, # uncomment to try everything with PSF blurring (takes longer)
            )
        )

    MODEL = ap.Model(
        name="group",
        model_type="group model",
        models=model_list,
        target=target,
    )
    # Make sure every model is ready to go
    MODEL.initialize()

    return MODEL


def generate_target():

    N = 99
    pixelscale = 1.0
    rng = np.random.default_rng(42)

    # PSF has sigma of 2x pixelscale
    PSF = ap.utils.initialize.gaussian_psf(2, 21, pixelscale)
    PSF /= np.sum(PSF)

    target = ap.TargetImage(
        data=np.zeros((N, N)),
        pixelscale=pixelscale,
        psf=PSF,
    )

    MODEL = initialize_model(target, True)

    # Sample the model with the true values to make a mock image
    img = MODEL().data.detach().cpu().numpy()
    # Add poisson noise
    target.data = torch.Tensor(img + rng.normal(scale=np.sqrt(img) / 2))
    target.variance = torch.Tensor(img / 4)

    fig, ax = plt.subplots(figsize=(8, 8))
    ap.plots.target_image(fig, ax, target)
    ax.axis("off")
    plt.show()

    return target


def corner_plot(
    chain,
    labels=None,
    bins=None,
    true_values=None,
    plot_density=True,
    plot_contours=True,
    figsize=(10, 10),
):
    ndim = chain.shape[1]

    fig, axes = plt.subplots(ndim, ndim, figsize=figsize)
    plt.subplots_adjust(wspace=0.0, hspace=0.0)
    if bins is None:
        bins = int(np.sqrt(chain.shape[0]))

    for i in range(ndim):
        for j in range(ndim):
            ax = axes[i, j]

            i_range = (np.min(chain[:, i]), np.max(chain[:, i]))
            j_range = (np.min(chain[:, j]), np.max(chain[:, j]))
            if i == j:
                # Plot the histogram of parameter i
                # ax.hist(chain[:, i], bins=bins, histtype="step", range = i_range, density=True, color="k", lw=1)

                if plot_density:
                    # Plot the kernel density estimate
                    kde_x = np.linspace(i_range[0], i_range[1], 100)
                    kde_y = kde(chain[:, i])(kde_x)
                    ax.plot(kde_x, kde_y, color="green", lw=1)

                if true_values is not None:
                    ax.axvline(true_values[i], color="red", linestyle="-", lw=1)
                ax.set_xlim(i_range)

            elif i > j:
                # Plot the 2D histogram of parameters i and j
                # ax.hist2d(chain[:, j], chain[:, i], bins=bins, cmap="Greys")

                if plot_contours:
                    # Plot the kernel density estimate contours
                    kde_ij = kde([chain[:, j], chain[:, i]])
                    x, y = np.mgrid[j_range[0] : j_range[1] : 100j, i_range[0] : i_range[1] : 100j]
                    positions = np.vstack([x.ravel(), y.ravel()])
                    kde_pos = np.reshape(kde_ij(positions).T, x.shape)
                    ax.contour(x, y, kde_pos, colors="green", linewidths=1, levels=3)

                if true_values is not None:
                    ax.axvline(true_values[j], color="red", linestyle="-", lw=1)
                    ax.axhline(true_values[i], color="red", linestyle="-", lw=1)
                ax.set_xlim(j_range)
                ax.set_ylim(i_range)

            else:
                ax.axis("off")

            if j == 0 and labels is not None:
                ax.set_ylabel(labels[i])
            ax.yaxis.set_major_locator(plt.NullLocator())

            if i == ndim - 1 and labels is not None:
                ax.set_xlabel(labels[j])
            ax.xaxis.set_major_locator(plt.NullLocator())

    plt.show()


target = generate_target()
Initializing model sky
Initializing model sersic_0
Initializing model sersic_1
../_images/ee7ff4a368f9370d3c716a24ef39d3816b4f4d172278dd3c08d93ff771be7d23.png

Levenberg-Marquardt#

This fitter is identitied as ap.fit.LM and it employs a variant of the second order Newton’s method to converge very quickly to the local minimum. This is the generally accepted best algorithm for most use cases in \(\chi^2\) minimization. If you don’t know what to pick, start with this minimizer. The LM optimizer bridges the gap between first-order gradient descent and second order Newton’s method. When far from the minimum, Newton’s method is unstable and can give wildly wrong results, so LM takes gradient descent steps. However, near the minimum it switches to the Newton’s method which has “quadratic convergence” this means that it takes only a few iterations to converge to several decimal places. This can be represented as:

\((H + LI)h = g\)

Where H is the Hessian matrix of second derivatives, L is the damping parameter, I is the identity matrix, h is the step we will take in parameter space, and g is the gradient. We solve this linear system for h to get the next update step. The “L” scale parameter goes from L >> 1 which represents gradient descent to L << 1 which is Newton’s Method. When L >> 1 the hessian is effectively zero and we get \(h = g/L\) which is just gradient descent with \(1/L\) as the learning rate. In AstroPhot the damping parameter is treated somewhat differently, but the concept is the same.

LM can handle a lot of scenarios and converge to the minimum. Keep in mind, however, that it is seeking a local minimum, so it is best to start off the algorithm as close as possible to the best fit parameters. AstroPhot can automatically initialize, as discussed in other notebooks, but even that needs help sometimes (often in the form of a segmentation map).

The main drawback of LM is its memory consumption which goes as \(\mathcal{O}(PN)\) where P is the number of pixels and N is the number of parameters.

MODEL = initialize_model(target, False)

res_lm = ap.fit.LM(MODEL, verbose=1).fit()
print(res_lm.message)
Initializing model sky
Initializing model sersic_0
Initializing model sersic_1
==Starting LM fit for 'group' with 15 dynamic parameters and 9801 pixels==
Chi^2/DoF: 202.138, L: 1
/home/docs/checkouts/readthedocs.org/user_builds/astrophot/envs/v0.17.2/lib/python3.12/site-packages/torch/jit/_script.py:1488: DeprecationWarning: `torch.jit.script` is deprecated. Please switch to `torch.compile` or `torch.export`.
  warnings.warn(
Chi^2/DoF: 166.515, L: 1
Chi^2/DoF: 15.1555, L: 1
Chi^2/DoF: 7.65442, L: 1
Chi^2/DoF: 5.80401, L: 1
Chi^2/DoF: 4.87884, L: 1
Chi^2/DoF: 4.32103, L: 1
Chi^2/DoF: 3.14634, L: 0.111
Chi^2/DoF: 2.80655, L: 0.111
Chi^2/DoF: 2.11155, L: 0.0123
Chi^2/DoF: 1.79243, L: 0.0123
Chi^2/DoF: 1.59195, L: 0.0123
Chi^2/DoF: 1.11131, L: 0.00137
Chi^2/DoF: 1.01572, L: 0.000152
Chi^2/DoF: 1.01388, L: 1.69e-05
Final Chi^2/DoF: 1.01388, L: 2.09e-07. Converged: success
success

Hide code cell source

MODEL_init = initialize_model(target, False)
fig, axarr = plt.subplots(1, 4, figsize=(24, 5))
plt.subplots_adjust(wspace=0.1)
ap.plots.model_image(fig, axarr[0], MODEL_init)
axarr[0].set_title("Model before optimization")
ap.plots.residual_image(fig, axarr[1], MODEL_init, normalize_residuals=True)
axarr[1].set_title("Residuals before optimization")

ap.plots.model_image(fig, axarr[2], MODEL)
axarr[2].set_title("Model after optimization")
ap.plots.residual_image(fig, axarr[3], MODEL, normalize_residuals=True)
axarr[3].set_title("Residuals after optimization")
plt.show()
Initializing model sky
Initializing model sersic_0
Initializing model sersic_1
../_images/e6f71787690f4445ce7b0cfddc1aed990376d2cbd53f510b0e6eb86cb8391537.png

Now that LM has found the \(\chi^2\) minimum, we can do a really neat trick. Since LM needs the hessian matrix, we have access to the hessian matrix at the minimum. This is in fact equal to the negative Fisher information matrix. If we take the matrix inverse of this matrix then we get the covariance matrix for a multivariate gaussian approximation of the \(\chi^2\) surface near the minimum. With the covariance matrix we can create a corner plot just like we would with an MCMC. We will see later that the MCMC methods (at least the ones which converge) produce very similar results!

param_names = list(MODEL.build_params_array_names())
set, sky = true_params()
fig, ax = ap.plots.covariance_matrix(
    res_lm.covariance_matrix.detach().cpu().numpy(),
    MODEL.get_values().detach().cpu().numpy(),
    labels=param_names,
    figsize=(20, 20),
    reference_values=np.concatenate((sky, set.ravel())),
)
../_images/240d33e68082ca2fc3a5da4b90fa5b4124bcaf39854442ec0f54d63ca4eaf1c1.png

Iterative Fit (models)#

This iterative fitter is identified as ap.fit.Iter, this method is generally employed for large models where it is not feasible to hold all the relevant data in memory at once. The iterative fitter will cycle through the models in a GroupModel object and fit them one at a time to the image, using the residuals from the previous cycle. This can be a very robust way to deal with some fits, especially if the overlap between models is not too strong. It is however more dependent on good initialization than other methods like the Levenberg-Marquardt. Also, it is possible for the Iter method to get stuck in a local minimum under certain circumstances.

Note that while the Iterative fitter needs a GroupModel object to iterate over, it is not necessarily true that the sub models are ComponentModel objects, they could be GroupModel objects as well. In this way it is possible to cycle through and fit “clusters” of objects that are nearby, so long as it doesn’t consume too much memory.

By only fitting one model at a time it is possible to get caught in a local minimum, or to get out of a local minimum that a different fitter was stuck in. For this reason it can be good to mix-and-match the iterative optimizers so they can help each other get unstuck if a fit is very challenging.

MODEL = initialize_model(target, False)

res_iter = ap.fit.Iter(MODEL, verbose=1).fit()

Hide code cell output

Initializing model sky
Initializing model sersic_0
Initializing model sersic_1
--------iter-------
sky
sersic_0
sersic_1
Update Chi^2 with new parameters
Loss: 6.125456843345972
--------iter-------
sky
sersic_0
sersic_1
Update Chi^2 with new parameters
Loss: 3.184002624720552
--------iter-------
sky
sersic_0
sersic_1
Update Chi^2 with new parameters
Loss: 2.247040221666734
--------iter-------
sky
sersic_0
sersic_1
Update Chi^2 with new parameters
Loss: 1.7224249504823559
--------iter-------
sky
sersic_0
sersic_1
Update Chi^2 with new parameters
Loss: 1.4226720837982196
--------iter-------
sky
sersic_0
sersic_1
Update Chi^2 with new parameters
Loss: 1.2516529128429281
--------iter-------
sky
sersic_0
sersic_1
Update Chi^2 with new parameters
Loss: 1.1533383392178902
--------iter-------
sky
sersic_0
sersic_1
Update Chi^2 with new parameters
Loss: 1.0963263829078134
--------iter-------
sky
sersic_0
sersic_1
Update Chi^2 with new parameters
Loss: 1.0629573623320654
--------iter-------
sky
sersic_0
sersic_1
Update Chi^2 with new parameters
Loss: 1.0432609015675476
--------iter-------
sky
sersic_0
sersic_1
Update Chi^2 with new parameters
Loss: 1.0315496442310186
--------iter-------
sky
sersic_0
sersic_1
Update Chi^2 with new parameters
Loss: 1.0245447452385281
--------iter-------
sky
sersic_0
sersic_1
Update Chi^2 with new parameters
Loss: 1.0203350537698253
--------iter-------
sky
sersic_0
sersic_1
Update Chi^2 with new parameters
Loss: 1.017795417098709
--------iter-------
sky
sersic_0
sersic_1
Update Chi^2 with new parameters
Loss: 1.0162588023245591
--------iter-------
sky
sersic_0
sersic_1
Update Chi^2 with new parameters
Loss: 1.015326946676114
--------iter-------
sky
sersic_0
sersic_1
Update Chi^2 with new parameters
Loss: 1.0147608398298165
--------iter-------
sky
sersic_0
sersic_1
Update Chi^2 with new parameters
Loss: 1.0144164580152701
--------iter-------
sky
sersic_0
sersic_1
Update Chi^2 with new parameters
Loss: 1.0142067397117054
--------iter-------
sky
sersic_0
sersic_1
Update Chi^2 with new parameters
Loss: 1.0140789250460662
--------iter-------
sky
sersic_0
sersic_1
Update Chi^2 with new parameters
Loss: 1.0140009795098814
--------iter-------
sky
sersic_0
sersic_1
Update Chi^2 with new parameters
Loss: 1.0139534235346461

Hide code cell source

MODEL_init = initialize_model(target, False)
fig, axarr = plt.subplots(1, 4, figsize=(24, 5))
plt.subplots_adjust(wspace=0.1)
ap.plots.model_image(fig, axarr[0], MODEL_init)
axarr[0].set_title("Model before optimization")
ap.plots.residual_image(fig, axarr[1], MODEL_init, normalize_residuals=True)
axarr[1].set_title("Residuals before optimization")

ap.plots.model_image(fig, axarr[2], MODEL)
axarr[2].set_title("Model after optimization")
ap.plots.residual_image(fig, axarr[3], MODEL, normalize_residuals=True)
axarr[3].set_title("Residuals after optimization")
plt.show()
Initializing model sky
Initializing model sersic_0
Initializing model sersic_1
../_images/f78fc59b0e3125fbf31afee6e699f8e1464e58870b2426ffd70f4cb78200fc5b.png

Iterative Fit (Param)#

This iterative fitter is identified as ap.fit.IterParam, this is generally employed for large and interconnected models where it is not feasible to hold all the relevant data in memory at once. Unlike ap.fit.Iter which is intended to cycle through the sub models in a group model, this fitter iterates through parameters. The set of parameters which make up the model is broken into chunks and then fitting proceeds only on those chunks, rather than on all parameters simultaneously. For large models that have lots of interconnected/shared parameters, it doesn’t really make sense to cycle through one sub-model at a time as optimizing that model may throw another model that is sharing a parameter into a bad part of parameter space. Thus ap.fit.IterParam is safe to use on any AstroPhot model without concern for this issue, the fitter will industriously proceed to high likelihood solutions monotonically.

The tradeoff for this fitter is the same as for the other iterative fitter, if there are strong covariances in the likelihood structure then this fitter can take a long time to converge. The advantage here is that as the user you may take greater control over the combinations if you wish. The chunks argument can be set to an integer like 6 in which case, 6 parameters at a time will be fit (the last chunk may be smaller). Alternatively, the chunks parameter may be set to a tuple of numpy arrays, these should be boolean arrays that select the parameters for each chunk. For example, here is a possible chunks setup for a 7 parameter sersic model: ([1,1,0,0,0,0,0], [0,0,1,1,0,0,0], [0,0,0,0,1,1,1]) which makes three chunks to fit the x,y then q, PA then n, Re, Ie parameters. Note that you do not need to make the chunks exclusive, it is totally fine to have a parameter pop up in multiple chunks! Finally, there’s the order the chunks are fit in. This can either chunk_order="sequential" the default the chunks are fit in the order given, or chunk_order="random" where each iteration a new random order is decided for the chunks to be evaluated.

MODEL = initialize_model(target, False)

res_iterparam = ap.fit.IterParam(MODEL, chunks=5, verbose=1).fit()
Initializing model sky
Initializing model sersic_0
Initializing model sersic_1
==Starting LM fit for 'group' with 15 dynamic parameters and 9801 pixels==
Chi^2/DoF: 202.138, L: 1
Chi^2/DoF: 40.3008, L: 1
Chi^2/DoF: 20.1483, L: 1
Chi^2/DoF: 13.4895, L: 1
Chi^2/DoF: 10.0988, L: 1
Chi^2/DoF: 7.53482, L: 1
Chi^2/DoF: 5.2923, L: 1
Chi^2/DoF: 3.96364, L: 1
Chi^2/DoF: 2.99242, L: 1
Chi^2/DoF: 2.45525, L: 1
Chi^2/DoF: 2.07622, L: 1
Chi^2/DoF: 1.80351, L: 1
Chi^2/DoF: 1.60405, L: 1
Chi^2/DoF: 1.24465, L: 0.111
Chi^2/DoF: 1.0652, L: 0.00137
Chi^2/DoF: 1.02529, L: 0.00137
Chi^2/DoF: 1.01645, L: 0.00137
Chi^2/DoF: 1.01445, L: 0.000152
Chi^2/DoF: 1.01401, L: 0.000152
Chi^2/DoF: 1.01391, L: 0.000152
Chi^2/DoF: 1.01389, L: 0.000152
Final Chi^2/DoF: 1.01388, L: 0.000152. Converged: success
MODEL_init = initialize_model(target, False)
fig, axarr = plt.subplots(1, 4, figsize=(24, 5))
plt.subplots_adjust(wspace=0.1)
ap.plots.model_image(fig, axarr[0], MODEL_init)
axarr[0].set_title("Model before optimization")
ap.plots.residual_image(fig, axarr[1], MODEL_init, normalize_residuals=True)
axarr[1].set_title("Residuals before optimization")

ap.plots.model_image(fig, axarr[2], MODEL)
axarr[2].set_title("Model after optimization")
ap.plots.residual_image(fig, axarr[3], MODEL, normalize_residuals=True)
axarr[3].set_title("Residuals after optimization")
plt.show()
Initializing model sky
Initializing model sersic_0
Initializing model sersic_1
../_images/a97daa11f869e0c2693f76df080d90bd58e997b50d79630b0967ee1d82450416.png

The ap.fit.IterParam fitter can also generate a covariance matrix of uncertainties, just keep in mind that it only evaluates the covariances for parameters in the same chunk.

param_names = list(MODEL.build_params_array_names())
set, sky = true_params()
fig, ax = ap.plots.covariance_matrix(
    res_iterparam.covariance_matrix.detach().cpu().numpy(),
    MODEL.get_values().detach().cpu().numpy(),
    labels=param_names,
    figsize=(20, 20),
    reference_values=np.concatenate((sky, set.ravel())),
)
../_images/2840a5a5d4a14919122d01c012444981ac31b4a62e6bfcd02bb08cfafc40226f.png

Scipy Minimize#

Any AstroPhot model becomes a function model(x) where x is a 1D tensor of all the current dynamic parameters. This functional format is common for external packages to use. AstroPhot includes a wrapper to access the scipy.optimize.minimize minimizer list. AstroPhot will ensure the minimizers respect the valid ranges set for each parameter.

Typically, the AstroPhot LM optimizer is faster and more accurate than the Scipy ones. The exact reason is unclear, but the Scipy minimizers are intended for very general use, while the LM optimizer is specifically optimized for gaussian log likelihoods.

In the case below, the minimizer thinks it has terminated successfully, although in fact it is quite far from the minimum. Consider this a lesson in trusting the “success” message from an optimizer. It turns out to be very challenging to identify if an optimizer is at a minimum, let alone the global minimum.

MODEL = initialize_model(target, False)

res_scipy = ap.fit.ScipyFit(MODEL, method="Powell", verbose=1).fit()
print(res_scipy.scipy_res)
Initializing model sky
Initializing model sersic_0
Initializing model sersic_1
Final 2NLL/DoF: 3.27593. Converged: success: True, message: Optimization terminated successfully.
 message: Optimization terminated successfully.
 success: True
  status: 0
     fun: 16029.110164752814
       x: [ 2.297e+01  5.861e+01 ...  1.303e+01  4.921e+02]
     nit: 26
   direc: [[ 1.945e+00  1.968e-02 ...  2.035e-01 -5.820e+01]
           [ 0.000e+00  1.000e+00 ...  0.000e+00  0.000e+00]
           ...
           [ 0.000e+00  0.000e+00 ...  1.000e+00  0.000e+00]
           [ 2.425e+00  7.320e-03 ... -9.140e-01  3.942e+01]]
    nfev: 4189

Hide code cell source

MODEL_init = initialize_model(target, False)
fig, axarr = plt.subplots(1, 4, figsize=(24, 5))
plt.subplots_adjust(wspace=0.1)
ap.plots.model_image(fig, axarr[0], MODEL_init)
axarr[0].set_title("Model before optimization")
ap.plots.residual_image(fig, axarr[1], MODEL_init, normalize_residuals=True)
axarr[1].set_title("Residuals before optimization")

ap.plots.model_image(fig, axarr[2], MODEL)
axarr[2].set_title("Model after optimization")
ap.plots.residual_image(fig, axarr[3], MODEL, normalize_residuals=True)
axarr[3].set_title("Residuals after optimization")
plt.show()
Initializing model sky
Initializing model sersic_0
Initializing model sersic_1
../_images/0b3611b2471f04fa8bb7c0ec3a5a6131458f1c38b98c6f593fa414b9a419d059.png

Gradient Descent (Slalom)#

A gradient descent fitter uses local gradient information to determine the direction of increased likelihood in parameter space. The challenge with gradient descent is choosing a step size. The Slalom algorithm developed for AstroPhot uses a few samples along the gradient direction to determine a parabola which it can then jump to the minimum of. In some sense this is like a 1D version of the Levenberg-Marquardt algorithm and the 1 dimension it choses is that along the gradient (plus momentum).

It is also possible to access the PyTorch gradient descent algorithms like Adam through the AstroPhot wrapper ap.fit.Grad which perform gradient descent using various algorithm designed for machine learning. In general though, those algorithms perform better on stochastic gradient descent problems, not static problems like seen by AstroPhot. So Slalom tends to perform better.

As you see below, Slalom ends with a decent fit, though not good enough for perfect residuals like some other methods (Levenberg-Marquardt). This is typically the case. However, gradient descent can be very helpful for complex optimization tasks, because it is a slower optimization algorithm, it can be more stable in some circumstances. Try using it in cases where LM fails to get things back on track. Just make sure to finish off with an LM round to ensure you have settled into the minimum.

MODEL = initialize_model(target, False)

res_grad = ap.fit.Slalom(MODEL, verbose=1, momentum=0.1).fit()
Initializing model sky
Initializing model sersic_0
Initializing model sersic_1
iter: 0, step size: 3.662535e-01, posterior density: 5.116711e+05
iter: 10, step size: 3.200572e-02, posterior density: 1.076052e+05
iter: 20, step size: 3.636985e-01, posterior density: 6.932667e+04
iter: 30, step size: 9.491573e-02, posterior density: 4.954043e+04
iter: 40, step size: 7.715230e-03, posterior density: 3.508777e+04
iter: 50, step size: 7.691659e-03, posterior density: 3.078378e+04
iter: 60, step size: 8.186814e-03, posterior density: 2.880637e+04
iter: 70, step size: 1.040778e+00, posterior density: 1.249034e+04
iter: 80, step size: 4.185484e-03, posterior density: 8.939124e+03
iter: 90, step size: 6.185570e-03, posterior density: 8.454234e+03
iter: 100, step size: 9.386691e-03, posterior density: 8.193303e+03
iter: 110, step size: 1.394801e-03, posterior density: 7.879821e+03
iter: 120, step size: 2.629153e-03, posterior density: 7.644821e+03
iter: 130, step size: 1.106532e-03, posterior density: 7.453023e+03
iter: 140, step size: 9.470766e-04, posterior density: 7.310017e+03
iter: 150, step size: 1.226329e-03, posterior density: 7.220375e+03
iter: 160, step size: 5.506503e-04, posterior density: 7.009075e+03
iter: 170, step size: 2.057291e-03, posterior density: 6.964639e+03
iter: 180, step size: 7.426339e-04, posterior density: 6.868799e+03
iter: 190, step size: 2.125832e-03, posterior density: 6.832346e+03
iter: 200, step size: 5.556855e-04, posterior density: 6.756988e+03
iter: 210, step size: 4.663705e-04, posterior density: 6.705010e+03
iter: 220, step size: 5.489810e-04, posterior density: 6.592376e+03
iter: 230, step size: 1.171404e-03, posterior density: 6.564158e+03
iter: 240, step size: 4.643993e-04, posterior density: 6.550652e+03
iter: 250, step size: 6.572783e-04, posterior density: 6.522439e+03
iter: 260, step size: 2.718795e-04, posterior density: 6.518673e+03
iter: 270, step size: 2.571539e-04, posterior density: 6.506138e+03
iter: 280, step size: 2.480211e-04, posterior density: 6.494628e+03
iter: 290, step size: 2.374904e-04, posterior density: 6.478310e+03
iter: 300, step size: 6.344610e-04, posterior density: 6.464007e+03
Slalom Fitting complete in 50.4788544178009 sec with message:  success

Hide code cell source

MODEL_init = initialize_model(target, False)
fig, axarr = plt.subplots(1, 4, figsize=(24, 5))
plt.subplots_adjust(wspace=0.1)
ap.plots.model_image(fig, axarr[0], MODEL_init)
axarr[0].set_title("Model before optimization")
ap.plots.residual_image(fig, axarr[1], MODEL_init, normalize_residuals=True)
axarr[1].set_title("Residuals before optimization")

ap.plots.model_image(fig, axarr[2], MODEL)
axarr[2].set_title("Model after optimization")
ap.plots.residual_image(fig, axarr[3], MODEL, normalize_residuals=True)
axarr[3].set_title("Residuals after optimization")
plt.show()
Initializing model sky
Initializing model sersic_0
Initializing model sersic_1
../_images/d4525fa0ca0dfdc26d813b04c488be60cb4adc1a1c4db88d153a02bbb8f955dd.png

Metropolis Adjusted Langevin Algorithm (MALA)#

This is one of the simplest gradient based samplers, and is very powerful. The standard Metropolis Hastings algorithm will use a gaussian proposal distribution then use the Metropolis Hastings accept/reject stage. MALA uses gradient information to determine a better proposal distribution locally (while maintaining detailed balance) and then uses the Metropolis Hastings accept/reject stage. The ap.fit.MALA fitter object is just a basic wrapper over the ap.fit.func.mala function, so feel free to check it out if you want more details on this simple and powerful sampler!

MODEL = initialize_model(target, False)

# Use LM to start the sampler at a high likelihood location, no burn-in needed!
res1 = ap.fit.LM(MODEL, verbose=0).fit()

res_mala = ap.fit.MALA(
    model=MODEL,
    chains=4,
    max_iter=300,
    epsilon=8e-1,
    likelihood="poisson",
    mass_matrix=res1.covariance_matrix.detach().cpu().numpy(),
).fit()
chain_mala = res_mala.chain.reshape(-1, res_mala.chain.shape[-1])

Hide code cell output

Initializing model sky
Initializing model sersic_0
Initializing model sersic_1
MALA:   0%|          | 0/300 [00:00<?, ?it/s]
MALA:   0%|          | 0/300 [00:00<?, ?it/s, acc_rate=1.00]
MALA:   0%|          | 1/300 [00:00<01:15,  3.96it/s, acc_rate=1.00]
MALA:   0%|          | 1/300 [00:00<01:15,  3.96it/s, acc_rate=1.00]
MALA:   1%|          | 2/300 [00:00<01:13,  4.05it/s, acc_rate=1.00]
MALA:   1%|          | 2/300 [00:00<01:13,  4.05it/s, acc_rate=0.92]
MALA:   1%|          | 3/300 [00:00<01:12,  4.08it/s, acc_rate=0.92]
MALA:   1%|          | 3/300 [00:00<01:12,  4.08it/s, acc_rate=0.94]
MALA:   1%|▏         | 4/300 [00:00<01:14,  3.99it/s, acc_rate=0.94]
MALA:   1%|▏         | 4/300 [00:01<01:14,  3.99it/s, acc_rate=0.95]
MALA:   2%|▏         | 5/300 [00:01<01:09,  4.24it/s, acc_rate=0.95]
MALA:   2%|▏         | 5/300 [00:01<01:09,  4.24it/s, acc_rate=0.96]
MALA:   2%|▏         | 6/300 [00:01<01:06,  4.42it/s, acc_rate=0.96]
MALA:   2%|▏         | 6/300 [00:01<01:06,  4.42it/s, acc_rate=0.96]
MALA:   2%|▏         | 7/300 [00:01<01:06,  4.39it/s, acc_rate=0.96]
MALA:   2%|▏         | 7/300 [00:01<01:06,  4.39it/s, acc_rate=0.97]
MALA:   3%|▎         | 8/300 [00:01<01:08,  4.28it/s, acc_rate=0.97]
MALA:   3%|▎         | 8/300 [00:02<01:08,  4.28it/s, acc_rate=0.97]
MALA:   3%|▎         | 9/300 [00:02<01:09,  4.22it/s, acc_rate=0.97]
MALA:   3%|▎         | 9/300 [00:02<01:09,  4.22it/s, acc_rate=0.97]
MALA:   3%|▎         | 10/300 [00:02<01:11,  4.04it/s, acc_rate=0.97]
MALA:   3%|▎         | 10/300 [00:02<01:11,  4.04it/s, acc_rate=0.98]
MALA:   4%|▎         | 11/300 [00:02<01:14,  3.86it/s, acc_rate=0.98]
MALA:   4%|▎         | 11/300 [00:02<01:14,  3.86it/s, acc_rate=0.98]
MALA:   4%|▍         | 12/300 [00:02<01:17,  3.74it/s, acc_rate=0.98]
MALA:   4%|▍         | 12/300 [00:03<01:17,  3.74it/s, acc_rate=0.94]
MALA:   4%|▍         | 13/300 [00:03<01:17,  3.69it/s, acc_rate=0.94]
MALA:   4%|▍         | 13/300 [00:03<01:17,  3.69it/s, acc_rate=0.95]
MALA:   5%|▍         | 14/300 [00:03<01:18,  3.67it/s, acc_rate=0.95]
MALA:   5%|▍         | 14/300 [00:03<01:18,  3.67it/s, acc_rate=0.95]
MALA:   5%|▌         | 15/300 [00:03<01:11,  3.97it/s, acc_rate=0.95]
MALA:   5%|▌         | 15/300 [00:03<01:11,  3.97it/s, acc_rate=0.95]
MALA:   5%|▌         | 16/300 [00:03<01:11,  3.97it/s, acc_rate=0.95]
MALA:   5%|▌         | 16/300 [00:04<01:11,  3.97it/s, acc_rate=0.94]
MALA:   6%|▌         | 17/300 [00:04<01:10,  4.02it/s, acc_rate=0.94]
MALA:   6%|▌         | 17/300 [00:04<01:10,  4.02it/s, acc_rate=0.94]
MALA:   6%|▌         | 18/300 [00:04<01:09,  4.06it/s, acc_rate=0.94]
MALA:   6%|▌         | 18/300 [00:04<01:09,  4.06it/s, acc_rate=0.95]
MALA:   6%|▋         | 19/300 [00:04<01:11,  3.91it/s, acc_rate=0.95]
MALA:   6%|▋         | 19/300 [00:05<01:11,  3.91it/s, acc_rate=0.95]
MALA:   7%|▋         | 20/300 [00:05<01:14,  3.74it/s, acc_rate=0.95]
MALA:   7%|▋         | 20/300 [00:05<01:14,  3.74it/s, acc_rate=0.95]
MALA:   7%|▋         | 21/300 [00:05<01:15,  3.68it/s, acc_rate=0.95]
MALA:   7%|▋         | 21/300 [00:05<01:15,  3.68it/s, acc_rate=0.95]
MALA:   7%|▋         | 22/300 [00:05<01:16,  3.66it/s, acc_rate=0.95]
MALA:   7%|▋         | 22/300 [00:05<01:16,  3.66it/s, acc_rate=0.96]
MALA:   8%|▊         | 23/300 [00:05<01:16,  3.61it/s, acc_rate=0.96]
MALA:   8%|▊         | 23/300 [00:06<01:16,  3.61it/s, acc_rate=0.96]
MALA:   8%|▊         | 24/300 [00:06<01:17,  3.55it/s, acc_rate=0.96]
MALA:   8%|▊         | 24/300 [00:06<01:17,  3.55it/s, acc_rate=0.96]
MALA:   8%|▊         | 25/300 [00:06<01:15,  3.63it/s, acc_rate=0.96]
MALA:   8%|▊         | 25/300 [00:06<01:15,  3.63it/s, acc_rate=0.96]
MALA:   9%|▊         | 26/300 [00:06<01:14,  3.66it/s, acc_rate=0.96]
MALA:   9%|▊         | 26/300 [00:06<01:14,  3.66it/s, acc_rate=0.96]
MALA:   9%|▉         | 27/300 [00:06<01:13,  3.69it/s, acc_rate=0.96]
MALA:   9%|▉         | 27/300 [00:07<01:13,  3.69it/s, acc_rate=0.96]
MALA:   9%|▉         | 28/300 [00:07<01:14,  3.64it/s, acc_rate=0.96]
MALA:   9%|▉         | 28/300 [00:07<01:14,  3.64it/s, acc_rate=0.96]
MALA:  10%|▉         | 29/300 [00:07<01:14,  3.62it/s, acc_rate=0.96]
MALA:  10%|▉         | 29/300 [00:07<01:14,  3.62it/s, acc_rate=0.96]
MALA:  10%|█         | 30/300 [00:07<01:15,  3.56it/s, acc_rate=0.96]
MALA:  10%|█         | 30/300 [00:08<01:15,  3.56it/s, acc_rate=0.96]
MALA:  10%|█         | 31/300 [00:08<01:16,  3.53it/s, acc_rate=0.96]
MALA:  10%|█         | 31/300 [00:08<01:16,  3.53it/s, acc_rate=0.96]
MALA:  11%|█         | 32/300 [00:08<01:16,  3.51it/s, acc_rate=0.96]
MALA:  11%|█         | 32/300 [00:08<01:16,  3.51it/s, acc_rate=0.96]
MALA:  11%|█         | 33/300 [00:08<01:15,  3.52it/s, acc_rate=0.96]
MALA:  11%|█         | 33/300 [00:08<01:15,  3.52it/s, acc_rate=0.96]
MALA:  11%|█▏        | 34/300 [00:08<01:13,  3.61it/s, acc_rate=0.96]
MALA:  11%|█▏        | 34/300 [00:09<01:13,  3.61it/s, acc_rate=0.96]
MALA:  12%|█▏        | 35/300 [00:09<01:12,  3.67it/s, acc_rate=0.96]
MALA:  12%|█▏        | 35/300 [00:09<01:12,  3.67it/s, acc_rate=0.97]
MALA:  12%|█▏        | 36/300 [00:09<01:11,  3.68it/s, acc_rate=0.97]
MALA:  12%|█▏        | 36/300 [00:09<01:11,  3.68it/s, acc_rate=0.97]
MALA:  12%|█▏        | 37/300 [00:09<01:12,  3.65it/s, acc_rate=0.97]
MALA:  12%|█▏        | 37/300 [00:10<01:12,  3.65it/s, acc_rate=0.97]
MALA:  13%|█▎        | 38/300 [00:10<01:13,  3.58it/s, acc_rate=0.97]
MALA:  13%|█▎        | 38/300 [00:10<01:13,  3.58it/s, acc_rate=0.97]
MALA:  13%|█▎        | 39/300 [00:10<01:13,  3.54it/s, acc_rate=0.97]
MALA:  13%|█▎        | 39/300 [00:10<01:13,  3.54it/s, acc_rate=0.97]
MALA:  13%|█▎        | 40/300 [00:10<01:13,  3.53it/s, acc_rate=0.97]
MALA:  13%|█▎        | 40/300 [00:10<01:13,  3.53it/s, acc_rate=0.97]
MALA:  14%|█▎        | 41/300 [00:10<01:13,  3.54it/s, acc_rate=0.97]
MALA:  14%|█▎        | 41/300 [00:11<01:13,  3.54it/s, acc_rate=0.97]
MALA:  14%|█▍        | 42/300 [00:11<01:12,  3.54it/s, acc_rate=0.97]
MALA:  14%|█▍        | 42/300 [00:11<01:12,  3.54it/s, acc_rate=0.97]
MALA:  14%|█▍        | 43/300 [00:11<01:12,  3.53it/s, acc_rate=0.97]
MALA:  14%|█▍        | 43/300 [00:11<01:12,  3.53it/s, acc_rate=0.97]
MALA:  15%|█▍        | 44/300 [00:11<01:11,  3.58it/s, acc_rate=0.97]
MALA:  15%|█▍        | 44/300 [00:12<01:11,  3.58it/s, acc_rate=0.97]
MALA:  15%|█▌        | 45/300 [00:12<01:10,  3.62it/s, acc_rate=0.97]
MALA:  15%|█▌        | 45/300 [00:12<01:10,  3.62it/s, acc_rate=0.97]
MALA:  15%|█▌        | 46/300 [00:12<01:10,  3.62it/s, acc_rate=0.97]
MALA:  15%|█▌        | 46/300 [00:12<01:10,  3.62it/s, acc_rate=0.97]
MALA:  16%|█▌        | 47/300 [00:12<01:11,  3.55it/s, acc_rate=0.97]
MALA:  16%|█▌        | 47/300 [00:12<01:11,  3.55it/s, acc_rate=0.97]
MALA:  16%|█▌        | 48/300 [00:12<01:11,  3.52it/s, acc_rate=0.97]
MALA:  16%|█▌        | 48/300 [00:13<01:11,  3.52it/s, acc_rate=0.97]
MALA:  16%|█▋        | 49/300 [00:13<01:11,  3.53it/s, acc_rate=0.97]
MALA:  16%|█▋        | 49/300 [00:13<01:11,  3.53it/s, acc_rate=0.97]
MALA:  17%|█▋        | 50/300 [00:13<01:11,  3.52it/s, acc_rate=0.97]
MALA:  17%|█▋        | 50/300 [00:13<01:11,  3.52it/s, acc_rate=0.97]
MALA:  17%|█▋        | 51/300 [00:13<01:10,  3.52it/s, acc_rate=0.97]
MALA:  17%|█▋        | 51/300 [00:14<01:10,  3.52it/s, acc_rate=0.97]
MALA:  17%|█▋        | 52/300 [00:14<01:10,  3.52it/s, acc_rate=0.97]
MALA:  17%|█▋        | 52/300 [00:14<01:10,  3.52it/s, acc_rate=0.97]
MALA:  18%|█▊        | 53/300 [00:14<01:06,  3.70it/s, acc_rate=0.97]
MALA:  18%|█▊        | 53/300 [00:14<01:06,  3.70it/s, acc_rate=0.97]
MALA:  18%|█▊        | 54/300 [00:14<01:05,  3.78it/s, acc_rate=0.97]
MALA:  18%|█▊        | 54/300 [00:14<01:05,  3.78it/s, acc_rate=0.97]
MALA:  18%|█▊        | 55/300 [00:14<00:59,  4.13it/s, acc_rate=0.97]
MALA:  18%|█▊        | 55/300 [00:14<00:59,  4.13it/s, acc_rate=0.97]
MALA:  19%|█▊        | 56/300 [00:14<00:54,  4.45it/s, acc_rate=0.97]
MALA:  19%|█▊        | 56/300 [00:15<00:54,  4.45it/s, acc_rate=0.97]
MALA:  19%|█▉        | 57/300 [00:15<00:52,  4.62it/s, acc_rate=0.97]
MALA:  19%|█▉        | 57/300 [00:15<00:52,  4.62it/s, acc_rate=0.97]
MALA:  19%|█▉        | 58/300 [00:15<00:52,  4.61it/s, acc_rate=0.97]
MALA:  19%|█▉        | 58/300 [00:15<00:52,  4.61it/s, acc_rate=0.97]
MALA:  20%|█▉        | 59/300 [00:15<00:54,  4.45it/s, acc_rate=0.97]
MALA:  20%|█▉        | 59/300 [00:15<00:54,  4.45it/s, acc_rate=0.97]
MALA:  20%|██        | 60/300 [00:15<00:54,  4.43it/s, acc_rate=0.97]
MALA:  20%|██        | 60/300 [00:15<00:54,  4.43it/s, acc_rate=0.98]
MALA:  20%|██        | 61/300 [00:15<00:54,  4.42it/s, acc_rate=0.98]
MALA:  20%|██        | 61/300 [00:16<00:54,  4.42it/s, acc_rate=0.98]
MALA:  21%|██        | 62/300 [00:16<00:54,  4.39it/s, acc_rate=0.98]
MALA:  21%|██        | 62/300 [00:16<00:54,  4.39it/s, acc_rate=0.98]
MALA:  21%|██        | 63/300 [00:16<00:58,  4.04it/s, acc_rate=0.98]
MALA:  21%|██        | 63/300 [00:16<00:58,  4.04it/s, acc_rate=0.98]
MALA:  21%|██▏       | 64/300 [00:16<01:00,  3.91it/s, acc_rate=0.98]
MALA:  21%|██▏       | 64/300 [00:17<01:00,  3.91it/s, acc_rate=0.98]
MALA:  22%|██▏       | 65/300 [00:17<01:02,  3.77it/s, acc_rate=0.98]
MALA:  22%|██▏       | 65/300 [00:17<01:02,  3.77it/s, acc_rate=0.98]
MALA:  22%|██▏       | 66/300 [00:17<01:04,  3.63it/s, acc_rate=0.98]
MALA:  22%|██▏       | 66/300 [00:17<01:04,  3.63it/s, acc_rate=0.98]
MALA:  22%|██▏       | 67/300 [00:17<01:05,  3.56it/s, acc_rate=0.98]
MALA:  22%|██▏       | 67/300 [00:17<01:05,  3.56it/s, acc_rate=0.98]
MALA:  23%|██▎       | 68/300 [00:17<01:05,  3.52it/s, acc_rate=0.98]
MALA:  23%|██▎       | 68/300 [00:18<01:05,  3.52it/s, acc_rate=0.98]
MALA:  23%|██▎       | 69/300 [00:18<01:05,  3.52it/s, acc_rate=0.98]
MALA:  23%|██▎       | 69/300 [00:18<01:05,  3.52it/s, acc_rate=0.98]
MALA:  23%|██▎       | 70/300 [00:18<01:06,  3.47it/s, acc_rate=0.98]
MALA:  23%|██▎       | 70/300 [00:18<01:06,  3.47it/s, acc_rate=0.98]
MALA:  24%|██▎       | 71/300 [00:18<01:05,  3.47it/s, acc_rate=0.98]
MALA:  24%|██▎       | 71/300 [00:19<01:05,  3.47it/s, acc_rate=0.98]
MALA:  24%|██▍       | 72/300 [00:19<01:05,  3.47it/s, acc_rate=0.98]
MALA:  24%|██▍       | 72/300 [00:19<01:05,  3.47it/s, acc_rate=0.98]
MALA:  24%|██▍       | 73/300 [00:19<01:06,  3.40it/s, acc_rate=0.98]
MALA:  24%|██▍       | 73/300 [00:19<01:06,  3.40it/s, acc_rate=0.98]
MALA:  25%|██▍       | 74/300 [00:19<01:06,  3.38it/s, acc_rate=0.98]
MALA:  25%|██▍       | 74/300 [00:20<01:06,  3.38it/s, acc_rate=0.98]
MALA:  25%|██▌       | 75/300 [00:20<01:06,  3.39it/s, acc_rate=0.98]
MALA:  25%|██▌       | 75/300 [00:20<01:06,  3.39it/s, acc_rate=0.98]
MALA:  25%|██▌       | 76/300 [00:20<01:05,  3.40it/s, acc_rate=0.98]
MALA:  25%|██▌       | 76/300 [00:20<01:05,  3.40it/s, acc_rate=0.98]
MALA:  26%|██▌       | 77/300 [00:20<01:04,  3.44it/s, acc_rate=0.98]
MALA:  26%|██▌       | 77/300 [00:20<01:04,  3.44it/s, acc_rate=0.98]
MALA:  26%|██▌       | 78/300 [00:20<01:05,  3.40it/s, acc_rate=0.98]
MALA:  26%|██▌       | 78/300 [00:21<01:05,  3.40it/s, acc_rate=0.98]
MALA:  26%|██▋       | 79/300 [00:21<01:04,  3.43it/s, acc_rate=0.98]
MALA:  26%|██▋       | 79/300 [00:21<01:04,  3.43it/s, acc_rate=0.98]
MALA:  27%|██▋       | 80/300 [00:21<01:04,  3.43it/s, acc_rate=0.98]
MALA:  27%|██▋       | 80/300 [00:21<01:04,  3.43it/s, acc_rate=0.98]
MALA:  27%|██▋       | 81/300 [00:21<01:03,  3.45it/s, acc_rate=0.98]
MALA:  27%|██▋       | 81/300 [00:22<01:03,  3.45it/s, acc_rate=0.98]
MALA:  27%|██▋       | 82/300 [00:22<01:04,  3.36it/s, acc_rate=0.98]
MALA:  27%|██▋       | 82/300 [00:22<01:04,  3.36it/s, acc_rate=0.98]
MALA:  28%|██▊       | 83/300 [00:22<01:04,  3.36it/s, acc_rate=0.98]
MALA:  28%|██▊       | 83/300 [00:22<01:04,  3.36it/s, acc_rate=0.98]
MALA:  28%|██▊       | 84/300 [00:22<01:04,  3.36it/s, acc_rate=0.98]
MALA:  28%|██▊       | 84/300 [00:22<01:04,  3.36it/s, acc_rate=0.98]
MALA:  28%|██▊       | 85/300 [00:22<01:03,  3.40it/s, acc_rate=0.98]
MALA:  28%|██▊       | 85/300 [00:23<01:03,  3.40it/s, acc_rate=0.98]
MALA:  29%|██▊       | 86/300 [00:23<01:03,  3.37it/s, acc_rate=0.98]
MALA:  29%|██▊       | 86/300 [00:23<01:03,  3.37it/s, acc_rate=0.98]
MALA:  29%|██▉       | 87/300 [00:23<01:02,  3.39it/s, acc_rate=0.98]
MALA:  29%|██▉       | 87/300 [00:23<01:02,  3.39it/s, acc_rate=0.97]
MALA:  29%|██▉       | 88/300 [00:23<01:02,  3.37it/s, acc_rate=0.97]
MALA:  29%|██▉       | 88/300 [00:24<01:02,  3.37it/s, acc_rate=0.97]
MALA:  30%|██▉       | 89/300 [00:24<01:02,  3.40it/s, acc_rate=0.97]
MALA:  30%|██▉       | 89/300 [00:24<01:02,  3.40it/s, acc_rate=0.97]
MALA:  30%|███       | 90/300 [00:24<01:02,  3.38it/s, acc_rate=0.97]
MALA:  30%|███       | 90/300 [00:24<01:02,  3.38it/s, acc_rate=0.98]
MALA:  30%|███       | 91/300 [00:24<01:01,  3.40it/s, acc_rate=0.98]
MALA:  30%|███       | 91/300 [00:25<01:01,  3.40it/s, acc_rate=0.98]
MALA:  31%|███       | 92/300 [00:25<01:02,  3.31it/s, acc_rate=0.98]
MALA:  31%|███       | 92/300 [00:25<01:02,  3.31it/s, acc_rate=0.98]
MALA:  31%|███       | 93/300 [00:25<01:01,  3.37it/s, acc_rate=0.98]
MALA:  31%|███       | 93/300 [00:25<01:01,  3.37it/s, acc_rate=0.98]
MALA:  31%|███▏      | 94/300 [00:25<01:01,  3.37it/s, acc_rate=0.98]
MALA:  31%|███▏      | 94/300 [00:25<01:01,  3.37it/s, acc_rate=0.98]
MALA:  32%|███▏      | 95/300 [00:25<01:00,  3.39it/s, acc_rate=0.98]
MALA:  32%|███▏      | 95/300 [00:26<01:00,  3.39it/s, acc_rate=0.98]
MALA:  32%|███▏      | 96/300 [00:26<00:59,  3.40it/s, acc_rate=0.98]
MALA:  32%|███▏      | 96/300 [00:26<00:59,  3.40it/s, acc_rate=0.98]
MALA:  32%|███▏      | 97/300 [00:26<00:59,  3.40it/s, acc_rate=0.98]
MALA:  32%|███▏      | 97/300 [00:26<00:59,  3.40it/s, acc_rate=0.98]
MALA:  33%|███▎      | 98/300 [00:26<00:59,  3.41it/s, acc_rate=0.98]
MALA:  33%|███▎      | 98/300 [00:27<00:59,  3.41it/s, acc_rate=0.98]
MALA:  33%|███▎      | 99/300 [00:27<00:59,  3.36it/s, acc_rate=0.98]
MALA:  33%|███▎      | 99/300 [00:27<00:59,  3.36it/s, acc_rate=0.98]
MALA:  33%|███▎      | 100/300 [00:27<00:59,  3.35it/s, acc_rate=0.98]
MALA:  33%|███▎      | 100/300 [00:27<00:59,  3.35it/s, acc_rate=0.98]
MALA:  34%|███▎      | 101/300 [00:27<00:58,  3.39it/s, acc_rate=0.98]
MALA:  34%|███▎      | 101/300 [00:27<00:58,  3.39it/s, acc_rate=0.98]
MALA:  34%|███▍      | 102/300 [00:27<00:58,  3.38it/s, acc_rate=0.98]
MALA:  34%|███▍      | 102/300 [00:28<00:58,  3.38it/s, acc_rate=0.98]
MALA:  34%|███▍      | 103/300 [00:28<00:58,  3.39it/s, acc_rate=0.98]
MALA:  34%|███▍      | 103/300 [00:28<00:58,  3.39it/s, acc_rate=0.97]
MALA:  35%|███▍      | 104/300 [00:28<00:57,  3.41it/s, acc_rate=0.97]
MALA:  35%|███▍      | 104/300 [00:28<00:57,  3.41it/s, acc_rate=0.97]
MALA:  35%|███▌      | 105/300 [00:28<00:56,  3.44it/s, acc_rate=0.97]
MALA:  35%|███▌      | 105/300 [00:29<00:56,  3.44it/s, acc_rate=0.97]
MALA:  35%|███▌      | 106/300 [00:29<00:56,  3.45it/s, acc_rate=0.97]
MALA:  35%|███▌      | 106/300 [00:29<00:56,  3.45it/s, acc_rate=0.97]
MALA:  36%|███▌      | 107/300 [00:29<00:55,  3.45it/s, acc_rate=0.97]
MALA:  36%|███▌      | 107/300 [00:29<00:55,  3.45it/s, acc_rate=0.97]
MALA:  36%|███▌      | 108/300 [00:29<00:55,  3.46it/s, acc_rate=0.97]
MALA:  36%|███▌      | 108/300 [00:30<00:55,  3.46it/s, acc_rate=0.97]
MALA:  36%|███▋      | 109/300 [00:30<00:54,  3.47it/s, acc_rate=0.97]
MALA:  36%|███▋      | 109/300 [00:30<00:54,  3.47it/s, acc_rate=0.97]
MALA:  37%|███▋      | 110/300 [00:30<00:54,  3.48it/s, acc_rate=0.97]
MALA:  37%|███▋      | 110/300 [00:30<00:54,  3.48it/s, acc_rate=0.98]
MALA:  37%|███▋      | 111/300 [00:30<00:54,  3.45it/s, acc_rate=0.98]
MALA:  37%|███▋      | 111/300 [00:30<00:54,  3.45it/s, acc_rate=0.98]
MALA:  37%|███▋      | 112/300 [00:30<00:55,  3.41it/s, acc_rate=0.98]
MALA:  37%|███▋      | 112/300 [00:31<00:55,  3.41it/s, acc_rate=0.98]
MALA:  38%|███▊      | 113/300 [00:31<00:54,  3.44it/s, acc_rate=0.98]
MALA:  38%|███▊      | 113/300 [00:31<00:54,  3.44it/s, acc_rate=0.98]
MALA:  38%|███▊      | 114/300 [00:31<00:53,  3.46it/s, acc_rate=0.98]
MALA:  38%|███▊      | 114/300 [00:31<00:53,  3.46it/s, acc_rate=0.98]
MALA:  38%|███▊      | 115/300 [00:31<00:53,  3.47it/s, acc_rate=0.98]
MALA:  38%|███▊      | 115/300 [00:32<00:53,  3.47it/s, acc_rate=0.98]
MALA:  39%|███▊      | 116/300 [00:32<00:53,  3.46it/s, acc_rate=0.98]
MALA:  39%|███▊      | 116/300 [00:32<00:53,  3.46it/s, acc_rate=0.98]
MALA:  39%|███▉      | 117/300 [00:32<00:52,  3.47it/s, acc_rate=0.98]
MALA:  39%|███▉      | 117/300 [00:32<00:52,  3.47it/s, acc_rate=0.98]
MALA:  39%|███▉      | 118/300 [00:32<00:52,  3.48it/s, acc_rate=0.98]
MALA:  39%|███▉      | 118/300 [00:32<00:52,  3.48it/s, acc_rate=0.98]
MALA:  40%|███▉      | 119/300 [00:32<00:52,  3.46it/s, acc_rate=0.98]
MALA:  40%|███▉      | 119/300 [00:33<00:52,  3.46it/s, acc_rate=0.98]
MALA:  40%|████      | 120/300 [00:33<00:52,  3.45it/s, acc_rate=0.98]
MALA:  40%|████      | 120/300 [00:33<00:52,  3.45it/s, acc_rate=0.98]
MALA:  40%|████      | 121/300 [00:33<00:52,  3.38it/s, acc_rate=0.98]
MALA:  40%|████      | 121/300 [00:33<00:52,  3.38it/s, acc_rate=0.98]
MALA:  41%|████      | 122/300 [00:33<00:52,  3.41it/s, acc_rate=0.98]
MALA:  41%|████      | 122/300 [00:34<00:52,  3.41it/s, acc_rate=0.98]
MALA:  41%|████      | 123/300 [00:34<00:51,  3.44it/s, acc_rate=0.98]
MALA:  41%|████      | 123/300 [00:34<00:51,  3.44it/s, acc_rate=0.98]
MALA:  41%|████▏     | 124/300 [00:34<00:50,  3.50it/s, acc_rate=0.98]
MALA:  41%|████▏     | 124/300 [00:34<00:50,  3.50it/s, acc_rate=0.98]
MALA:  42%|████▏     | 125/300 [00:34<00:45,  3.87it/s, acc_rate=0.98]
MALA:  42%|████▏     | 125/300 [00:34<00:45,  3.87it/s, acc_rate=0.98]
MALA:  42%|████▏     | 126/300 [00:34<00:41,  4.16it/s, acc_rate=0.98]
MALA:  42%|████▏     | 126/300 [00:34<00:41,  4.16it/s, acc_rate=0.98]
MALA:  42%|████▏     | 127/300 [00:34<00:39,  4.36it/s, acc_rate=0.98]
MALA:  42%|████▏     | 127/300 [00:35<00:39,  4.36it/s, acc_rate=0.98]
MALA:  43%|████▎     | 128/300 [00:35<00:39,  4.38it/s, acc_rate=0.98]
MALA:  43%|████▎     | 128/300 [00:35<00:39,  4.38it/s, acc_rate=0.98]
MALA:  43%|████▎     | 129/300 [00:35<00:38,  4.41it/s, acc_rate=0.98]
MALA:  43%|████▎     | 129/300 [00:35<00:38,  4.41it/s, acc_rate=0.98]
MALA:  43%|████▎     | 130/300 [00:35<00:43,  3.93it/s, acc_rate=0.98]
MALA:  43%|████▎     | 130/300 [00:36<00:43,  3.93it/s, acc_rate=0.98]
MALA:  44%|████▎     | 131/300 [00:36<00:44,  3.80it/s, acc_rate=0.98]
MALA:  44%|████▎     | 131/300 [00:36<00:44,  3.80it/s, acc_rate=0.98]
MALA:  44%|████▍     | 132/300 [00:36<00:45,  3.67it/s, acc_rate=0.98]
MALA:  44%|████▍     | 132/300 [00:36<00:45,  3.67it/s, acc_rate=0.98]
MALA:  44%|████▍     | 133/300 [00:36<00:46,  3.63it/s, acc_rate=0.98]
MALA:  44%|████▍     | 133/300 [00:36<00:46,  3.63it/s, acc_rate=0.98]
MALA:  45%|████▍     | 134/300 [00:36<00:46,  3.58it/s, acc_rate=0.98]
MALA:  45%|████▍     | 134/300 [00:37<00:46,  3.58it/s, acc_rate=0.98]
MALA:  45%|████▌     | 135/300 [00:37<00:46,  3.57it/s, acc_rate=0.98]
MALA:  45%|████▌     | 135/300 [00:37<00:46,  3.57it/s, acc_rate=0.98]
MALA:  45%|████▌     | 136/300 [00:37<00:46,  3.55it/s, acc_rate=0.98]
MALA:  45%|████▌     | 136/300 [00:37<00:46,  3.55it/s, acc_rate=0.98]
MALA:  46%|████▌     | 137/300 [00:37<00:45,  3.55it/s, acc_rate=0.98]
MALA:  46%|████▌     | 137/300 [00:38<00:45,  3.55it/s, acc_rate=0.98]
MALA:  46%|████▌     | 138/300 [00:38<00:45,  3.53it/s, acc_rate=0.98]
MALA:  46%|████▌     | 138/300 [00:38<00:45,  3.53it/s, acc_rate=0.98]
MALA:  46%|████▋     | 139/300 [00:38<00:45,  3.51it/s, acc_rate=0.98]
MALA:  46%|████▋     | 139/300 [00:38<00:45,  3.51it/s, acc_rate=0.98]
MALA:  47%|████▋     | 140/300 [00:38<00:44,  3.58it/s, acc_rate=0.98]
MALA:  47%|████▋     | 140/300 [00:38<00:44,  3.58it/s, acc_rate=0.98]
MALA:  47%|████▋     | 141/300 [00:38<00:44,  3.56it/s, acc_rate=0.98]
MALA:  47%|████▋     | 141/300 [00:39<00:44,  3.56it/s, acc_rate=0.98]
MALA:  47%|████▋     | 142/300 [00:39<00:44,  3.56it/s, acc_rate=0.98]
MALA:  47%|████▋     | 142/300 [00:39<00:44,  3.56it/s, acc_rate=0.98]
MALA:  48%|████▊     | 143/300 [00:39<00:44,  3.52it/s, acc_rate=0.98]
MALA:  48%|████▊     | 143/300 [00:39<00:44,  3.52it/s, acc_rate=0.98]
MALA:  48%|████▊     | 144/300 [00:39<00:44,  3.52it/s, acc_rate=0.98]
MALA:  48%|████▊     | 144/300 [00:39<00:44,  3.52it/s, acc_rate=0.98]
MALA:  48%|████▊     | 145/300 [00:39<00:43,  3.52it/s, acc_rate=0.98]
MALA:  48%|████▊     | 145/300 [00:40<00:43,  3.52it/s, acc_rate=0.98]
MALA:  49%|████▊     | 146/300 [00:40<00:43,  3.50it/s, acc_rate=0.98]
MALA:  49%|████▊     | 146/300 [00:40<00:43,  3.50it/s, acc_rate=0.98]
MALA:  49%|████▉     | 147/300 [00:40<00:43,  3.48it/s, acc_rate=0.98]
MALA:  49%|████▉     | 147/300 [00:40<00:43,  3.48it/s, acc_rate=0.98]
MALA:  49%|████▉     | 148/300 [00:40<00:43,  3.47it/s, acc_rate=0.98]
MALA:  49%|████▉     | 148/300 [00:41<00:43,  3.47it/s, acc_rate=0.98]
MALA:  50%|████▉     | 149/300 [00:41<00:42,  3.55it/s, acc_rate=0.98]
MALA:  50%|████▉     | 149/300 [00:41<00:42,  3.55it/s, acc_rate=0.98]
MALA:  50%|█████     | 150/300 [00:41<00:42,  3.55it/s, acc_rate=0.98]
MALA:  50%|█████     | 150/300 [00:41<00:42,  3.55it/s, acc_rate=0.98]
MALA:  50%|█████     | 151/300 [00:41<00:42,  3.54it/s, acc_rate=0.98]
MALA:  50%|█████     | 151/300 [00:41<00:42,  3.54it/s, acc_rate=0.98]
MALA:  51%|█████     | 152/300 [00:41<00:42,  3.52it/s, acc_rate=0.98]
MALA:  51%|█████     | 152/300 [00:42<00:42,  3.52it/s, acc_rate=0.98]
MALA:  51%|█████     | 153/300 [00:42<00:43,  3.41it/s, acc_rate=0.98]
MALA:  51%|█████     | 153/300 [00:42<00:43,  3.41it/s, acc_rate=0.98]
MALA:  51%|█████▏    | 154/300 [00:42<00:42,  3.43it/s, acc_rate=0.98]
MALA:  51%|█████▏    | 154/300 [00:42<00:42,  3.43it/s, acc_rate=0.98]
MALA:  52%|█████▏    | 155/300 [00:42<00:41,  3.46it/s, acc_rate=0.98]
MALA:  52%|█████▏    | 155/300 [00:43<00:41,  3.46it/s, acc_rate=0.98]
MALA:  52%|█████▏    | 156/300 [00:43<00:41,  3.46it/s, acc_rate=0.98]
MALA:  52%|█████▏    | 156/300 [00:43<00:41,  3.46it/s, acc_rate=0.98]
MALA:  52%|█████▏    | 157/300 [00:43<00:40,  3.50it/s, acc_rate=0.98]
MALA:  52%|█████▏    | 157/300 [00:43<00:40,  3.50it/s, acc_rate=0.98]
MALA:  53%|█████▎    | 158/300 [00:43<00:40,  3.49it/s, acc_rate=0.98]
MALA:  53%|█████▎    | 158/300 [00:44<00:40,  3.49it/s, acc_rate=0.98]
MALA:  53%|█████▎    | 159/300 [00:44<00:40,  3.46it/s, acc_rate=0.98]
MALA:  53%|█████▎    | 159/300 [00:44<00:40,  3.46it/s, acc_rate=0.98]
MALA:  53%|█████▎    | 160/300 [00:44<00:40,  3.47it/s, acc_rate=0.98]
MALA:  53%|█████▎    | 160/300 [00:44<00:40,  3.47it/s, acc_rate=0.98]
MALA:  54%|█████▎    | 161/300 [00:44<00:39,  3.49it/s, acc_rate=0.98]
MALA:  54%|█████▎    | 161/300 [00:44<00:39,  3.49it/s, acc_rate=0.98]
MALA:  54%|█████▍    | 162/300 [00:44<00:39,  3.48it/s, acc_rate=0.98]
MALA:  54%|█████▍    | 162/300 [00:45<00:39,  3.48it/s, acc_rate=0.98]
MALA:  54%|█████▍    | 163/300 [00:45<00:39,  3.49it/s, acc_rate=0.98]
MALA:  54%|█████▍    | 163/300 [00:45<00:39,  3.49it/s, acc_rate=0.98]
MALA:  55%|█████▍    | 164/300 [00:45<00:38,  3.51it/s, acc_rate=0.98]
MALA:  55%|█████▍    | 164/300 [00:45<00:38,  3.51it/s, acc_rate=0.98]
MALA:  55%|█████▌    | 165/300 [00:45<00:38,  3.51it/s, acc_rate=0.98]
MALA:  55%|█████▌    | 165/300 [00:46<00:38,  3.51it/s, acc_rate=0.98]
MALA:  55%|█████▌    | 166/300 [00:46<00:38,  3.51it/s, acc_rate=0.98]
MALA:  55%|█████▌    | 166/300 [00:46<00:38,  3.51it/s, acc_rate=0.98]
MALA:  56%|█████▌    | 167/300 [00:46<00:37,  3.51it/s, acc_rate=0.98]
MALA:  56%|█████▌    | 167/300 [00:46<00:37,  3.51it/s, acc_rate=0.98]
MALA:  56%|█████▌    | 168/300 [00:46<00:37,  3.52it/s, acc_rate=0.98]
MALA:  56%|█████▌    | 168/300 [00:46<00:37,  3.52it/s, acc_rate=0.98]
MALA:  56%|█████▋    | 169/300 [00:46<00:37,  3.46it/s, acc_rate=0.98]
MALA:  56%|█████▋    | 169/300 [00:47<00:37,  3.46it/s, acc_rate=0.98]
MALA:  57%|█████▋    | 170/300 [00:47<00:37,  3.47it/s, acc_rate=0.98]
MALA:  57%|█████▋    | 170/300 [00:47<00:37,  3.47it/s, acc_rate=0.98]
MALA:  57%|█████▋    | 171/300 [00:47<00:37,  3.47it/s, acc_rate=0.98]
MALA:  57%|█████▋    | 171/300 [00:47<00:37,  3.47it/s, acc_rate=0.98]
MALA:  57%|█████▋    | 172/300 [00:47<00:36,  3.47it/s, acc_rate=0.98]
MALA:  57%|█████▋    | 172/300 [00:48<00:36,  3.47it/s, acc_rate=0.98]
MALA:  58%|█████▊    | 173/300 [00:48<00:36,  3.48it/s, acc_rate=0.98]
MALA:  58%|█████▊    | 173/300 [00:48<00:36,  3.48it/s, acc_rate=0.98]
MALA:  58%|█████▊    | 174/300 [00:48<00:36,  3.46it/s, acc_rate=0.98]
MALA:  58%|█████▊    | 174/300 [00:48<00:36,  3.46it/s, acc_rate=0.98]
MALA:  58%|█████▊    | 175/300 [00:48<00:36,  3.47it/s, acc_rate=0.98]
MALA:  58%|█████▊    | 175/300 [00:48<00:36,  3.47it/s, acc_rate=0.98]
MALA:  59%|█████▊    | 176/300 [00:48<00:35,  3.46it/s, acc_rate=0.98]
MALA:  59%|█████▊    | 176/300 [00:49<00:35,  3.46it/s, acc_rate=0.98]
MALA:  59%|█████▉    | 177/300 [00:49<00:35,  3.48it/s, acc_rate=0.98]
MALA:  59%|█████▉    | 177/300 [00:49<00:35,  3.48it/s, acc_rate=0.98]
MALA:  59%|█████▉    | 178/300 [00:49<00:35,  3.41it/s, acc_rate=0.98]
MALA:  59%|█████▉    | 178/300 [00:49<00:35,  3.41it/s, acc_rate=0.98]
MALA:  60%|█████▉    | 179/300 [00:49<00:35,  3.43it/s, acc_rate=0.98]
MALA:  60%|█████▉    | 179/300 [00:50<00:35,  3.43it/s, acc_rate=0.98]
MALA:  60%|██████    | 180/300 [00:50<00:34,  3.46it/s, acc_rate=0.98]
MALA:  60%|██████    | 180/300 [00:50<00:34,  3.46it/s, acc_rate=0.98]
MALA:  60%|██████    | 181/300 [00:50<00:34,  3.48it/s, acc_rate=0.98]
MALA:  60%|██████    | 181/300 [00:50<00:34,  3.48it/s, acc_rate=0.98]
MALA:  61%|██████    | 182/300 [00:50<00:34,  3.47it/s, acc_rate=0.98]
MALA:  61%|██████    | 182/300 [00:50<00:34,  3.47it/s, acc_rate=0.98]
MALA:  61%|██████    | 183/300 [00:50<00:33,  3.47it/s, acc_rate=0.98]
MALA:  61%|██████    | 183/300 [00:51<00:33,  3.47it/s, acc_rate=0.98]
MALA:  61%|██████▏   | 184/300 [00:51<00:33,  3.48it/s, acc_rate=0.98]
MALA:  61%|██████▏   | 184/300 [00:51<00:33,  3.48it/s, acc_rate=0.98]
MALA:  62%|██████▏   | 185/300 [00:51<00:32,  3.49it/s, acc_rate=0.98]
MALA:  62%|██████▏   | 185/300 [00:51<00:32,  3.49it/s, acc_rate=0.98]
MALA:  62%|██████▏   | 186/300 [00:51<00:32,  3.48it/s, acc_rate=0.98]
MALA:  62%|██████▏   | 186/300 [00:52<00:32,  3.48it/s, acc_rate=0.98]
MALA:  62%|██████▏   | 187/300 [00:52<00:32,  3.47it/s, acc_rate=0.98]
MALA:  62%|██████▏   | 187/300 [00:52<00:32,  3.47it/s, acc_rate=0.98]
MALA:  63%|██████▎   | 188/300 [00:52<00:32,  3.42it/s, acc_rate=0.98]
MALA:  63%|██████▎   | 188/300 [00:52<00:32,  3.42it/s, acc_rate=0.98]
MALA:  63%|██████▎   | 189/300 [00:52<00:32,  3.45it/s, acc_rate=0.98]
MALA:  63%|██████▎   | 189/300 [00:52<00:32,  3.45it/s, acc_rate=0.98]
MALA:  63%|██████▎   | 190/300 [00:52<00:31,  3.45it/s, acc_rate=0.98]
MALA:  63%|██████▎   | 190/300 [00:53<00:31,  3.45it/s, acc_rate=0.98]
MALA:  64%|██████▎   | 191/300 [00:53<00:31,  3.45it/s, acc_rate=0.98]
MALA:  64%|██████▎   | 191/300 [00:53<00:31,  3.45it/s, acc_rate=0.98]
MALA:  64%|██████▍   | 192/300 [00:53<00:31,  3.46it/s, acc_rate=0.98]
MALA:  64%|██████▍   | 192/300 [00:53<00:31,  3.46it/s, acc_rate=0.98]
MALA:  64%|██████▍   | 193/300 [00:53<00:30,  3.47it/s, acc_rate=0.98]
MALA:  64%|██████▍   | 193/300 [00:54<00:30,  3.47it/s, acc_rate=0.98]
MALA:  65%|██████▍   | 194/300 [00:54<00:30,  3.48it/s, acc_rate=0.98]
MALA:  65%|██████▍   | 194/300 [00:54<00:30,  3.48it/s, acc_rate=0.98]
MALA:  65%|██████▌   | 195/300 [00:54<00:30,  3.49it/s, acc_rate=0.98]
MALA:  65%|██████▌   | 195/300 [00:54<00:30,  3.49it/s, acc_rate=0.98]
MALA:  65%|██████▌   | 196/300 [00:54<00:29,  3.49it/s, acc_rate=0.98]
MALA:  65%|██████▌   | 196/300 [00:54<00:29,  3.49it/s, acc_rate=0.98]
MALA:  66%|██████▌   | 197/300 [00:54<00:28,  3.56it/s, acc_rate=0.98]
MALA:  66%|██████▌   | 197/300 [00:55<00:28,  3.56it/s, acc_rate=0.98]
MALA:  66%|██████▌   | 198/300 [00:55<00:28,  3.59it/s, acc_rate=0.98]
MALA:  66%|██████▌   | 198/300 [00:55<00:28,  3.59it/s, acc_rate=0.98]
MALA:  66%|██████▋   | 199/300 [00:55<00:24,  4.08it/s, acc_rate=0.98]
MALA:  66%|██████▋   | 199/300 [00:55<00:24,  4.08it/s, acc_rate=0.98]
MALA:  67%|██████▋   | 200/300 [00:55<00:22,  4.44it/s, acc_rate=0.98]
MALA:  67%|██████▋   | 200/300 [00:55<00:22,  4.44it/s, acc_rate=0.98]
MALA:  67%|██████▋   | 201/300 [00:55<00:20,  4.81it/s, acc_rate=0.98]
MALA:  67%|██████▋   | 201/300 [00:55<00:20,  4.81it/s, acc_rate=0.98]
MALA:  67%|██████▋   | 202/300 [00:55<00:19,  5.09it/s, acc_rate=0.98]
MALA:  67%|██████▋   | 202/300 [00:56<00:19,  5.09it/s, acc_rate=0.98]
MALA:  68%|██████▊   | 203/300 [00:56<00:18,  5.31it/s, acc_rate=0.98]
MALA:  68%|██████▊   | 203/300 [00:56<00:18,  5.31it/s, acc_rate=0.98]
MALA:  68%|██████▊   | 204/300 [00:56<00:17,  5.40it/s, acc_rate=0.98]
MALA:  68%|██████▊   | 204/300 [00:56<00:17,  5.40it/s, acc_rate=0.98]
MALA:  68%|██████▊   | 205/300 [00:56<00:17,  5.29it/s, acc_rate=0.98]
MALA:  68%|██████▊   | 205/300 [00:56<00:17,  5.29it/s, acc_rate=0.98]
MALA:  69%|██████▊   | 206/300 [00:56<00:18,  5.18it/s, acc_rate=0.98]
MALA:  69%|██████▊   | 206/300 [00:56<00:18,  5.18it/s, acc_rate=0.98]
MALA:  69%|██████▉   | 207/300 [00:56<00:21,  4.38it/s, acc_rate=0.98]
MALA:  69%|██████▉   | 207/300 [00:57<00:21,  4.38it/s, acc_rate=0.98]
MALA:  69%|██████▉   | 208/300 [00:57<00:23,  3.99it/s, acc_rate=0.98]
MALA:  69%|██████▉   | 208/300 [00:57<00:23,  3.99it/s, acc_rate=0.98]
MALA:  70%|██████▉   | 209/300 [00:57<00:23,  3.84it/s, acc_rate=0.98]
MALA:  70%|██████▉   | 209/300 [00:57<00:23,  3.84it/s, acc_rate=0.98]
MALA:  70%|███████   | 210/300 [00:57<00:24,  3.72it/s, acc_rate=0.98]
MALA:  70%|███████   | 210/300 [00:58<00:24,  3.72it/s, acc_rate=0.98]
MALA:  70%|███████   | 211/300 [00:58<00:24,  3.65it/s, acc_rate=0.98]
MALA:  70%|███████   | 211/300 [00:58<00:24,  3.65it/s, acc_rate=0.98]
MALA:  71%|███████   | 212/300 [00:58<00:24,  3.60it/s, acc_rate=0.98]
MALA:  71%|███████   | 212/300 [00:58<00:24,  3.60it/s, acc_rate=0.98]
MALA:  71%|███████   | 213/300 [00:58<00:24,  3.58it/s, acc_rate=0.98]
MALA:  71%|███████   | 213/300 [00:58<00:24,  3.58it/s, acc_rate=0.98]
MALA:  71%|███████▏  | 214/300 [00:58<00:24,  3.54it/s, acc_rate=0.98]
MALA:  71%|███████▏  | 214/300 [00:59<00:24,  3.54it/s, acc_rate=0.98]
MALA:  72%|███████▏  | 215/300 [00:59<00:24,  3.50it/s, acc_rate=0.98]
MALA:  72%|███████▏  | 215/300 [00:59<00:24,  3.50it/s, acc_rate=0.98]
MALA:  72%|███████▏  | 216/300 [00:59<00:23,  3.50it/s, acc_rate=0.98]
MALA:  72%|███████▏  | 216/300 [00:59<00:23,  3.50it/s, acc_rate=0.98]
MALA:  72%|███████▏  | 217/300 [00:59<00:24,  3.43it/s, acc_rate=0.98]
MALA:  72%|███████▏  | 217/300 [01:00<00:24,  3.43it/s, acc_rate=0.98]
MALA:  73%|███████▎  | 218/300 [01:00<00:23,  3.43it/s, acc_rate=0.98]
MALA:  73%|███████▎  | 218/300 [01:00<00:23,  3.43it/s, acc_rate=0.98]
MALA:  73%|███████▎  | 219/300 [01:00<00:23,  3.43it/s, acc_rate=0.98]
MALA:  73%|███████▎  | 219/300 [01:00<00:23,  3.43it/s, acc_rate=0.98]
MALA:  73%|███████▎  | 220/300 [01:00<00:23,  3.43it/s, acc_rate=0.98]
MALA:  73%|███████▎  | 220/300 [01:01<00:23,  3.43it/s, acc_rate=0.98]
MALA:  74%|███████▎  | 221/300 [01:01<00:22,  3.45it/s, acc_rate=0.98]
MALA:  74%|███████▎  | 221/300 [01:01<00:22,  3.45it/s, acc_rate=0.98]
MALA:  74%|███████▍  | 222/300 [01:01<00:22,  3.48it/s, acc_rate=0.98]
MALA:  74%|███████▍  | 222/300 [01:01<00:22,  3.48it/s, acc_rate=0.98]
MALA:  74%|███████▍  | 223/300 [01:01<00:20,  3.73it/s, acc_rate=0.98]
MALA:  74%|███████▍  | 223/300 [01:01<00:20,  3.73it/s, acc_rate=0.98]
MALA:  75%|███████▍  | 224/300 [01:01<00:19,  3.89it/s, acc_rate=0.98]
MALA:  75%|███████▍  | 224/300 [01:01<00:19,  3.89it/s, acc_rate=0.98]
MALA:  75%|███████▌  | 225/300 [01:01<00:18,  4.03it/s, acc_rate=0.98]
MALA:  75%|███████▌  | 225/300 [01:02<00:18,  4.03it/s, acc_rate=0.98]
MALA:  75%|███████▌  | 226/300 [01:02<00:19,  3.74it/s, acc_rate=0.98]
MALA:  75%|███████▌  | 226/300 [01:02<00:19,  3.74it/s, acc_rate=0.98]
MALA:  76%|███████▌  | 227/300 [01:02<00:19,  3.65it/s, acc_rate=0.98]
MALA:  76%|███████▌  | 227/300 [01:02<00:19,  3.65it/s, acc_rate=0.98]
MALA:  76%|███████▌  | 228/300 [01:02<00:20,  3.59it/s, acc_rate=0.98]
MALA:  76%|███████▌  | 228/300 [01:03<00:20,  3.59it/s, acc_rate=0.98]
MALA:  76%|███████▋  | 229/300 [01:03<00:19,  3.57it/s, acc_rate=0.98]
MALA:  76%|███████▋  | 229/300 [01:03<00:19,  3.57it/s, acc_rate=0.98]
MALA:  77%|███████▋  | 230/300 [01:03<00:19,  3.52it/s, acc_rate=0.98]
MALA:  77%|███████▋  | 230/300 [01:03<00:19,  3.52it/s, acc_rate=0.98]
MALA:  77%|███████▋  | 231/300 [01:03<00:19,  3.49it/s, acc_rate=0.98]
MALA:  77%|███████▋  | 231/300 [01:04<00:19,  3.49it/s, acc_rate=0.98]
MALA:  77%|███████▋  | 232/300 [01:04<00:19,  3.48it/s, acc_rate=0.98]
MALA:  77%|███████▋  | 232/300 [01:04<00:19,  3.48it/s, acc_rate=0.98]
MALA:  78%|███████▊  | 233/300 [01:04<00:19,  3.49it/s, acc_rate=0.98]
MALA:  78%|███████▊  | 233/300 [01:04<00:19,  3.49it/s, acc_rate=0.98]
MALA:  78%|███████▊  | 234/300 [01:04<00:18,  3.47it/s, acc_rate=0.98]
MALA:  78%|███████▊  | 234/300 [01:04<00:18,  3.47it/s, acc_rate=0.98]
MALA:  78%|███████▊  | 235/300 [01:04<00:18,  3.46it/s, acc_rate=0.98]
MALA:  78%|███████▊  | 235/300 [01:05<00:18,  3.46it/s, acc_rate=0.98]
MALA:  79%|███████▊  | 236/300 [01:05<00:18,  3.39it/s, acc_rate=0.98]
MALA:  79%|███████▊  | 236/300 [01:05<00:18,  3.39it/s, acc_rate=0.98]
MALA:  79%|███████▉  | 237/300 [01:05<00:18,  3.45it/s, acc_rate=0.98]
MALA:  79%|███████▉  | 237/300 [01:05<00:18,  3.45it/s, acc_rate=0.98]
MALA:  79%|███████▉  | 238/300 [01:05<00:18,  3.44it/s, acc_rate=0.98]
MALA:  79%|███████▉  | 238/300 [01:06<00:18,  3.44it/s, acc_rate=0.98]
MALA:  80%|███████▉  | 239/300 [01:06<00:17,  3.42it/s, acc_rate=0.98]
MALA:  80%|███████▉  | 239/300 [01:06<00:17,  3.42it/s, acc_rate=0.98]
MALA:  80%|████████  | 240/300 [01:06<00:17,  3.41it/s, acc_rate=0.98]
MALA:  80%|████████  | 240/300 [01:06<00:17,  3.41it/s, acc_rate=0.98]
MALA:  80%|████████  | 241/300 [01:06<00:17,  3.44it/s, acc_rate=0.98]
MALA:  80%|████████  | 241/300 [01:06<00:17,  3.44it/s, acc_rate=0.98]
MALA:  81%|████████  | 242/300 [01:06<00:16,  3.43it/s, acc_rate=0.98]
MALA:  81%|████████  | 242/300 [01:07<00:16,  3.43it/s, acc_rate=0.98]
MALA:  81%|████████  | 243/300 [01:07<00:16,  3.43it/s, acc_rate=0.98]
MALA:  81%|████████  | 243/300 [01:07<00:16,  3.43it/s, acc_rate=0.98]
MALA:  81%|████████▏ | 244/300 [01:07<00:16,  3.44it/s, acc_rate=0.98]
MALA:  81%|████████▏ | 244/300 [01:07<00:16,  3.44it/s, acc_rate=0.98]
MALA:  82%|████████▏ | 245/300 [01:07<00:15,  3.57it/s, acc_rate=0.98]
MALA:  82%|████████▏ | 245/300 [01:08<00:15,  3.57it/s, acc_rate=0.98]
MALA:  82%|████████▏ | 246/300 [01:08<00:15,  3.56it/s, acc_rate=0.98]
MALA:  82%|████████▏ | 246/300 [01:08<00:15,  3.56it/s, acc_rate=0.98]
MALA:  82%|████████▏ | 247/300 [01:08<00:15,  3.52it/s, acc_rate=0.98]
MALA:  82%|████████▏ | 247/300 [01:08<00:15,  3.52it/s, acc_rate=0.98]
MALA:  83%|████████▎ | 248/300 [01:08<00:14,  3.50it/s, acc_rate=0.98]
MALA:  83%|████████▎ | 248/300 [01:08<00:14,  3.50it/s, acc_rate=0.98]
MALA:  83%|████████▎ | 249/300 [01:08<00:14,  3.50it/s, acc_rate=0.98]
MALA:  83%|████████▎ | 249/300 [01:09<00:14,  3.50it/s, acc_rate=0.98]
MALA:  83%|████████▎ | 250/300 [01:09<00:14,  3.48it/s, acc_rate=0.98]
MALA:  83%|████████▎ | 250/300 [01:09<00:14,  3.48it/s, acc_rate=0.98]
MALA:  84%|████████▎ | 251/300 [01:09<00:14,  3.48it/s, acc_rate=0.98]
MALA:  84%|████████▎ | 251/300 [01:09<00:14,  3.48it/s, acc_rate=0.98]
MALA:  84%|████████▍ | 252/300 [01:09<00:13,  3.49it/s, acc_rate=0.98]
MALA:  84%|████████▍ | 252/300 [01:10<00:13,  3.49it/s, acc_rate=0.98]
MALA:  84%|████████▍ | 253/300 [01:10<00:13,  3.50it/s, acc_rate=0.98]
MALA:  84%|████████▍ | 253/300 [01:10<00:13,  3.50it/s, acc_rate=0.98]
MALA:  85%|████████▍ | 254/300 [01:10<00:13,  3.49it/s, acc_rate=0.98]
MALA:  85%|████████▍ | 254/300 [01:10<00:13,  3.49it/s, acc_rate=0.98]
MALA:  85%|████████▌ | 255/300 [01:10<00:13,  3.44it/s, acc_rate=0.98]
MALA:  85%|████████▌ | 255/300 [01:10<00:13,  3.44it/s, acc_rate=0.98]
MALA:  85%|████████▌ | 256/300 [01:10<00:12,  3.45it/s, acc_rate=0.98]
MALA:  85%|████████▌ | 256/300 [01:11<00:12,  3.45it/s, acc_rate=0.98]
MALA:  86%|████████▌ | 257/300 [01:11<00:12,  3.47it/s, acc_rate=0.98]
MALA:  86%|████████▌ | 257/300 [01:11<00:12,  3.47it/s, acc_rate=0.98]
MALA:  86%|████████▌ | 258/300 [01:11<00:12,  3.48it/s, acc_rate=0.98]
MALA:  86%|████████▌ | 258/300 [01:11<00:12,  3.48it/s, acc_rate=0.98]
MALA:  86%|████████▋ | 259/300 [01:11<00:11,  3.52it/s, acc_rate=0.98]
MALA:  86%|████████▋ | 259/300 [01:11<00:11,  3.52it/s, acc_rate=0.98]
MALA:  87%|████████▋ | 260/300 [01:11<00:10,  3.99it/s, acc_rate=0.98]
MALA:  87%|████████▋ | 260/300 [01:12<00:10,  3.99it/s, acc_rate=0.98]
MALA:  87%|████████▋ | 261/300 [01:12<00:10,  3.63it/s, acc_rate=0.98]
MALA:  87%|████████▋ | 261/300 [01:12<00:10,  3.63it/s, acc_rate=0.98]
MALA:  87%|████████▋ | 262/300 [01:12<00:10,  3.57it/s, acc_rate=0.98]
MALA:  87%|████████▋ | 262/300 [01:12<00:10,  3.57it/s, acc_rate=0.98]
MALA:  88%|████████▊ | 263/300 [01:12<00:10,  3.54it/s, acc_rate=0.98]
MALA:  88%|████████▊ | 263/300 [01:13<00:10,  3.54it/s, acc_rate=0.98]
MALA:  88%|████████▊ | 264/300 [01:13<00:10,  3.54it/s, acc_rate=0.98]
MALA:  88%|████████▊ | 264/300 [01:13<00:10,  3.54it/s, acc_rate=0.98]
MALA:  88%|████████▊ | 265/300 [01:13<00:10,  3.48it/s, acc_rate=0.98]
MALA:  88%|████████▊ | 265/300 [01:13<00:10,  3.48it/s, acc_rate=0.98]
MALA:  89%|████████▊ | 266/300 [01:13<00:09,  3.47it/s, acc_rate=0.98]
MALA:  89%|████████▊ | 266/300 [01:14<00:09,  3.47it/s, acc_rate=0.98]
MALA:  89%|████████▉ | 267/300 [01:14<00:09,  3.46it/s, acc_rate=0.98]
MALA:  89%|████████▉ | 267/300 [01:14<00:09,  3.46it/s, acc_rate=0.98]
MALA:  89%|████████▉ | 268/300 [01:14<00:09,  3.46it/s, acc_rate=0.98]
MALA:  89%|████████▉ | 268/300 [01:14<00:09,  3.46it/s, acc_rate=0.98]
MALA:  90%|████████▉ | 269/300 [01:14<00:08,  3.48it/s, acc_rate=0.98]
MALA:  90%|████████▉ | 269/300 [01:14<00:08,  3.48it/s, acc_rate=0.98]
MALA:  90%|█████████ | 270/300 [01:14<00:08,  3.49it/s, acc_rate=0.98]
MALA:  90%|█████████ | 270/300 [01:15<00:08,  3.49it/s, acc_rate=0.98]
MALA:  90%|█████████ | 271/300 [01:15<00:08,  3.50it/s, acc_rate=0.98]
MALA:  90%|█████████ | 271/300 [01:15<00:08,  3.50it/s, acc_rate=0.98]
MALA:  91%|█████████ | 272/300 [01:15<00:07,  3.51it/s, acc_rate=0.98]
MALA:  91%|█████████ | 272/300 [01:15<00:07,  3.51it/s, acc_rate=0.98]
MALA:  91%|█████████ | 273/300 [01:15<00:07,  3.49it/s, acc_rate=0.98]
MALA:  91%|█████████ | 273/300 [01:16<00:07,  3.49it/s, acc_rate=0.98]
MALA:  91%|█████████▏| 274/300 [01:16<00:07,  3.55it/s, acc_rate=0.98]
MALA:  91%|█████████▏| 274/300 [01:16<00:07,  3.55it/s, acc_rate=0.98]
MALA:  92%|█████████▏| 275/300 [01:16<00:07,  3.52it/s, acc_rate=0.98]
MALA:  92%|█████████▏| 275/300 [01:16<00:07,  3.52it/s, acc_rate=0.98]
MALA:  92%|█████████▏| 276/300 [01:16<00:06,  3.51it/s, acc_rate=0.98]
MALA:  92%|█████████▏| 276/300 [01:16<00:06,  3.51it/s, acc_rate=0.98]
MALA:  92%|█████████▏| 277/300 [01:16<00:06,  3.55it/s, acc_rate=0.98]
MALA:  92%|█████████▏| 277/300 [01:17<00:06,  3.55it/s, acc_rate=0.98]
MALA:  93%|█████████▎| 278/300 [01:17<00:06,  3.52it/s, acc_rate=0.98]
MALA:  93%|█████████▎| 278/300 [01:17<00:06,  3.52it/s, acc_rate=0.98]
MALA:  93%|█████████▎| 279/300 [01:17<00:05,  3.51it/s, acc_rate=0.98]
MALA:  93%|█████████▎| 279/300 [01:17<00:05,  3.51it/s, acc_rate=0.98]
MALA:  93%|█████████▎| 280/300 [01:17<00:05,  3.55it/s, acc_rate=0.98]
MALA:  93%|█████████▎| 280/300 [01:17<00:05,  3.55it/s, acc_rate=0.98]
MALA:  94%|█████████▎| 281/300 [01:17<00:04,  3.90it/s, acc_rate=0.98]
MALA:  94%|█████████▎| 281/300 [01:18<00:04,  3.90it/s, acc_rate=0.98]
MALA:  94%|█████████▍| 282/300 [01:18<00:04,  4.20it/s, acc_rate=0.98]
MALA:  94%|█████████▍| 282/300 [01:18<00:04,  4.20it/s, acc_rate=0.98]
MALA:  94%|█████████▍| 283/300 [01:18<00:03,  4.43it/s, acc_rate=0.98]
MALA:  94%|█████████▍| 283/300 [01:18<00:03,  4.43it/s, acc_rate=0.98]
MALA:  95%|█████████▍| 284/300 [01:18<00:03,  4.10it/s, acc_rate=0.98]
MALA:  95%|█████████▍| 284/300 [01:18<00:03,  4.10it/s, acc_rate=0.98]
MALA:  95%|█████████▌| 285/300 [01:18<00:03,  3.92it/s, acc_rate=0.98]
MALA:  95%|█████████▌| 285/300 [01:19<00:03,  3.92it/s, acc_rate=0.98]
MALA:  95%|█████████▌| 286/300 [01:19<00:03,  3.79it/s, acc_rate=0.98]
MALA:  95%|█████████▌| 286/300 [01:19<00:03,  3.79it/s, acc_rate=0.98]
MALA:  96%|█████████▌| 287/300 [01:19<00:03,  3.68it/s, acc_rate=0.98]
MALA:  96%|█████████▌| 287/300 [01:19<00:03,  3.68it/s, acc_rate=0.98]
MALA:  96%|█████████▌| 288/300 [01:19<00:03,  3.62it/s, acc_rate=0.98]
MALA:  96%|█████████▌| 288/300 [01:20<00:03,  3.62it/s, acc_rate=0.98]
MALA:  96%|█████████▋| 289/300 [01:20<00:03,  3.58it/s, acc_rate=0.98]
MALA:  96%|█████████▋| 289/300 [01:20<00:03,  3.58it/s, acc_rate=0.98]
MALA:  97%|█████████▋| 290/300 [01:20<00:02,  3.56it/s, acc_rate=0.98]
MALA:  97%|█████████▋| 290/300 [01:20<00:02,  3.56it/s, acc_rate=0.98]
MALA:  97%|█████████▋| 291/300 [01:20<00:02,  3.53it/s, acc_rate=0.98]
MALA:  97%|█████████▋| 291/300 [01:20<00:02,  3.53it/s, acc_rate=0.98]
MALA:  97%|█████████▋| 292/300 [01:20<00:02,  3.52it/s, acc_rate=0.98]
MALA:  97%|█████████▋| 292/300 [01:21<00:02,  3.52it/s, acc_rate=0.98]
MALA:  98%|█████████▊| 293/300 [01:21<00:01,  3.55it/s, acc_rate=0.98]
MALA:  98%|█████████▊| 293/300 [01:21<00:01,  3.55it/s, acc_rate=0.98]
MALA:  98%|█████████▊| 294/300 [01:21<00:01,  3.53it/s, acc_rate=0.98]
MALA:  98%|█████████▊| 294/300 [01:21<00:01,  3.53it/s, acc_rate=0.98]
MALA:  98%|█████████▊| 295/300 [01:21<00:01,  3.50it/s, acc_rate=0.98]
MALA:  98%|█████████▊| 295/300 [01:22<00:01,  3.50it/s, acc_rate=0.98]
MALA:  99%|█████████▊| 296/300 [01:22<00:01,  3.49it/s, acc_rate=0.98]
MALA:  99%|█████████▊| 296/300 [01:22<00:01,  3.49it/s, acc_rate=0.98]
MALA:  99%|█████████▉| 297/300 [01:22<00:00,  3.50it/s, acc_rate=0.98]
MALA:  99%|█████████▉| 297/300 [01:22<00:00,  3.50it/s, acc_rate=0.98]
MALA:  99%|█████████▉| 298/300 [01:22<00:00,  3.49it/s, acc_rate=0.98]
MALA:  99%|█████████▉| 298/300 [01:22<00:00,  3.49it/s, acc_rate=0.98]
MALA: 100%|█████████▉| 299/300 [01:22<00:00,  3.49it/s, acc_rate=0.98]
MALA: 100%|█████████▉| 299/300 [01:23<00:00,  3.49it/s, acc_rate=0.98]
MALA: 100%|██████████| 300/300 [01:23<00:00,  3.50it/s, acc_rate=0.98]
MALA: 100%|██████████| 300/300 [01:23<00:00,  3.61it/s, acc_rate=0.98]

Hide code cell source

# # corner plot of the posterior
param_names = list(MODEL.build_params_array_names())

set, sky = true_params()
fig = corner(chain_mala, labels=param_names, truths=np.concatenate((sky, set.ravel())))
../_images/3ad520366ac5a96d4ae26db7105f972745b40e1caebeabaa0bf65492f47f355c.png

Hamiltonian Monte-Carlo (HMC)#

The ap.fit.HMC takes a fixed number of steps at a fixed step size following Hamiltonian dynamics. This is in contrast to NUTS which attempts to optimally choose these parameters. The simplest way to think of HMC is as performing a number of MALA steps all in one go, so if leapfrog_steps = 10 then HMC is very similar to running MALA then taking every tenth step and adding it to the chain. HMC results will still have autocorrelation which will depend on the problem and choice of step parameters.

MODEL = initialize_model(target, False)

# Use LM to start the sampler at a high likelihood location, no burn-in needed!
res1 = ap.fit.LM(MODEL, verbose=0).fit()

# Run the HMC sampler
res_hmc = ap.fit.HMC(
    MODEL,
    warmup=1,
    max_iter=150,
    epsilon=1e-1,
    leapfrog_steps=10,
    inv_mass=res1.covariance_matrix,
).fit()

Hide code cell output

Initializing model sky
Initializing model sersic_0
Initializing model sersic_1
Warmup:   0%|          | 0/151 [00:00, ?it/s]
Warmup:   1%|          | 1/151 [00:00,  1.99it/s, step size=1.00e-01, acc. prob=0.993]
Warmup:   1%|▏         | 2/151 [00:00,  2.22it/s, step size=1.00e-01, acc. prob=0.989]
Sample:   2%|▏         | 3/151 [00:01,  2.32it/s, step size=1.00e-01, acc. prob=0.992]
Sample:   3%|▎         | 4/151 [00:01,  2.33it/s, step size=1.00e-01, acc. prob=0.995]
Sample:   3%|▎         | 5/151 [00:02,  2.37it/s, step size=1.00e-01, acc. prob=0.996]
Sample:   4%|▍         | 6/151 [00:02,  2.35it/s, step size=1.00e-01, acc. prob=0.996]
Sample:   5%|▍         | 7/151 [00:03,  2.37it/s, step size=1.00e-01, acc. prob=0.996]
Sample:   5%|▌         | 8/151 [00:03,  2.40it/s, step size=1.00e-01, acc. prob=0.997]
Sample:   6%|▌         | 9/151 [00:03,  2.40it/s, step size=1.00e-01, acc. prob=0.997]
Sample:   7%|▋         | 10/151 [00:04,  2.39it/s, step size=1.00e-01, acc. prob=0.997]
Sample:   7%|▋         | 11/151 [00:04,  2.40it/s, step size=1.00e-01, acc. prob=0.996]
Sample:   8%|▊         | 12/151 [00:05,  2.42it/s, step size=1.00e-01, acc. prob=0.996]
Sample:   9%|▊         | 13/151 [00:05,  2.41it/s, step size=1.00e-01, acc. prob=0.997]
Sample:   9%|▉         | 14/151 [00:05,  2.41it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  10%|▉         | 15/151 [00:06,  2.41it/s, step size=1.00e-01, acc. prob=0.996]
Sample:  11%|█         | 16/151 [00:06,  2.42it/s, step size=1.00e-01, acc. prob=0.996]
Sample:  11%|█▏        | 17/151 [00:07,  2.43it/s, step size=1.00e-01, acc. prob=0.996]
Sample:  12%|█▏        | 18/151 [00:07,  2.45it/s, step size=1.00e-01, acc. prob=0.996]
Sample:  13%|█▎        | 19/151 [00:07,  2.45it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  13%|█▎        | 20/151 [00:08,  2.46it/s, step size=1.00e-01, acc. prob=0.996]
Sample:  14%|█▍        | 21/151 [00:08,  2.45it/s, step size=1.00e-01, acc. prob=0.996]
Sample:  15%|█▍        | 22/151 [00:09,  2.45it/s, step size=1.00e-01, acc. prob=0.996]
Sample:  15%|█▌        | 23/151 [00:09,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  16%|█▌        | 24/151 [00:09,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  17%|█▋        | 25/151 [00:10,  2.43it/s, step size=1.00e-01, acc. prob=0.996]
Sample:  17%|█▋        | 26/151 [00:10,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  18%|█▊        | 27/151 [00:11,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  19%|█▊        | 28/151 [00:11,  2.43it/s, step size=1.00e-01, acc. prob=0.996]
Sample:  19%|█▉        | 29/151 [00:12,  2.43it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  20%|█▉        | 30/151 [00:12,  2.43it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  21%|██        | 31/151 [00:12,  2.42it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  21%|██        | 32/151 [00:13,  2.42it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  22%|██▏       | 33/151 [00:13,  2.39it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  23%|██▎       | 34/151 [00:14,  2.40it/s, step size=1.00e-01, acc. prob=0.996]
Sample:  23%|██▎       | 35/151 [00:14,  2.42it/s, step size=1.00e-01, acc. prob=0.996]
Sample:  24%|██▍       | 36/151 [00:14,  2.40it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  25%|██▍       | 37/151 [00:15,  2.40it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  25%|██▌       | 38/151 [00:15,  2.40it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  26%|██▌       | 39/151 [00:16,  2.40it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  26%|██▋       | 40/151 [00:16,  2.40it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  27%|██▋       | 41/151 [00:17,  2.39it/s, step size=1.00e-01, acc. prob=0.996]
Sample:  28%|██▊       | 42/151 [00:17,  2.40it/s, step size=1.00e-01, acc. prob=0.996]
Sample:  28%|██▊       | 43/151 [00:17,  2.40it/s, step size=1.00e-01, acc. prob=0.996]
Sample:  29%|██▉       | 44/151 [00:18,  2.40it/s, step size=1.00e-01, acc. prob=0.996]
Sample:  30%|██▉       | 45/151 [00:18,  2.40it/s, step size=1.00e-01, acc. prob=0.996]
Sample:  30%|███       | 46/151 [00:19,  2.40it/s, step size=1.00e-01, acc. prob=0.996]
Sample:  31%|███       | 47/151 [00:19,  2.37it/s, step size=1.00e-01, acc. prob=0.996]
Sample:  32%|███▏      | 48/151 [00:19,  2.38it/s, step size=1.00e-01, acc. prob=0.996]
Sample:  32%|███▏      | 49/151 [00:20,  2.39it/s, step size=1.00e-01, acc. prob=0.996]
Sample:  33%|███▎      | 50/151 [00:20,  2.39it/s, step size=1.00e-01, acc. prob=0.996]
Sample:  34%|███▍      | 51/151 [00:21,  2.39it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  34%|███▍      | 52/151 [00:21,  2.41it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  35%|███▌      | 53/151 [00:22,  2.40it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  36%|███▌      | 54/151 [00:22,  2.40it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  36%|███▋      | 55/151 [00:22,  2.42it/s, step size=1.00e-01, acc. prob=0.996]
Sample:  37%|███▋      | 56/151 [00:23,  2.42it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  38%|███▊      | 57/151 [00:23,  2.43it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  38%|███▊      | 58/151 [00:24,  2.42it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  39%|███▉      | 59/151 [00:24,  2.43it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  40%|███▉      | 60/151 [00:24,  2.43it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  40%|████      | 61/151 [00:25,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  41%|████      | 62/151 [00:25,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  42%|████▏     | 63/151 [00:26,  2.43it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  42%|████▏     | 64/151 [00:26,  2.42it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  43%|████▎     | 65/151 [00:26,  2.43it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  44%|████▎     | 66/151 [00:27,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  44%|████▍     | 67/151 [00:27,  2.45it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  45%|████▌     | 68/151 [00:28,  2.47it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  46%|████▌     | 69/151 [00:28,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  46%|████▋     | 70/151 [00:29,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  47%|████▋     | 71/151 [00:29,  2.42it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  48%|████▊     | 72/151 [00:29,  2.43it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  48%|████▊     | 73/151 [00:30,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  49%|████▉     | 74/151 [00:30,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  50%|████▉     | 75/151 [00:31,  2.45it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  50%|█████     | 76/151 [00:31,  2.45it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  51%|█████     | 77/151 [00:31,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  52%|█████▏    | 78/151 [00:32,  2.43it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  52%|█████▏    | 79/151 [00:32,  2.41it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  53%|█████▎    | 80/151 [00:33,  2.41it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  54%|█████▎    | 81/151 [00:33,  2.40it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  54%|█████▍    | 82/151 [00:33,  2.40it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  55%|█████▍    | 83/151 [00:34,  2.41it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  56%|█████▌    | 84/151 [00:34,  2.41it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  56%|█████▋    | 85/151 [00:35,  2.41it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  57%|█████▋    | 86/151 [00:35,  2.41it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  58%|█████▊    | 87/151 [00:36,  2.42it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  58%|█████▊    | 88/151 [00:36,  2.42it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  59%|█████▉    | 89/151 [00:36,  2.43it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  60%|█████▉    | 90/151 [00:37,  2.42it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  60%|██████    | 91/151 [00:37,  2.41it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  61%|██████    | 92/151 [00:38,  2.41it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  62%|██████▏   | 93/151 [00:38,  2.41it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  62%|██████▏   | 94/151 [00:38,  2.41it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  63%|██████▎   | 95/151 [00:39,  2.42it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  64%|██████▎   | 96/151 [00:39,  2.41it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  64%|██████▍   | 97/151 [00:40,  2.43it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  65%|██████▍   | 98/151 [00:40,  2.43it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  66%|██████▌   | 99/151 [00:41,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  66%|██████▌   | 100/151 [00:41,  2.43it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  67%|██████▋   | 101/151 [00:41,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  68%|██████▊   | 102/151 [00:42,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  68%|██████▊   | 103/151 [00:42,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  69%|██████▉   | 104/151 [00:43,  2.45it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  70%|██████▉   | 105/151 [00:43,  2.42it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  70%|███████   | 106/151 [00:43,  2.39it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  71%|███████   | 107/151 [00:44,  2.40it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  72%|███████▏  | 108/151 [00:44,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  72%|███████▏  | 109/151 [00:45,  2.46it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  73%|███████▎  | 110/151 [00:45,  2.47it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  74%|███████▎  | 111/151 [00:45,  2.46it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  74%|███████▍  | 112/151 [00:46,  2.45it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  75%|███████▍  | 113/151 [00:46,  2.45it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  75%|███████▌  | 114/151 [00:47,  2.46it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  76%|███████▌  | 115/151 [00:47,  2.45it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  77%|███████▋  | 116/151 [00:47,  2.45it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  77%|███████▋  | 117/151 [00:48,  2.45it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  78%|███████▊  | 118/151 [00:48,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  79%|███████▉  | 119/151 [00:49,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  79%|███████▉  | 120/151 [00:49,  2.45it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  80%|████████  | 121/151 [00:50,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  81%|████████  | 122/151 [00:50,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  81%|████████▏ | 123/151 [00:50,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  82%|████████▏ | 124/151 [00:51,  2.45it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  83%|████████▎ | 125/151 [00:51,  2.45it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  83%|████████▎ | 126/151 [00:52,  2.45it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  84%|████████▍ | 127/151 [00:52,  2.42it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  85%|████████▍ | 128/151 [00:52,  2.43it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  85%|████████▌ | 129/151 [00:53,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  86%|████████▌ | 130/151 [00:53,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  87%|████████▋ | 131/151 [00:54,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  87%|████████▋ | 132/151 [00:54,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  88%|████████▊ | 133/151 [00:54,  2.42it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  89%|████████▊ | 134/151 [00:55,  2.42it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  89%|████████▉ | 135/151 [00:55,  2.43it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  90%|█████████ | 136/151 [00:56,  2.43it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  91%|█████████ | 137/151 [00:56,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  91%|█████████▏| 138/151 [00:56,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  92%|█████████▏| 139/151 [00:57,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  93%|█████████▎| 140/151 [00:57,  2.43it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  93%|█████████▎| 141/151 [00:58,  2.43it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  94%|█████████▍| 142/151 [00:58,  2.37it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  95%|█████████▍| 143/151 [00:59,  2.38it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  95%|█████████▌| 144/151 [00:59,  2.39it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  96%|█████████▌| 145/151 [00:59,  2.40it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  97%|█████████▋| 146/151 [01:00,  2.42it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  97%|█████████▋| 147/151 [01:00,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  98%|█████████▊| 148/151 [01:01,  2.46it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  99%|█████████▊| 149/151 [01:01,  2.48it/s, step size=1.00e-01, acc. prob=0.997]
Sample:  99%|█████████▉| 150/151 [01:01,  2.49it/s, step size=1.00e-01, acc. prob=0.997]
Sample: 100%|██████████| 151/151 [01:02,  2.44it/s, step size=1.00e-01, acc. prob=0.997]
Sample: 100%|██████████| 151/151 [01:02,  2.42it/s, step size=1.00e-01, acc. prob=0.997]

Hide code cell source

# corner plot of the posterior
param_names = list(MODEL.build_params_array_names())

set, sky = true_params()
fig = corner(
    res_hmc.chain.detach().cpu().numpy(),
    labels=param_names,
    truths=np.concatenate((sky, set.ravel())),
    plot_contours=False,
    smooth=0.8,
)
../_images/78f0b0d2d5e9062865c1022fcf8216cae39c83bbaddec478d24356d06639c96e.png

Metropolis Hastings#

This is the more standard MCMC algorithm using the Metropolis Hastngs accept step identified with ap.fit.MHMCMC. Under the hood, this is just a wrapper for the excellent emcee package, if you want to take advantage of more emcee features you can very easily use ap.fit.MHMCMC as a starting point. However, one should keep in mind that for large models it can take exceedingly long to actually converge to the posterior. Instead of waiting that long, we demonstrate the functionality with 100 steps (and 30 chains), but suggest using MALA for any real world problem. Still, if there is something MALA can’t handle (a function that isn’t differentiable) then MHMCMC can save the day (even if it takes all day to do it).

MODEL = initialize_model(target, False)

# Use LM to start the sampler at a high likelihood location, no burn-in needed!
print("running LM fit")
res1 = ap.fit.LM(MODEL, verbose=0).fit()

# Run the MHMCMC sampler
print("running MHMCMC sampling")
res_mh = ap.fit.MHMCMC(MODEL, verbose=1, max_iter=100).fit()
Initializing model sky
Initializing model sersic_0
Initializing model sersic_1
running LM fit
running MHMCMC sampling
# corner plot of the posterior
# note that, even 3000 samples is not enough to overcome the autocorrelation so the posterior has not converged.
param_names = list(MODEL.build_params_array_names())

set, sky = true_params()
fig = corner(
    res_mh.chain,
    labels=param_names,
    truths=np.concatenate((sky, set.ravel())),
    smooth=0.8,
)
../_images/9fe7d1c5ae0fdb7d396451a64e7919d8f630538ae8215500fbcd26b2c9c0a099.png