Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_bayesian_fit_class(self):
o = Observable( 'test_obs' )
def f(wc_obj, par_dict):
return par_dict['m_b']*2
pr = Prediction( 'test_obs', f )
d = NormalDistribution(4.2, 0.2)
m = Measurement( 'measurement of test_obs' )
m.add_constraint(['test_obs'], d)
par = copy.deepcopy(flavio.parameters.default_parameters)
par.set_constraint('m_b', '4.2+-0.2')
par.set_constraint('m_c', '1.2+-0.1')
par.set_constraint('m_s', '0.10(5)')
wc = flavio.physics.eft.WilsonCoefficients()
fit = BayesianFit('bayesian_test_fit_1', par, wc, ['m_b','m_c'], ['m_s'], ['CVLL_bsbs','CVRR_bsbs'], ['measurement of test_obs'])
self.assertEqual(fit.dimension, 7)
# test from array to dict ...
d = fit.array_to_dict(np.array([1.,2.,3.,4.,5.,6.,7.]))
self.assertEqual(d, {'nuisance_parameters': {'m_s': 3.0}, 'fit_parameters': {'m_c': 2.0, 'm_b': 1.0}, 'fit_coefficients': {'CVLL_bsbs': 4.0+5.j, 'CVRR_bsbs': 6.0+7.0j}})
# ... and back
np.testing.assert_array_equal(fit.dict_to_array(d), np.array([1.,2.,3.,4.,5.,6.,7.]))
self.assertEqual(fit.get_random.shape, (7,))
fit.log_prior_parameters(np.array([4.5,1.0,0.08,4.,5.,6.,7.]))
fit.get_predictions(np.array([4.5,1.0,0.08,4.,5.,6.,7.]))
fit.log_likelihood(np.array([4.5,1.0,0.08,4.,5.,6.,7.]))
# removing dummy instances
BayesianFit.del_instance('bayesian_test_fit_1')
Observable.del_instance('test_obs')
Measurement.del_instance('measurement of test_obs')
def test_fastfit_covariance_sm(self):
# This test is to assure that calling make_measurement does not
# actually call the fit_wc_function
# dummy observables
o1 = Observable( 'test_obs 1' )
# dummy predictions
def f1(wc_obj, par_dict):
return par_dict['m_b']
Prediction( 'test_obs 1', f1 )
d1 = NormalDistribution(5, 0.2)
m1 = Measurement( 'measurement 1 of test_obs 1' )
m1.add_constraint(['test_obs 1'], d1)
def fit_wc_fct_tmp(X):
pass
fit = FastFit('fastfit_test_1', flavio.default_parameters, ['m_b'], [], ['test_obs 1'],
fit_wc_function=fit_wc_fct_tmp)
fit.fit_wc_function = fit_wc_fct_error
fit.fit_wc_names = tuple(inspect.signature(fit.fit_wc_function).parameters.keys())
fit.make_measurement() # single thread calculation
FastFit.del_instance('fastfit_test_1')
fit = FastFit('fastfit_test_1', flavio.default_parameters, ['m_b'], [], ['test_obs 1'],
fit_wc_function=fit_wc_fct_tmp)
fit.fit_wc_function = fit_wc_fct_error
fit.fit_wc_names = tuple(inspect.signature(fit.fit_wc_function).parameters.keys())
fit.make_measurement(threads=2) # multi thread calculation
FastFit.del_instance('fastfit_test_1')
Observable.del_instance('test_obs 1')
Measurement.del_instance('measurement 1 of test_obs 1')
def f1(wc_obj, par_dict):
return par_dict['m_b']
def f2(wc_obj, par_dict):
return 2.5
Prediction( 'test_obs 1', f1 )
Prediction( 'test_obs 2', f2 )
d1 = NormalDistribution(5, 0.2)
cov2 = [[0.1**2, 0.5*0.1*0.3], [0.5*0.1*0.3, 0.3**2]]
d2 = MultivariateNormalDistribution([6,2], cov2)
m1 = Measurement( 'measurement 1 of test_obs 1' )
m2 = Measurement( 'measurement 2 of test_obs 1 and test_obs 2' )
m1.add_constraint(['test_obs 1'], d1)
m2.add_constraint(['test_obs 1', 'test_obs 2'], d2)
fit2 = FastFit('fastfit_test_2', flavio.default_parameters, ['m_b'], [], ['test_obs 1', 'test_obs 2'])
# fit with only a single observable and measurement
fit1 = FastFit('fastfit_test_1', flavio.default_parameters, ['m_b'], [], ['test_obs 2',])
# fit with 2 observables but 1 measurement
fit3 = FastFit('fastfit_test_3', flavio.default_parameters, ['m_b'], [], ['test_obs 1', 'test_obs 2'],
include_measurements=['measurement 2 of test_obs 1 and test_obs 2'])
for fit in (fit2, fit3):
fit.make_measurement()
centr_cov_exp_before = fit._exp_central_covariance
filename = os.path.join(tempfile.gettempdir(), 'tmp.p')
fit.save_exp_central_covariance(filename)
fit.load_exp_central_covariance(filename)
centr_cov_exp_after = fit._exp_central_covariance
npt.assert_array_equal(centr_cov_exp_before[0], centr_cov_exp_after[0])
npt.assert_array_equal(centr_cov_exp_before[1], centr_cov_exp_after[1])
os.remove(filename)
cov_before = fit._sm_covariance
filename = os.path.join(tempfile.gettempdir(), 'tmp-no-p')
fit.save_sm_covariance(filename)
def test_bxclnu_lfu(self):
self.assertAlmostEqual(
flavio.sm_prediction('Rmue(B->Xclnu)'), 1, delta=0.01)
self.assertAlmostEqual(
flavio.sm_prediction('Rtaumu(B->Xclnu)'),
flavio.sm_prediction('Rtaul(B->Xclnu)'),
delta=0.01)
self.assertEqual(
flavio.physics.bdecays.bxlnu.BR_tot_leptonflavour(wc_obj, par, 'e', 'e'), 1)
self.assertAlmostEqual(
flavio.physics.bdecays.bxlnu.BR_tot_leptonflavour(wc_obj, par, 'tau', 'e'),
1/flavio.physics.bdecays.bxlnu.BR_tot_leptonflavour(wc_obj, par, 'e', 'tau'))
def test_bxll_lratio(self):
# compare to arXiv:1503.04849
self.assertAlmostEqual(flavio.sm_prediction('(B->Xsll)', 1, 3.5),
0.96, delta=0.01)
self.assertAlmostEqual(flavio.sm_prediction('(B->Xsll)', 14.4, 25),
1.15, delta=0.01)
# for tau, just check this doesn't raise an error
flavio.sm_prediction('(B->Xsll)', 14.4, 25)
def test_ft(self):
# compare to exp values in table 4 of 1803.08732
wc_obj = flavio.WilsonCoefficients()
Ft = flavio.physics.betadecays.ft.Ft_superallowed(par, wc_obj, '10C')
self.assertAlmostEqual(Ft / s, 3078, delta=2 * 5)
Ft = flavio.physics.betadecays.ft.Ft_superallowed(par, wc_obj, '26mAl')
self.assertAlmostEqual(Ft / s, 3072.9, delta=3 * 1)
Ft = flavio.physics.betadecays.ft.Ft_superallowed(par, wc_obj, '46V')
self.assertAlmostEqual(Ft / s, 3074.1, delta=2 * 2)
Ft = flavio.sm_prediction('Ft(38Ca)')
self.assertAlmostEqual(Ft / s, 3076.4, delta=2 * 7.2)
def test_corrn(self):
# compare to exp values in table 5 of 1803.08732
self.assertAlmostEqual(flavio.sm_prediction('a_n'), -0.1034, delta=2 * 0.0037)
self.assertAlmostEqual(flavio.sm_prediction('atilde_n', me_E=0.695), -0.1090, delta=0.01)
self.assertAlmostEqual(flavio.sm_prediction('Atilde_n', me_E=0.569), -0.11869, delta=0.01)
self.assertAlmostEqual(flavio.sm_prediction('Btilde_n', me_E=0.591), 0.9805, delta=3 * 0.003)
self.assertAlmostEqual(flavio.sm_prediction('lambdaAB_n', me_E=0.581), -1.2686, delta=0.04)
self.assertEqual(flavio.sm_prediction('D_n'), 0)
self.assertEqual(flavio.sm_prediction('R_n'), 0)
def test_noerror(self):
flavio.sm_prediction('mu_gg(h->Zgamma)')
flavio.sm_prediction('mu_tth(h->tautau)')
flavio.sm_prediction('mu_Wh(h->mumu)')
flavio.sm_prediction('mu_Zh(h->bb)')
flavio.sm_prediction('mu_VBF(h->WW)')
def test_noerror(self):
flavio.sm_prediction('mu_gg(h->Zgamma)')
flavio.sm_prediction('mu_tth(h->tautau)')
flavio.sm_prediction('mu_Wh(h->mumu)')
flavio.sm_prediction('mu_Zh(h->bb)')
flavio.sm_prediction('mu_VBF(h->WW)')
self.assertAlmostEqual(flavio.sm_prediction("P4p(B0->K*mumu)", q2=1)/0.15140284164390483, 1, delta=delta)
self.assertAlmostEqual(flavio.sm_prediction("P5p(B0->K*mumu)", q2=1)/0.5064923738851054, 1, delta=delta)
self.assertAlmostEqual(flavio.sm_prediction("AFB(B0->K*mumu)", q2=6)/0.17920578512694066, 1, delta=delta)
self.assertAlmostEqual(flavio.sm_prediction("FL(B0->K*mumu)", q2=6)/0.6746725598733865, 1, delta=delta)
self.assertAlmostEqual(flavio.sm_prediction("S4(B0->K*mumu)", q2=6)/-0.24476949751293892, 1, delta=delta)
self.assertAlmostEqual(flavio.sm_prediction("S5(B0->K*mumu)", q2=6)/-0.3735858481345886, 1, delta=delta)
self.assertAlmostEqual(flavio.sm_prediction("P4p(B0->K*mumu)", q2=6)/-0.529132300413707, 1, delta=delta)
self.assertAlmostEqual(flavio.sm_prediction("P5p(B0->K*mumu)", q2=6)/-0.807602014278806, 1, delta=delta)
self.assertAlmostEqual(flavio.sm_prediction("AFB(B0->K*mumu)", q2=15)/0.406194755197377, 1, delta=delta)
self.assertAlmostEqual(flavio.sm_prediction("FL(B0->K*mumu)", q2=15)/0.36807161547568185, 1, delta=delta)
self.assertAlmostEqual(flavio.sm_prediction("S3(B0->K*mumu)", q2=15)/-0.14872271241628923, 1, delta=delta)
self.assertAlmostEqual(flavio.sm_prediction("S4(B0->K*mumu)", q2=15)/-0.2919191029733921, 1, delta=delta)
self.assertAlmostEqual(flavio.sm_prediction("S5(B0->K*mumu)", q2=15)/-0.34293705381921147, 1, delta=delta)
self.assertAlmostEqual(flavio.sm_prediction("P4p(B0->K*mumu)", q2=15)/-0.6065016593226283, 1, delta=delta)
self.assertAlmostEqual(flavio.sm_prediction("P5p(B0->K*mumu)", q2=15)/-0.7124983944730857, 1, delta=delta)
self.assertAlmostEqual(flavio.sm_prediction("dBR/dq2(B0->K*mumu)", q2=1)/5.7131974656129313e-8, 1, delta=delta)
self.assertAlmostEqual(flavio.sm_prediction("dBR/dq2(B0->K*mumu)", q2=6)/5.524478678779278e-8, 1, delta=delta)
self.assertAlmostEqual(flavio.sm_prediction("dBR/dq2(B0->K*mumu)", q2=15)/7.412258847550239e-8, 1, delta=delta)
# direct CP asymmetry should be close to 0
self.assertAlmostEqual(flavio.sm_prediction("ACP(B0->K*mumu)", q2=1), 0, delta=0.01)
self.assertAlmostEqual(flavio.sm_prediction("ACP(B0->K*mumu)", q2=6), 0, delta=0.01)
self.assertAlmostEqual(flavio.sm_prediction("ACP(B0->K*mumu)", q2=17), 0, delta=0.01)
self.assertAlmostEqual(flavio.sm_prediction("ACP(B+->K*mumu)", q2=1), 0, delta=0.01)
self.assertAlmostEqual(flavio.sm_prediction("ACP(B+->K*mumu)", q2=6), 0, delta=0.01)
self.assertAlmostEqual(flavio.sm_prediction("ACP(B+->K*mumu)", q2=17), 0, delta=0.01)
# this is just a very rough comparison to the literature
delta = 0.2
self.assertAlmostEqual(flavio.sm_prediction("P1(B0->K*mumu)", q2=3), 0, delta=delta)
delta = 0.1
self.assertAlmostEqual(flavio.sm_prediction("P1(B0->K*mumu)", q2=17), -0.64, delta=delta)
delta = 0.1
self.assertAlmostEqual(flavio.sm_prediction("P2(B0->K*mumu)", q2=6)/(2/3.*0.179/0.325), 1, delta=delta)