def test_composite_parameter_name_clashes(): with pytest.raises(ValueError) as e: params.CompositeParameter( params.BiasedCoin(0.5), arg0=params.BiasedCoin(0.5), ) assert 'duplicate' in e.value.args[0].lower() with pytest.raises(ValueError) as e: params.CompositeParameter(__init__=params.BiasedCoin(0.5), ) assert 'invalid' in e.value.args[0].lower()
class FixedBoundedFloatStrategy(SearchStrategy): """A strategy for floats distributed between two endpoints. The conditional distribution tries to produce values clustered closer to one of the ends. """ descriptor = float parameter = params.CompositeParameter( cut=params.UniformFloatParameter(0, 1), leftwards=params.BiasedCoin(0.5), ) def __init__(self, lower_bound, upper_bound): SearchStrategy.__init__(self) self.lower_bound = float(lower_bound) self.upper_bound = float(upper_bound) def produce(self, random, pv): if pv.leftwards: left = self.lower_bound right = pv.cut else: left = pv.cut right = self.upper_bound return left + random.random() * (right - left) def simplify(self, value): yield self.lower_bound yield self.upper_bound yield (self.lower_bound + self.upper_bound) * 0.5
class ExponentialFloatStrategy(FloatStrategy): """ A float strategy such that every conditional distribution is of the form aX + b where a = +/- 1 and X is an exponentially distributed random variable. """ parameter = params.CompositeParameter( lambd=params.GammaParameter(2, 50), zero_point=params.NormalParameter(0, 1), negative=params.BiasedCoin(0.5), ) def produce(self, random, pv): value = random.expovariate(pv.lambd) if pv.negative: value = -value return pv.zero_point + value
def test_biased_coin_only_accepts_proper_probabilities(): for bad in [-1.0, 0, 1.0, 10e6]: with pytest.raises(ValueError): params.BiasedCoin(bad)