elicito.utils#
helper functions for setting up the Elicit object
Classes:
| Name | Description |
|---|---|
DoubleBound |
constrain double-bounded distributions |
LowerBound |
constrain lower-bounded distributions |
UpperBound |
transform |
Functions:
| Name | Description |
|---|---|
compute_num_weights |
Compute number of weights of a tf.keras model. |
dry_run |
Run generative model in forward mode for a single epoch |
get_expert_data |
Load the training data |
get_expert_datformat |
Inspect which data format for the expert data is expected by the method. |
gumbel_softmax_trick |
Apply softmax-gumble trick |
identity |
Identity function. Returns the input |
load |
Load a saved |
one_forward_simulation |
Run one forward simulation from prior samples to elicited statistics. |
parallel |
Specify parallelization |
save |
Save the eliobj as pickle. |
save_as_pkl |
Save file as pickle. |
DoubleBound #
constrain double-bounded distributions
Methods:
| Name | Description |
|---|---|
__init__ |
Constrain double-bounded distribution |
forward |
Scale and translate logit transformed variable |
inv_logit |
Implement the inverse-logit transformation |
inverse |
Apply inverse of the log-odds transform |
logit |
Implement the logit transformation for :math: |
Source code in src/elicito/utils.py
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 | |
__init__ #
Constrain double-bounded distribution
A variable constrained to be in the open interval
(lower, upper) is transformed to an unconstrained variable Y
via a scaled and translated log-odds transform.
Basis for the here used constraints, is the `constraint transforms implementation in Stan.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lower
|
float
|
Lower bound of variable x. |
required |
upper
|
float
|
Upper bound of variable x. |
required |
Source code in src/elicito/utils.py
forward #
Scale and translate logit transformed variable
transform variable x with lower and upper bound
into an unconstrained variable y.
.. math::
Y = logit\left(\frac{X - lower}{upper - lower}\right)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Variable with lower and upper bound. |
required |
Returns:
| Type | Description |
|---|---|
y
|
Unconstrained variable. |
Source code in src/elicito/utils.py
inv_logit #
Implement the inverse-logit transformation
The inverse-logit transformation is the logistic
sigmoid for :math:v \in (-\infty,+\infty):
.. math::
logit^{-1}(v) = \frac{1}{1+\exp(-v)}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Tensor
|
Unconstrained variable |
required |
Returns:
| Type | Description |
|---|---|
u
|
Logistic sigmoid of the unconstrained variable |
Source code in src/elicito/utils.py
inverse #
Apply inverse of the log-odds transform
unconstrained variable y is transformed into a constrained variable x
with lower and upper bound.
.. math::
X = lower + (upper - lower) \cdot logit^{-1}(Y)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
Tensor
|
Unconstrained variable |
required |
Returns:
| Name | Type | Description |
|---|---|---|
x |
Tensor
|
Constrained variable with lower and upper bound |
Source code in src/elicito/utils.py
logit #
Implement the logit transformation for :math:u \in (0,1):
.. math::
logit(u) = \log\left(\frac{u}{1-u}\right)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u
|
Tensor
|
Variable in open unit interval. |
required |
Returns:
| Type | Description |
|---|---|
v
|
Log-odds of u. |
Source code in src/elicito/utils.py
LowerBound #
constrain lower-bounded distributions
Methods:
| Name | Description |
|---|---|
__init__ |
Transform |
forward |
Transform |
inverse |
Apply softplus to unconstrained y to get |
Source code in src/elicito/utils.py
__init__ #
__init__(lower: float)
Transform lower bound variable to unconstrained variable Y
use inverse-softplus transform.
References
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lower
|
float
|
Lower bound of variable X. |
required |
Source code in src/elicito/utils.py
forward #
Transform lower-bounded x via inverse-softplus into an unconstrained y.
.. math::
Y = softplus^{-1}(X - lower)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Variable with a lower bound. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
y |
Any
|
Unconstrained variable. |
Source code in src/elicito/utils.py
inverse #
inverse(y: float) -> Tensor
Apply softplus to unconstrained y to get lower-bounded x
.. math::
X = softplus(Y) + lower
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
float
|
Unconstrained variable. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
x |
Tensor
|
Variable with a lower bound. |
Source code in src/elicito/utils.py
UpperBound #
transform upper bounded distribution
Methods:
| Name | Description |
|---|---|
__init__ |
Transform |
forward |
Transform upper-bouned into unconstarined variable |
inverse |
Transform uncstrained into lower-bounded variable |
Source code in src/elicito/utils.py
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 | |
__init__ #
__init__(upper: float)
Transform upper bounded x into unconstrained y
use inverse-softplus transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
upper
|
float
|
Upper bound of variable X. |
required |
References
Source code in src/elicito/utils.py
forward #
Transform upper-bouned into unconstarined variable
use inverse-softplus transform
.. math::
Y = softplus^{-1}(upper - X)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Variable with an upper bound. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
y |
Any
|
Unconstrained variable. |
Source code in src/elicito/utils.py
inverse #
inverse(y: float) -> Tensor
Transform uncstrained into lower-bounded variable
use softplus transform
.. math::
X = upper - softplus(Y)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
float
|
Unconstrained variable. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
x |
Tensor
|
Variable with an upper bound. |
Source code in src/elicito/utils.py
compute_num_weights #
Compute number of weights of a tf.keras model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_NN_weights
|
list[TensorShape]
|
list of tf.TensorShape objects of each layer in the model. |
required |
Returns:
| Type | Description |
|---|---|
int
|
number of weights of the model (incl. biases) |
Source code in src/elicito/utils.py
dry_run #
dry_run(
model: dict[str, Any],
parameters: list[Parameter],
targets: list[Target],
trainer: Trainer,
initializer: Initializer,
network: Optional[NFDict],
) -> tuple[
dict[Any, Any],
Tensor,
dict[Any, Any],
dict[Any, Any],
Any,
]
Run generative model in forward mode for a single epoch
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
dict[str, Any]
|
User-input from |
required |
parameters
|
list[Parameter]
|
User-input from |
required |
targets
|
list[Target]
|
User-input from |
required |
trainer
|
Trainer
|
User-input from |
required |
initializer
|
Initializer
|
User-input from |
required |
network
|
Optional[NFDict]
|
User-input from one of the methods implemented in the
|
required |
Returns:
| Type | Description |
|---|---|
tuple[dict[Any, Any], Tensor, dict[Any, Any], dict[Any, Any], Any]
|
(elicited_statistics, prior_samples, model_simulations, target_quantities, prior_model) |
Source code in src/elicito/utils.py
810 811 812 813 814 815 816 817 818 819 820 821 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 | |
get_expert_data #
get_expert_data(
trainer: Trainer,
model: dict[str, Any],
targets: list[Target],
expert: ExpertDict,
parameters: list[Parameter],
network: Optional[NFDict],
seed: int,
) -> tuple[Any, ...]
Load the training data
data can be expert data or data simulations using a pre-defined ground truth.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trainer
|
Trainer
|
Specification of training settings and meta-information for workflow |
required |
model
|
dict[str, Any]
|
Specification of generative model |
required |
targets
|
list[Target]
|
List of target quantities |
required |
expert
|
ExpertDict
|
Provide input data from expert or simulate data from oracle with
either the |
required |
parameters
|
list[Parameter]
|
List of model parameters specified with :func: |
required |
network
|
Optional[NFDict]
|
Specification of neural network
Only required for |
required |
seed
|
int
|
Internal seed for reproducible results |
required |
Returns:
| Name | Type | Description |
|---|---|---|
expert_data |
Any
|
dictionary containing the training data. Must have same form as the
model-simulated elicited statistics. Correct specification of
keys can be checked using :func: |
expert_prior |
...
|
samples from ground truth. Exists only if expert data are simulated
from an oracle. Otherwise this output is |
Source code in src/elicito/utils.py
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 | |
get_expert_datformat #
Inspect which data format for the expert data is expected by the method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
targets
|
list[Target]
|
list of target quantities |
required |
Returns:
| Name | Type | Description |
|---|---|---|
elicit_dict |
dict[str, list[Any]]
|
expected format of expert data. |
Source code in src/elicito/utils.py
gumbel_softmax_trick #
Apply softmax-gumble trick
The softmax-gumbel trick computes a continuous approximation of ypred from a discrete likelihood and thus allows for the computation of gradients for discrete random variables.
Currently, this approach is only implemented for models without upper boundary (e.g., Poisson model).
References
- Maddison, C. J., Mnih, A. & Teh, Y. W. The concrete distribution: A continuous relaxation of discrete random variables in International Conference on Learning Representations (2017). https://doi.org/10.48550/arXiv.1611.00712
- Jang, E., Gu, S. & Poole, B. Categorical reparameterization with gumbel-softmax in International Conference on Learning Representations (2017). https://openreview.net/forum?id=rkE3y85ee.
- Joo, W., Kim, D., Shin, S. & Moon, I.-C. Generalized gumbel-softmax gradient estimator for generic discrete random variables. Preprint at https://doi.org/10.48550/arXiv.2003.01847 (2020).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
likelihood
|
Any
|
shape = [B, num_samples, num_obs, 1] likelihood function used in the generative model. Must be a tfp.distributions object. |
required |
upper_thres
|
float
|
upper threshold at which the distribution of the outcome variable is truncated. For double-bounded distribution (e.g. Binomial) this is simply the "total count" information. Lower-bounded distribution (e.g. Poisson) must be truncated to create an artificial double-boundedness. |
required |
temp
|
float
|
temperature hyperparameter of softmax function. A temperature going towards zero yields approximates a categorical distribution, while a temperature >> 0 approximates a continuous distribution. |
1.6
|
Returns:
| Name | Type | Description |
|---|---|---|
ypred |
Any
|
continuously approximated ypred from the discrete likelihood. |
Raise
ValueError
if rank of likelihood is not 4. The shape of the likelihood obj
must have an extra final dimension, i.e., (B, num_samples, num_obs, 1),
for the softmax-gumbel computation. Use for example
tf.expand_dims(mu,-1) for expanding the batch-shape of the
likelihood.
if likelihood is not in tfp.distributions module. The likelihood
must be a tfp.distributions object.
Source code in src/elicito/utils.py
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 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 | |
identity #
load #
Load a saved eliobj from specified path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
str
|
path where |
required |
Returns:
| Name | Type | Description |
|---|---|---|
eliobj |
Any
|
loaded |
Source code in src/elicito/utils.py
one_forward_simulation #
one_forward_simulation(
prior_model: Priors,
model: dict[str, Any],
targets: list[Target],
seed: int,
) -> tuple[
dict[Any, Any], Tensor, dict[Any, Any], dict[Any, Any]
]
Run one forward simulation from prior samples to elicited statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prior_model
|
Priors
|
Initialized prior distributions which can be used for sampling. |
required |
model
|
dict[str, Any]
|
Specification of generative model |
required |
targets
|
list[Target]
|
List of target quantities |
required |
seed
|
int
|
Random seed. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
elicited_statistics |
dict[Any, Any]
|
Dictionary containing the elicited statistics that can be used to compute the loss components |
prior_samples |
Tensor
|
Samples from prior distributions |
model_simulations |
dict[Any, Any]
|
Samples from the generative model (likelihood) given the prior samples for the model parameters |
target_quantities |
dict[Any, Any]
|
Target quantities as a function of the model simulations. |
Source code in src/elicito/utils.py
parallel #
parallel(
runs: int = 4,
cores: Optional[int] = None,
seeds: Optional[list[int]] = None,
) -> Parallel
Specify parallelization
Specification for parallelizing training by running multiple training instances with different seeds simultaneously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
runs
|
int
|
Number of replication. |
4
|
cores
|
Optional[int]
|
Number of cores that should be used. |
None
|
seeds
|
Optional[list[int]]
|
A list of seeds. If |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
parallel_dict |
Parallel
|
dictionary containing the parallelization settings. |
Source code in src/elicito/utils.py
save #
save(
eliobj: Any,
name: Optional[str] = None,
file: Optional[str] = None,
overwrite: bool = False,
) -> None
Save the eliobj as pickle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
eliobj
|
Any
|
Instance of the :func: |
required |
name
|
Optional[str]
|
Name of the saved .pkl file. File is saved as .results/{method}/{name}_{seed}.pkl |
None
|
file
|
Optional[str]
|
Path to file, including file name, e.g. file="res" (saved as res.pkl) or file="method1/res" (saved as method1/res.pkl) |
None
|
overwrite
|
bool
|
Whether to overwrite existing file. |
False
|
Source code in src/elicito/utils.py
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 | |
save_as_pkl #
Save file as pickle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Variable that needs to be saved. |
required |
save_dir
|
str
|
Path indicating the file location. |
required |
Examples: