@@ -62,9 +62,10 @@ function neg_elbo_gtf_components(rng, ϕ::AbstractVector{FT}, g, f, py,
6262 is_testmode,
6363 is_omit_priors,
6464 zero_prior_logdensity,
65+ approx:: AbstractHVIApproximation ,
6566) where {FT}
6667 n_MCr = isempty (priors_θP_mean) ? n_MC : max (n_MC, n_MC_mean)
67- ζsP, ζsMs, σ = generate_ζ (rng, g, ϕ, xM; n_MC= n_MCr, cor_ends, pbm_covar_indices,
68+ ζsP, ζsMs, σ = generate_ζ (approx, rng, g, ϕ, xM; n_MC= n_MCr, cor_ends, pbm_covar_indices,
6869 int_ϕq, int_ϕg_ϕq, is_testmode)
6970 ζsP_cpu = cdev (ζsP) # fetch to CPU, because for <1000 sites (n_batch) this is faster
7071 ζsMs_cpu = cdev (ζsMs) # fetch to CPU, because for <1000 sites (n_batch) this is faster
@@ -382,6 +383,7 @@ function sample_posterior(rng, prob::AbstractHybridProblem, xM::AbstractMatrix;
382383 scenario= Val (()),
383384 n_sample_pred= 200 ,
384385 gdevs = get_gdev_MP (scenario),
386+ approx = nothing ,
385387 kwargs...
386388)
387389 n_site, n_batch = get_hybridproblem_n_site_and_batch (prob; scenario)
@@ -400,9 +402,13 @@ function sample_posterior(rng, prob::AbstractHybridProblem, xM::AbstractMatrix;
400402 int_ϕq = interpreters. ϕq
401403 transMs = StackedArray (transM, n_batch)
402404 g_dev, ϕ_dev = gdevs. gdev_M (g), gdevs. gdev_M (ϕ)
405+ if isnothing (approx)
406+ approx = prob. approx # assuming has field approx, e.g. if its a HybridProblem
407+ end
403408 (; θsP, θsMs, entropy_ζ) = sample_posterior (rng, g_dev, ϕ_dev, xM;
404409 int_ϕg_ϕq, int_ϕq, transP, transM,
405- n_sample_pred, cdev= infer_cdev (gdevs), cor_ends, pbm_covar_indices, kwargs... )
410+ n_sample_pred, cdev= infer_cdev (gdevs), cor_ends, pbm_covar_indices, approx,
411+ kwargs... )
406412 θsPc = ComponentArrayInterpreter (par_templates. θP, (n_sample_pred,))(θsP)
407413 θsMsc = ComponentArrayInterpreter ((n_site,), par_templates. θM, (n_sample_pred,))(θsMs)
408414 (; θsP= θsPc, θsMs= θsMsc, entropy_ζ)
@@ -418,8 +424,9 @@ function sample_posterior(rng, g, ϕ::AbstractVector, xM::AbstractMatrix;
418424 pbm_covar_indices,
419425 is_inferred:: Val{is_infer} = Val (false ),
420426 is_testmode,
427+ approx:: AbstractHVIApproximation ,
421428) where is_infer
422- ζsP_gpu, ζsMs_gpu, σ = generate_ζ (rng, g, CA. getdata (ϕ), CA. getdata (xM);
429+ ζsP_gpu, ζsMs_gpu, σ = generate_ζ (approx, rng, g, CA. getdata (ϕ), CA. getdata (xM);
423430 int_ϕg_ϕq, int_ϕq,
424431 n_MC= n_sample_pred, cor_ends, pbm_covar_indices, is_testmode)
425432 ζsP = cdev (ζsP_gpu)
@@ -434,6 +441,7 @@ function sample_posterior(rng, g, ϕ::AbstractVector, xM::AbstractMatrix;
434441 (; θsP, θsMs, entropy_ζ)
435442end
436443
444+
437445"""
438446Generate samples of (inv-transformed) model parameters, ζ,
439447and the vector of standard deviations, σ, i.e. the diagonal of the cholesky-factor.
@@ -445,7 +453,8 @@ model.
445453The output shape of size `(n_site x n_par x n_MC)` is tailored to iterating
446454each MC sample and then transforming each parameter on block across sites.
447455"""
448- function generate_ζ (rng, g, ϕ:: AbstractVector{FT} , xM:: MT ;
456+ function generate_ζ (approx:: AbstractMeanHVIApproximation , rng:: AbstractRNG ,
457+ g, ϕ:: AbstractVector{FT} , xM:: MT ;
449458 int_ϕg_ϕq:: AbstractComponentArrayInterpreter ,
450459 int_ϕq:: AbstractComponentArrayInterpreter ,
451460 n_MC= 3 , cor_ends, pbm_covar_indices,
@@ -462,7 +471,7 @@ function generate_ζ(rng, g, ϕ::AbstractVector{FT}, xM::MT;
462471 # TODO replace pbm_covar_indices by ComponentArray? dimensions to be type-inferred?
463472 xMP0 = _append_each_covars (xM, CA. getdata (μ_ζP), pbm_covar_indices)
464473 μ_ζMs0 = g (xMP0, ϕg; is_testmode)
465- ζP_resids, ζMs_parfirst_resids, σ = sample_ζresid_norm (rng, μ_ζP, μ_ζMs0, ϕq; n_MC, cor_ends, int_ϕq)
474+ ζP_resids, ζMs_parfirst_resids, σ = sample_ζresid_norm (approx, rng, μ_ζP, μ_ζMs0, ϕq; n_MC, cor_ends, int_ϕq)
466475 if pbm_covar_indices isa SA. SVector{0 }
467476 # do not need to predict again but just add the residuals to μ_ζP and μ_ζMs
468477 # ζsP = μ_ζP .+ ζP_resids # n_par x n_MC # .+ on empty view does not work
@@ -559,8 +568,9 @@ ML-model predcitions of size `(n_θM, n_site)`.
559568* `int_ϕq`: Interpret vector as ComponentVector with components
560569 ρsP, ρsM, logσ2_ζP, coef_logσ2_ζMs(intercept + slope),
561570"""
562- function sample_ζresid_norm (rng:: Random.AbstractRNG , ζP:: AbstractVector , ζMs:: AbstractMatrix ,
563- args... ; n_MC, cor_ends, int_ϕq)
571+ function sample_ζresid_norm (approx:: AbstractHVIApproximation , rng:: Random.AbstractRNG ,
572+ ζP:: AbstractVector , ζMs:: AbstractMatrix , args... ;
573+ n_MC, cor_ends, int_ϕq)
564574 n_θP, n_θMs = length (ζP), length (ζMs)
565575 # intm_PMs_parfirst = !isnothing(intm_PMs_parfirst) ? intm_PMs_parfirst : begin
566576 # n_θM, n_site_batch = size(ζMs)
@@ -569,14 +579,14 @@ function sample_ζresid_norm(rng::Random.AbstractRNG, ζP::AbstractVector, ζMs:
569579 # end
570580 # urandn = _create_randn(rng, CA.getdata(ζP), n_MC, n_θP + n_θMs)
571581 urandn = _create_randn (rng, CA. getdata (ζP), n_θP + n_θMs, n_MC)
572- sample_ζresid_norm (urandn, CA. getdata (ζP), CA. getdata (ζMs), args... ;
582+ sample_ζresid_norm (approx, urandn, CA. getdata (ζP), CA. getdata (ζMs), args... ;
573583 cor_ends,
574584 int_ϕq= get_concrete (int_ϕq)
575585 )
576586end
577587
578- function sample_ζresid_norm (urandn :: AbstractMatrix , ζP :: TP , ζMs :: TM ,
579- ϕq:: AbstractVector ;
588+ function sample_ζresid_norm (approx :: MeanHVIApproximationMat , urandn :: AbstractMatrix ,
589+ ζP :: TP , ζMs :: TM , ϕq:: AbstractVector ;
580590 int_ϕq= get_concrete (ComponentArrayInterpreter (ϕq)),
581591 cor_ends
582592) where {T,TP<: AbstractVector{T} ,TM<: AbstractMatrix{T} }
0 commit comments