From 9af4f018cc621831ab8b869c5ae4399a7fc052ee Mon Sep 17 00:00:00 2001 From: Zachary Atkins Date: Tue, 6 Aug 2024 13:14:08 -0400 Subject: [PATCH] move matrix math from logp to loglike --- mflike/mflike.py | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/mflike/mflike.py b/mflike/mflike.py index dd8da55..f81ccac 100644 --- a/mflike/mflike.py +++ b/mflike/mflike.py @@ -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}` @@ -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)