API Reference¶
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 | |
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, 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. Needs to be limitited to keep the number of bins manageable.
variance property ¶
Compute variance of the distribution, of shape (*batch_shape,).
__init__(logits, bound_low=-1000.0, bound_up=1000.0, log_spacing=False, bin_normalization_method='sigmoid', validate_args=None) ¶
Initializer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logits | Tensor | Raw logits for 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 |
bin_normalization_method | Literal['sigmoid', 'softmax'] | How to normalize bin 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\).
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 | |
variance property ¶
Compute variance of the distribution, of shape (*batch_shape,).
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.
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.
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 |