Statistics¶
These functions take an inference problem and its fit, and report the uncertainty and identifiability of the fit. See statistical estimation and identifiability.
Recommended defaults
After a fit, call information, laplace, identifiability and
profile on the problem and its fit, and predict for bands. Run
sample only after these look sound.
Local analysis¶
diff_soup.inference.information
¶
information(problem: Any, *, at: Any, kind: str = 'observed') -> InformationResult
Assemble the information matrix, the curvature of the negative log likelihood, at a point.
For a MAP problem the result also holds the prior's curvature and their sum, the posterior
precision. A multiple-shooting fit that fits its window starts has them integrated out, so
the matrix covers the parameters alone.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
problem
|
Any
|
An |
required |
at
|
Any
|
The point, an |
required |
kind
|
str
|
|
'observed'
|
Returns:
| Type | Description |
|---|---|
InformationResult
|
An |
diff_soup.inference.laplace
¶
laplace(problem: Any, fit: Any, *, curvature: str = 'observed', on_singular: str = 'raise')
Build the Laplace approximation, the Gaussian centered on a fit with the inverse curvature as covariance.
For a MAP problem the curvature includes the prior's, and log_evidence is the Laplace
log evidence. A multiple-shooting fit that fits its window starts has them integrated out.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
problem
|
Any
|
An |
required |
fit
|
Any
|
The |
required |
curvature
|
str
|
|
'observed'
|
on_singular
|
str
|
What happens when the data leave a direction undetermined, |
'raise'
|
Returns:
| Type | Description |
|---|---|
|
A |
diff_soup.inference.evidence
¶
evidence(problem: Any, fit: Any, *, curvature: str = 'observed') -> EvidenceResult
Approximate the log marginal likelihood of the data at a MAP fit, by Laplace's method.
The value is log p(y | mode) + log p(mode) + d/2 log(2 pi) - 1/2 log det H, with H the
posterior precision of ds.inference.laplace(problem, fit) and the priors the problem
declares. A fit that is not at a stationary point is refused.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
problem
|
Any
|
An |
required |
fit
|
Any
|
The |
required |
curvature
|
str
|
|
'observed'
|
Returns:
| Type | Description |
|---|---|
EvidenceResult
|
An |
diff_soup.inference.identifiability
¶
identifiability(problem: EstimationProblem, fit: OptResult | Mapping, *, parameter_scales: Any = None, ridge: float = 1e-10, null_tol: float = 1e-10) -> IdentifiabilityReport
Find which parameter directions the data determine at a fit, and which they leave undetermined.
The prior and any regularization are left out, so neither can hide a direction the data
leave open. The analysis is reported twice, in the parameters' own units and after scaling
by parameter_scales. A multiple-shooting fit that fits its window starts has them
integrated out.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
problem
|
EstimationProblem
|
An |
required |
fit
|
OptResult | Mapping
|
The |
required |
parameter_scales
|
Any
|
Positive scales per fitted coordinate, as a vector or a mapping from
handle or |
None
|
ridge
|
float
|
A small value added to each eigenvalue before inverting it, for the correlations of the report only. |
1e-10
|
null_tol
|
float
|
The eigenvalue, relative to the largest, at or below which a direction counts as undetermined. |
1e-10
|
Returns:
| Type | Description |
|---|---|
IdentifiabilityReport
|
An |
diff_soup.inference.profile
¶
profile(problem: Any, fit: OptResult, handle: Any, *, owner=None, optimizer=None, levels=(0.95,), bounds=None, max_steps=24, multistart=0, multistart_scale=1.0)
Compute the profile likelihood of one scalar parameter, refitting every other parameter at each value.
Each side of the fit is walked until the profile passes the threshold of the largest level,
a bound, or max_steps. A side that stays below the threshold is an open direction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
problem
|
Any
|
An |
required |
fit
|
OptResult
|
The |
required |
handle
|
Any
|
A scalar handle from |
required |
owner
|
The member whose value is walked, required for a handle that varies by member and refused for a shared one. |
None
|
|
optimizer
|
The optimizer of the refits,
|
None
|
|
levels
|
The confidence levels whose likelihood-ratio thresholds the result uses. |
(0.95,)
|
|
bounds
|
An optional range |
None
|
|
max_steps
|
The most accepted steps on each side. |
24
|
|
multistart
|
The number of extra jittered starts per refit, of which the best is kept. |
0
|
|
multistart_scale
|
The size of the jitter, relative to |
1.0
|
Returns:
| Type | Description |
|---|---|
|
A |
diff_soup.inference.residual_report
¶
residual_report(problem, fit, *, experiment_id=None) -> ResidualReport
Standardize the residuals of each measurement by its likelihood at a fit, and flag departures from the noise model.
Under the declared noise model the standardized residuals have mean zero, standard deviation one, and no lag-1 autocorrelation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
problem
|
An |
required | |
fit
|
An |
required | |
experiment_id
|
The experiment to check, required when the problem has several. |
None
|
Returns:
| Type | Description |
|---|---|
ResidualReport
|
A |
diff_soup.inference.practical_report
¶
practical_report(problem: Any, fit: Any, *, posterior: Any = None, profiles: tuple = (), tolerances: Optional[dict] = None, report: Any = None, experiment_id: Any = None) -> PracticalReport
Collect the evidence on practical identifiability at a fit into one report.
It holds the information spectrum, the supplied profiles and posterior, and the residual report, and names the evidence that was not supplied.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
problem
|
Any
|
An |
required |
fit
|
Any
|
The |
required |
posterior
|
Any
|
The result of |
None
|
profiles
|
tuple
|
Results of |
()
|
tolerances
|
Optional[dict]
|
A mapping from parameter name to the largest acceptable standard error,
which needs |
None
|
report
|
Any
|
The result of |
None
|
experiment_id
|
Any
|
The experiment whose residuals are checked, required when the problem has several. |
None
|
Returns:
| Type | Description |
|---|---|
PracticalReport
|
A |
diff_soup.inference.function_report
¶
function_report(information: Any, block: Any, *, metric: str = 'gram', basis: Any = None, gram: Any = None, nuisance: str = 'schur', scale: str = 'raw', at: Any = None, null_tol: float = 1e-10) -> FunctionReport
Report how well the data determine one function-valued parameter, measured in the function's own norm.
It takes the function's coefficients out of an information matrix and reports the
eigenvalues of their information measured against metric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
information
|
Any
|
The result of |
required |
block
|
Any
|
The name of the function handle, or the column indices of its coefficients. |
required |
metric
|
str
|
|
'gram'
|
basis
|
Any
|
The function's basis, such as a |
None
|
gram
|
Any
|
The Gram matrix of the function's basis. |
None
|
nuisance
|
str
|
|
'schur'
|
scale
|
str
|
|
'raw'
|
at
|
Any
|
The function's coefficient values, needed for |
None
|
null_tol
|
float
|
The eigenvalue, relative to the largest, at or below which a mode counts as undetermined. |
1e-10
|
Returns:
| Type | Description |
|---|---|
FunctionReport
|
A |
diff_soup.inference.cluster_covariance
¶
cluster_covariance(problem: EstimationProblem, fit: OptResult, *, cluster: str, null_tol: float = 1e-10) -> ClusterCovarianceResult
Estimate the cluster-robust sandwich covariance of a fit, with each cluster an independent sampling unit.
Rows within a cluster may be correlated, and the declared noise scale need not be right. For
a MAP fit the result is the spread of the estimator, not a posterior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
problem
|
EstimationProblem
|
The |
required |
fit
|
OptResult
|
The |
required |
cluster
|
str
|
The name of a cluster of the problem's study, with at least two distinct labels. |
required |
null_tol
|
float
|
The curvature, relative to the largest, at or below which a direction counts
as undetermined, which raises |
1e-10
|
Returns:
| Type | Description |
|---|---|
ClusterCovarianceResult
|
A |
Bootstrap¶
diff_soup.inference.bootstrap
¶
bootstrap(problem: Any, fit: Any, *, replicates: int, key: Any, over: Any = None, weights: str = 'bayesian', steps: Any = 3, weight_priors: bool = True, solver: Any = None, max_attempts: Any = None, require_converged: bool = True, checkpoint: Any = None, keep: str = 'last', every: int = 1, on_conflict: str = 'error', callbacks: Any = (), chunk: Any = 'auto', shard: Any = None) -> BootstrapResult
Bootstrap a hierarchical fit by reweighting the members of the study's top level.
Each replicate draws one weight per member and steps from the fit's point under those weights. The top level needs at least three members.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
problem
|
Any
|
The |
required |
fit
|
Any
|
The |
required |
replicates
|
int
|
The number of replicates, a positive integer. |
required |
key
|
Any
|
The JAX random key that fixes the weights, such as |
required |
over
|
Any
|
The level to reweight, by name or as |
None
|
weights
|
str
|
|
'bayesian'
|
steps
|
Any
|
The accepted trust-region steps each replicate takes from the fit, |
3
|
weight_priors
|
bool
|
Whether each member's prior and regularization terms take the member's weight. |
True
|
solver
|
Any
|
The |
None
|
max_attempts
|
Any
|
The step attempts allowed per replicate, or |
None
|
require_converged
|
bool
|
Whether to refuse a fit that did not converge. |
True
|
checkpoint
|
Any
|
A directory to save the run in and resume it from, made when missing, used
when |
None
|
keep
|
str
|
|
'last'
|
every
|
int
|
The number of iterations between two checkpoint files. |
1
|
on_conflict
|
str
|
|
'error'
|
callbacks
|
Any
|
Functions, or objects with |
()
|
chunk
|
Any
|
The number of replicates evaluated together, |
'auto'
|
shard
|
Any
|
A pair |
None
|
Returns:
| Type | Description |
|---|---|
BootstrapResult
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If the fit is of another study, did not converge under
|
diff_soup.inference.BootstrapResult
dataclass
¶
The replicates of a bootstrap run, with their intervals and bands.
boot[handle] is boot.values(handle). Save a result with save, read it back with
BootstrapResult.load, and combine the parts of a sharded run with BootstrapResult.merge.
A part alone answers only values and laplace.
Attributes:
| Name | Type | Description |
|---|---|---|
weights |
ndarray
|
The weights each replicate gave the members, one row per replicate. |
members |
tuple
|
The labels of the reweighted members, in the order of the columns of |
over |
str
|
The name of the reweighted level. |
weight_scheme |
str
|
The |
weight_priors |
bool
|
The |
steps |
Any
|
The |
max_attempts |
Any
|
The step attempts allowed per replicate, or |
outcomes |
Any
|
How each replicate and each leave-one-member-out estimate ended, or |
fit_params |
ndarray
|
The fit's point, where every replicate starts. |
fit_status |
str
|
The status the bootstrapped fit ended with. |
fit_converged |
bool
|
Whether the bootstrapped fit converged. |
shard |
Any
|
The |
values
¶
values(handle: Any, *, members: bool = False) -> Any
Return a parameter's values at the kept replicates, one row per replicate.
A row holds one value per member for a parameter of each member, and a function parameter
gives functions in place of arrays. Under weights="multinomial" a member's value is NaN, or
None for a function, in the replicates that did not draw it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
handle
|
Any
|
A parameter handle of the bootstrapped study. |
required |
members
|
bool
|
Must be |
False
|
Returns:
| Type | Description |
|---|---|
Any
|
The values, stacked over the kept replicates. |
interval
¶
interval(statistic: Any, *, level: float = 0.95, kind: str = 'bca', members: bool = False) -> BootstrapInterval
Return the bootstrap interval of a parameter, or of a statistic of the parameters.
The acceleration of kind="bca" comes from the leave-one-member-out estimates. A member's
own parameter needs members=True, weights="bayesian" and kind="percentile".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
statistic
|
Any
|
A parameter handle, or a function |
required |
level
|
float
|
The coverage, strictly between 0 and 1. |
0.95
|
kind
|
str
|
|
'bca'
|
members
|
bool
|
Whether |
False
|
Returns:
| Type | Description |
|---|---|
BootstrapInterval
|
band
¶
band(statistic: Any, *, at: Any, level: float = 0.95, kind: str = 'bca', members: bool = False) -> BootstrapBand
Return the pointwise bootstrap band of a function parameter, or of a statistic, on a grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
statistic
|
Any
|
A function parameter handle, or a function |
required |
at
|
Any
|
The grid, a non-empty array of finite numbers. |
required |
level
|
float
|
The coverage, strictly between 0 and 1. |
0.95
|
kind
|
str
|
|
'bca'
|
members
|
bool
|
Whether |
False
|
Returns:
| Type | Description |
|---|---|
BootstrapBand
|
laplace
¶
laplace(handle: Any) -> np.ndarray
Return a parameter's Laplace covariance at the fit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
handle
|
Any
|
A parameter handle of the bootstrapped study. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The covariance block, or one block per member, stacked, for a parameter of each member. |
diff_soup.inference.BootstrapInterval
dataclass
¶
A bootstrap interval, returned by BootstrapResult.interval.
The ends are NaN where status is not "ok" or "unresolved", and reason says why.
Attributes:
| Name | Type | Description |
|---|---|---|
lower |
Any
|
The lower end, a number or an array shaped like the statistic. |
upper |
Any
|
The upper end, shaped like |
level |
float
|
The coverage. |
kind |
str
|
|
status |
Any
|
|
reason |
str
|
Sentences explaining any status other than |
n_kept |
int
|
The number of kept replicates. |
n_failed |
int
|
The number of failed replicates. |
members |
int
|
The number of reweighted members. |
centre |
Any
|
The statistic at the fit's point. |
diff_soup.inference.BootstrapBand
dataclass
¶
A pointwise bootstrap band on a grid, returned by BootstrapResult.band.
It has every attribute of BootstrapInterval, with lower, upper, status and centre
given per grid point.
Attributes:
| Name | Type | Description |
|---|---|---|
at |
ndarray
|
The grid. |
draws |
ndarray
|
The statistic on the grid at each kept replicate, one row per replicate. |
Prediction¶
diff_soup.inference.predict
¶
predict(problem, fit_or_draws, *, experiment_id=None, kind='latent', level=0.95)
Carry the parameter uncertainty of a fit or of posterior draws through to the predicted measurements.
From a fit, each prediction is linearized at the fit and the Laplace covariance is
propagated. From Draws, the band is the mean and spread of the
predictions over the draws.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
problem
|
The |
required | |
fit_or_draws
|
A fit as |
required | |
experiment_id
|
The experiment to predict, required when the problem has several. |
None
|
|
kind
|
|
'latent'
|
|
level
|
The coverage of the band, strictly between 0 and 1. |
0.95
|
Returns:
| Type | Description |
|---|---|
|
A |
diff_soup.inference.Prediction
dataclass
¶
The bands of predict, a mapping from measurement name to PredictionBand.
prediction["u"] is the band of the measurement "u", and prediction.names lists the
measurement names.
Attributes:
| Name | Type | Description |
|---|---|---|
bands |
Mapping
|
The bands, by measurement name. |
diff_soup.inference.PredictionBand
dataclass
¶
The prediction band of one measurement over its observation times.
Attributes:
| Name | Type | Description |
|---|---|---|
times |
Any
|
The observation times, without the masked-out ones. |
mean |
Any
|
The predicted mean at each time. |
sd |
Any
|
The predicted standard deviation at each time. |
lower |
Any
|
The lower end, |
upper |
Any
|
The upper end, |