API Reference¶
binned_cdf.bezier_cdf ¶
BezierCDF ¶
Bases: Distribution
A continuous probability distribution parameterized by Bernstein polynomials with custom constraints.
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. In fact, this formulation is mathematically equivalent to a mixture of Beta distributions, where the mixture weights are given by the deltas (softmax of the logits) and the Beta components are defined by the control points.
Since we know that any CDF must start at 0 and end at 1, we can enforce these constraints by fixing the first control point to 0 and the last control point to 1.
The spacing of the control points along the domain-axis ("x-axis") is strictly uniform and determined by the degree of the Bernstein polynomial, hence, number of input logits.
Note
Bernstein polynomials converge slowly: the worst-case pointwise approximation error is \(O(1/n)\) where \(n\) is the polynomial degree, leading to a standard deviation error of \(O(1/\sqrt{n})\). However, for smooth CDFs the effective rate is better, and Bernstein density estimators achieve the optimal minimax rate (Babu et al., 2002; Petrone, 1999). This slower convergence is an inherent trade-off for the structural guarantees they provide: monotonicity, values in \([0, 1]\), non-negative PDF, and an unconstrained parameterization (any real-valued logits yield a valid distribution). No other polynomial basis offers all of these simultaneously. In practice, the bias matters less when logits are learned end-to-end via gradient descent, as the optimizer can compensate.
The sharpest peak a degree-n Bernstein polynomial can produce is a single Beta component with \(std \approx 1/(2\sqrt{n})\) in [0,1]-space. Scaled to support range R, the peak std is \(R / (2\sqrt{n})\).
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 | |
arg_constraints property ¶
Constraints that should be satisfied by each argument of this distribution. None for this class.
degree 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 mean of the distribution, i.e., the weighted average of the control points.
We transform the random variable \(X\) to \(T\) in [0, 1] by scaling and shifting according to the bounds. Then, the mean of \(T\) can be computed as
where \(\Delta_i\) is the weight of the \(i\)-th control point, and \(n\) is the degree of the Bernstein polynomial. We can then get the mean by rescaling \(E[T]\) back to the original support:
where \(L\) and \(U\) are the lower and upper bounds of the distribution support, respectively.
Note
This method uses the exact Beta mixture formula.
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 of the distribution.
We transform the random variable \(X\) to \(T\) in [0, 1] by scaling and shifting according to the bounds. Then, the variance of \(T\) can be computed as
with
where \(\Delta_i\) is the weight of the \(i\)-th control point, and \(n\) is the degree of the Bernstein polynomial. We can then get the variance by rescaling \(Var[T]\) back to the original support:
Note
This method uses the exact Beta mixture formula.
Returns:
| Type | Description |
|---|---|
Tensor | Tensor of shape (*batch_shape,). |
__init__(logits, bound_low=-1000.0, bound_up=1000.0, normalization_method='softmax', validate_args=None) ¶
Initializer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logits | Tensor | Raw logits for the probabilities before normalization, of shape (*batch_shape, degree). The logits also determine the degree of the Bernstein polynomial \(n\). | 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 |
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. Output shape: same as |
Source code in binned_cdf/bezier_cdf.py
entropy(num_quadrature_points=251) ¶
Compute differential entropy of the distribution via numerical quadrature.
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 points for the trapezoidal rule approximation. | 251 |
Returns:
| Type | Description |
|---|---|
Tensor | Tensor of shape (*batch_shape,). |
Source code in binned_cdf/bezier_cdf.py
icdf(value, num_iter=8, use_newton=True, newton_damping=0.9, convergence_eps_factor=20.0) ¶
Compute the inverse CDF, i.e., the quantile function, at the given values.
Two solvers are available for inverting $ F(x) - q = 0 $:
Newton's method uses the PDF as the exact derivative of the CDF and iterates
where \(F(x)\) is the CDF, \(f(x)\) is the PDF, \(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 \([L_k, U_k]\) is maintained alongside: whenever \(F(x_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 early once all elements satisfy \(|F(x) - q| < \epsilon\).
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. Newton typically converges undamped in ~6-7 iterations; bisection needs ~15-20 for full float32 precision. | 8 |
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. | 0.9 |
convergence_eps_factor | float | The factor multiplied by machine epsilon to determine the convergence criterion. | 20.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
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 | |
log_prob(value) ¶
Compute the log-probability density at given values, entirely in log-space for numerical stability.
Uses the identity
where \(t = (x - L) / (U - L)\) is the normalized input. Every term is computed in log-space, avoiding the numerically problematic log(polynomial + eps) path.
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. 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. Output shape: same as |
Source code in binned_cdf/bezier_cdf.py
rsample(sample_shape=_size) ¶
Draws reparameterized samples from the distribution, and allows gradients to flow backawards.
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 469 470 | |
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 |