def test_profiler(self): # defining some dummy parameters and observables Parameter('tmp a') Parameter('tmp b') Parameter('tmp c') Parameter('tmp d') p = ParameterConstraints() p.set_constraint('tmp b', '2+-0.3') p.set_constraint('tmp c', '0.2+-0.1') p.set_constraint('tmp d', '1+-0.5') def prediction(wc_obj, par): return par['tmp a']**2 + par['tmp b'] + par['tmp c'] + par[ 'tmp d']**2 flavio.Observable('tmp obs') Prediction('tmp obs', prediction) m = Measurement('tmp measurement') m.add_constraint(['tmp obs'], flavio.statistics.probability.NormalDistribution( 1, 0.2)) # test 1D profiler fit_1d = FrequentistFit('test profiler 1d', p, ['tmp a'], ['tmp b', 'tmp c', 'tmp d'], ['tmp obs']) profiler_1d = profiler.Profiler1D(fit_1d, -10, 10) x, z, n = profiler_1d.run(steps=4) self.assertEqual(x.shape, (4, )) self.assertEqual(z.shape, (4, )) self.assertEqual(n.shape, (3, 4)) npt.assert_array_equal(x, profiler_1d.x) npt.assert_array_equal(z, profiler_1d.log_profile_likelihood) npt.assert_array_equal(n, profiler_1d.profile_nuisance) pdat = profiler_1d.pvalue_prob_plotdata() npt.assert_array_equal(pdat['x'], x) # test 2D profiler p.remove_constraint('d') fit_2d = FrequentistFit('test profiler 2d', p, ['tmp a', 'tmp d'], ['tmp b', 'tmp c'], ['tmp obs']) profiler_2d = profiler.Profiler2D(fit_2d, -10, 10, -10, 10) x, y, z, n = profiler_2d.run(steps=(3, 4)) self.assertEqual(x.shape, (3, )) self.assertEqual(y.shape, (4, )) self.assertEqual(z.shape, (3, 4)) self.assertEqual(n.shape, (2, 3, 4)) npt.assert_array_equal(x, profiler_2d.x) npt.assert_array_equal(y, profiler_2d.y) npt.assert_array_equal(z, profiler_2d.log_profile_likelihood) npt.assert_array_equal(n, profiler_2d.profile_nuisance) pdat = profiler_2d.contour_plotdata() npt.assert_array_almost_equal(pdat['z'], -2 * (z - np.max(z))) # delete dummy instances for p in ['tmp a', 'tmp b', 'tmp c', 'tmp d']: Parameter.del_instance(p) FrequentistFit.del_instance('test profiler 1d') Observable.del_instance('tmp obs') Measurement.del_instance('tmp measurement')
def test_sm_covariance(self): o1 = Observable('test_obs 1') o2 = Observable('test_obs 2') def f1(wc_obj, par_dict): return par_dict['m_b'] def f2(wc_obj, par_dict): return par_dict['m_c'] Prediction('test_obs 1', f1) Prediction('test_obs 2', f2) cov_par = np.array([[0.1**2, 0.1 * 0.2 * 0.3], [0.1 * 0.2 * 0.3, 0.2**2]]) d = flavio.statistics.probability.MultivariateNormalDistribution( [4.2, 1.2], covariance=cov_par) par = copy.deepcopy(flavio.parameters.default_parameters) par.add_constraint(['m_b', 'm_c'], d) # test serial np.random.seed(135) cov = flavio.sm_covariance(['test_obs 1', 'test_obs 2'], N=1000, par_vary='all', par_obj=par) npt.assert_array_almost_equal(cov, cov_par, decimal=2) # test parallel np.random.seed(135) cov_parallel = flavio.sm_covariance(['test_obs 1', 'test_obs 2'], N=1000, par_vary='all', par_obj=par, threads=4) npt.assert_array_almost_equal(cov, cov_parallel, decimal=6) np.random.seed(135) cov_1 = flavio.sm_covariance(['test_obs 1'], N=1000, par_vary='all', par_obj=par) # test with single observable npt.assert_array_almost_equal(cov_1, cov[0, 0]) # test with fixed parameter cov_f = flavio.sm_covariance(['test_obs 1', 'test_obs 2'], N=1000, par_vary=['m_b'], par_obj=par) npt.assert_array_almost_equal(cov_f, [[cov_par[0, 0], 0], [0, 0]], decimal=3) # delete dummy instances Observable.del_instance('test_obs 1') Observable.del_instance('test_obs 2')
def test_functions(self): o = Observable('test_obs') o.arguments = ['x'] def f(wc_obj, par_dict, x): return x pr = Prediction('test_obs', f ) wc_obj = None self.assertEqual(flavio.sm_prediction('test_obs', 7), 7) self.assertEqual(flavio.np_prediction('test_obs', x=7, wc_obj=wc_obj), 7) self.assertEqual(flavio.sm_uncertainty('test_obs', 7), 0) self.assertEqual(flavio.np_uncertainty('test_obs', x=7, wc_obj=wc_obj), 0) self.assertEqual(flavio.sm_uncertainty('test_obs', 7, threads=2), 0) self.assertEqual(flavio.np_uncertainty('test_obs', x=7, wc_obj=wc_obj, threads=2), 0) # delete dummy instance Observable.del_instance('test_obs')
def test_exp_combo(self): o = Observable('test_obs') o.arguments = ['x'] m = Measurement('test_obs measurement 1') m.add_constraint([('test_obs', 1)], MultivariateNormalDistribution([1, 2], np.eye(2))) # error: no measurement with self.assertRaises(ValueError): flavio.combine_measurements('test_obs', x=1, include_measurements=['bla']) m.add_constraint([('test_obs', 1)], NormalDistribution(2, 3)) combo = flavio.combine_measurements('test_obs', x=1) self.assertEqual(combo.central_value, 2) self.assertEqual(combo.standard_deviation, 3) m2 = Measurement('test_obs measurement 2') m2.add_constraint([('test_obs', 1)], NormalDistribution(3, 3)) combo = flavio.combine_measurements('test_obs', x=1) self.assertAlmostEqual(combo.central_value, 2.5) self.assertAlmostEqual(combo.standard_deviation, sqrt(9 / 2)) Observable.del_instance('test_obs')
def test_sm_covariance(self): o1 = Observable( 'test_obs 1' ) o2 = Observable( 'test_obs 2' ) def f1(wc_obj, par_dict): return par_dict['m_b'] def f2(wc_obj, par_dict): return par_dict['m_c'] Prediction('test_obs 1', f1) Prediction('test_obs 2', f2) cov_par = np.array([[0.1**2, 0.1*0.2*0.3], [0.1*0.2*0.3, 0.2**2]]) d = flavio.statistics.probability.MultivariateNormalDistribution([4.2, 1.2], covariance=cov_par) par = copy.deepcopy(flavio.parameters.default_parameters) par.add_constraint(['m_b', 'm_c'], d) # test serial np.random.seed(135) cov = flavio.sm_covariance(['test_obs 1', 'test_obs 2'], N=1000, par_vary='all', par_obj=par) npt.assert_array_almost_equal(cov, cov_par, decimal=2) # test parallel np.random.seed(135) cov_parallel = flavio.sm_covariance(['test_obs 1', 'test_obs 2'], N=1000, par_vary='all', par_obj=par, threads=4) npt.assert_array_almost_equal(cov, cov_parallel, decimal=6) np.random.seed(135) cov_1 = flavio.sm_covariance(['test_obs 1'], N=1000, par_vary='all', par_obj=par) # test with single observable npt.assert_array_almost_equal(cov_1, cov[0, 0]) # test with fixed parameter cov_f = flavio.sm_covariance(['test_obs 1', 'test_obs 2'], N=1000, par_vary=['m_b'], par_obj=par) npt.assert_array_almost_equal(cov_f, [[cov_par[0, 0], 0], [0, 0]], decimal=3) # delete dummy instances Observable.del_instance('test_obs 1') Observable.del_instance('test_obs 2')
def test_profiler(self): # defining some dummy parameters and observables Parameter('tmp a'); Parameter('tmp b'); Parameter('tmp c'); Parameter('tmp d'); p = ParameterConstraints() p.set_constraint('tmp b', '2+-0.3') p.set_constraint('tmp c', '0.2+-0.1') p.set_constraint('tmp d', '1+-0.5') def prediction(wc_obj, par): return par['tmp a']**2+par['tmp b']+par['tmp c']+par['tmp d']**2 flavio.Observable('tmp obs'); Prediction('tmp obs', prediction); m=Measurement('tmp measurement') m.add_constraint(['tmp obs'], flavio.statistics.probability.NormalDistribution(1, 0.2)) # test 1D profiler fit_1d = FrequentistFit('test profiler 1d', p, ['tmp a'], ['tmp b', 'tmp c', 'tmp d'], ['tmp obs']) profiler_1d = profiler.Profiler1D(fit_1d, -10, 10) x, z, n = profiler_1d.run(steps=4) self.assertEqual(x.shape, (4,)) self.assertEqual(z.shape, (4,)) self.assertEqual(n.shape, (3, 4)) npt.assert_array_equal(x, profiler_1d.x) npt.assert_array_equal(z, profiler_1d.log_profile_likelihood) npt.assert_array_equal(n, profiler_1d.profile_nuisance) pdat = profiler_1d.pvalue_prob_plotdata() npt.assert_array_equal(pdat['x'], x) # test multiprocessing for threads in [2, 3, 4]: xt, zt, nt = profiler_1d.run(steps=4, threads=threads) npt.assert_array_almost_equal(x, xt, decimal=4) npt.assert_array_almost_equal(z, zt, decimal=4) npt.assert_array_almost_equal(n, nt, decimal=4) with self.assertRaises(ValueError): profiler_1d.run(steps=4, threads=5) # test 2D profiler p.remove_constraint('d') fit_2d = FrequentistFit('test profiler 2d', p, ['tmp a', 'tmp d'], ['tmp b', 'tmp c'], ['tmp obs']) profiler_2d = profiler.Profiler2D(fit_2d, -10, 10, -10, 10) x, y, z, n = profiler_2d.run(steps=(3,4)) self.assertEqual(x.shape, (3,)) self.assertEqual(y.shape, (4,)) self.assertEqual(z.shape, (3, 4)) self.assertEqual(n.shape, (2, 3, 4)) npt.assert_array_equal(x, profiler_2d.x) npt.assert_array_equal(y, profiler_2d.y) npt.assert_array_equal(z, profiler_2d.log_profile_likelihood) npt.assert_array_equal(n, profiler_2d.profile_nuisance) pdat = profiler_2d.contour_plotdata() npt.assert_array_almost_equal(pdat['z'], -2*(z-np.max(z))) # test multiprocessing for threads in [2, 5, 12]: xt, yt, zt, nt = profiler_2d.run(steps=(3,4)) npt.assert_array_almost_equal(x, xt, decimal=4) npt.assert_array_almost_equal(y, yt, decimal=4) npt.assert_array_almost_equal(z, zt, decimal=4) npt.assert_array_almost_equal(n, nt, decimal=4) with self.assertRaises(ValueError): profiler_2d.run(steps=(3,4), threads=13) # delete dummy instances for p in ['tmp a', 'tmp b', 'tmp c', 'tmp d']: Parameter.del_instance(p) FrequentistFit.del_instance('test profiler 1d') Observable.del_instance('tmp obs') Measurement.del_instance('tmp measurement')