Skip to content

MAP Estimation

cmb_lensing.map_joint.map_joint

map_joint(data_set, num_steps=30, constant_step=False)

Joint maximum a posteriori estimate of the unlensed field f and the lensing potential phi.

Alternates a Wiener-filter step for f at the current phi with a step in phi along the preconditioned gradient of the mixed logpdf, the step length set by a BFGS line search. Starts from f = phi = 0. The cosmology is the one stored in the data set's covariances.

Parameters:

Name Type Description Default
data_set

A DataSetT, DataSetEB or DataSetTEB, e.g. from load_sim.

required
num_steps

Number of (f step, phi step) iterations.

30
constant_step

If True, skip the line search and take unit-length phi steps.

False

Returns:

Type Description
(f, phi)

the estimated unlensed field (same type as data_set.data) and the

estimated lensing potential (a FlatS0), both in Fourier space.

Example
from cmb_lensing.simulate import load_sim
from cmb_lensing.map_joint import map_joint
data_set = load_sim(nside = 64, theta_pix = 5, pol = "I", master_seed = 1)
f_map, phi_map = map_joint(data_set)
Source code in cmb_lensing/map_joint.py
def map_joint(data_set, num_steps = 30, constant_step = False):
    """Joint maximum a posteriori estimate of the unlensed field f and the lensing potential phi.

    Alternates a Wiener-filter step for f at the current phi with a step in phi along the
    preconditioned gradient of the mixed logpdf, the step length set by a BFGS line search.
    Starts from f = phi = 0. The cosmology is the one stored in the data set's covariances.

    Args:
        data_set: A `DataSetT`, `DataSetEB` or `DataSetTEB`, e.g. from `load_sim`.
        num_steps: Number of (f step, phi step) iterations.
        constant_step: If True, skip the line search and take unit-length phi steps.

    Returns:
        (f, phi): the estimated unlensed field (same type as `data_set.data`) and the
        estimated lensing potential (a `FlatS0`), both in Fourier space.

    Example:
        ```python
        from cmb_lensing.simulate import load_sim
        from cmb_lensing.map_joint import map_joint
        data_set = load_sim(nside = 64, theta_pix = 5, pol = "I", master_seed = 1)
        f_map, phi_map = map_joint(data_set)
        ```
    """

    #unpack the necessary data from the data set object
    noise_covariance = data_set.noise_covariance
    mixing_d = data_set.mixing_d
    #MAP joint method is invariant to G matrix so set to identity for simplicity here
    mixing_g = get_scalar_identity(mixing_d)
    field_covariance = data_set.field_covariance
    phi_covariance = data_set.phi_covariance
    quadratic_estimate = data_set.quadratic_estimate
    mask = data_set.mask
    beam = data_set.beam
    data = data_set.data

    #set starting guesses for f and phi to all zeros
    field_predict = 0*data #NOTE this is in general a TEB vector
    phi_predict = zero_scalar_field_like(data) #Phi only ever represents one singular field

    #compute the hessian which we will use to calculate the step in the phi direction 
    hessian = pinv(phi_gradient_hessian(phi_covariance, quadratic_estimate))
    alpha = jnp.array(1.0)
    for _ in range(num_steps):

        #compute the wiener filter of the predicted field
        field_predict = wiener_filter(field_predict, phi_predict, data, field_covariance, noise_covariance, mask, beam)

        #must mix f and phi before taking the gradient
        mixed_field, mixed_phi = mix(field_predict, phi_predict, mixing_d, mixing_g)
        mixed_grad_phi = mixed_grad_phi_logpdf(mixed_field, mixed_phi, data, noise_covariance, 
                                                phi_covariance, field_covariance, 
                                                mask, beam, mixing_d, mixing_g)

        #create a new partial function whose only input is alpha for use in the brent optimizer (we want to
        #minimize the value of this function w.r.t. the single parameter alpha)
        min_logpdf = partial(logpdf_at_phi_new, grad_phi = mixed_grad_phi, mixed_field = mixed_field, 
                             phi_predict = mixed_phi, data = data, beam = beam, mask = mask, field_covariance = field_covariance,
                             phi_covariance = phi_covariance, noise_covariance = noise_covariance,
                             mixing_d = mixing_d, mixing_g = mixing_g, hessian = hessian)

        if not constant_step:
            #call the optimizer to find which alpha minimizes the best
            optimizer = minimize(min_logpdf, 1, method = "BFGS") #Set a starting guess of "alpha = 1"
            alpha = optimizer.x[0]

        #take a step in the opposite direction of the gradient
        phi_predict = phi_predict + alpha * hessian * mixed_grad_phi

    #after max iterations have been looped through return the final learned fields
    return field_predict, phi_predict