Example #1
0
 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]))
Example #2
0
 def _buildmodel(self, withosc, cachestr="testnoosc"):
     #build a simple dummy model
     binning = [("reco_energy", np.linspace(0.0, 5.0, num=100.0)),
                    ("true_energy", np.linspace(0.0, 5.0, num=25.0)),
                     ("true_nupdg", [0.0, 1.0, 2.0, 3.0, 4.0]),
                     ("beammode", [0.0, 1.0, 2.0]),
     ]
     signalsyst = np.array([[-4.0, 1.0, 6.0], [1.0, 1.0, 1.0]])
     bkgdsyst = np.array([[1.0, 1.0, 1.0], [-4.0, 1.0, 6.0]])
     def gen(N):
         for _ in xrange(N):
             nupdg = np.random.randint(4)
             if np.random.uniform() > 0.5:
                 #signal
                 while True:
                     true = np.random.normal(1.0, 0.25)
                     if 0.0 <= true <= 5.0:
                         break
                 syst = signalsyst
             else:
                 #bkgd
                 true = np.random.uniform(0.0, 5.0)
                 syst = bkgdsyst
             while True:
                 reco = np.random.normal(true, 0.0001)
                 if 0.0 <= reco <= 5.0:
                     break
             beammode = np.random.randint(2)
             if beammode == 1:
                 nupdg = {0:1, 1:0, 2:3, 3:2}[nupdg]
             if reco > 0.0 and true > 0.0:
                 yield (reco, true, nupdg, beammode), 1.0, syst
     def gensk(N):
         eff = 0.5
         for (reco, true, nupdg, beammode), weight, syst in gen(N):
                yield (reco, true, nupdg), eff*weight, weight, syst
     iternd280 = gen(10**5)
     itersuperk = gensk(1000)
     systematics = [("signal", [-5.0, 0.0, 5.0]), ("bkgd", [-5.0, 0.0, 5.0])]
     flux_error_binning = [((beamname, flavname), beambin, flavbin, [0.0, 5.0]) for beambin, beamname in enumerate(["RHC", "FHC"]) for flavbin, flavname in enumerate(["numu", "nue", "numubar", "nuebar"])]
     fluxparametermap = FluxSystematics.make_flux_parameter_map(binning[1][1], flux_error_binning)
     systematics = FluxAndSplineSystematics(systematics, enudim=1, nupdgdim=2, beammodedim=3, fluxparametermap=fluxparametermap)
     observables = ["reco_energy"]
     nd280 = BinnedSample("nd280", binning, observables, iternd280, cache_name="nd280_" + cachestr, systematics=systematics)
     xsecprior = GaussianPrior(["signal", "bkgd"], [0.0, 0.0], [0.1, 0.1], seed=1231)
     fluxprior = GaussianPrior([("f_%s_%s_0" %(beamname, flavname)) for beambin, beamname in enumerate(["RHC", "FHC"]) for flavbin, flavname in enumerate(["numu", "nue", "numubar", "nuebar"])], [1.0]*8, [0.1]*8, seed=23123)
     prior = CombinedPrior([xsecprior, fluxprior])
     if withosc:
         superk = BinnedSampleWithOscillation("superk", binning, observables, itersuperk, "true_energy", "true_nupdg", 295.0, cache_name="superk_" + cachestr)
         model = CombinedBinnedSample([nd280, superk])
         prior = CombinedPrior([prior, OscillationParametersPrior()])
     else:
         model = CombinedBinnedSample([nd280])
     #generate monte carlo
     ratevector = model
     toymc = ToyMC(ratevector, prior.generator)
     lhd_data = EventRateLikelihood(model, data=toymc.asimov().vec)
     lhd_prior = prior.likelihood
     lhd = SumLikelihood([lhd_data, lhd_prior])
     return model, toymc, lhd
Example #3
0
 def build(self,
           name,
           toymc,
           sample,
           cache_name=None,
           npe=1000,
           probabilitycalc=None,
           fixed=None,
           oscparmode=OscParMode.SINSQTHETA):
     self.name = name
     oscpars = toymc.generator.parameter_names[:6]
     cov, mean = self._generate_covariance_with_cache(toymc=toymc,
                                                      keep=oscpars,
                                                      npe=npe,
                                                      cache_name=cache_name,
                                                      fixed=fixed)
     generator = self._buildgenerator(toymc, oscpars, cov)
     ratevector = self._buildratevector(oscpars,
                                        toymc,
                                        sample,
                                        generator,
                                        probabilitycalc=probabilitycalc,
                                        oscparmode=oscparmode)
     toymc = ToyMC(ratevector, generator)
     return toymc, cov
Example #4
0
 def build(self,
           name,
           toymc,
           keep=None,
           cache_name=None,
           npe=1000,
           fixed=None):
     self.name = name
     try:
         #assume keep is dict(parnames, splinepoints)
         keys = keep.keys()
         spline_points = OrderedDict()
         for k in keep:
             spline_points[k] = keep[k]
     except AttributeError:
         #assume keep is list(parnames)
         spline_points = None
     cov, mean = self._generate_covariance_with_cache(toymc=toymc,
                                                      keep=keep,
                                                      npe=npe,
                                                      cache_name=cache_name,
                                                      fixed=fixed)
     splines = self._generate_splines_with_cache(
         toymc=toymc,
         nominal=toymc.asimov().vec,
         keep=keep,
         spline_points=spline_points,
         cache_name=cache_name)
     ratevector = self._buildratevector(mean, splines)
     generator = self._buildgenerator(toymc, keep, cov)
     toymc = ToyMC(ratevector, generator)
     return toymc, cov
Example #5
0
    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
Example #6
0
 def test_toymcexperiment_clone(self):
     toymc = ToyMC(self.model, self.gen)
     exp1 = toymc()
     exp2 = exp1.clone()
     self.assertTrue(np.array_equal(exp1.vec, exp2.vec))
     self.assertTrue(np.array_equal(exp1.pars, exp2.pars))
     self.assertFalse(exp1.vec is exp2.vec)
     self.assertFalse(exp1.pars is exp2.pars)
     return
Example #7
0
 def test_toymc_covariance(self):
     npe = 10**4
     toymc = ToyMC(self.model, self.gen)
     expected = np.diag(np.power(self.sigma, 2))
     cov = calculate_statistics_from_toymc(toymc, Covariance(), npe)[0]
     cval = cov.eval()
     cerr = cov.err()
     for ii, jj in itertools.product(xrange(len(self.mu)), repeat=2):
         self.assertAlmostEquals(cval[ii, jj], expected[ii][jj], delta=3.0*cerr[ii, jj])
     return
Example #8
0
 def test_toymc_stat(self):
     npe = 10**4
     toymc = ToyMC(self.model, self.gen)
     statistics = [Mean(), StandardDeviation(), FractionalStandardDeviation()]
     calculate_statistics_from_toymc(toymc, statistics, npe)
     expected = [self.mu, self.sigma, np.divide(self.sigma,self.mu), ]
     for s, exp in itertools.izip_longest(statistics, expected):
         val = s.eval()
         error = s.err()
         for v, er, ex in itertools.izip_longest(val, error, exp):
             self.assertAlmostEquals(v, ex, delta=3.0*er)
     return
Example #9
0
    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
Example #10
0
    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
Example #11
0
    def _buildmodel(self, withosc, cachestr="testnoosc"):
        #build a simple dummy model
        binning = [
            ("reco_energy", np.linspace(0.0, 5.0, num=100.0)),
            ("true_energy", np.linspace(0.0, 5.0, num=25.0)),
            ("true_nupdg", [0.0, 1.0, 2.0, 3.0, 4.0]),
            ("beammode", [0.0, 1.0, 2.0]),
        ]
        signalsyst = np.array([[-4.0, 1.0, 6.0], [1.0, 1.0, 1.0]])
        bkgdsyst = np.array([[1.0, 1.0, 1.0], [-4.0, 1.0, 6.0]])

        def gen(N):
            for _ in xrange(N):
                nupdg = np.random.randint(4)
                if np.random.uniform() > 0.5:
                    #signal
                    while True:
                        true = np.random.normal(1.0, 0.25)
                        if 0.0 <= true <= 5.0:
                            break
                    syst = signalsyst
                else:
                    #bkgd
                    true = np.random.uniform(0.0, 5.0)
                    syst = bkgdsyst
                while True:
                    reco = np.random.normal(true, 0.0001)
                    if 0.0 <= reco <= 5.0:
                        break
                beammode = np.random.randint(2)
                if beammode == 1:
                    nupdg = {0: 1, 1: 0, 2: 3, 3: 2}[nupdg]
                if reco > 0.0 and true > 0.0:
                    yield (reco, true, nupdg, beammode), 1.0, syst

        def gensk(N):
            eff = 0.5
            for (reco, true, nupdg, beammode), weight, syst in gen(N):
                yield (reco, true, nupdg), eff * weight, weight, syst

        iternd280 = gen(10**5)
        itersuperk = gensk(1000)
        systematics = [("signal", [-5.0, 0.0, 5.0]), ("bkgd", [-5.0, 0.0,
                                                               5.0])]
        flux_error_binning = [
            ((beamname, flavname), beambin, flavbin, [0.0, 5.0])
            for beambin, beamname in enumerate(["RHC", "FHC"])
            for flavbin, flavname in enumerate(
                ["numu", "nue", "numubar", "nuebar"])
        ]
        fluxparametermap = FluxSystematics.make_flux_parameter_map(
            binning[1][1], flux_error_binning)
        systematics = FluxAndSplineSystematics(
            systematics,
            enudim=1,
            nupdgdim=2,
            beammodedim=3,
            fluxparametermap=fluxparametermap)
        observables = ["reco_energy"]
        nd280 = BinnedSample("nd280",
                             binning,
                             observables,
                             iternd280,
                             cache_name="nd280_" + cachestr,
                             systematics=systematics)
        xsecprior = GaussianPrior(["signal", "bkgd"], [0.0, 0.0], [0.1, 0.1],
                                  seed=1231)
        fluxprior = GaussianPrior(
            [("f_%s_%s_0" % (beamname, flavname))
             for beambin, beamname in enumerate(["RHC", "FHC"])
             for flavbin, flavname in enumerate(
                 ["numu", "nue", "numubar", "nuebar"])], [1.0] * 8, [0.1] * 8,
            seed=23123)
        prior = CombinedPrior([xsecprior, fluxprior])
        if withosc:
            superk = BinnedSampleWithOscillation("superk",
                                                 binning,
                                                 observables,
                                                 itersuperk,
                                                 "true_energy",
                                                 "true_nupdg",
                                                 295.0,
                                                 cache_name="superk_" +
                                                 cachestr)
            model = CombinedBinnedSample([nd280, superk])
            prior = CombinedPrior([prior, OscillationParametersPrior()])
        else:
            model = CombinedBinnedSample([nd280])
        #generate monte carlo
        ratevector = model
        toymc = ToyMC(ratevector, prior.generator)
        lhd_data = EventRateLikelihood(model, data=toymc.asimov().vec)
        lhd_prior = prior.likelihood
        lhd = SumLikelihood([lhd_data, lhd_prior])
        return model, toymc, lhd
Example #12
0
 def test_toymcexperiment_infostring(self):
     toymc = ToyMC(self.model, self.gen)
     str(toymc) # just check no exceptions are raised
     return
Example #13
0
 def test_toymc_stat(self):
     npe = 1000
     toymc = ToyMC(self.model, self.gen)
     for _ in xrange(npe):
         mc()
     return