def test_toymc_exceptions(self): with self.assertRaises(MonteCarloParameterMismatch): ToyMC(self.model, GaussianGenerator(list(reversed(self.model.parameter_names)), self.mu, self.sigma)) with self.assertRaises(MonteCarloParameterMismatch): ToyMC(self.model, GaussianGenerator(["x", "y"], self.mu, self.sigma)) with self.assertRaises(MonteCarloParameterMismatch): ToyMC(self.model, GaussianGenerator(self.model.parameter_names + ["z"], self.mu + [3.0], self.sigma + [3.0]))
def test_generatorlistexception(self): gen1 = GaussianGenerator(["a", "b"], [1.0, 1.0], [1.0, 1.0]) gen2 = GaussianGenerator(["c", "d"], [1.0, 1.0], [1.0, 1.0]) lgen = GeneratorList(gen1, gen2) with self.assertRaises(ValueError): lgen.getmu("x") with self.assertRaises(ValueError): lgen.getsigma("x") with self.assertRaises(ValueError): lgen.getcovariance("a", "x")
def _buildtestmc(self, cachestr=None): systematics = [ ("x", [-5.0, 0.0, 5.0]), ("y", [-5.0, 0.0, 5.0]), ("z", [-10.0, -5.0, 0.0, 5.0, 10.0]), ] systematics = SplineSystematics(systematics) random = np.random.RandomState(seed=1223) def gen(N): for _ in xrange(N): coord = random.poisson(size=2) yield coord, 1.0, [ (_smear(-4.0, random), _smear(1.0, random), _smear(5.0, random)), (_smear(-4.0, random), _smear(1.0, random), _smear(5.0, random)), (_smear(-9.0, random), _smear(-4.0, random), _smear(1.0, random), _smear(5.0, random), _smear(10.0, random)) ] binning = [("a", np.arange(0.0, 5.0)), ("b", np.arange(0.0, 5.0))] observables = ["a"] model = BinnedSample("simplemodel", binning, observables, gen(10**4), systematics=systematics, cache_name=cachestr) generator = GaussianGenerator(["x", "y", "z"], [1.0, 2.0, 3.0], [1.0, 2.0, 3.0], seed=1224) toymc = ToyMC(model, generator) return toymc
def test_generatorlist(self): #make a generator list from every kind of generator we have mu = self.mu sigma = self.sigma cov = self.cov names = self.names gnames = ["gaus"+n for n in names] cnames = ["const"+n for n in names] mnames = ["multigaus"+n for n in names] gengaus = GaussianGenerator(gnames, mu, sigma, seed=12021) genconst = ConstantGenerator(cnames, mu) genmulti = MultiVariateGaussianGenerator(mnames, mu, cov, seed=2314) gen = GeneratorList(gengaus, genconst, genmulti) #generate data npe = 10**4 data = np.array([gen() for _ in xrange(npe)]) #compare to expected exmu = np.concatenate([mu, mu, mu]) exsigma = np.concatenate([sigma, np.zeros(len(mu)), sigma]) excov = np.diag(np.power(exsigma, 2)) excov[len(mu)*2:,len(mu)*2:] = cov #do checks self._checkmean(data, mu=exmu, sigma=exsigma, gen=gen) self._checkstddev(data, sigma=exsigma, gen=gen) self._checkcovariance(data, gen=gen, cov=excov) return
def speedtest_gaus(npe=_NPE, npars=_NPARS): n = [str(x) for x in range(npars)] mu = range(npars) sigma = range(npars) gen = GaussianGenerator(n, mu, sigma) for _ in xrange(npe): gen() return
def test_gaussian_withfixed(self): mu = self.mu sigma = self.sigma names = self.names gen = GaussianGenerator(names, mu, sigma, seed=19021) fixed = {n:ii for ii, n in enumerate(self.names[-5:])} fixedmu = list(self.mu) fixedsigma = list(self.sigma) for n, v in fixed.iteritems(): fixedmu[names.index(n)] = v fixedsigma[names.index(n)] = 0.0 gen.setfixed(fixed) npe = 10**4 precision = 5.0*1.0/math.sqrt(npe) data = np.array([gen() for _ in xrange(npe)]) self._checkmean(data, mu=fixedmu, sigma=fixedsigma, gen=gen) self._checkstddev(data, sigma=fixedsigma) self._checkcovariance(data, cov=np.diag(np.power(fixedsigma,2)))
def setUp(self): names = ["a", "b"] def simplemodel(pars): return np.copy(pars) simplemodel.parameter_names = names self.model = simplemodel self.mu = [1.0, 2.0] self.sigma = [1.0, 2.0] self.gen = GaussianGenerator(names, self.mu, self.sigma, seed=12912)
def test_fixedexcept_parameters(self): npe = 10**4 mu = self.mu sigma = self.sigma names = self.names gen = GaussianGenerator(names, mu, sigma, seed=19021) for low, high in [(3, 7), (0, 10), (1, 3)]: fixedval = 0.5 fixed = {n:0.5 for n in names[low:high]} gen.fixallexcept(fixed) data = np.array([gen() for _ in xrange(npe)]) expectedmu = np.copy(mu) expectedsigma = np.copy(sigma) expectedsigma[:low] = 0.0 expectedsigma[high:] = 0.0 self._checkmean(data, mu=expectedmu) self._checkstddev(data, sigma=expectedsigma) return
def test_gaussian(self): mu = self.mu sigma = self.sigma names = self.names gen = GaussianGenerator(names, mu, sigma, seed=19021) npe = 10**4 precision = 5.0*1.0/math.sqrt(npe) data = np.array([gen() for _ in xrange(npe)]) self._checkmean(data, gen=gen) self._checkstddev(data, gen=gen) self._checkcovariance(data, cov=np.diag(np.power(sigma,2)), gen=gen)
def speedtest_list(npe=_NPE, npars=_NPARS): names = [str(x) for x in range(npars)] mu = range(npars) sigma = range(npars) gaus = GaussianGenerator(["gaus" + n for n in names], mu, sigma) cov = np.diag(sigma) multigaus = MultiVariateGaussianGenerator(["multi" + n for n in names], mu, cov) gen = GeneratorList(gaus, multigaus) for _ in xrange(npe): gen() return
def build1flavmodel(self, iflav, jflav, mode): systematics = [ ("x", [-5.0, 0.0, 5.0]), ("y", [-5.0, 0.0, 5.0]), ("z", [-5.0, 0.0, 5.0]), ] enubinning = self.enubinning systematics = SplineSystematics(systematics) random = np.random.RandomState(1222) def gen(N, iflav=iflav, jflav=jflav): for trueenu in self.enubincentres: recoenu = trueenu for flavbin in [0.0, 1.0, 2.0, 3.0]: if flavbin == jflav: eff = 1.0 else: eff = 1e-9 if flavbin == iflav: weight = 1.0 else: weight = 1e-9 coord = (trueenu, flavbin, recoenu) yield coord, eff * weight, weight, [(-4.0, 1.0, 6.0), (-4.0, 1.0, 6.0), (-4.0, 1.0, 6.0)] binning = [("trueenu", enubinning), ("nupdg", np.arange(0.0, 5.0)), ("recoenu", enubinning)] observables = ["recoenu"] model = BinnedSampleWithOscillation("flatmodelwithoscillation", binning, observables, gen(10**4), enuaxis="trueenu", flavaxis="nupdg", distance=295.0, systematics=systematics, probabilitycalc=None, oscparmode=mode) oscgen = OscillationParametersPrior(seed=1225, oscparmode=mode).generator systgen = GaussianGenerator(["x", "y", "z"], [0.0, 0.0, 0.0], [0.1, 0.1, 0.1], seed=1226) #toymc = ToyMC(model, GeneratorList(oscgen)) toymc = ToyMC(model, GeneratorList(oscgen, systgen)) return toymc
def test_generatorexception(self): g = Generator(["a","b"], [0.0, 0.0]) with self.assertRaises(NotImplementedError): g() # not implemented method with self.assertRaises(NotImplementedError): g.getsigma("a") # not implemented method with self.assertRaises(ValueError): g.setfixed({"x":0.0}) # fix non-existant parameter with self.assertRaises(ValueError): Generator(["a","b"], [0.0, 0.0, 0.0]) # wrong num parameters with self.assertRaises(ValueError): GaussianGenerator(["a","b"], [0.0, 0.0], [1.0, 1.0, 1.0]) # wrong num parameters (gaus) with self.assertRaises(ValueError): MultiVariateGaussianGenerator(["a","b"], [0.0, 0.0], [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]) # wrong num parameters (gaus) with self.assertRaises(ValueError): Generator(["a","a"], [0.0, 0.0]) # duplicate par name
def _buildtestmc(self, cachestr=None): systematics = [ ("x", [-5.0, 0.0, 5.0]), ("y", [-5.0, 0.0, 5.0]), ("z", [-5.0, 0.0, 5.0]), ] systematics = SplineSystematics(systematics) random = np.random.RandomState(1222) def gen(N): for _ in xrange(N): nupdg = random.uniform(0.0, 4.0) trueenu = random.uniform(0.0, 5.0) recoenu = _smear(trueenu, random) coord = (trueenu, nupdg, recoenu) yield coord, 1.0, 1.0, [ (_smear(-4.0, random), _smear(1.0, random), _smear(6.0, random)), (_smear(-4.0, random), _smear(1.0, random), _smear(6.0, random)), (_smear(-4.0, random), _smear(1.0, random), _smear(6.0, random)) ] binning = [("trueenu", np.linspace(0.0, 5.0, num=10.0)), ("nupdg", np.arange(0.0, 5.0)), ("recoenu", np.linspace(0.0, 5.0, num=10.0))] observables = ["recoenu"] model = BinnedSampleWithOscillation("simplemodelwithoscillation", binning, observables, gen(10**4), enuaxis="trueenu", flavaxis="nupdg", distance=295.0, systematics=systematics, probabilitycalc=None) oscgen = OscillationParametersPrior(seed=1225).generator systgen = GaussianGenerator(["x", "y", "z"], [0.0, 0.0, 0.0], [0.1, 0.1, 0.1], seed=1226) #toymc = ToyMC(model, GeneratorList(oscgen)) toymc = ToyMC(model, GeneratorList(oscgen, systgen)) return toymc
def test_mean_and_stddev(self): expectedmu = np.arange(0.0, 10.0) names = ["par" + str(x) for x in expectedmu] expectedsigma = np.arange(0.0, 10.0) mu = Mean() gen = GaussianGenerator(names, expectedmu, expectedsigma, seed=1290) statistics = [ Mean(), StandardDeviation(), FractionalStandardDeviation() ] with nostdout(): calculate_statistics(gen, statistics, 10**4, name="test") for s, expected in itertools.izip_longest( statistics, [expectedmu, expectedsigma, safedivide(expectedsigma, expectedmu)]): for x, err, exp in itertools.izip_longest(s.eval(), s.err(), expected): self.assertAlmostEquals(x, exp, delta=5.0 * err) return
def test_roothistogram(self): names = ["a", "b"] expectedmu = np.array([2.0, 4.0]) expectedsigma = np.array([1.0, 2.0]) gen = GaussianGenerator(names, expectedmu, expectedsigma) #try 3 ways of constructing object to ensure all code paths are tested hist1 = RootHistogram("testhist", "testhist") hist2 = RootHistogram("testhist", "testhist", range=[-5.0, 5.0]) hist3 = RootHistogram("testhist", "testhist", range=[(-5.0, 5.0), (-10., 10.)]) mean = Mean() sigma = StandardDeviation() calculate_statistics(gen, [hist1, hist2, hist3, mean, sigma], 10**4) for h1, h2, h3, mu, sigma in itertools.izip_longest( hist1.eval(), hist2.eval(), hist2.eval(), mean.eval(), sigma.eval()): self.assertAlmostEqual(h1.GetMean(), mu) self.assertAlmostEqual(h1.GetRMS(), sigma) self.assertAlmostEqual(h2.GetMean(), mu) self.assertAlmostEqual(h2.GetRMS(), sigma) self.assertAlmostEqual(h3.GetMean(), mu) self.assertAlmostEqual(h3.GetRMS(), sigma) return
def test_fixed_parameters(self): npe = 10**4 mu = self.mu sigma = self.sigma names = self.names gen = GaussianGenerator(names, mu, sigma, seed=19021) for low, high in [(3, 7), (0, 10), (1, 3)]: fixedval = 0.5 fixed = {n:0.5 for n in names[low:high]} gen.setfixed(fixed) data = np.array([gen() for _ in xrange(npe)]) expectedmu = np.copy(mu) expectedmu[low:high] = fixedval expectedsigma = np.copy(sigma) expectedsigma[low:high] = 0.0 self._checkmean(data, mu=expectedmu) self._checkstddev(data, sigma=expectedsigma) #check switching if off we get original behaviour gen.setfixed(None) data = np.array([gen() for _ in xrange(npe)]) self._checkmean(data, mu=mu) self._checkstddev(data, sigma=sigma) return
def __init__(self, parameter_names, mu, sigma, seed=None): gen = GaussianGenerator(parameter_names, mu, sigma, seed=seed) lhd = GaussianLikelihood(parameter_names, mu, sigma) super(GaussianPrior, self).__init__(gen, lhd)