Skip to content

Commit

Permalink
move matrix math from logp to loglike
Browse files Browse the repository at this point in the history
  • Loading branch information
zatkins2 committed Aug 6, 2024
1 parent dea084c commit 9af4f01
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions mflike/mflike.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,31 +297,14 @@ def logp(self, _derived=None, **params_values):
k: params_values[k] for k in self.expected_params_fg + self.expected_params_nuis
}

# derive the physical systematic parameters from the latent ones
# NOTE: the latent parameters have 1 fewer than physical; the
# matrix of orthogonal columns latent2phys_mat ensures that the
# physical params sum to 0 through a non-scaling transformation.
# this way, the latent params, which are sampled, will converge
for sys in self.requested_sys: # (deltaT, ...)
for sysparam in self.requested_sysparams: # (m, b, ...)
latent_sys_vector = np.array([params_values[k] for k in self.latent_sys_amps[sys][sysparam]])
phys_sys_vector = self.latent2phys_mat[sys] @ latent_sys_vector

if _derived is not None:
for i, k in enumerate(self.phys_sys_amps[sys][sysparam]):
_derived[k] = phys_sys_vector[i]

# pass the physical params into the interior likelihood call
for i, k in enumerate(self.phys_sys_amps[sys][sysparam]):
params_values_nocosmo[k] = phys_sys_vector[i]

return self.loglike(cl, **params_values_nocosmo)
return self.loglike(cl, derived=_derived, **params_values_nocosmo)

def loglike(self, cl, **params_values_nocosmo):
def loglike(self, cl, derived=None, **params_values_nocosmo):
"""
Computes the gaussian log-likelihood
:param cl: the dictionary of theory + foregrounds :math:`D_{\ell}`
:param derived: if not None, a dictionary to hold derived parameters
:param params_values_nocosmo: the dictionary of required foreground + systematic parameters
:return: the exact loglikelihood :math:`\ln \mathcal{L}`
Expand All @@ -332,6 +315,24 @@ def loglike(self, cl, **params_values_nocosmo):
if not hasattr(self, "non_sampled_params"):
self.initialize_non_sampled_params()

# derive the physical systematic parameters from the latent ones
# NOTE: the latent parameters have 1 fewer than physical; the
# matrix of orthogonal columns latent2phys_mat ensures that the
# physical params sum to 0 through a non-scaling transformation.
# this way, the latent params, which are sampled, will converge
for sys in self.requested_sys: # (deltaT, ...)
for sysparam in self.requested_sysparams: # (m, b, ...)
latent_sys_vector = np.array([params_values_nocosmo[k] for k in self.latent_sys_amps[sys][sysparam]])
phys_sys_vector = self.latent2phys_mat[sys] @ latent_sys_vector

if derived is not None:
for i, k in enumerate(self.phys_sys_amps[sys][sysparam]):
derived[k] = phys_sys_vector[i]

# pass the physical params into the interior likelihood call
for i, k in enumerate(self.phys_sys_amps[sys][sysparam]):
params_values_nocosmo[k] = phys_sys_vector[i]

params_values_nocosmo = self.non_sampled_params | params_values_nocosmo

ps_vec = self._get_power_spectra(cl, **params_values_nocosmo)
Expand Down

0 comments on commit 9af4f01

Please sign in to comment.