def hypotest( test_poi: float, data: Array, model: pyhf.Model, lr: float, bonly_pars: Array, test_stat: str = "qmu", return_constrained_pars: bool = False, ) -> tuple[Array, Array] | Array: # hard-code 1 as inits for now # TODO: need to parse different inits for constrained and global fits init_pars = jnp.asarray(model.config.suggested_init())[ model.config.par_slice("correlated_bkg_uncertainty")] conditional_pars = relaxed.mle.fixed_poi_fit(data, model, poi_condition=test_poi, init_pars=init_pars, lr=lr) mle_pars = bonly_pars if test_stat == "qmu": profile_likelihood = -2 * (model.logpdf(conditional_pars, data)[0] - model.logpdf(mle_pars, data)[0]) poi_hat = mle_pars[model.config.poi_index] tstat = jnp.where(poi_hat < test_poi, profile_likelihood, 0.0) CLsb = 1 - pyhf.tensorlib.normal_cdf(jnp.sqrt(tstat)) altval = 0.0 CLb = 1 - pyhf.tensorlib.normal_cdf(altval) CLs = CLsb / CLb if return_constrained_pars: return CLs, conditional_pars else: return CLs
def build_Asimov_data(model: pyhf.Model, with_aux: bool = True) -> List[float]: """Get asimov dataset for a model Args: model (pyhf.Model): the model for which to construct the asimov data with_aux (bool, optional): with or without auxdata. Defaults to True. Returns: List[float]: asimov data """ asimov_data = model.expected_data( get_asimov_parameters(model), include_auxdata=with_aux ).tolist() return asimov_data
def build_Asimov_data(model: pyhf.Model, with_aux: bool = True) -> List[float]: """Returns the Asimov dataset (optionally with auxdata) for a model. Initial parameter settings for normalization factors in the workspace are treated as the default settings for that parameter. Fitting the Asimov dataset will recover these initial settings as the maximum likelihood estimate for normalization factors. Initial settings for other modifiers are ignored. Args: model (pyhf.Model): the model from which to construct the dataset with_aux (bool, optional): whether to also return auxdata, defaults to True Returns: List[float]: the Asimov dataset """ asimov_data = model.expected_data(get_asimov_parameters(model), include_auxdata=with_aux).tolist() return asimov_data
def uncorrelated_background(signal, bkg, bkg_uncertainty, batch_size=None): """ Construct a simple single channel :class:`~pyhf.pdf.Model` with a :class:`~pyhf.modifiers.shapesys` modifier representing an uncorrelated background uncertainty. Example: >>> import pyhf >>> pyhf.set_backend("numpy") >>> model = pyhf.simplemodels.uncorrelated_background( ... signal=[12.0, 11.0], bkg=[50.0, 52.0], bkg_uncertainty=[3.0, 7.0] ... ) >>> model.schema 'model.json' >>> model.config.channels ['singlechannel'] >>> model.config.samples ['background', 'signal'] >>> model.config.parameters ['mu', 'uncorr_bkguncrt'] >>> model.expected_data(model.config.suggested_init()) array([ 62. , 63. , 277.77777778, 55.18367347]) Args: signal (:obj:`list`): The data in the signal sample bkg (:obj:`list`): The data in the background sample bkg_uncertainty (:obj:`list`): The statistical uncertainty on the background sample counts batch_size (:obj:`None` or :obj:`int`): Number of simultaneous (batched) Models to compute Returns: ~pyhf.pdf.Model: The statistical model adhering to the :obj:`model.json` schema """ spec = { 'channels': [{ 'name': 'singlechannel', 'samples': [ { 'name': 'signal', 'data': signal, 'modifiers': [{ 'name': 'mu', 'type': 'normfactor', 'data': None }], }, { 'name': 'background', 'data': bkg, 'modifiers': [{ 'name': 'uncorr_bkguncrt', 'type': 'shapesys', 'data': bkg_uncertainty, }], }, ], }] } return Model(spec, batch_size=batch_size)
def correlated_background(signal, bkg, bkg_up, bkg_down, batch_size=None): r""" Construct a simple single channel :class:`~pyhf.pdf.Model` with a :class:`~pyhf.modifiers.histosys` modifier representing a background with a fully correlated bin-by-bin uncertainty. Args: signal (:obj:`list`): The data in the signal sample. bkg (:obj:`list`): The data in the background sample. bkg_up (:obj:`list`): The background sample under an upward variation corresponding to :math:`\alpha=+1`. bkg_down (:obj:`list`): The background sample under a downward variation corresponding to :math:`\alpha=-1`. batch_size (:obj:`None` or :obj:`int`): Number of simultaneous (batched) Models to compute. Returns: ~pyhf.pdf.Model: The statistical model adhering to the :obj:`model.json` schema. Example: >>> import pyhf >>> pyhf.set_backend("numpy") >>> model = pyhf.simplemodels.correlated_background( ... signal=[12.0, 11.0], ... bkg=[50.0, 52.0], ... bkg_up=[45.0, 57.0], ... bkg_down=[55.0, 47.0], ... ) >>> model.schema 'model.json' >>> model.config.channels ['single_channel'] >>> model.config.samples ['background', 'signal'] >>> model.config.parameters ['correlated_bkg_uncertainty', 'mu'] >>> model.expected_data(model.config.suggested_init()) array([62., 63., 0.]) """ spec = { "channels": [{ "name": "single_channel", "samples": [ { "name": "signal", "data": signal, "modifiers": [{ "name": "mu", "type": "normfactor", "data": None }], }, { "name": "background", "data": bkg, "modifiers": [{ "name": "correlated_bkg_uncertainty", "type": "histosys", "data": { "hi_data": bkg_up, "lo_data": bkg_down }, }], }, ], }] } return Model(spec, batch_size=batch_size)