fit.posterior¶
The posterior of the parameters of a fit: the Laplace approximation.
The Laplace approximation of the posterior is the multivariate normal around
the estimate of a fit whose precision is the curvature of the negative log
posterior at the estimate, in the space the optimizer searches, the
parameter_scale of the fit (Tierney & Kadane 1986; Gelman et al. 2013, ch.
4.1):
precision = J' J / σ² + H_prior, covariance = precision⁻¹
with the jacobian J of the residuals of the data at the estimate, the
Gauss-Newton approximation of the curvature of the negative log-likelihood
(exact for normal noise of a known scale and a model linear in the
parameters; an approximation for a scale of the noise which is estimated and
for Laplace noise), see sbmlsim.fit.fisher, and the Hessian of the negative
log density of the priors H_prior, by central differences in the space of
the optimizer. What the curvature contains is what the fit minimized, its
ObjectiveType:
posterior: the likelihood and the priors, around the maximum a posteriori estimate;likelihood: the likelihood with the uniform priors of the bounds, which have no curvature; priors of the parameters are no part of the objective and are left out;least_squares: the weighted least squares as the likelihood of normal noise of the scaleσ² = 2 cost / (n - k), the posterior of the uniform priors, whose covariance is the covariance of the Fisher information.
On a logarithmic scale of the optimizer the normal of the logarithms is a log-normal of the values. The priors are the densities of the values, as the fit evaluated them, without the jacobian of the scale.
A direction which neither the data nor the priors constrain has no curvature,
the posterior is the uniform prior of the bounds there, which a normal does
not describe: posterior_laplace raises and names the parameters of such a
direction. It warns where the approximation is questionable: a parameter at
a bound, where the posterior is cut off, and a posterior whose interval
reaches past a bound. These are the problems of a sampler of the posterior
(#374).
A ParameterPosterior is stored as JSON (posterior.json next to the records
of a fit, see sbmlsim.run), so an analysis of the posterior does not run the
fit, and is the source of the parameters of a propagation
(ParameterPosterior.uncertainty_model), of the designs of
sbmlsim.simulation.sampling (posterior_parameters, or distributions and
rank_correlation for lhs and random) and of a sensitivity analysis.
Tierney L, Kadane JB (1986). Accurate approximations for posterior moments and marginal densities. J Am Stat Assoc 81:82-86. Gelman A, Carlin JB, Stern HS, Dunson DB, Vehtari A, Rubin DB (2013). Bayesian Data Analysis, 3rd ed. CRC Press.
ParameterPosterior
dataclass
¶
ParameterPosterior(
opid,
sid,
pids,
values,
scales,
covariance,
units,
targets,
lower_bounds,
upper_bounds,
objective,
method="laplace",
n=0,
sigma2=1.0,
step=0.0,
problems=list(),
)
The posterior of the parameters of a fit, a normal in the space of its optimizer.
Attributes:
| Name | Type | Description |
|---|---|---|
opid |
str
|
id of the optimization problem. |
sid |
str
|
id of the parameter set it is centered on. |
pids |
list[str]
|
the parameters, in the order of the matrix. |
values |
ndarray
|
the values of the parameters at the center, in the units of the model. |
scales |
list[ParameterScaleType]
|
the space of every parameter, in which the posterior is normal; a logarithmic scale is a log-normal of the values. |
covariance |
ndarray
|
the covariance in the space of the parameters. |
units |
list[str | None]
|
the unit of every parameter, a unit of pint with the unit ids
of the model resolved, |
targets |
list[str]
|
the target of the model of every parameter
( |
lower_bounds |
ndarray
|
the lower bounds of the fit. |
upper_bounds |
ndarray
|
the upper bounds of the fit. |
objective |
ObjectiveType
|
what the fit minimized, see the module. |
method |
str
|
how the posterior was computed, |
n |
int
|
the number of data points. |
sigma2 |
float
|
the variance of the residuals the curvature of the data was divided by, 1 for a likelihood. |
step |
float
|
the relative step of the jacobian of the data. |
problems |
list[str]
|
where the approximation is questionable, see the module. |
standard_deviations
property
¶
Get the standard deviation of every parameter in its space.
correlation_matrix
property
¶
Get the correlation of the parameters, of their normal scores.
summary_df
property
¶
Get the parameters with their standard deviations and 95 % intervals.
intervals
¶
Get the central credible interval of every parameter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
float
|
the probability of the interval. |
0.95
|
Returns:
| Type | Description |
|---|---|
ndarray
|
The lower and the upper ends in the units of the model, the |
ndarray
|
quantiles of the normal in the space of every parameter. |
distributions
¶
Get the marginals of the posterior as distributions of their targets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
targets
|
Mapping[str, str] | None
|
pid -> the target of the model, overrides |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Distribution]
|
target -> its marginal: a normal of the value on the linear scale, |
dict[str, Distribution]
|
a log-normal of the value on a logarithmic one, in the unit of the |
dict[str, Distribution]
|
parameter; with |
dict[str, Distribution]
|
copula of these marginals is the normal of the space of the |
dict[str, Distribution]
|
parameters). |
Raises:
| Type | Description |
|---|---|
ValueError
|
for two parameters of one target. |
normal_correlation
¶
Get the correlation of the normal scores of the posterior, by the targets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
targets
|
Mapping[str, str] | None
|
pid -> the target of the model, overrides |
None
|
Returns:
| Type | Description |
|---|---|
Covariance
|
The correlation, which with the marginals of |
Covariance
|
the posterior, see |
rank_correlation
¶
Get the Spearman rank correlation of the parameters.
The rank correlation of the normal of the space of the parameters,
6 / π asin(r / 2) (Pearson 1907), which the designs lhs and
random of sbmlsim.simulation.sampling take as correlation and
convert back into this correlation of the normal scores.
Returns:
| Type | Description |
|---|---|
ndarray
|
The matrix in the order of the parameters. |
uncertainty_model
¶
Get the posterior as the uncertainty of the parameters of a propagation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
targets
|
Mapping[str, str] | None
|
pid -> the target of the model, overrides |
None
|
kwargs
|
Any
|
the other fields of the |
{}
|
Returns:
| Type | Description |
|---|---|
UncertaintyModel
|
The model with the marginals of |
UncertaintyModel
|
of |
UncertaintyModel
|
of the parameters. |
write
¶
Write the posterior as JSON, atomically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
the file, or a directory for its |
required |
Returns:
| Type | Description |
|---|---|
Path
|
The file. |
read
classmethod
¶
Read a posterior written by write.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
the file, or a directory with its |
required |
Returns:
| Type | Description |
|---|---|
ParameterPosterior
|
The posterior. |
prior_hessian
¶
Get the Hessian of the negative log density of a prior in the space of the optimizer.
Central differences of -log p(x(z)) with the step step * max(|z|, 1);
a stencil which would leave the bounds is moved inside them, so the
Hessian of a parameter at its bound is the one next to it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_density
|
Callable[[ndarray], float]
|
the log density of the prior, of the values on the linear scale. |
required |
z
|
ndarray
|
the parameters in the space of the optimizer. |
required |
to_linear
|
Callable[[ndarray], ndarray]
|
the transformation of the space of the optimizer to the linear scale. |
required |
lower
|
ndarray
|
the lower bounds in the space of the optimizer. |
required |
upper
|
ndarray
|
the upper bounds in the space of the optimizer. |
required |
step
|
float
|
the relative step. |
PRIOR_STEP
|
Returns:
| Type | Description |
|---|---|
ndarray
|
The symmetric Hessian. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the log density is not finite at the stencil. |
posterior_laplace
¶
posterior_laplace(
problem,
settings,
parameter_set,
*,
fisher=None,
step=None,
rank_tolerance=DEFAULT_RANK_TOLERANCE,
)
Get the Laplace approximation of the posterior of the parameters of a fit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
problem
|
OptimizationProblem
|
optimization problem, initialized with the settings. |
required |
settings
|
FitSettings
|
settings the parameters were fitted with, whose objective decides what the posterior contains, see the module. |
required |
parameter_set
|
ParameterSet
|
the estimate of the fit, the center of the posterior. |
required |
fisher
|
FisherInformation | None
|
the Fisher information of the estimate on the problem, which is computed without; its curvature of the data is reused. |
None
|
step
|
float | None
|
relative step of the jacobian of the data, see
|
None
|
rank_tolerance
|
float
|
a precision whose smallest eigenvalue is below this fraction of its largest one constrains a direction too little for a normal, see the module. |
DEFAULT_RANK_TOLERANCE
|
Returns:
| Type | Description |
|---|---|
ParameterPosterior
|
The posterior, a normal in the space of the optimizer. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the Fisher information is not the one of the parameter set, a fit of least squares has no degrees of freedom, or the data and the priors leave a direction unconstrained. |