designspace.Prior#
- class designspace.Prior(*args, **kwargs)#
Bases:
ProtocolA distribution you supply yourself, for .prior().
The built-in families cover the common shapes; this is the escape hatch for anything else. Any object with a ppf satisfies it, which includes a frozen scipy.stats distribution as-is, and the library takes no distribution-library dependency and needs none.
Supply cdf as well whenever the distribution’s support runs past the parameter’s bounds: the chart is then the truncation ppf(cdf(lo) + u * (cdf(hi) - cdf(lo))), and without a cdf that case is an error rather than a silent clipping of tail mass onto the bounds. A cdf also makes the resulting chart invertible, which is what lets a representation over that parameter encode as well as decode.
An external prior is opaque, so a space using one is not serializable without on_unserializable=”mark”.
Examples
A triangular prior, written out in full. ppf alone is enough here because its support is exactly the parameter’s domain.
>>> import math >>> class Triangular: ... def __init__(self, lo, hi): ... self.lo, self.hi = lo, hi ... ... def ppf(self, q): ... return self.lo + (self.hi - self.lo) * math.sqrt(q) >>> s = ds.space(ds.param("x").real(0.0, 1.0).prior(Triangular(0.0, 1.0))) >>> round(s.sample_one(seed=0)["x"], 6) 0.798099
The mass leans toward the upper end, as a triangular prior should:
>>> draws = [c["x"] for c in s.sample_dicts(200, seed=0)] >>> sum(d > 0.5 for d in draws) > 140 True