def test_sourcepdf(tmpdir): ''' Test the "SourcePDF" class. ''' # Test the construction of a normal PDF m = minkit.Parameter('m', bounds=(-5, +5)) c = minkit.Parameter('c', 0., bounds=(-2, +2)) s = minkit.Parameter('s', 1., bounds=(-3, +3)) g = minkit.Gaussian('gaussian', m, c, s) # Test the construction of a PDF with variable number of arguments m = minkit.Parameter('m', bounds=(-5, +5)) p1 = minkit.Parameter('p1', 1.) p2 = minkit.Parameter('p2', 2.) pol0 = minkit.Polynomial('pol0', m) pol1 = minkit.Polynomial('pol1', m, p1) pol2 = minkit.Polynomial('pol2', m, p1, p2) # Test the JSON conversion with open(os.path.join(tmpdir, 'pdf.json'), 'wt') as fi: json.dump(minkit.pdf_to_json(pol0), fi) with open(os.path.join(tmpdir, 'pdf.json'), 'rt') as fi: s = minkit.pdf_from_json(json.load(fi)) check_pdfs(s, pol0)
def test_polynomial(): ''' Test the "Polynomial" PDF. ''' m = minkit.Parameter('m', bounds=(-5, +5)) p1 = minkit.Parameter('p1', 1.) p2 = minkit.Parameter('p2', 2.) p3 = minkit.Parameter('p3', 3.) # Test constant PDF pol0 = minkit.Polynomial('pol0', m) data = np.random.uniform(-5, 5, 100000) compare_with_numpy(pol0, data, m) rndm = pol0.generate(1000) assert np.allclose(pol0.integral(), 1) assert np.allclose(pol0.numerical_normalization(), pol0.norm()) # Test straight line pol1 = minkit.Polynomial('pol1', m, p1) assert np.allclose(pol1.integral(), 1) assert np.allclose(pol1.numerical_normalization(), pol1.norm()) # Test a parabola pol2 = minkit.Polynomial('pol2', m, p1, p2) assert np.allclose(pol2.integral(), 1) assert np.allclose(pol2.numerical_normalization(), pol2.norm()) # Test a three-degree polynomial pol3 = minkit.Polynomial('pol3', m, p1, p2, p3) assert np.allclose(pol3.integral(), 1) assert np.allclose(pol3.numerical_normalization(), pol3.norm())
def test_pdf(): ''' General tests for the PDF class. ''' # Create a Polynomial PDF m = minkit.Parameter('m', bounds=(0, 10)) p1 = minkit.Parameter('p1', 0.) p2 = minkit.Parameter('p2', 0.) p = minkit.Polynomial('polynomial', m, p1, p2) m.set_range('sides', [(0, 4), (6, 10)]) # integral assert np.allclose(p.integral(integral_range='full', range='full'), 1.) assert np.allclose(p.integral(integral_range='sides', range='full'), 0.8) assert np.allclose(p.integral(integral_range='sides', range='sides'), 1.)