litmus.models
Contains NumPyro generative models.
HM 24
stats_model(prior_ranges=None, out_stream=sys.stdout, err_stream=sys.stderr, verbose=True, debug=False, warn=1)
Bases: logger
Base class for bayesian generative models. Includes a series of utilities for evaluating likelihoods, gradients etc., as well as various
On init, takes dict `prior_ranges' of the uniform boundaries of the parameter priors, or a single (float/int) value if the value is fixed.
Also takes logging arg from the litmus.logging.logger object.
Source code in litmus/models.py
prior_ranges: dict[str, list[float, float]] = {} | self._default_prior_ranges
instance-attribute
Keyed dict like {key: [max,min] } of bounds for parameter uniform priors
prior_volume = 1.0
instance-attribute
Volume of the prior, i.e. prod(max_i-min_i) for in in params
name = type(self).__name__
instance-attribute
Name of the model for print strings
set_priors(prior_ranges: dict) -> None
Sets the stats model prior ranges for uniform priors. Does some sanity checking to avoid negative priors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prior_ranges
|
dict
|
keyed dict of {key: [minval, maxval]} for ranges or {key: fixedval} for fixed values |
required |
Source code in litmus/models.py
prior() -> [float]
A NumPyro callable prior.
Returns:
Type | Description |
---|---|
[float]
|
Values of the parameters as sampled from the prior |
model_function(data)
A NumPyro callable function. Does not return
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Data to condition the model on |
required |
lc_to_data(lc_1: lightcurve, lc_2: lightcurve) -> dict
Converts light-curves into the data format required for the model. For most models this will return as some sort of sorted dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lc_1
|
lightcurve
|
First lightcurve object |
required |
lc_2
|
lightcurve
|
Second lightcurve object |
required |
Returns:
Type | Description |
---|---|
dict
|
Varies from model to model, by default will be a keyed dict: {'T': Time values of observations series, 'Y': Signal strength values of observations series, 'E': Uncertainty values of values in Y, 'bands': int array identifying which lightcurve (0,1) that the observations belong to } |
Source code in litmus/models.py
to_uncon(params) -> dict[str, float]
Converts model parametes from "real" constrained domain values into HMC friendly unconstrained values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
keyed dict of parameters in constrained domain |
required |
Returns:
Type | Description |
---|---|
dict[str, float]
|
keyed dict of parameters in unconstrained domain |
Source code in litmus/models.py
to_con(params) -> dict[str, float]
Converts model parametes back into "real" constrained domain values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
keyed dict of parameters in unconstrained domain |
required |
Returns:
Type | Description |
---|---|
dict[str, float]
|
keyed dict of parameters in constrained domain |
Source code in litmus/models.py
uncon_grad(params) -> float
Evaluates the log of det(Jac) by evaluating pi(x) and pi'(x'). Used for correcting integral elements between constrained and unconstrained space
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
Model parameters in constrained domain |
required |
Returns:
Type | Description |
---|---|
float
|
float of det(Jacobian) |
Source code in litmus/models.py
uncon_grad_lag(params) -> float
Returns the log-jacobian correction for the constrained / unconstrained correction for the lag parameter Assumes a uniform distribution for the lag prior
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
Model parameters in constrained domain |
required |
Returns:
Type | Description |
---|---|
float
|
float of det(Jacobian) for lag_uncon <-> lag_con |
Source code in litmus/models.py
paramnames() -> [str]
Returns the names of all model parameters. Purely for brevity of code.
Returns:
Type | Description |
---|---|
[str]
|
list of param names in order listed in prior_ranges |
fixed_params() -> [str]
Returns the names of all fixed model parameters. Purely for brevity.
Returns:
Type | Description |
---|---|
[str]
|
list of param names in order listed in prior_ranges |
Source code in litmus/models.py
free_params() -> [str]
Returns the names of all free model parameters. Purely for brevity of code.
Returns:
Type | Description |
---|---|
[str]
|
list of param names in order listed in prior_ranges |
Source code in litmus/models.py
dim() -> int
Quick and easy call for the number of model parameters.
Returns:
Type | Description |
---|---|
int
|
number of model parameters as int |
log_density(params, data, use_vmap=False) -> _types.ArrayN
Returns the log density of the joint distribution at some constrained space position 'params' and conditioned on some 'data'. data must match the output of the model's lc_to_data(), and params is either a keyed dict of parameter values or a key dict of arrays of values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
model params in constrained space as keyed dict |
required | |
data
|
data to condition the model on |
required | |
use_vmap
|
whether to use vmap instead of unconstrained space (not implemented) |
False
|
Returns:
Type | Description |
---|---|
ArrayN
|
Returns as array of floats |
Source code in litmus/models.py
log_likelihood(params, data, use_vmap=False) -> _types.ArrayN
Returns the log likelihood at some constrained space position 'params' and conditioned on some 'data'. data must match the output of the model's lc_to_data(), and params is either a keyed dict of parameter values or a key dict of arrays of values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
model params in constrained space as keyed dict |
required | |
data
|
data to condition the model on |
required | |
use_vmap
|
whether to use vmap instead of unconstrained space (not implemented) |
False
|
Returns:
Type | Description |
---|---|
ArrayN
|
Returns as array of floats |
Source code in litmus/models.py
log_density_uncon(params, data, use_vmap=False) -> _types.ArrayN
Returns the log density of the joint distribution at some unconstrained space position 'params' and conditioned on some 'data'. data must match the output of the model's lc_to_data(), and params is either a keyed dict of parameter values or a key dict of arrays of values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
model params in constrained space as keyed dict |
required | |
data
|
data to condition the model on |
required | |
use_vmap
|
whether to use vmap instead of unconstrained space (not implemented) |
False
|
Returns:
Type | Description |
---|---|
ArrayN
|
Returns as array of floats |
Source code in litmus/models.py
log_prior(params, data=None, use_vmap=False) -> _types.ArrayN
Returns the log density of the prior at some constrained space position 'params' Params is either a keyed dict of parameter values or a key dict of arrays of values. Returns as array of floats use_vmap currently not implemented with no side effect
Source code in litmus/models.py
log_density_grad(params, data, use_vmap=False, keys=None) -> dict[str, float]
Returns the gradient of the log density of the joint distribution at some constrained space position 'params', conditionded on some 'data' matching the format of the model's lc_to_data() output.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
model params in constrained space as keyed dict |
required | |
data
|
data to condition the model on |
required | |
use_vmap
|
whether to use vmap instead of unconstrained space (not implemented) |
False
|
|
keys
|
list of keys / parameters to take the gradient over |
None
|
Returns:
Type | Description |
---|---|
dict[str, float]
|
Returns as keyed dict of grads along each axsi or keyed dict of array of similar values |
Source code in litmus/models.py
log_density_uncon_grad(params, data, use_vmap=False, keys=None, asdict=False) -> float
Returns the gradient of the log density of the joint distribution at some unconstrained space position 'params', conditionded on some 'data' matching the format of the model's lc_to_data() output.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
model params in constrained space as keyed dict |
required | |
data
|
data to condition the model on |
required | |
use_vmap
|
whether to use vmap instead of unconstrained space (not implemented) |
False
|
|
keys
|
list of keys / parameters to take the gradient over |
None
|
Returns:
Type | Description |
---|---|
float
|
Returns as keyed dict of grads along each axsi or keyed dict of array of similar values |
Source code in litmus/models.py
log_prior_grad(params, data=None, use_vmap=False, keys=None) -> dict[str, float]
Returns the gradient of the log prior of the prior at some constrained space position 'params' Params is either a keyed dict of parameter values or a key dict of arrays of values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
model params in constrained space as keyed dict |
required | |
data
|
data to condition the model on |
None
|
|
use_vmap
|
whether to use vmap instead of unconstrained space (not implemented) |
False
|
|
keys
|
list of keys / parameters to take the gradient over |
None
|
Returns:
Type | Description |
---|---|
dict[str, float]
|
Returns as keyed dict of grads along each axsi or keyed dict of array of similar values |
Source code in litmus/models.py
log_density_hess(params, data, use_vmap=False, keys=None) -> _types.ArrayNxMxM
Returns the hessian matrix of the log joint distribution at some constrained space position 'params', conditioned on some 'data' matching the output of the model's lc_to_data() output.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
model params in constrained space as keyed dict |
required | |
data
|
data to condition the model on |
required | |
use_vmap
|
whether to use vmap instead of unconstrained space (not implemented) |
False
|
|
keys
|
The params to slice and sort the hessian matrices. |
None
|
Returns:
Type | Description |
---|---|
ArrayNxMxM
|
Returns in order / dimension: [num param sites, num keys, num keys] |
Source code in litmus/models.py
log_density_uncon_hess(params, data, use_vmap=False, keys=None) -> _types.ArrayNxMxM
Returns the hessian matrix of the log joint distribution at some unconstrained space position 'params', conditioned on some 'data' matching the output of the model's lc_to_data() output.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
model params in constrained space as keyed dict |
required | |
data
|
data to condition the model on |
required | |
use_vmap
|
whether to use vmap instead of unconstrained space (not implemented) |
False
|
|
keys
|
The params to slice and sort the hessian matrices. |
None
|
Returns:
Type | Description |
---|---|
ArrayNxMxM
|
Returns in order / dimension: [num param sites, num keys, num keys] |
Source code in litmus/models.py
log_prior_hess(params, data=None, use_vmap=False, keys=None) -> _types.ArrayNxMxM
Returns the hessian matrix of the log prior of the prior at some constrained space position 'params' Params is either a keyed dict of parameter values or a key dict of arrays of values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
model params in constrained space as keyed dict |
required | |
data
|
data to condition the model on |
None
|
|
use_vmap
|
whether to use vmap instead of unconstrained space (not implemented) |
False
|
|
keys
|
The params to slice and sort the hessian matrices. |
None
|
Returns:
Type | Description |
---|---|
ArrayNxMxM
|
Returns in order / dimension: [num param sites, num keys, num keys] |
Source code in litmus/models.py
scan(start_params: dict[str:float], data: _types.Any, optim_params: [str] = None, use_vmap: bool = False, optim_kwargs: dict[str:float] = {}, precondition: _types.Literal[cholesky, eig, half - eig, diag, none] = 'diag', solver: _types.Literal[BFGS, GradientDescent] = 'BFGS') -> dict[str, float]
Beginning at position 'start_params', optimize parameters in 'optim_params' to find maximum. optim_kwargs will overwrite defaults and be passed directly to jaxopt.BFGS object
Currently using jaxopt with optim_kwargs: 'stepsize': 0.0, 'min_stepsize': 1E-5, 'increase_factor': 1.2, 'maxiter': 256, 'linesearch': 'backtracking', 'verbose': False,
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start_params
|
dict[str:float]
|
Position in constrained parameter spac to begin the search at |
required |
data
|
Any
|
data to condition the model on |
required |
optim_params
|
[str]
|
parameters to optimize over |
None
|
use_vmap
|
bool
|
Whether to use jax vmapping over multiple start_params (not implemented) |
False
|
optim_kwargs
|
dict[str:float]
|
kwargs for the jaxopt optimiser |
{}
|
precondition
|
Literal[cholesky, eig, half - eig, diag, none]
|
Type of preconditioning to use in optimisation. Should be "cholesky", "eig", "half-eig", "diag" or "none" |
'diag'
|
solver
|
Literal[BFGS, GradientDescent]
|
Type of jaxopt solver to use. Defaults to "BFGS". Allowed solvers are "BFGS" or "GradientDescent". |
'BFGS'
|
Returns:
Type | Description |
---|---|
dict[str, float]
|
Keyed dict of optimised params |
Source code in litmus/models.py
807 808 809 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 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 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 |
|
laplace_log_evidence(params, data, integrate_axes=None, use_vmap=False, constrained=False) -> float
At some point 'params' in parameter space, gets the hessian in unconstrained space and uses to estimate the model evidence
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
Keyed dict with params in constrained / unconstrained parameter space |
required | |
data
|
data to condition the model on |
required | |
integrate_axes
|
Which axes to perform laplace approx for. If none, use all |
None
|
|
use_vmap
|
Whether to use jax vmapping over different params. (Not implemented) |
False
|
|
constrained
|
If true, perform laplace approx in constrained domain. Default to false |
False
|
Returns:
Type | Description |
---|---|
float
|
laplace log evidence as float or array of floats |
Source code in litmus/models.py
laplace_log_info(params, data, integrate_axes=None, use_vmap=False, constrained=False)
At some point 'params' in parameter space, gets the hessian in unconstrained space and uses to estimate the model information relative to the prior
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
site(s) to evaluate info at in the constrained domain |
required | |
data
|
data to condition the model on |
required | |
integrate_axes
|
free axes to perform laplace integral over. If none, use all |
None
|
|
use_vmap
|
Whether to use jax vmapping over different params. (Not implemented) |
False
|
|
constrained
|
If true perform laplace approx in the constrained domain |
False
|
Returns:
Type | Description |
---|---|
Laplace log info evaluated at params |
Source code in litmus/models.py
opt_tol(params: dict[str, float], data: _types.Any, integrate_axes: [str] = None, use_vmap: bool = False, constrained: bool = False) -> float
Estimates the number of standard deviations away from the peak that a point is. Only works if you're nearby the local peak.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
dict[str, float]
|
site(s) to evaluate info at in the constrained domain |
required |
data
|
Any
|
data to condition the model on |
required |
integrate_axes
|
[str]
|
free axes to perform laplace approx for. If none, use all |
None
|
use_vmap
|
bool
|
Whether to use jax vmapping over different params. (Not implemented) |
False
|
constrained
|
bool
|
If true, calculate distance in constrained domain approx in constrained domain. Default to false |
False
|
Returns:
Type | Description |
---|---|
float
|
Number of "standard deviations" away from local peak |
Source code in litmus/models.py
prior_sample(num_samples: int = 1, seed: int = None) -> dict
Blind sampling from the prior without conditioning. Returns model parameters only
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_samples
|
int
|
Number of realizations to generate |
1
|
seed
|
int
|
seed for random generation |
None
|
Returns:
Type | Description |
---|---|
dict
|
keyed dict of parameters drawn from prior |
Source code in litmus/models.py
realization(data=None, num_samples: int = 1, seed: int = None)
Generates realizations of the observables by blindly sampling from the prior
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
data to condition the lightcurve on |
None
|
|
num_samples
|
int
|
Number of realizations to generate |
1
|
seed
|
int
|
seed for random generation |
None
|
Returns:
Type | Description |
---|---|
keyed dict of the model observables |
Source code in litmus/models.py
make_lightcurves(data, params: dict, Tpred, num_samples: int = 1) -> (lightcurve, lightcurve)
Returns lightcurves at time 'T' for 'parameters' conditioned on 'data' over num_samples
draws from `params'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Data to condition the model on |
required | |
params
|
dict
|
keyed dictionary of parameters |
required |
Tpred
|
Array of time values to predict the lightcurve at |
required | |
num_samples
|
int
|
number of samples to draw from params use in integration to get covar / mu |
1
|
Returns:
Type | Description |
---|---|
(lightcurve, lightcurve)
|
tuple of mean and covariance of LC's (loc_1, loc_2, covar_1, covar_2) |
Source code in litmus/models.py
params_inprior(params) -> bool
Utility to check if model params fall within the uniform prior bounds
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
constrained space params to check validity of |
required |
Returns:
Type | Description |
---|---|
bool
|
True if site falls within prior boundaries, false if not |
Source code in litmus/models.py
find_seed(data, guesses=None, fixed={}) -> (dict, float)
Find a good initial seed. Unless otherwise over-written, while blindly sample the prior and return the best fit.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
data to condition the model on |
required | |
guesses
|
number of evals to use for finding the seed |
None
|
|
fixed
|
keyed dict of parameters to be fixed instead of estimating a seed |
{}
|
Returns:
Type | Description |
---|---|
(dict, float)
|
tuple of dict of seed params and log density at this position |
Source code in litmus/models.py
GP_simple(prior_ranges=None, **kwargs)
Bases: stats_model
An example of how to construct your own stats_model in the simplest form. Requirements are to: 1. Set a default prior range for all parameters used in model_function 2. Define a numpyro generative model model_function You can add / adjust methods as required, but these are the only main steps
Source code in litmus/models.py
basekernel: tinygp.kernels.quasisep = kwargs['basekernel'] if 'basekernel' in kwargs.keys() else tinygp.kernels.quasisep.Exp
instance-attribute
The gaussian kernel
prior() -> list[float, float, float, float, float, float]
Source code in litmus/models.py
model_function(data) -> None
Source code in litmus/models.py
find_seed(data, guesses=None, fixed={}) -> (float, dict[str, float])
Source code in litmus/models.py
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 |
|
GP_simple_null(prior_ranges=None, **kwargs)
Bases: GP_simple
A variant of GP_simple for uncoupled gaussian processes, equivalent to lag->infty in GP_simple. Used for null hypothesis testing through model comparison.
Source code in litmus/models.py
lc_to_data(lc_1: lightcurve, lc_2: lightcurve) -> dict
model_function(data) -> None
Source code in litmus/models.py
whitenoise_null(prior_ranges=None, **kwargs)
Bases: GP_simple_null
Source code in litmus/models.py
model_function(data) -> None
Source code in litmus/models.py
GP_simple_normalprior(prior_ranges=None, **kwargs)
Bases: GP_simple