Reference of internal functions
In this reference, you will find a detailed overview of internal functions. They are documented here mostly for development of the package. They are not part of the public API and may change without notice.
CommonSolve.solve — Methodsolve(prob::AbstractHybridProblem, solver::HybridPosteriorSolver; ...)Perform the inversion of HVI Problem.
Optional keyword arguments
scenario: Scenario to query prob, defaults toVal(()).rng: Random generator, defaults toRandom.default_rng().gdevs:NamedTuple(;gdev_M, gdev_P)functions to move computation and data of ML model on and PBM respectively to gpu (e.g.gpu_device()or cpu (identity). defaults toget_gdev_MP(scenario)θmean_quantdefault to0.0: deprecatedis_inferred: set toVal(true)to activate type stability checks
Returns a NamedTuple of
probo: A copy of the HybridProblem, with updated optimized parametersinterpreters: TODOϕ: the optimized HVI parameters: aComponentVectorwith entriesμP:ComponentVectorof the mean global PBM parameters at unconstrained scaleϕg: The MLmodel parameter vector,unc:ComponentVectorof further uncertainty parameters
θP:ComponentVectorof the mean global PBM parameters at constrained scaleresopt: the structure returned byOptimization.solve. It can contain more information on convergence.
HybridVariationalInference.as_ca — Functionas_ca(v::AbstractArray, interpretor)Returns a ComponentArray with underlying data v.
HybridVariationalInference.compose_axes — Methodcompose_axes(axtuples::NamedTuple)Create a new 1d-axis that combines several other named axes-tuples such as of key = getaxes(::AbstractComponentArray).
The new axis consists of several ViewAxes. If an axis-tuple consists only of one axis, it is used for the view. Otherwise a ShapedAxis is created with the axes-length of the others, essentially dropping component information that might be present in the dimensions.
HybridVariationalInference.compute_cholcor_coefficient_single — MethodCompute the cholesky-factor parameter for a given single correlation in a 2x2 matrix. Invert the transformation of cholesky-factor parameterization.
HybridVariationalInference.compute_elbo_components — MethodCompute the components of the elbo for given initial conditions of the problems for the first batch of the trainloader.
HybridVariationalInference.construct_priors_θ_mean — MethodIn order to let mean of θ stay close to initial point parameter estimates construct a prior on mean θ to a Normal around initial prediction.
HybridVariationalInference.front — Functionomit the last n elements of an iterator
HybridVariationalInference.generate_ζ — MethodGenerate samples of (inv-transformed) model parameters, ζ, and the vector of standard deviations, σ, i.e. the diagonal of the cholesky-factor.
Adds the MV-normally distributed residuals, retrieved by sample_ζresid_norm to the means extracted from parameters and predicted by the machine learning model.
The output shape of size (n_site x n_par x n_MC) is tailored to iterating each MC sample and then transforming each parameter on block across sites.
HybridVariationalInference.get_loss_elbo — MethodCreate a loss function for parameter vector ϕ, given
g(x, ϕ): machine learning modeltransPMS: transformation from unconstrained space to parameter spacef(θMs, θP): mechanistic modelinterpreters: assigning structure to pure vectors, seeneg_elbo_gtfn_MC: number of Monte-Carlo sample to approximate the expected value across distributionpbm_covars: tuple of symbols of process-based parameters provided to the ML modelθP: ComponentVector as a template to select indices of pbm_covars
The loss function takes in addition to ϕ, data that changes with minibatch
rng: random generatorxM: matrix of covariates, sites in columnsxP: drivers for the processmodel: Iterator of size n_sitey_o,y_unc: matrix of observations and uncertainties, sites in columns
HybridVariationalInference.gtrans — Methodcomposition transM ∘ g: transformation after machine learning parameter prediction Provide a transMs = StackedArray(transM, n_batch)
HybridVariationalInference.invsumn — MethodInverse of s = sumn(n) for positive integer n.
Gives an inexact error, if given s was not such a sum.
HybridVariationalInference.neg_elbo_ζtf — MethodCompute the neg_elbo for each sampled parameter vector (last dimension of ζs).
- Transform and compute log-jac
- call forward model
- compute log-density of joint density of predictions and unconstrained parameters,
nLjointand its componentsnLy: The likelihood of the data, given the parametersneg_log_prior: the prior of parameters at constrained scalelogjac, negative logarithm of the absolute value of the determinant of the Jacobian of the transformationθ=T(ζ).
loss_penalty: additional loss terms from floss_penalty- compute entropy of transformation
HybridVariationalInference.sample_ζresid_norm — MethodExtract relevant parameters from ζ and return nMC generated multivariate normal draws together with the vector of standard deviations, σ: `(ζPresids, ζMsparfirstresids, σ)The output shape(nθ, nsite?, nMC)is tailored to addingζMsparfirstresidsto ML-model predcitions of size(nθM, n_site)`.
Arguments
int_unc: Interpret vector as ComponentVector with components ρsP, ρsM, logσ2ζP, coeflogσ2_ζMs(intercept + slope),
HybridVariationalInference.transformU_block_cholesky1 — MethodtransformU_block_cholesky1(v::AbstractVector, cor_ends)Transform a parameterization v of a blockdiagonal of upper triangular matrices into the this matrix. cor_ends is an AbstractVector of Integers specifying the last column of each block. E.g. For a matrix with a 3x3, a 2x2, and another single-entry block, the blocks start at columns (3,5,6). It defaults to a single entire block.
An correlation parameterization can parameterize a block of a single parameter, or an empty parameter block. To indicate the empty block, provide cor_ends == [0].
HybridVariationalInference.transformU_cholesky1 — MethodTakes a vector of parameters for UnitUpperTriangular matrix and transforms it to an UpperTriangular that satisfies diag(U' * U) = 1.
This can be used to fit parameters that yield an upper Cholesky-Factor of a Covariance matrix.
It uses the upper triangular matrix rather than the lower because it involves a sum across columns, whereas the alternative of a lower triangular uses sum across rows. Sum across columns is often faster, because entries of columns are contiguous.
An empty parameterization
HybridVariationalInference.transform_ζs — MethodTransform parameters
- from unconstrained (e.g. log) ζ scale of format ((nsite x npar) x n_mc)
- to constrained θ scale of the same format
HybridVariationalInference.transpose_mPMs_sitefirst — MethodTransforms each row of a matrix (nMC x nPar) with site parameters Ms inside nPar of form (npar x nsite) to Ms of the form (nsite x n_par), i.e. neighboring entries (inside a column) are of the same parameter.
This format of having n_par as the last dimension helps transforming parameters on block.
HybridVariationalInference.utri2vec — MethodExtract entries of upper diagonal matrix of UppterTriangular to columnwise vector
HybridVariationalInference.utri2vec_pos — MethodCompute the index in the vector of entries in an upper tridiagonal matrix
HybridVariationalInference.uutri2vec — MethodExtract entries of upper diagonal matrix of UnitUppterTriangular to columnwise vector
HybridVariationalInference.vec2utri — MethodConvert vector v columnwise entries of upper diagonal matrix to UppterTriangular
HybridVariationalInference.vec2utri_pos — MethodCompute the (one-based) position (row, col) within an upper tridiagonal matrix for given (one-based) position, s within a packed vector representation.
HybridVariationalInference.vectuptotupvec — Methodvectuptotupvec(vectup)
vectuptotupvec_allowmissing(vectup)Typesafe convert from Vector of Tuples to Tuple of Vectors. The first variant does not allow for missing in vectup. The second variant allows for missing but has eltype of Union{Missing, ...} in all components of the returned Tuple, also when there were not missing in vectup.
Arguments
vectup: A Vector of identical Tuples
Examples
vectup = [(1,1.01, "string 1"), (2,2.02, "string 2")]
HybridVariationalInference.vectuptotupvec_allowmissing(vectup) ==
([1, 2], [1.01, 2.02], ["string 1", "string 2"])