Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check on Cramer Rao bounds #32

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion src/smefit/analyze/fisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
from mpl_toolkits.axes_grid1 import make_axes_locatable
from rich.progress import track

from ..log import logging
from .latex_tools import latex_packages
from .pca import impose_constrain

_logger = logging.getLogger(__name__)


class FisherCalculator:

Expand Down Expand Up @@ -66,7 +69,15 @@ def compute_linear(self):
)

def compute_quadratic(self, posterior_df, smeft_predictions):
"""Compute quadratic Fisher information."""
"""Compute quadratic Fisher information.

Parameters
----------
posterior_df : pd.DataFrame
fit results
smeft_predictions: np.ndarray
array with all the predictions for each replica
"""
quad_fisher = []

# compute some average values over the replicas
Expand Down Expand Up @@ -167,6 +178,43 @@ def normalize(table, norm, log):
table = np.log(table[table > 0.0])
return table.replace(np.nan, 0.0)

def test_cramer_rao_bound(self, posterior_df):
r"""Test Cramer Rao bound, asserting if:

.. math ::
I(c_{i}) - Var(c_{i}) \le 0

Parameters
----------
posterior_df : pd.DataFrame
fit results

"""
fish_mat = (
self.new_LinearCorrections
@ self.datasets.InvCovMat
@ self.new_LinearCorrections.T
)
fish_mat = (fish_mat + fish_mat.T) / 2

posterior_df = posterior_df[self.free_parameters]
covariance = posterior_df.cov().values
v, u = np.linalg.eig(fish_mat)
inv_fish = u @ np.diag(1 / v) @ u.T
cr_limit, _ = np.linalg.eig(inv_fish - covariance)
cr_limit = pd.Series(cr_limit.real, posterior_df.columns)

# cr_limit, _ = np.linalg.eig(np.eye(covariance.shape[0]) - fish_mat @ covariance)
# cr_limit = pd.Series(cr_limit.real, posterior_df.columns)
try:
np.testing.assert_array_less(cr_limit, np.zeros_like(cr_limit))
_logger.info(f"Cramer Rao bounds are satisfied!")
except AssertionError:
_logger.warning(
"Following coefficients violate Cramer Rao bound: I(c)^-1 - Var(c) < 0"
)
_logger.warning(cr_limit[cr_limit > 0])

def groupby_data(self, table, data_groups, norm, log):
"""Merge fisher per data group."""
summary_table = pd.merge(
Expand Down
11 changes: 11 additions & 0 deletions src/smefit/analyze/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ def fisher(
plot=None,
fit_list=None,
log=False,
test_cramer_rao_bound=False,
):
"""Fisher information table and plots runner.

Expand All @@ -394,16 +395,23 @@ def fisher(
By default all the fits included in the report
log: bool, optional
if True shows the log of the Fisher informaltion
test_cramer_rao_bound: bool, optinal
if True test the Cramer Rao bound

"""
figs_list, links_list = None, None
if fit_list is not None:
fit_list = self.fits[self.fits == fit_list]

for fit in fit_list:
_logger.info(f"Computing Fisher for fit {fit.name}")

compute_quad = fit.config["use_quad"]
fisher_cal = FisherCalculator(fit.coefficients, fit.datasets, compute_quad)
fisher_cal.compute_linear()
if test_cramer_rao_bound and not compute_quad:
fisher_cal.test_cramer_rao_bound(fit.results)

fisher_cal.lin_fisher = fisher_cal.normalize(
fisher_cal.lin_fisher, norm=norm, log=log
)
Expand All @@ -414,6 +422,9 @@ def fisher(
# if necessary compute the quadratic Fisher
if compute_quad:
fisher_cal.compute_quadratic(fit.results, fit.smeft_predictions)
# if test_cramer_rao_bound:
# fisher_cal.test_cramer_rao_bound(fit.results)

fisher_cal.quad_fisher = fisher_cal.normalize(
fisher_cal.quad_fisher, norm=norm, log=log
)
Expand Down