API Reference¶
binned_cdf.bezier_cdf ¶
BezierCDF ¶
Bases: Distribution
A continuous probability distribution represented by a global parametric Bezier curve.
The idea is that the CDF is represented as a Bezier curve, which is a weighted sum of Bernstein basis polynomials, defined by control points (betas) that are derived from the input logits. This allows for a smooth, flexible CDF that can capture complex shapes while still being differentiable. With two learned axes, this is a global parametric curve. Its vertical derivative defines a mixture of Beta densities in parameter space, with mixture weights given by the normalized vertical increments (deltas).
Normalized, positive increments define monotone control points on each axis, with endpoints fixed at 0 and 1. For \(t \in [0, 1]\),
so
The two polynomials may have different degrees. Omitting logits_x, passing None, or supplying a one-element logits_x fixes the horizontal curve to
Learning multiple horizontal increments allows density to concentrate without increasing the vertical polynomial degree, but CDF and density evaluation then require numerical inversion. Quantiles only invert the vertical curve.
Inversion uses bracketed Newton steps and implicit first-order gradients, without retaining the solver graph. No application-level minimum increment is imposed; extremely concentrated curves remain limited by the input dtype's precision. Higher-order derivatives through the implicit inverse are not supported.
Note
Monotone control points provide the structural guarantees of a CDF: monotonicity, values in \([0, 1]\), and a non-negative PDF. In exact arithmetic, normalizing positive increments gives these guarantees for any finite logits, without imposing constraints on the parameters during optimization.
With fixed horizontal spacing, the density is a Beta mixture on the normalized support. Classical Bernstein approximation has uniform error \(O(1/n)\) for functions with a bounded second derivative; this is not a general convergence rate for an end-to-end learned two-axis model. A degree-\(n\) mixture component near the center of a fixed support of width \(R\) has approximate standard deviation
This explains the smoothing of narrow interior peaks with fixed spacing. It is not a universal minimum width: boundary components can be narrower, and learned horizontal spacing can further compress peaks.
Source code in binned_cdf/bezier_cdf.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 | |
arg_constraints property ¶
Both logit tensors must contain real values.
degree_x property ¶
Degree of the horizontal polynomial; one for the fixed affine map.
degree_y property ¶
Get the degree \(n\) of the Bernstein polynomial based on the number of logits.
For a Bernstein polynomial of degree \(n\), there are \(n + 1\) control points (betas) and \(n\) weights (deltas).
mean property ¶
Compute the mean by integrating the parametric curve.
Note
Fixed horizontal spacing uses exact Beta-mixture moments; otherwise Gaussian quadrature is polynomial-exact, up to floating-point error.
Returns:
| Type | Description |
|---|---|
Tensor | Tensor of shape (*batch_shape,). |
support property ¶
Support of this distribution.
support_range property ¶
Range of the support, i.e., upper bound - lower bound.
variance property ¶
Compute variance from the normalized horizontal coordinate's first two raw moments.
For \(Z=(X-L)/(U-L)\), \(E[Z^k]=\int_0^1 B_x(t)^k B_y'(t)\,dt\), and
Note
Fixed horizontal spacing uses exact Beta-mixture moments; otherwise Gaussian quadrature is polynomial-exact, up to floating-point error.
Returns:
| Type | Description |
|---|---|
Tensor | Tensor of shape (*batch_shape,). |
__init__(logits_y, logits_x=None, bound_low=-1000.0, bound_up=1000.0, normalization_method='softmax', validate_args=None) ¶
Initializer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logits_y | Tensor | Raw vertical-increment logits, of shape (*batch_shape, degree_y). | required |
logits_x | Tensor | None | Raw horizontal-increment logits, of shape (*batch_shape, degree_x). Batch shapes must broadcast with logits_y, and both tensors must have the same floating dtype and device. None uses a one-element zero tensor matching logits_y, fixing the horizontal map to the identity. | None |
bound_low | float | Lower bound of the distribution support, needs to be finite. | -1000.0 |
bound_up | float | Upper bound of the distribution support, needs to be finite. | 1000.0 |
normalization_method | Literal['sigmoid', 'softmax'] | How to normalize the probabilities. Either "sigmoid" or "softmax". With "sigmoid", each control point is independently activated, while with "softmax", the control point activations influence each other. | 'softmax' |
validate_args | bool | None | Whether to validate arguments. Carried over to keep the interface with the base class. | None |
Source code in binned_cdf/bezier_cdf.py
__repr__() ¶
String representation of the distribution.
Source code in binned_cdf/bezier_cdf.py
cdf(value) ¶
Compute cumulative distribution function at given values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value | Tensor | Values at which to compute the CDF. Expected shape: (*sample_shape, *batch_shape). | required |
Returns:
| Type | Description |
|---|---|
Tensor | CDF values in [0, 1] corresponding to the input values. |
Tensor | Output shape: same as |
Source code in binned_cdf/bezier_cdf.py
entropy(num_quadrature_points=251) ¶
Compute differential entropy via Gaussian quadrature in parameter space, without inversion.
where \(L\) and \(U\) are the lower and upper bounds of the distribution support, respectively.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_quadrature_points | int | Number of Gauss-Legendre nodes for the approximation. | 251 |
Returns:
| Type | Description |
|---|---|
Tensor | Tensor of shape (*batch_shape,). |
Source code in binned_cdf/bezier_cdf.py
icdf(value, num_iter=64, use_newton=True, newton_damping=1.0, convergence_eps_factor=2.0) ¶
Compute the inverse CDF, i.e., the quantile function, at the given values.
Both solvers invert \(B_y(t)-q=0\) and then evaluate \(x(t)\), without a nested horizontal solve:
Newton's method uses the vertical derivative and iterates
where \(q\) is the target quantile in [0, 1], and \(\alpha \in (0, 1]\) is a damping factor that shrinks each Newton step to improve robustness. A bracket within [0, 1] is maintained alongside: whenever \(B_y(t_k) < q\) the lower bound tightens, otherwise the upper bound tightens. If the Newton step would leave the bracket, a bisection step is used instead, guaranteeing monotonic bracket contraction and preventing oscillation. The loop exits when the parameter-space Newton correction or bracket width is below tolerance.
Bisection halves the search interval each iteration, gaining ~1 bit of precision per step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value | Tensor | Values in [0, 1] at which to compute the inverse CDF. Expected shape: (*sample_shape, *batch_shape). | required |
num_iter | int | Maximum number of solver iterations. The loop exits early upon convergence. | 64 |
use_newton | bool | If True, use Newton's method. If False, use pure bisection. | True |
newton_damping | float | Damping factor in (0, 1] applied to the Newton step. A value of 1.0 gives the full Newton step (quadratic convergence), while smaller values improve robustness at the cost of slower convergence. | 1.0 |
convergence_eps_factor | float | The factor multiplied by machine epsilon to determine the convergence criterion. | 2.0 |
Returns:
| Type | Description |
|---|---|
Tensor | Quantiles in [bound_low, bound_up] corresponding to the input CDF values. |
Tensor | Output shape: same as |
Source code in binned_cdf/bezier_cdf.py
log_prob(value) ¶
Compute the log-probability density at given values, entirely in log-space for numerical stability.
Both derivatives are evaluated with logsumexp. With fixed horizontal spacing, \(B_x'(t)=1\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value | Tensor | Values at which to compute the log-PDF. Expected shape: (*sample_shape, *batch_shape). | required |
Returns:
| Type | Description |
|---|---|
Tensor | Log-PDF values corresponding to the input values. |
Tensor | Output shape: same as |
Source code in binned_cdf/bezier_cdf.py
prob(value) ¶
Compute probability density at given values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value | Tensor | Values at which to compute the PDF. Expected shape: (*sample_shape, *batch_shape). | required |
Returns:
| Type | Description |
|---|---|
Tensor | PDF values corresponding to the input values. |
Tensor | Output shape: same as |
Source code in binned_cdf/bezier_cdf.py
rsample(sample_shape=_size) ¶
Draw reparameterized samples with first-order gradients through both axes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample_shape | Size | list[int] | tuple[int, ...] | Desired shape of the samples to be drawn. Default is empty, which means one sample per batch element. | _size |
Returns:
| Type | Description |
|---|---|
Tensor | Samples drawn from the distribution, with shape (*sample_shape, *batch_shape). |
Source code in binned_cdf/bezier_cdf.py
binned_cdf.piecewise_constant_binned_cdf ¶
PiecewiseConstantBinnedCDF ¶
Bases: Distribution
A discrete probability distribution parameterized by binned logits for the CDF.
Each bin contributes a step function to the CDF when active. The activation of each bin is determined by applying a sigmoid to the corresponding logit. The distribution is defined over the interval [bound_low, bound_up] with either linear or logarithmic bin spacing.
Note
This distribution is differentiable with respect to the logits, i.e., the arguments of __init__, but not through the inputs of the prob or cfg method.
Source code in binned_cdf/piecewise_constant_binned_cdf.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 | |
arg_constraints property ¶
Constraints that should be satisfied by each argument of this distribution. None for this class.
bin_probs property ¶
Get normalized probabilities for each bin, of shape (*batch_shape, num_bins).
mean property ¶
Compute mean of the distribution, i.e., the weighted average of bin centers.
Returns:
| Type | Description |
|---|---|
Tensor | Tensor of shape (*batch_shape,). |
num_bins property ¶
Number of bins making up the PiecewiseConstantBinnedCDF.
num_edges property ¶
Number of bins edges of the PiecewiseConstantBinnedCDF.
support property ¶
Support of this distribution. The resolution also depends on the number of bins.
variance property ¶
Compute variance of the distribution.
Returns:
| Type | Description |
|---|---|
Tensor | Tensor of shape (*batch_shape,). |
__init__(logits, bound_low=-1000.0, bound_up=1000.0, log_spacing=False, normalization_method='sigmoid', validate_args=None) ¶
Initializer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logits | Tensor | Raw logits for the bin probabilities (before sigmoid), of shape (*batch_shape, num_bins) | required |
bound_low | float | Lower bound of the distribution support, needs to be finite. | -1000.0 |
bound_up | float | Upper bound of the distribution support, needs to be finite. | 1000.0 |
log_spacing | bool | Whether logarithmic (base = 2) spacing for the bins or linear spacing should be used. | False |
normalization_method | Literal['sigmoid', 'softmax'] | How to normalize the probabilities. Either "sigmoid" or "softmax". With "sigmoid", each bin is independently activated, while with "softmax", the bins activations influence each other. | 'sigmoid' |
validate_args | bool | None | Whether to validate arguments. Carried over to keep the interface with the base class. | None |
Source code in binned_cdf/piecewise_constant_binned_cdf.py
__repr__() ¶
String representation of the distribution.
Source code in binned_cdf/piecewise_constant_binned_cdf.py
cdf(value) ¶
Compute cumulative distribution function at given values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value | Tensor | Values at which to compute the CDF. Expected shape: (*sample_shape, *batch_shape) or broadcastable to it. | required |
Returns:
| Type | Description |
|---|---|
Tensor | CDF values in [0, 1] corresponding to the input values. |
Tensor | Output shape: same as |
Source code in binned_cdf/piecewise_constant_binned_cdf.py
entropy() ¶
Compute Shannon entropy of the discrete distribution.
$\(H[X] = -\sum_{i=1}^{n} p_i \log p_i\)$ where \(p_i\) is the probability mass of bin \(i\).
Returns:
| Type | Description |
|---|---|
Tensor | Tensor of shape (*batch_shape,). |
Source code in binned_cdf/piecewise_constant_binned_cdf.py
expand(batch_shape, _instance=None) ¶
Expand distribution to new batch shape. This creates a new instance.
Source code in binned_cdf/piecewise_constant_binned_cdf.py
icdf(value) ¶
Compute the inverse CDF, i.e., the quantile function, at the given values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value | Tensor | Values in [0, 1] at which to compute the inverse CDF. Expected shape: (*sample_shape, *batch_shape) or broadcastable to it. | required |
Returns:
| Type | Description |
|---|---|
Tensor | Quantiles in [bound_low, bound_up] corresponding to the input CDF values. |
Tensor | Output shape: same as |
Source code in binned_cdf/piecewise_constant_binned_cdf.py
log_prob(value) ¶
Compute the log-probability density at given values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value | Tensor | Values at which to compute the log-PDF. Expected shape: (*sample_shape, *batch_shape) or broadcastable to it. | required |
Returns:
| Type | Description |
|---|---|
Tensor | Log-PDF values corresponding to the input values. |
Tensor | Output shape: same as |
Source code in binned_cdf/piecewise_constant_binned_cdf.py
prob(value) ¶
Compute probability density at given values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value | Tensor | Values at which to compute the PDF. Expected shape: (*sample_shape, *batch_shape) or broadcastable to it. | required |
Returns:
| Type | Description |
|---|---|
Tensor | PDF values corresponding to the input values. |
Tensor | Output shape: same as |
Source code in binned_cdf/piecewise_constant_binned_cdf.py
sample(sample_shape=_size) ¶
Sample from the distribution by passing uniformly random draws from [0, 1] thought the inverse CDF.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample_shape | Size | list[int] | tuple[int, ...] | Shape of the samples to draw. | _size |
Returns:
| Type | Description |
|---|---|
Tensor | Samples of shape (sample_shape + batch_shape), where batch_shape is the batch shape of the distribution. |
Source code in binned_cdf/piecewise_constant_binned_cdf.py
binned_cdf.piecewise_linear_binned_cdf ¶
PiecewiseLinearBinnedCDF ¶
Bases: PiecewiseConstantBinnedCDF
A continuous probability distribution parameterized by binned logits for the CDF.
Unlike [PiecewiseConstantBinnedCDF][binned_cdf.piecewise_constant_cdf.PiecewiseConstantBinnedCDF], which evaluates the CDF as a step function over bin centers, this class implements a true piecewise-linear CDF, i.e., histogram PDF, interpolating smoothly between bin edges.
Source code in binned_cdf/piecewise_linear_binned_cdf.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
variance property ¶
Compute variance of the distribution.
Note
Since the distribution is piecewise linear, the variance includes both the discrete variance from the bin probabilities and the intra-bin variance due to linear interpolation called Sheppard's correction, which assumes that probabilities are uniformly distributed within each bin.
Returns:
| Type | Description |
|---|---|
Tensor | Tensor of shape (*batch_shape,). |
cdf(value) ¶
Compute cumulative distribution function at given values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value | Tensor | Values at which to compute the CDF. Expected shape: (*sample_shape, *batch_shape) or broadcastable to it. | required |
Returns:
| Type | Description |
|---|---|
Tensor | CDF values in [0, 1] corresponding to the input values. |
Tensor | Output shape: same as |
Source code in binned_cdf/piecewise_linear_binned_cdf.py
entropy() ¶
Compute differential entropy of the distribution.
Entropy H(X) = -\sum_{x \in \mathcal{X}} p(x) \log( p(x) )
Note
Here, we are doing an approximation by treating each bin as a uniform distribution over its width.
Returns:
| Type | Description |
|---|---|
Tensor | Tensor of shape (*batch_shape,). |
Source code in binned_cdf/piecewise_linear_binned_cdf.py
icdf(value) ¶
Compute the inverse CDF, i.e., the quantile function, at the given values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value | Tensor | Values in [0, 1] at which to compute the inverse CDF. Expected shape: (*sample_shape, *batch_shape) or broadcastable to it. | required |
Returns:
| Type | Description |
|---|---|
Tensor | Quantiles in [bound_low, bound_up] corresponding to the input CDF values. |
Tensor | Output shape: same as |
Source code in binned_cdf/piecewise_linear_binned_cdf.py
log_prob(value) ¶
Compute the log-probability density at given values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value | Tensor | Values at which to compute the log-PDF. Expected shape: (*sample_shape, *batch_shape) or broadcastable to it. | required |
Returns:
| Type | Description |
|---|---|
Tensor | Log-PDF values corresponding to the input values. |
Tensor | Output shape: same as |
Source code in binned_cdf/piecewise_linear_binned_cdf.py
prob(value) ¶
Compute probability density at given values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value | Tensor | Values at which to compute the PDF. Expected shape: (*sample_shape, *batch_shape) or broadcastable to it. | required |
Returns:
| Type | Description |
|---|---|
Tensor | PDF values corresponding to the input values. |
Tensor | Output shape: same as |