flesaker

laplace for a series that never crosses a known bound.

flesaker forecasts log(y − b) with laplace and maps the forecast back through b + exp. Every quantile it reports lies above the bound, the predictive keeps the skew of the map, and its log density includes the Jacobian. Use it for a rate, a price, a volume or a volatility: anything you know is positive, especially when it can get close to zero.

pip install skaters

from skaters import flesaker

f = flesaker(k=8)            # lower=0 by default
state = None
for y in observations:
    dists, state = f(y, state)

dists[0].quantile(0.01)      # always > 0
state["z"], state["pit"]     # laplace's own, unchanged by the map

Live

Grey is plain laplace on the same series; red is the probability it puts below zero. Full demo page · Discuss.

Options

callsupport
flesaker()(0, ∞)
flesaker(lower=-3.0)(−3, ∞)
flesaker(upper=1.0)(−∞, 1)
flesaker(k=8, tails="gaussian")other keywords go to laplace

Every finite observation must lie strictly inside the bound, or flesaker raises ValueError. A series with exact zeros needs the floor set a little below zero. Missing values pass through to laplace. The same model by hand is conjugate(laplace(), log_transform(lower=0.0)).

Scoring across the map

The log score moves between coordinates exactly: the Jacobian term depends on the observation, not the forecast, so differences between forecasters are the same in y and in log y. PIT values and coverage are unchanged too. CRPS and pinball loss are not: they integrate over y, so the map reweights them, and two forecasters can rank differently in levels than in logs. Score flesaker in the units you care about.

The same applies inside the model. laplace fits its terminal leaves to CRPS by default, and inside flesaker that CRPS is measured in log units. flesaker(objective="likelihood") fits to the log score instead, which gives the same fit whichever coordinates it is computed in.

Measured on a synthetic rate

A mean-reverting rate that dips from about 0.5 to about 0.05 and back, 600 points. It is generated on the log scale, so the comparison favours flesaker by construction. Source: examples/positive_series.py.

skatermean log score80% coveragemax P(Y<0)
laplace()1.28979.4%0.329
flesaker()1.66584.4%0.000
laplace on Yeo–Johnson1.44978.2%0.223

Status

flesaker is not part of the official API: it is experimental, from 0.17.0, in Python and JavaScript. There are many possible domains (positive, bounded above, inside an interval, on a circle), and skaters provides a clean way to add your own: write the transform and conjugate laplace by it, as conjugate(laplace(), log_transform(lower=0.0)) does here. We lean towards making flesaker permanent if people keep making YouTube videos criticising the fact that an inanimate library doesn't know ahead of time that a series will always be positive (but maybe you do).

Named after Bjorn Flesaker, whose positive-interest framework with Hughston builds the guarantee that rates stay positive into the model. His interest-rate work was decidedly less trivial than this little homage.