.md →
TangleDistribution API#
Sometimes a single number is too confident. TangleDistribution puts a whole
distribution inside your prose, rendered as 50.0 ± 10.0 by default. Drag
left/right to change one parameter and up/down to change the other; while you
drag, a small chart floats above the page with the starting shape dashed and the
current one filled, so you can see exactly what you are changing.
Fourteen families are supported (TangleDistribution.distributions), named after
their scipy.stats counterparts and using the same parameters. The maths is built
in, so cdf, ppf, pdf/pmf and sample work without scipy, and only for the
families listed here.
| family | kind | ↔ drag | ↕ drag |
|---|---|---|---|
norm |
continuous | loc |
scale |
lognorm |
continuous | scale (the median) |
s |
gamma |
continuous | scale |
a (the shape) |
expon |
continuous | scale (the mean) |
(none) |
weibull_min |
continuous | scale |
c (the shape) |
uniform |
continuous | loc (left edge) |
scale (width) |
laplace |
continuous | loc |
scale |
logistic |
continuous | loc |
scale |
beta |
continuous | a |
b |
poisson |
discrete | mu |
(none) |
binom |
discrete | p |
n (whole number) |
nbinom |
discrete | p |
n |
geom |
discrete | p |
(none) |
randint |
discrete | low |
high (excluded, as in scipy) |
params only changes when you let go of a drag. While dragging, live_params
follows the pointer and dragging is True, so the rest of a notebook can show
the old and the new distribution side by side. Every method takes live=True to
use the in-progress values:
from wigglystuff import TangleDistribution
delivery = TangleDistribution("lognorm", params={"scale": 25, "s": 0.3}, template="{mean:.0f} ± {sd:.0f} minutes")
delivery.ppf(0.9) # 90% of deliveries arrive within this many minutes
delivery.cdf(30) # chance it arrives within half an hour
delivery.sample(1000) # numpy array of draws
delivery.ppf(0.9, live=True) # the same, for the distribution being dragged right now
The inline text comes from template. Its placeholders are the keys of params
plus the computed mean and sd (widget.template_fields lists them), each with
an optional :.Nf precision, e.g. "median {scale:.0f} days (s={s:.2f})".
See also: Tangle widgets for a single draggable number, and TangleFunction for turning a whole function call into draggable arguments.
Bases: AnyWidget
Inline tangle number that carries a whole distribution.
Drag left/right to change the first parameter and up/down to change the
second. While dragging, a small chart of the distribution floats above the
page so you can see the shape change. Family and parameter names follow
scipy.stats, but only the families below are supported (see
TangleDistribution.distributions), and the maths is built in, so scipy
is not needed.
| family | kind | ↔ drag | ↕ drag |
|---|---|---|---|
"norm" |
continuous | loc |
scale |
"lognorm" |
continuous | scale (the median) |
s |
"gamma" |
continuous | scale |
a (the shape) |
"expon" |
continuous | scale (the mean) |
(none) |
"weibull_min" |
continuous | scale |
c (the shape) |
"uniform" |
continuous | loc (left edge) |
scale (width) |
"laplace" |
continuous | loc |
scale |
"logistic" |
continuous | loc |
scale |
"beta" |
continuous | a |
b |
"poisson" |
discrete | mu |
(none) |
"binom" |
discrete | p |
n (whole number) |
"nbinom" |
discrete | p |
n |
"geom" |
discrete | p |
(none) |
"randint" |
discrete | low |
high (excluded, as in scipy) |
Template fields are the parameters in the table plus mean and sd.
Reading the distribution back in Python:
paramsholds the committed values and only changes when a drag is released. While dragging,live_paramsfollows the pointer (throttled bysync_throttle_ms) anddraggingis True, so the rest of the notebook can compare the old distribution against the new one live.meanandsdare computed fromparams.pdf(x)(continuous) orpmf(k)(discrete),cdf(x),ppf(q)andsample(n)all acceptlive=Trueto uselive_paramsinstead.sampleneeds numpy, as do the others when given an array.
The inline text comes from template, by default "{mean} ± {sd}".
Placeholders are the keys of params plus the computed mean and
sd (widget.template_fields lists them), each with an optional
:.Nf precision, e.g. "median {scale:.0f} days (s={s:.2f})".
Examples:
import marimo as mo
from wigglystuff import TangleDistribution
duration = mo.ui.anywidget(
TangleDistribution("lognorm", params={"scale": 10, "s": 0.4}, template="{mean} ± {sd} days")
)
duration
duration.ppf(0.9) # 90% of runs finish within this many days
duration.cdf(14) # chance of finishing within two weeks
Create an inline distribution control.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distribution
|
str
|
One of |
'norm'
|
params
|
Optional[Dict[str, float]]
|
Starting parameters by scipy name; missing ones use the family default. |
None
|
bounds
|
Optional[Dict[str, Tuple[Optional[float], Optional[float]]]]
|
Optional |
None
|
steps
|
Optional[Dict[str, float]]
|
Change per drag step, per parameter. Defaults to 1 for whole numbers and otherwise a power of ten that suits the starting value. |
None
|
pixels_per_step
|
int
|
Drag distance per step (both directions). |
2
|
template
|
str
|
Inline text. |
'{mean} ± {sd}'
|
digits
|
Optional[int]
|
Default number of decimals in the inline text; defaults to what the smallest step and the starting sd need. Whole number parameters always show without decimals. |
None
|
sync_throttle_ms
|
int
|
Cap on how often |
100
|
**kwargs
|
Any
|
Forwarded to |
{}
|
Source code in wigglystuff/tangle.py
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | |
discrete
property
#
True for families over whole numbers (poisson, binom, nbinom, geom, randint).
template_fields
property
#
Placeholders template accepts: the family's parameters plus mean and sd.
cdf #
Probability of a value at or below x (scalar or array-like).
pdf #
Probability density at x (scalar or array-like); continuous families only.
Source code in wigglystuff/tangle.py
pmf #
Probability of exactly k (scalar or array-like); discrete families only.
Source code in wigglystuff/tangle.py
ppf #
Value below which a fraction q of the distribution lies (inverse of cdf).
sample #
Draw n samples as a numpy array.
Source code in wigglystuff/tangle.py
Synced traitlets#
| Traitlet | Type | Notes |
|---|---|---|
distribution |
str |
Family name, one of TangleDistribution.distributions. |
params |
dict |
Committed parameters by scipy name; changes when a drag is released. |
live_params |
dict |
In-progress parameters while dragging; equals params otherwise. |
dragging |
bool |
True while the pointer is held down on the widget. |
bounds |
dict |
[low, high] per parameter; either end may be None. |
steps |
dict |
Change per drag step, per parameter. |
pixels_per_step |
int |
Drag distance per step. |
template |
str |
Inline text with {param}, {mean} and {sd} placeholders. |
digits |
int |
Default number of decimals in the inline text. |
sync_throttle_ms |
int |
Cap on how often live_params reaches Python while dragging. |