def test_prior_transform(self): self.p[0].bounds = PDF(stats.uniform(-10, 20)) self.p[1].bounds = PDF(stats.norm(loc=5, scale=10)) x = self.objective.prior_transform([0.1, 0.9]) assert_allclose( x, stats.uniform.ppf(0.1, -10, 20), stats.norm.ppf(0.9, loc=5, scale=10), )
def test_pickle(self): bounds = PDF(norm(1.0, 2.0)) pkl = pickle.dumps(bounds) pickle.loads(pkl) bounds = Interval() pkl = pickle.dumps(bounds) pickle.loads(pkl)
def test_pymc3(self): # test objective logl against pymc3 # don't run this test if pymc3 is not installed try: import pymc3 as pm except ImportError: return logl = self.objective.logl() from refnx.analysis import pymc_objective from refnx.analysis.objective import _to_pymc3_distribution mod = pymc_objective(self.objective) with mod: pymc_logl = mod.logp({ 'p0': self.p[0].value, 'p1': self.p[1].value }) assert_allclose(logl, pymc_logl) # now check some of the distributions with pm.Model(): p = Parameter(1, bounds=(1, 10)) d = _to_pymc3_distribution('a', p) assert_almost_equal(d.distribution.logp(2).eval(), p.logp(2)) assert_(np.isneginf(d.distribution.logp(-1).eval())) q = Parameter(1, bounds=PDF(stats.uniform(1, 9))) d = _to_pymc3_distribution('b', q) assert_almost_equal(d.distribution.logp(2).eval(), q.logp(2)) assert_(np.isneginf(d.distribution.logp(-1).eval())) p = Parameter(1, bounds=PDF(stats.uniform)) d = _to_pymc3_distribution('c', p) assert_almost_equal(d.distribution.logp(0.5).eval(), p.logp(0.5)) p = Parameter(1, bounds=PDF(stats.norm)) d = _to_pymc3_distribution('d', p) assert_almost_equal(d.distribution.logp(2).eval(), p.logp(2)) p = Parameter(1, bounds=PDF(stats.norm(1, 10))) d = _to_pymc3_distribution('e', p) assert_almost_equal(d.distribution.logp(2).eval(), p.logp(2))
def test_bounds_list(self): bnds = bounds_list(self.p) assert_allclose(bnds, [(-100, 100), (-100, 100)]) # try making a Parameter bound a normal distribution, then get an # approximation to box bounds self.p[0].bounds = PDF(norm(0, 1)) assert_allclose(bounds_list(self.p), [norm(0, 1).ppf([0.005, 0.995]), (-100, 100)])
def bounds(self, bounds): if isinstance(bounds, Bounds): self._bounds = bounds elif bounds is None: self._bounds = Interval() elif hasattr(bounds, '__len__') and len(bounds) == 2: self.range(*bounds) else: rv = PDF(bounds) self._bounds = rv
def test_pdf(self): pdf = PDF(norm) # even if it's really far out it's still a valid value assert_equal(pdf.valid(1003), 1003) # logp assert_equal(pdf.lnprob(0), norm.logpdf(0)) # examine dist with finite support pdf = PDF(truncnorm(-1, 1), seed=1) assert_equal(pdf.lnprob(-2), -np.inf) assert_equal(pdf.lnprob(-0.5), truncnorm.logpdf(-0.5, -1, 1)) # obtain a random value of a bounds instance vals = pdf.rvs(size=1000) assert_(np.min(vals) >= -1) assert_(np.min(vals) <= 1)
def test_user_pdf(self): pdf = UserPDF() bounds = PDF(pdf) assert_equal(bounds.valid(1.0), 1) bounds.rvs(1) assert_equal(bounds.logp(1.0), 0)
def test_pickle(self): # a parameter and a constrained parameter should be pickleable bounds = PDF(norm(1., 2.)) x = Parameter(1, bounds=bounds) pkl = pickle.dumps(x) unpkl = pickle.loads(pkl) # test pickling on a constrained parameter system a = Parameter(1.) b = Parameter(2.) b.constraint = np.sin(a) assert_(hasattr(a, 'sin')) c = [a, b] pkl = pickle.dumps(c) unpkl = pickle.loads(pkl) d, e = unpkl d.value = 2. assert_equal(e.value, np.sin(2.)) # should still have all math functions assert_(hasattr(d, 'sin'))
def test_pdf(self): pdf = PDF(norm) # even if it's really far out it's still a valid value assert_equal(pdf.valid(1003), 1003) # logp assert_equal(pdf.logp(0), norm.logpdf(0)) # examine dist with finite support pdf = PDF(truncnorm(-1, 1)) assert_equal(pdf.logp(-2), -np.inf) assert_equal(pdf.logp(-0.5), truncnorm.logpdf(-0.5, -1, 1)) # obtain a random value of a bounds instance vals = pdf.rvs(size=1000) assert_(np.min(vals) >= -1) assert_(np.min(vals) <= 1) # test a uniform distribution pdf = PDF(uniform(1, 9)) assert_equal(pdf.logp(2), np.log(1.0 / 9.0)) assert_equal(pdf.logp(10.0), np.log(1.0 / 9.0)) # test the invcdf rando = np.random.uniform(size=10) pdf = PDF(truncnorm(-1, 1)) assert_equal(pdf.invcdf(rando), truncnorm.ppf(rando, -1, 1))