-
Notifications
You must be signed in to change notification settings - Fork 6
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
Introduction of an inconsistency within a Closure Test #1682
base: master
Are you sure you want to change the base?
Changes from all commits
ce32d07
4456035
a72185f
275e4ac
e8d802e
a6b2e5b
e226442
08948ab
2e39d06
e56bba0
ef5b042
a2e58cd
25afee5
f009a10
9b2ac59
1edff9b
854b4ba
6d19e0a
68c8698
b79245f
15cf785
e98306a
8e07ffd
f6830cc
fe80a54
fc83349
2772e61
bbad659
ac900d9
02ca89a
a85da6e
3e1e58e
52b3f2f
7f0f1b5
32cb98a
80994cd
0c46e4d
6727ca2
f869e80
90a5e02
0d4bab8
918c42d
709121e
abd9949
f05a03e
628e416
b3fbd76
2ee2e1e
b740b85
39c6f37
8dfe470
6fb1db7
f407196
fe146ea
9667cfb
53bdaa4
45c95e6
0f7e15e
71fef24
eb1902b
b354a7f
7a6e575
4207044
82ab29f
a839c26
e17907c
4c75ea5
0c19a36
7f37d54
a5b615d
66473d8
967395c
0c33c16
98d9708
7fc132e
40a6e3f
55d3a44
2581a26
9ac8b96
d683b5a
d8a71ab
cccdf44
3d995a7
b3e49c2
ffee474
4c2634d
b37823a
eea237e
90bb97f
43f047d
533c4b1
4d5d473
ed266ae
2f58693
8ba15bb
50550bf
e1404e1
24c2985
26530f4
bef1cb5
f1aaed2
1371de2
d90201a
d9a421b
655320a
1064d35
a6f0b15
e309d4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
|
||
from reportengine import collect | ||
from reportengine.table import table | ||
from reportengine.figure import figure, figuregen | ||
|
||
from validphys.calcutils import calc_chi2, bootstrap_values | ||
from validphys.checks import check_pdf_is_montecarlo | ||
|
@@ -20,7 +21,9 @@ | |
check_fits_same_filterseed, | ||
check_fits_underlying_law_match, | ||
) | ||
|
||
from validphys import plotutils | ||
from validphys.inconsistent_ct import InconsistentCommonData | ||
from validphys.covmats import dataset_inputs_covmat_from_systematics | ||
|
||
BiasData = namedtuple("BiasData", ("bias", "ndata")) | ||
|
||
|
@@ -394,3 +397,64 @@ def fit_underlying_pdfs_summary(fit, fitunderlyinglaw): | |
def summarise_closure_underlying_pdfs(fits_underlying_pdfs_summary): | ||
"""Collects the underlying pdfs for all fits and concatenates them into a single table""" | ||
return pd.concat(fits_underlying_pdfs_summary, axis=1) | ||
|
||
|
||
@table | ||
def covmat_diffs(data, inconsistent_datasets, sys_rescaling_factor): | ||
"""Calculate trace difference between consistent and inconsistent covmat. Put results | ||
in table labeling by the type of inconsistency modified and the dataset in which the inconsistency | ||
was introduced | ||
|
||
""" | ||
|
||
dataset_input_list = list(data.dsinputs) | ||
commondata_wc = data.load_commondata_instance() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I am not understanding. You are overwriting the content of this variable the line below so what is the reason? |
||
commondata_wc = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If they are inconsistent please give it a telling name, like |
||
InconsistentCommonData(setname=cd.setname, ndata=cd.ndata, | ||
commondataproc=cd.commondataproc, | ||
nkin=cd.nkin, nsys=cd.nsys, | ||
commondata_table = cd.commondata_table, | ||
systype_table = cd.systype_table) | ||
for cd in commondata_wc | ||
] | ||
consistent_covmat = dataset_inputs_covmat_from_systematics( | ||
commondata_wc, | ||
dataset_input_list, | ||
use_weights_in_covmat=False, | ||
norm_threshold=None, | ||
_list_of_central_values=None, | ||
_only_additive=False, | ||
) | ||
|
||
trace = np.trace(consistent_covmat) | ||
|
||
# Study the impact on the trace of the covariance matrix if uncertainties | ||
# are rescaled by sys_rescaling_factor. Label by the type of error rescaled: | ||
# ADD/CORR | ||
# ADD/UNCORR | ||
# MULT/CORR | ||
# MULT/UNCORR | ||
# use the following entries_dict as input for process_commondata | ||
entries_dict = {"A/C":[True,False,True,False],"A/U":[True,False,False,True], | ||
"M/C":[False,True,True,False],"M/U":[False,True,False,True]} | ||
impact_dict = {} | ||
for inconsist_ds in inconsistent_datasets: | ||
cov_dict = {} | ||
for entry in entries_dict: | ||
|
||
inp = entries_dict[entry] | ||
commondata_wc_temp = [cd.process_commondata(inp[0],inp[1],inp[2],inp[3], | ||
inconsist_ds,sys_rescaling_factor) | ||
for cd in commondata_wc] | ||
modified_covmat = dataset_inputs_covmat_from_systematics( | ||
commondata_wc_temp, | ||
dataset_input_list, | ||
use_weights_in_covmat=False, | ||
norm_threshold=None, | ||
_list_of_central_values=None, | ||
_only_additive=False, | ||
) | ||
cov_dict[entry] = (trace-np.trace(modified_covmat))/trace*100 | ||
impact_dict[inconsist_ds] = cov_dict | ||
df = pd.DataFrame.from_records(impact_dict) | ||
return df |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
""" | ||
closuretest/inconsistent_plots.py | ||
|
||
Useful plots for analysis of inconsistent closure tests | ||
""" | ||
from reportengine.figure import figure | ||
from reportengine.table import table | ||
from validphys import plotutils | ||
from validphys.inconsistent_ct import InconsistentCommonData | ||
from validphys.covmats import dataset_inputs_covmat_from_systematics | ||
import numpy as np | ||
import pandas as pd | ||
|
||
def covmat_trace(dataset_input_list,commondata_wc): | ||
"""Return trace of experimental matrix | ||
""" | ||
normal_covmat = dataset_inputs_covmat_from_systematics( | ||
commondata_wc, | ||
dataset_input_list, | ||
use_weights_in_covmat=False, | ||
norm_threshold=None, | ||
_list_of_central_values=None, | ||
_only_additive=False, | ||
) | ||
return np.trace(normal_covmat) | ||
|
||
def mod_covmat_trace(dataset_input_list,commondata_wc, inconsistent_datasets, ADD, MULT, CORR, UNCORR, SPECIAL, sys_rescaling_factor): | ||
""" Calculate trace of inconsistent covmat rescaled by | ||
sys_rescaling_factor affecting ADD/MULT & CORR/UNCORR/SPECIAL. | ||
""" | ||
commondata_wc_temp = [cd.process_commondata(ADD,MULT,CORR,UNCORR,SPECIAL, | ||
inconsistent_datasets,sys_rescaling_factor) | ||
for cd in commondata_wc] | ||
modified_covmat = dataset_inputs_covmat_from_systematics( | ||
commondata_wc_temp, | ||
dataset_input_list, | ||
use_weights_in_covmat=False, | ||
norm_threshold=None, | ||
_list_of_central_values=None, | ||
_only_additive=False, | ||
) | ||
#Calculate trace of modified trace (either for type 1 or 2) | ||
modified_trace = np.trace(modified_covmat) | ||
return modified_trace | ||
|
||
@figure | ||
def plot_trace_impact(data, inconsistent_datasets, ADD,MULT,CORR,UNCORR,SPECIAL): | ||
""" | ||
Plot trace ratio for different sys_rescaling_factors. Specify what kind | ||
of error has been modified and for which datasets. | ||
The marked points are the one for which the trace ratio corresponds between type1/type2 inconsistent fit. | ||
""" | ||
|
||
# Load here all the data, does not make sense to load them each time the funciton is called | ||
dataset_input_list = list(data.dsinputs) | ||
commondata_wc = data.load_commondata_instance() | ||
commondata_wc_ic = [ | ||
InconsistentCommonData(setname=cd.setname, ndata=cd.ndata, | ||
commondataproc=cd.commondataproc, | ||
nkin=cd.nkin, nsys=cd.nsys, | ||
commondata_table = cd.commondata_table, | ||
systype_table = cd.systype_table) | ||
for cd in commondata_wc | ||
] | ||
normal_trace = covmat_trace(dataset_input_list,commondata_wc) | ||
lam_factors = np.arange(0,3,0.02) | ||
ratios = [] | ||
fig, ax = plotutils.subplots() | ||
points = [] | ||
i = 0 | ||
for lam in lam_factors: | ||
mod_trace = mod_covmat_trace(dataset_input_list,commondata_wc_ic, inconsistent_datasets, | ||
ADD,MULT,CORR,UNCORR,SPECIAL, | ||
lam) | ||
if lam < 1: ratios.append(mod_trace/normal_trace*100) | ||
if lam >= 1: ratios.append(normal_trace/mod_trace*100) | ||
if i%10 == 0 and lam < 1: | ||
ax.plot(lam_factors[i],ratios[-1],marker = ".", | ||
markersize = 10, | ||
label = "lambda type 2: " + str(round(lam_factors[i],3)) + "; ratio: " + str(round(ratios[-1],3))) | ||
points.append(ratios[-1]) | ||
i += 1 | ||
for point in points: | ||
a,b = find_intersections(np.asarray(lam_factors), np.asarray(ratios), point) | ||
ax.plot(a,b,marker = ".",markersize = 10,label = "lambda type1: " + str(round(a[0],3))+"; ratio: " + str(round(b[0],3))) | ||
type_a_m = "" | ||
type_c_u_s = "" | ||
if ADD: type_a_m = " ADD " | ||
if MULT: type_a_m = type_a_m + " MULT " | ||
if CORR: type_c_u_s = " CORR " | ||
if UNCORR: type_c_u_s = type_c_u_s + " UNCORR " | ||
if SPECIAL: type_c_u_s = type_c_u_s + " SPECIAL" | ||
ax.plot(lam_factors, ratios, label = "percentage ratios") | ||
title = "Impact of inconsistency of type " + str(type_a_m) + " and " + str(type_c_u_s) + " in \n" + str(inconsistent_datasets) + " wrt all ds. \n" | ||
ax.legend() | ||
ax.set_title(title) | ||
ax.set_xlabel("rescaling factor") | ||
ax.set_ylabel("percentage ratio") | ||
return fig | ||
|
||
def find_intersections(x, y, C): | ||
# Contains numpy indexing tricks that can be hard to reproduce | ||
# in the case where both functions are non-constants | ||
ii, = np.nonzero((y[1:]-C)*(y[:-1]-C) < 0.) # intersection indices | ||
x_intersections = x[ii] + (C - y[ii])/(y[1+ii] - y[ii])*(x[1+ii] - x[ii]) | ||
y_intersections = C * np.ones(len(ii)) | ||
return x_intersections, y_intersections |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we move these things to
closure_plots.py
?