Fit

Fit(
    method,
    parameters,
    parameter_names,
    log_likelihood,
    prior_log_likelihood,
    aic,
    bic,
    nobs,
    converged,
    status,
    model,
    spec,
    dataset,
    construct_json,
    covariance=None,
    standard_errors=None,
    correlation=None,
    function_evaluations=None,
    profile=None,
    profile_intervals=None,
    optimizer=None,
    draws=None,
    posterior=None,
    posterior_rows=None,
    map=None,
    posterior_mean=None,
    mean_log_likelihood=None,
    posterior_summary=None,
    acceptance_rates=None,
    warmup=None,
    credible_level=None,
    dic=None,
    waic=None,
    waic_pd=None,
    looic=None,
    looic_se=None,
    loo_pd=None,
    j_stat=None,
    j_stat_pval=None,
    gmm_iterations=None,
    converged_within_tolerance=None,
    optimizer_fallback_count=None,
    degree_of_freedom=None,
    number_of_moment_conditions=None,
)

A fitted model: parameter estimates plus whichever estimator diagnostics the target populates.

Build one with :func:fit_mle, :func:fit_map, :func:fit_bayesian, or :func:fit_gmm rather than directly. MaximumLikelihood/MaximumAPosteriori fits additionally carry the Hessian-based covariance and (optionally) profile-likelihood intervals; a BayesianAnalysis fit carries the raw chains and posterior summary; a GMM fit carries the sandwich covariance and the J-statistic overidentification test.

Attributes

Name Type Description
method str "MaximumLikelihood", "MaximumAPosteriori", "BayesianAnalysis", or "GMM".
parameters dict Parameter name -> fitted value. Read the Notes below before using it on a model whose parameter names repeat.
parameter_names list of str Every fitted parameter’s name, in the runner’s order, repeats included. This is the parameter axis order of :attr:covariance/:attr:draws/etc., and it is the full-length record :attr:parameters can be short of.
covariance, correlation numpy.ndarray or None n x n, aligned with :attr:parameter_names. None when not computed (MLE/MAP with hessian=False).
standard_errors dict or None Parameter name -> standard error. None when not computed.
log_likelihood, aic, bic float or None None for a GMM fit (method-of-moments has no likelihood surface to report them from); see :func:_none_if_nan for why None rather than nan was chosen.
prior_log_likelihood float The prior half of the log-posterior. Kept as the runner’s raw value (including its structural nan for GMM) – this is bookkeeping, not a primary reporting field.
nobs int Number of observations the fit was computed against. For a GMM fit this is the record length the estimator itself divides its sandwich covariance by.
converged bool
status str "Success", "MaximumIterationsReached", etc.
function_evaluations int or None MLE/MAP only; None for Bayesian/GMM fits (the runner never populates it there).
model Model The fitted model: :attr:parameters applied to the construct’s spec. Simulate from it with :func:~corehydropy.model_simulate.
profile dict or None MLE/MAP only, present when the fit was built with profile=True: parameter name -> profile_bins x 2 array with columns [value, log_likelihood].
draws numpy.ndarray or None Bayesian only: the raw chains, shape (n_iterations, n_chains, n_params) – the same [iteration, chain, parameter] axis order corehydror returns.
posterior numpy.ndarray or None Bayesian only: the thinned draw matrix the analyses consume, shape (posterior_rows, n_params), its columns aligned with :attr:parameter_names (the same convention :attr:draws uses for its parameter axis).
posterior_rows int or None Bayesian only: the number of retained posterior draws, posterior.shape[0].
map, posterior_mean dict or None Bayesian only: the two posterior point estimates, parameter name -> value.
mean_log_likelihood numpy.ndarray or None Bayesian only: the chain-averaged log-likelihood trace, one value per iteration.
posterior_summary dict or None Bayesian only: parameter_names plus the arrays mean, median, sd, lower, upper, rhat, ess, each aligned with parameter_names. (Named posterior_summary rather than R’s $summary to avoid colliding with the :meth:summary text-report method Python’s class namespace forces onto one object.)
acceptance_rates numpy.ndarray or None Bayesian only, one value per chain.
warmup int or None Bayesian only: the warmup actually used (see :func:fit_bayesian’s derived default).
credible_level float or None Bayesian only: the credible level :attr:posterior_summary’s lower/upper were computed at (:func:fit_bayesian’s credible_level argument).
dic, waic, looic float or None Bayesian only, the usual Bayesian goodness-of-fit scalars.
looic_se float or None Bayesian only: the standard error of :attr:looic.
waic_pd, loo_pd float or None Bayesian only: the effective number of parameters behind :attr:waic and :attr:looic.
j_stat, j_stat_pval float or None GMM only. Bulletin 17C is always just-identified, so j_stat_pval is structurally None – there is no over-identified case to report a p-value for. At zero degrees of freedom j_stat is not interpretable either and :meth:summary does not print it.
gmm_iterations, converged_within_tolerance, optimizer_fallback_count GMM only, the estimator’s own bookkeeping.
degree_of_freedom, number_of_moment_conditions int or None :func:~corehydropy.fit_gmm_moments only, matching corehydror’s $degree_of_freedom and $number_of_moment_conditions: the number of moment conditions q your function returned, and the over-identifying degrees of freedom max(0, q - p) that decide whether :attr:j_stat_pval exists at all. The :func:~corehydropy.fit_gmm model path does not report them (Bulletin 17C is always just-identified) and leaves both None.

Notes

:attr:parameters is a dict keyed by parameter name, so a model whose parameter names repeat loses every value but the last one under each repeated name. A mixture is the case that reaches this: a two-component Normal mixture fits six parameters, two of them named D1 and two named D2, and .parameters comes back with four entries in which the surviving D1/D2 are the standard deviations carrying the means’ labels. :attr:standard_errors, keyed the same way, collapses identically.

For the full ordered vector use :attr:parameter_names together with fit.model.spec["parameter_values"], which are the same length and aligned elementwise, or read :attr:covariance/:attr:draws directly (their axes are parameter_names, never the dict). :attr:aic, :attr:bic, and every diagnostic come from the C++ estimator and count all six; only the dict is short. corehydror’s coef() returns a plain named vector, which permits duplicate names, so R is unaffected.

Methods

Name Description
confint Confidence or credible intervals for the fit.
diagnostics Estimation diagnostics off this fit. See :func:fit_diagnostics.
quantile_variance Delta-method variance of a fitted quantile. See :func:quantile_variance.
summary A multi-line text summary, mirroring R’s print.corehydro_fit/summary.corehydro_fit.
to_json The fitted model’s spec as the JSON the shared C++ core parses.
to_model The fitted model (:attr:model), as a verb alongside :meth:to_json.

confint

Fit.confint(level=0.95)

Confidence or credible intervals for the fit.

MLE/MAP fits get profile-likelihood confidence intervals; a Bayesian fit gets posterior credible intervals. GMM has no interval surface (see below).

For MLE/MAP: when the fit already carries a profile block at the requested level (built with profile=True or a prior :meth:confint call), those bounds are reused; otherwise the identical fit is re-run with profiling turned on – a deterministic optimizer reproduces the identical point estimate, so the rebuild’s confidence intervals are the ones the original fit would have carried had profile=True been requested at the matching level up front.

For Bayesian: :attr:Fit.posterior_summary’s lower/upper are already the posterior credible interval at :attr:Fit.credible_level (:func:fit_bayesian’s credible_level argument, 0.9 by default). When the requested level matches, those are returned directly; otherwise the identical seeded chain is re-run with credible_interval_width set to level (the same lazy-rebuild precedent as the profile path above – re-sampling with an identical seed reproduces the same chain bit-for-bit, so only the post-hoc credible-interval quantile computation changes). Because this method’s own default is 0.95 and a fit’s default is 0.9, a bare confint() on a Bayesian fit re-runs the chain; fit_bayesian(..., credible_level=0.95) avoids that.

GMM is method-of-moments and has no likelihood or posterior to draw an interval from; calling this raises. Use :meth:quantile_variance for the delta-method variance of a fitted quantile instead.

Parameters

Name Type Description Default
level float Confidence (MLE/MAP) or credible (Bayesian) level. Matches R’s confint() convention, not BayesianAnalysis’s own 0.9 class default. 0.95

Returns

Name Type Description
dict lower and upper, each a dict of parameter name -> bound.

Examples

>>> from corehydropy import fit_mle, model_univariate
>>> peaks = [12500, 15300, 8900, 22100, 18700, 14200, 9800, 28500, 17400, 11600]
>>> f = fit_mle(model_univariate("Normal", peaks))
>>> ci = f.confint(level=0.9)

diagnostics

Fit.diagnostics()

Estimation diagnostics off this fit. See :func:fit_diagnostics.

quantile_variance

Fit.quantile_variance(aep)

Delta-method variance of a fitted quantile. See :func:quantile_variance.

summary

Fit.summary()

A multi-line text summary, mirroring R’s print.corehydro_fit/summary.corehydro_fit.

Returns

Name Type Description
str Parameters, the common goodness-of-fit scalars (or the J-statistic for a GMM fit), and – for a Bayesian fit – the posterior R-hat/ESS, or – for an MLE/MAP fit – the standard errors.

to_json

Fit.to_json()

The fitted model’s spec as the JSON the shared C++ core parses.

Serializes :attr:model (parameters, family, trends, …) exactly like :meth:~corehydropy.models.Model.to_json; it does not include the covariance, draws, or any other diagnostic on this Fit.

Returns

Name Type Description
str The fitted model spec as JSON.

to_model

Fit.to_model()

The fitted model (:attr:model), as a verb alongside :meth:to_json.

Returns

Name Type Description
Model Same object as :attr:model.