questions about how to use pyhf #2070
-
Aloha pyhf community, Thank you all for making pyhf available! My name is Boyang, a physics student at the University of Hawaii. I want to use this package in my inclusive tagging R(D) measurement at Belle II. It would be greatly appreciated if you can help me with the 3 questions below.
I would be really grateful if you can give some feedback on my code below! xedges = np.linspace(-2, 10, 48) # -7.5 for weMiss2, -2 for weMiss3, -2.5 for weMiss4
yedges = np.linspace(0.4, 4.6, 42)
variable_x = 'B0_CMS3_weMissM2'
variable_y = 'p_D_l'
(counts, xedges, yedges) = np.histogram2d(df_merged.query(cut)[variable_x],
df_merged.query(cut)[variable_y],
bins=[xedges, yedges])
counts = counts.T
def fit():
global parameters, correlations
parameters, correlations= pyhf.infer.mle.fit(np.concatenate([counts.flat]), model_1, return_uncertainties=True, return_correlations=True)
sum_event=0
# calculate the fitted counts and errors
for i in range(len(parameters)):
norm = parameters[i,0]
template_counts = np.sum(model_1.spec['channels'][0]['samples'][i]['data'])
component_name = model_1.spec['channels'][0]['samples'][i]['name']
fitted_counts = round(parameters[i,0] * template_counts)
fitted_error = round(parameters[i,1] * template_counts)
print(f'\n{component_name} counts: {fitted_counts}')
print(f'{component_name} counts uncertainty: {fitted_error}')
sum_event+= fitted_counts
print(f'\nTotal fitted counts: {sum_event}')
print(f'Total fitted counts uncertainty: ??')
print(f'Total generated counts: {len(df_merged)}')
print(f'\nFitting parameters: \n{parameters}')
print(f'Fitting parameter correlations: \n{correlations}')
print(f'uncertainty: {parameters[0][1]}') |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi @zjkjsd, I'll try to get this started.
The fit will involve all the templates you have specified. You could control templates via a
Regarding your code snippet: fitted_error = round(parameters[i,1] * template_counts) This only is true for the specific case of a single np.sum(model_1.spec['channels'][0]['samples'][i]['data']) you can use the print(f'Total fitted counts uncertainty: ??') For this you need to consider the effects of all parameters (and their correlations) acting on the full model. I would recommend having a look at |
Beta Was this translation helpful? Give feedback.
Hi @zjkjsd, I'll try to get this started.
pyhf
internally orders the templates alphabetical by name so the order in the specification is irrelevant and only the name matters.The fit will involve all the templates you have specified. You could control templates via a
normfactor
and hold specific templates constant (and set to zero) in the fit, b…