Skip to content
Snippets Groups Projects

WIP: Resolve "Toy dataset"

Closed Frank Sauerburger requested to merge 7-toy-dataset into master
4 files
+ 532
1
Compare changes
  • Side-by-side
  • Inline
Files
4
+ 248
0
import unittest
import numpy as np
from nnfwtbn.toydata import rand, pdf_gluon, pdf_quark, thetap_pdf, \
gaussp_pdf, ipdf, draw, Random
class ToyDataTestBase(unittest.TestCase):
def test_rand_length(self):
"""
Check that the returned array has the requested length.
"""
self.assertEqual(len(rand()), 1)
self.assertEqual(len(rand(size=1)), 1)
self.assertEqual(len(rand(312)), 312)
def test_rand_repeatable(self):
"""
Check that the method returns the same array twice when called with
the same seed.
"""
numbers_1 = list(rand(143))
numbers_2 = list(rand(143))
self.assertEqual(numbers_1, numbers_2)
def test_rand_seed(self):
"""
Check that using different seeds returns different values.
"""
numbers_1 = list(rand(143, seed=1))
numbers_2 = list(rand(143, seed=2))
self.assertNotEqual(numbers_1, numbers_2)
def test_rand_independent(self):
"""
Check that setting the numpy seed does not affect return value.
"""
np.random.seed(1234)
numbers_1 = list(rand(143))
np.random.seed(4321)
numbers_2 = list(rand(143))
self.assertEqual(numbers_1, numbers_2)
def test_rand_values(self):
"""
Check for specific values.
"""
a, b, c = rand(3)
self.assertAlmostEqual(a, 0.90141859)
self.assertAlmostEqual(b, 0.85225178)
self.assertAlmostEqual(c, 0.25012239)
def test_rand_return_final(self):
"""
Check than the final integer (next seed) is returned when return_final
is True.
"""
(a, b, c), next_seed = rand(3, return_final=True)
self.assertAlmostEqual(a, 0.90141859)
self.assertAlmostEqual(b, 0.85225178)
self.assertAlmostEqual(c, 0.25012239)
self.assertEqual(next_seed, 1074267507)
def test_pdf_gluon_pos(self):
"""
Check that the pdf is always positive.
"""
x = np.linspace(0, 1, 200)
self.assertTrue((pdf_gluon(x) >= 0).all())
def test_pdf_gluon_normalized(self):
"""
Check that the pdf is normalized.
"""
self.assertAlmostEqual(ipdf(pdf_gluon, 0, 1), 1)
def test_pdf_gluon_max(self):
"""
Check that the pdf has it's maximum at x=0.
"""
x = np.linspace(0, 1, 200)
self.assertEqual(np.argmax(pdf_gluon(x)), 0)
def test_pdf_gluon_at_1(self):
"""
Check that the pdf vanishes at x=1.
"""
self.assertAlmostEqual(pdf_gluon(1), 0)
def test_pdf_quark_pos(self):
"""
Check that the pdf is always positive.
"""
x = np.linspace(0, 1, 200)
self.assertTrue((pdf_quark(x) >= 0).all())
def test_pdf_quark_normalized(self):
"""
Check that the pdf is normalized.
"""
self.assertAlmostEqual(ipdf(pdf_quark, 0, 1), 1)
def test_pdf_quark_max(self):
"""
Check that the pdf has it's maximum at x=0.
"""
x = np.linspace(0, 1, 200)
self.assertEqual(np.argmax(pdf_quark(x)), 0)
def test_pdf_quark_at_1(self):
"""
Check that the pdf vanishes at x=1.
"""
self.assertAlmostEqual(pdf_quark(1), 0, delta=0.1)
def test_pdf_quark_peak(self):
"""
Check that there is a peak at x=0.5.
"""
self.assertGreater(pdf_quark(0.5), pdf_quark(0.3))
def test_ipdf(self):
"""
Check that ipdf integrates the given function between 2 and 3.
"""
f = lambda x: x**2
self.assertAlmostEqual(ipdf(f, 2, 1), 19 / 3)
def test_draw_len(self):
"""
Check that draw returns the number of samples given by the size
parameter.
"""
self.assertEqual(len(draw(pdf_gluon)), 1)
self.assertEqual(len(draw(pdf_gluon, size=10)), 10)
def test_draw_reproducible(self):
"""
Check that draw returns the same array when called with identical
arguments.
"""
self.assertEqual(list(draw(pdf_gluon, size=100)),
list(draw(pdf_gluon, size=100)))
self.assertEqual(list(draw(pdf_gluon, size=100, seed=2019)),
list(draw(pdf_gluon, size=100, seed=2019)))
def test_draw_seed(self):
"""
Check that different arrays are returned when different seeds are
given.
"""
self.assertNotEqual(list(draw(pdf_gluon, size=100)),
list(draw(pdf_gluon, size=100, seed=2019)))
def test_draw_bump(self):
"""
Check that for the quark pdf, the method returns more samples in the
bump region.
"""
x = draw(pdf_quark, size=1000)
self.assertGreater(
((0.45 <= x) & (x <= 0.55)).sum(),
((0.35 <= x) & (x <= 0.45)).sum())
class RandomTestCase(unittest.TestCase):
"""
Test the implementation of Random, an independent random number generator.
"""
def test_rand_length(self):
"""
Check that the returned array has the requested length.
"""
rng = Random()
self.assertEqual(len(rng.rand()), 1)
self.assertEqual(len(rng.rand(size=1)), 1)
self.assertEqual(len(rng.rand(312)), 312)
def test_rand_non_repeating(self):
"""
Check that calling rand() twice does not return the same array again.
"""
rng = Random()
numbers_1 = list(rng.rand(143))
numbers_2 = list(rng.rand(143))
self.assertNotEqual(numbers_1, numbers_2)
def test_rand_repeatable(self):
"""
Check that two object with the same seed return the same array.
"""
rng_1 = Random()
numbers_1 = list(rng_1.rand(143))
rng_2 = Random()
numbers_2 = list(rng_2.rand(143))
self.assertEqual(numbers_1, numbers_2)
def test_rand_seed(self):
"""
Check that two object with different seeds return different values.
"""
rng_1 = Random(seed=1)
numbers_1 = list(rng_1.rand(143))
rng_2 = Random(seed=1)
numbers_2 = list(rng_2.rand(14))
self.assertNotEqual(numbers_1, numbers_2)
def test_thetap_endpoints(self):
"""
Check that the thetap pdf vanishes at the endpoints.
"""
self.assertAlmostEqual(thetap_pdf(0), 0)
self.assertAlmostEqual(thetap_pdf(1), 0)
def test_thetap_integral(self):
"""
Check that thetap has unit area.
"""
self.assertAlmostEqual(ipdf(thetap_pdf, 0, 1), 1)
def test_gaussp_integral(self):
"""
Check that thetap has unit area.
"""
self.assertAlmostEqual(ipdf(gaussp_pdf, 0, 1), 1)
def test_range(self):
"""
Check that the draw() never returns values outside
the interval [0, 1).
"""
rng = Random()
numbers = draw(thetap_pdf, rng=rng, size=1000000)
self.assertFalse((numbers >= 1).any())
self.assertFalse((numbers < 0).any())
Loading