LocScalePTM#
- class liesel_ptm.LocScalePTM(response, knots, intercepts='pseudo_sample', loc_intercept_inference=None, scale_intercept_inference=None, centered=False, scaled=False, trafo_lambda=0.1, trafo_target_slope='identity', bspline='ptm', to_float32=True)[source]#
Bases:
object
A Penalized Transformation Model for Location and Scale.
- Parameters:
response (
Any
|Series
) – Array of response values.knots (
Any
) – Array of equidistant knots. Should correspond to the chosen bspline variant. SeePTMKnots
andOnionKnots
.intercepts (
Literal
['compute'
,'pseudo_sample'
,'sample'
,'constant'
,'sample_mh'
]) –How to handle intercepts in the location and scale model parts. The options are:
”compute”: Intercepts are assumed constant and re-computed any time a value in the location or scale model part changes.
”pseudo_sample”: Intercepts are assumed constant and re-computed once in every MCMC iteration given the current values of the location and scale model parts.
”sample”: Intercepts are treated as ordinary parameters and sampled according to their inference specification in the arguments
loc_intercept_inference
andscale_intercept_inference
. This can lead to identification issues.”constant”: Intercepts are kept constant.
”sample_mh”: Intercepts are sampled with bespoke Metropolis-Hastings proposals. Experimental, undocumented. (default:
'pseudo_sample'
)
loc_intercept_inference (
Any
) –liesel.goose.MCMCSpec
objects that define MCMC inference for intercepts, ifintercepts="sample"
. (default:None
)scale_intercept_inference (
Any
) –liesel.goose.MCMCSpec
objects that define MCMC inference for intercepts, ifintercepts="sample"
. (default:None
)centered (
bool
) – Whether the transformation distribution should be centered and scaled to negate any side-effects the transformation might have on the location and scale of the response distribution. Can be used withintercepts="sample"
. See alsoTransformationDist
. (default:False
)scaled (
bool
) – Whether the transformation distribution should be centered and scaled to negate any side-effects the transformation might have on the location and scale of the response distribution. Can be used withintercepts="sample"
. See alsoTransformationDist
. (default:False
)trafo_lambda (
float
) – Parameter controlling the sharpness of transition to tail extrapolation. Is used to computetransition_width = eps * (knots[3] - knots[-4])
, wheretransition_width
indicates the width of the transition interval. Relevant only forbspline="ptm"
. (default:0.1
)trafo_target_slope (
Literal
['identity'
,'continue_linearly'
]) – If “continue_linearly”, there is no transition to the identity function. Instead, the spline will continue linearly in the tails with the slope fixed to the slopes at the boundaries of the core interval for left and right extrapolation, respectively. Relevant only forbspline="ptm"
. (default:'identity'
)bspline (
Literal
['ptm'
,'onion'
,'identity'
]) – Which B-spline formulation to use. The option"onion"
is experimental. (default:'ptm'
)to_float32 (
bool
) – Whether to convert appropriate values in the model to 32-bit floats. (default:True
)
- intercepts#
How to handle intercepts in the location and scale model parts.
- knots#
Array of knots.
- response#
Response variable, an instance of
liesel.model.Var
.
- graph#
The model graph, an instance of
liesel.model.Model
. Only available afterbuild()
has been called.
- to_float32#
Whether to convert appropriate values in the model to 32-bit floats.
- interface#
An instance of
liesel.goose.LieselInterface
representinggraph
. Only available afterbuild()
has been called.
- is_initialized#
Boolean, indicating whether the model as been initialized with posterior modes.
Examples
A basic unconditional model:
import liesel_ptm as ptm import jax y = jax.random.normal(jax.random.key(0), (50,)) model = ptm.LocScalePTM.new_ptm(y, a=-4.0, b=4.0, nparam=20) results = model.run_mcmc(seed=1, warmup=300, posterior=500) samples = results.get_posterior_samples() model.plot(samples) dist = model.init_dist(samples) # initialize a distribution object
A basic linear location-scale model:
import jax import liesel_ptm as ptm from liesel_ptm import lin, term y = jax.random.normal(jax.random.key(0), (50,)) x = jax.random.uniform(jax.random.key(1), (50,)) model = ptm.LocScalePTM.new_ptm(y, a=-4.0, b=4.0, nparam=20) # location and scale predictors can be filled by adding terms. xlin = lin(x, xname="x") model.loc += term.f(xlin, fname="s") # when adding terms to the scale model part, they are applied additively # to the log-level automatically model.scale += term.f(xlin, fname="g") results = model.run_mcmc(seed=1, warmup=300, posterior=500) samples = results.get_posterior_samples() model.plot(samples)
A basic model with one P-spline:
import jax import liesel_ptm as ptm from liesel_ptm import term, ps y = jax.random.normal(jax.random.key(0), (50,)) x = jax.random.uniform(jax.random.key(1), (50,)) model = ptm.LocScalePTM.new_ptm(y, a=-4.0, b=4.0, nparam=20) xps = ps(x, nbases=20, xname="x") model.loc += term.f_ig(xps, fname="s") results = model.run_mcmc(seed=1, warmup=300, posterior=500) samples = results.get_posterior_samples() model.plot(samples)
Methods
Build the model graph.
Construct a batched distribution from posterior samples.
Two-stage initialization that fits loc-scale, then transformation.
Shortcut for initializing a Gaussian model.
Shortcut for convenient model setup.
Optimize model parameters using the selected optimizer.
Produce a set of diagnostic plots (qq, trafo, pdf, cdf).
Produce a QQ-plot comparing transformed r and its Gaussian reference.
Plot the posterior CDF of the transformed variable r.
Plot the posterior density of the transformed variable r.
Plot the posterior mean and credible bands of the transformation h(r).
Run MCMC sampling and return sampling results.
Configure default MCMC kernels for model parts.
Logs the current MCMC configuration. If you do not see any output, you need to set up logging::.
Return summary arrays (z, prob, log_prob, cdf) for a grid of values.
Summarise transformation samples on a grid.
Attributes
Whether the model is Gaussian.
Location predictor
If True (default), intercepts are sampled under the assumption of an identity transformation.
Location predictor
Predictor for the transformation function.