Core¶
Probabilistic-program transformations and automatic program inversion. See Program inversion for the concepts.
Program transformations¶
probjax.core.trace
¶
Source code in probjax/core/transformation.py
probjax.core.joint_sample
¶
Samples all random variables called in the probabilistic function. If rvs is given, it only samples the random variables in rvs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fun
|
Callable
|
Probabilistic function |
required |
rvs
|
Optional[Iterable]
|
Subset of random variables in the probabilistic program. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Callable |
Callable
|
Sampling function |
Source code in probjax/core/transformation.py
probjax.core.log_joint_fn
¶
Compute the model log-joint (up to a constant).
Source code in probjax/core/transformation.py
probjax.core.log_potential_fn
¶
Compute the unnormalized log density of a probabilistic function.
This is the legacy name for :func:log_joint_fn.
This does not include the normalizing constant.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fun
|
Callable
|
Probabilistic function |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Callable |
Log potential function |
Source code in probjax/core/transformation.py
probjax.core.condition
¶
Condition a probabilistic program on observed site values.
The preferred probabilistic name is :func:observe.
Source code in probjax/core/transformation.py
probjax.core.observe
¶
Observe stochastic sites in a probabilistic program.
This is a probabilistic alias for :func:condition.
probjax.core.intervene
¶
Fix stochastic sites via intervention values.
This is equivalent to a causal do operation. The name do is the
probabilistic alias for this function.
This does not sample the random variables, but fixes them to the given values.
The wrapped function uses interpreter-level overrides for the selected random variables while leaving all other equations unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fun
|
Callable
|
A function to transform. |
required |
rvs
|
dict[str, Array]
|
A dictionary of random variable names and values to intervene. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Callable |
Callable
|
Wrapped probabilistic function with interventions. |
Source code in probjax/core/transformation.py
probjax.core.do
¶
Apply a causal do intervention to stochastic sites.
This is a probabilistic alias for :func:intervene.
probjax.core.substitute
¶
Substitute stochastic sites with fixed values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fun
|
Callable
|
Probabilistic function. |
required |
values
|
Mapping[str, Array]
|
Site-value mapping. |
required |
mode
|
str
|
|
'condition'
|
Source code in probjax/core/transformation.py
probjax.core.scope
¶
Inversion¶
probjax.core.inverse
¶
Return a function computing the inverse of fun.
Traces fun to a jaxpr and walks it backwards, replacing each primitive
with its registered inverse rule, so the result is ordinary JAX code with no
interpreter left at runtime:
inverse(lambda x: 2 * jnp.exp(x))(jnp.asarray(4.0)) Array(0.6931472, dtype=float32)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fun
|
Callable
|
The function to invert. May itself be a |
required |
static_argnums
|
Positional arguments held fixed rather than inverted. |
()
|
|
invertible_arg
|
Which positional argument to solve for (default 0).
A tuple of indices solves several arguments jointly -- e.g.
|
None
|
|
input_template
|
Optional tracing example. It must preserve boundary shapes; templates cannot enable expansion or other shape-changing inverses. |
None
|
Returns:
| Type | Description |
|---|---|
|
A callable mapping outputs back to the invertible argument. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if traced input/output shapes or pytree structures differ. Automatic inversion uses the supplied output as the presumed input signature; it requires a shape-preserving function. Some violations (such as a broadcast that becomes an identity at that signature) cannot be detected without the original input specification. |
Note
Detectable boundary shape/tree mismatches raise ValueError. Elementwise affine sections use symbolic coefficients, without Jacobians. Affine sections compose with nonlinear inverse rules; analysis and schedules are cached, including normalized nested jit programs.
An unresolved inversion returns NaN. The interpreter works one equation at a time, so it inverts a tree of operations; a value used twice stalls it, because the bivariate rules need exactly one unknown operand.
On a stall, structurally proven affine sections are recovered by linear
solves and propagation resumes. This supports exp(3*x-x) and
3*exp(x)-exp(x), including sequential compositions and nested jit.
Pointwise maps invert elementwise in O(n); small coupled maps build
the matrix with one vmapped sweep; large coupled maps solve
matrix-free. When a stall cannot be sectioned, a whole-program affine
solve replaces NaN with a value. Analysis and scheduling decisions
are cached; generated inverses are ordinary JAX computations and can
be jitted, vmapped, and differentiated.
These remain silently unsupported and produce NaN:
- a value used more than once nonlinearly --
x * x, or a residualx + f(x). A residual is invertible by fixed-point iteration when its branch is a contraction, but a jaxpr carries no Lipschitz bound, so register it with :class:custom_inverseinstead. lax.fori_loop, and anylax.scancarrying something that is not itself invertible (a counter, a running sum). Plainscanandlax.conddo work.inverse(inverse(f)).
lax.while_loop raises rather than returning NaN. Checking
jnp.isfinite on the result is the reliable way to detect the rest.
Source code in probjax/core/transformation.py
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 | |
probjax.core.inverse_and_logabsdet
¶
Return a function computing the inverse of fun and its log-det.
The log-determinant is that of the inverse map -- log|d(inv)/dy|,
summed over the event -- which is the term a change of variables needs:
inverse_and_logabsdet(lambda x: 2 * jnp.exp(x))(jnp.asarray(4.0)) (Array(0.6931472, dtype=float32), Array(-1.3862944, dtype=float32))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fun
|
Callable
|
The function to invert, possibly a |
required |
static_argnums
|
Positional arguments held fixed rather than inverted. |
()
|
|
invertible_arg
|
Which positional argument to solve for (default 0). A tuple of indices solves several arguments jointly and returns them as a tuple in order. |
None
|
|
input_template
|
Optional tracing example. It must preserve boundary shapes; templates cannot enable expansion or other shape-changing inverses. |
None
|
Returns:
| Type | Description |
|---|---|
|
|
|
|
not be completed. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if traced boundary shapes or pytree structures differ,
with the same input-signature limitation as :func: |
NotImplementedError
|
if a primitive on the inverse path has no log-determinant rule and is not elementwise. Guessing one by differentiating the inverse elementwise -- the old behaviour -- silently returned a number that was not a log-determinant. |
Note
Everything :func:inverse cannot do applies here too, and the log-det
additionally requires a rule for every primitive involved. A primitive
that inverts fine may still have no log-det: dynamic_slice recovers
only its window, leaving the input partially known and no square
Jacobian to take a determinant of.
Note
When the program is structurally volume-preserving in the target --
rearrangements, translations, neg, ±1 scalings -- the log-det
is proven zero from the jaxpr and no accumulation is staged at all, so
the compiled inverse matches a hand-written one equation for equation.
Source code in probjax/core/transformation.py
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 | |
probjax.core.custom_inverse
¶
Attach a custom inverse (and optional log-det) to a function via a primitive.
- Plain (non-traced) calls: direct call to
fun. - Under JAX transforms:
emits
custom_inverse_call_pwithforward_jaxpr: closed JAXPR of forwardinverse_jaxpr_thunk: lazy constructor for inverse JAXPR The thunk is only ever called by your inverse interpreter.
Source code in probjax/core/custom_primitives/custom_inverse.py
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 | |
definv
¶
Define inverse; log-det defaults to NaN.
Source code in probjax/core/custom_primitives/custom_inverse.py
definv_and_logdet
¶
Define inverse that returns (x, logdet).
Source code in probjax/core/custom_primitives/custom_inverse.py
defvalue_and_logdet
¶
Optionally expose forward value_and_logdet.
probjax.core.registry.inverse_checks
¶
Make inverse guards raise through checkify instead of only NaN-ing.
Off by default, and it must be: checkify.check cannot be staged out by a
plain jit -- it raises "Cannot abstractly evaluate a checkify.check which
was not functionalized" -- and an active checkify trace is not detectable
from inside a rule. So the default is a NaN, which is always safe, and this
switch adds the error channel for callers who are wrapping in
checkify.checkify anyway:
with inverse_checks(): ... err, out = checkify.checkify(inverse(f))(y)