Skip to content

Commit

Permalink
fixes for loglikes
Browse files Browse the repository at this point in the history
  • Loading branch information
santiagocasas committed Nov 28, 2024
1 parent 2359775 commit 1755756
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 106 deletions.
18 changes: 11 additions & 7 deletions cosmicfishpie/cosmology/cosmology.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,9 +956,9 @@ def changebasis_symb(self, cosmopars):
if "logAs" in symbpars:
symbpars["10^9As"] = 10**9 * (np.exp(symbpars.pop("logAs")) * 1.0e-10)
try:
As_value = symbpars.get("10^9As")
As_value = symbpars.get("10^9As", -np.inf)
upr.debug_print("DEBUG: symbpars['10^9As'] = ", As_value)
if np.isscalar(As_value):
if As_value != -np.inf:
try:
symbpars["sigma8"] = self.symblin.As_to_sigma8(
symbpars["10^9As"],
Expand All @@ -967,16 +967,20 @@ def changebasis_symb(self, cosmopars):
symbpars["h"],
symbpars["ns"],
)
upr.debug_print("DEBUG: symbpars['sigma8'] = ", symbpars["sigma8"])
upr.debug_print("DEBUG: derived sigma8 = ", symbpars["sigma8"])
except Exception as e:
print(f"An error occurred: {e}")
print("As to sigma8 conversion failed")
raise ValueError
else:
print("10^9As value not found")
upr.debug_print("DEBUG: symbpars = ", symbpars)
except KeyError:
pass
try:
sigma8_value = symbpars.get("sigma8")
if np.isscalar(sigma8_value):
sigma8_value = symbpars.get("sigma8", -np.inf)
#if np.isscalar(sigma8_value) or (isinstance(sigma8_value, np.ndarray) and sigma8_value.ndim == 0):
if sigma8_value != -np.inf:
try:
As_n = self.symblin.sigma8_to_As(
symbpars["sigma8"],
Expand All @@ -985,14 +989,14 @@ def changebasis_symb(self, cosmopars):
symbpars["h"],
symbpars["ns"],
)
upr.debug_print("DEBUG: As_n = ", As_n)
upr.debug_print("DEBUG: derived 10^9As = ", As_n)
symbpars["10^9As"] = As_n
except Exception as e:
print(f"An error occurred: {e}")
print("sigma8 to As conversion failed")
raise ValueError
else:
print("sigma8 value not scalar")
print("sigma8 value not found")
upr.debug_print("DEBUG: symbpars = ", symbpars)
except KeyError:
print("sigma8 or 10^9As not in symbpars")
Expand Down
45 changes: 28 additions & 17 deletions cosmicfishpie/likelihood/spectro_like.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,10 @@ def compute_wedge_chi2(P_obs_data, P_obs_theory, cosmoFM_data: cosmicfish.Fisher

return chi2


def loglike(param_vec, prior=None, leg_flag='wedges',
cosmoFM_theory: cosmicfish.FisherMatrix = None,
cosmoFM_data: cosmicfish.FisherMatrix = None,
data_obsPgg: np.ndarray = None):

z_bins = cosmoFM_data.pk_cov.global_z_bin_mids
def compute_theory_spectro(param_dict, cosmoFM_theory: cosmicfish.FisherMatrix,
leg_flag='wedges'):

if type(param_vec) == dict:
param_dict = deepcopy(param_vec)
elif is_indexable_iterable(param_vec) and prior is not None:
#print(f'Loading prior with keys: {prior.keys}')
param_dict={key: param_vec[i] for i, key in enumerate(prior.keys)}
z_bins = cosmoFM_theory.pk_cov.global_z_bin_mids

nuisance_shot = np.zeros(len(z_bins))
pshotpars = deepcopy(cosmoFM_theory.PShotpars)
Expand Down Expand Up @@ -216,19 +207,39 @@ def loglike(param_vec, prior=None, leg_flag='wedges',
obsPgg_vary = observable_Pgg(spectro_cov_vary,
cosmoFM_theory,
nuisance_shot=nuisance_shot)

if leg_flag == 'wedges':
return obsPgg_vary
elif leg_flag == 'legendre':
P_ell_vary = legendre_Pgg(obsPgg_vary, cosmoFM_theory)
return P_ell_vary

def loglike(param_vec=None,
theory_obsPgg=None,
prior=None, leg_flag='wedges',
cosmoFM_theory: cosmicfish.FisherMatrix = None,
cosmoFM_data: cosmicfish.FisherMatrix = None,
data_obsPgg: np.ndarray = None):
if theory_obsPgg is None and param_vec is not None:
if type(param_vec) == dict:
param_dict = deepcopy(param_vec)
elif is_indexable_iterable(param_vec) and prior is not None:
#print(f'Loading prior with keys: {prior.keys}')
param_dict={key: param_vec[i] for i, key in enumerate(prior.keys)}
theory_obsPgg = compute_theory_spectro(param_dict, cosmoFM_theory, leg_flag)
else:
upr.debug_print('No theory_obsPgg provided and no param_vec provided')
return -np.inf
if leg_flag == 'wedges':
chi2 = compute_wedge_chi2(
P_obs_data=data_obsPgg,
P_obs_theory=obsPgg_vary,
cosmoFM_data=cosmoFM_data
P_obs_theory=theory_obsPgg
)
elif leg_flag == 'legendre':
P_ell_vary = legendre_Pgg(obsPgg_vary, cosmoFM_theory)
P_ell_data = legendre_Pgg(data_obsPgg, cosmoFM_data)
covariance_leg, inv_covariance_leg = compute_covariance_legendre(
P_ell=P_ell_data,
cosmoFM=cosmoFM_data
)
chi2 = compute_chi2_legendre(P_ell_data, P_ell_vary, inv_covariance_leg)
chi2 = compute_chi2_legendre(P_ell_data, theory_obsPgg,
inv_covariance_leg)
return -0.5 * chi2
Loading

0 comments on commit 1755756

Please sign in to comment.