def test_repr(self): p = Interval(-5, 5) q = eval(repr(p)) assert_equal(q.lb, p.lb) assert_equal(q.ub, p.ub) p = Interval() q = eval(repr(p)) assert_equal(q.lb, p.lb) assert_equal(q.ub, p.ub)
def __init__(self, value=0., name=None, bounds=None, vary=False, constraint=None): """ Parameters ---------- name : str, optional Name of the parameter. value : float, optional Numerical Parameter value. bounds: `refnx.analysis.Bounds`, tuple, optional Sets the bounds for the parameter. Either supply a `refnx.analysis.Bounds` object (or one of its subclasses), or a `(lower_bound, upper_bound)` tuple. vary : bool, optional Whether the Parameter is fixed during a fit. constraint : expression, optional Python expression used to constrain the value during the fit. """ super(Parameter, self).__init__() self.name = name # set bounds before setting value because the value may not be valid # for the bounds self._bounds = Interval() if bounds is not None: self.bounds = bounds self.value = value self._vary = vary self._constraint = None self.constraint = constraint
def __init__(self, value=0., name=None, bounds=None, vary=False, constraint=None): """ Parameters ---------- name : str, optional Name of the parameter. value : float, optional Numerical Parameter value. vary : bool, optional Whether the Parameter is fixed during a fit. constraint : expression, optional Python expression used to constrain the value during the fit. """ super(Parameter, self).__init__() self.name = name # set bounds before setting value because the value may not be valid # for the bounds self._bounds = Interval() if bounds is not None: self.bounds = bounds self.value = value self._vary = vary self._constraint = None self.constraint = constraint
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 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 range(self, lower, upper): """ Sets the lower and upper limits on the Parameter Parameters ---------- lower : float lower bound upper : float upper bound Returns ------- None """ self.bounds = Interval(lower, upper)
def test_parameter_bounds(self): x = Parameter(4, bounds=Interval(-4, 4)) assert_equal(x.logp(), uniform.logpdf(0, -4, 8)) x.bounds = None assert_(isinstance(x._bounds, Interval)) assert_equal(x.bounds.lb, -np.inf) assert_equal(x.bounds.ub, np.inf) assert_equal(x.logp(), 0) x.setp(bounds=norm(0, 1)) assert_almost_equal(x.logp(1), norm.logpdf(1, 0, 1)) # all created parameters were mistakenly being given the same # default bounds instance! x = Parameter(4) y = Parameter(5) assert_(id(x.bounds) != id(y.bounds))
def test_interval(self): # open interval should have logp of 0 interval = Interval() assert_equal(interval.logp(0), 0) # semi closed interval interval.ub = 1000 assert_equal(interval.logp(0), 0) assert_equal(interval.logp(1001), -np.inf) # you should be able to send in multiple values assert_equal( interval.logp(np.array([1.0, 1002.0])), np.array([0, -np.inf]) ) # fully closed interval interval.lb = -1000 assert_equal(interval.logp(-1001), -np.inf) assert_equal(interval.lb, -1000) assert_equal(interval.ub, 1000) assert_equal(interval.logp(0), np.log(1 / 2000.0)) # you should be able to send in multiple values assert_equal( interval.logp(np.array([1.0, 2.0])), np.array([np.log(1 / 2000.0)] * 2), ) # try and set lb higher than ub interval.lb = 1002 assert_equal(interval.lb, 1000) assert_equal(interval.ub, 1002) # if val is outside closed range then rvs is used vals = interval.valid(np.linspace(990, 1005, 100)) assert_(np.max(vals) <= 1002) assert_(np.min(vals) >= 1000) assert_(np.isfinite(interval.logp(vals)).all()) # if bounds are semi-open then val is reflected from lb interval.ub = None interval.lb = 1002 x = np.linspace(990, 1001, 10) vals = interval.valid(x) assert_almost_equal(vals, 2 * interval.lb - x) assert_equal(interval.valid(1003), 1003) # if bounds are semi-open then val is reflected from ub interval.lb = None interval.ub = 1002 x = np.linspace(1003, 1005, 10) vals = interval.valid(x) assert_almost_equal(vals, 2 * interval.ub - x) assert_equal(interval.valid(1001), 1001) # ppf for Interval interval.lb = -10.0 interval.ub = 10.0 rando = np.random.uniform(size=10) assert_equal(interval.invcdf(rando), uniform.ppf(rando, -10, 20))
def test_interval(self): # open interval should have logp of 0 interval = Interval() assert_equal(interval.logp(0), 0) # semi closed interval interval.ub = 1000 assert_equal(interval.logp(0), 0) assert_equal(interval.logp(1001), -np.inf) # fully closed interval interval.lb = -1000 assert_equal(interval.logp(-1001), -np.inf) assert_equal(interval.lb, -1000) assert_equal(interval.ub, 1000) assert_equal(interval.logp(0), np.log(1 / 2000.)) # try and set lb higher than ub interval.lb = 1002 assert_equal(interval.lb, 1000) assert_equal(interval.ub, 1002) # if val is outside closed range then rvs is used vals = interval.valid(np.linspace(990, 1005, 100)) assert_(np.max(vals) <= 1002) assert_(np.min(vals) >= 1000) assert_(np.isfinite(interval.logp(vals)).all()) # if bounds are semi-open then val is reflected from lb interval.ub = None interval.lb = 1002 x = np.linspace(990, 1001, 10) vals = interval.valid(x) assert_almost_equal(vals, 2 * interval.lb - x) assert_equal(interval.valid(1003), 1003) # if bounds are semi-open then val is reflected from ub interval.lb = None interval.ub = 1002 x = np.linspace(1003, 1005, 10) vals = interval.valid(x) assert_almost_equal(vals, 2 * interval.ub - x) assert_equal(interval.valid(1001), 1001)
def range(self, lower, upper): self.bounds = Interval(lower, upper)