A question to Turing Specialists.
I performed a QR decomposition on my model matrix X
.
#### QR Decomposition ####
Q, R = qr(X)
Then i fitted a Turing Model to my Q Matrix, casting it to a Matrix
:
model_qr = varying_intercept(Matrix(Q), idx, float(y));
chn_qr = sample(model_qr, NUTS(1_000, 0.65), MCMCThreads(), 2_000, 4);
This is the output:
julia> chn_qr
Chains MCMC chain (2000×24×4 Array{Float64,3}):
Iterations = 1:2000
Thinning interval = 1
Chains = 1, 2, 3, 4
Samples per chain = 2000
parameters = β[1], β[2], μ, μⱼ[1], μⱼ[2], μⱼ[3], μⱼ[4], μⱼ[5], μⱼ[6], μⱼ[7], σ, σⱼ
internals = acceptance_rate, hamiltonian_energy, hamiltonian_energy_error, is_accept, log_density, lp, max_hamiltonian_energy_error, n_steps, nom_step_size, numerical_error, step_size, tree_depth
Summary Statistics
parameters mean std naive_se mcse ess rhat
Symbol Float64 Float64 Float64 Float64 Float64 Float64
β[1] 0.0615 1.6131 0.0180 0.0242 5229.9895 0.9997
β[2] -46.6065 4.4243 0.0495 0.0556 6779.8184 1.0009
μ 23.2964 1.7568 0.0196 0.0354 1548.2673 1.0012
μⱼ[1] 1.3817 1.8077 0.0202 0.0364 1599.0513 1.0011
μⱼ[2] 1.6890 1.7905 0.0200 0.0362 1604.2076 1.0009
μⱼ[3] -4.0131 1.7790 0.0199 0.0355 1565.3781 1.0010
μⱼ[4] 5.8102 2.0615 0.0230 0.0377 2110.1449 1.0009
μⱼ[5] -2.0791 1.8817 0.0210 0.0356 1732.1206 1.0011
μⱼ[6] -5.2884 1.7996 0.0201 0.0362 1600.3742 1.0012
μⱼ[7] 1.9578 1.8095 0.0202 0.0365 1607.9793 1.0011
σ 2.6639 0.1189 0.0013 0.0014 7292.1689 1.0006
σⱼ 4.2218 1.4394 0.0161 0.0266 3021.6447 1.0011
Quantiles
parameters 2.5% 25.0% 50.0% 75.0% 97.5%
Symbol Float64 Float64 Float64 Float64 Float64
β[1] -2.9792 -0.7044 0.0580 0.8240 3.1977
β[2] -55.0442 -49.6002 -46.6371 -43.6904 -37.9659
μ 19.9347 22.2450 23.2709 24.3002 26.9628
μⱼ[1] -2.3245 0.3304 1.3992 2.4828 4.9164
μⱼ[2] -2.0222 0.6622 1.6990 2.7723 5.1757
μⱼ[3] -7.7450 -5.0484 -3.9772 -2.9457 -0.6303
μⱼ[4] 1.7884 4.5075 5.7806 7.0934 9.8737
μⱼ[5] -6.0375 -3.1619 -2.0521 -0.9130 1.5270
μⱼ[6] -9.0518 -6.3368 -5.2189 -4.1916 -1.8506
μⱼ[7] -1.8358 0.9237 1.9936 3.0302 5.4514
σ 2.4414 2.5788 2.6598 2.7444 2.9033
σⱼ 2.4002 3.2597 3.9161 4.8519 7.7719
I’ve managed to reconstruct the quantiles of β
by multiplying R^-1 * β
(in a very ugly non-elegant way):
quantiles_beta = select(DataFrame(quantile(group(chn_qr, :β))), r"%");
mapcols(x -> R^-1 * x, quantiles_beta)
2×5 DataFrame
Row │ 2.5% 25.0% 50.0% 75.0% 97.5%
│ Float64 Float64 Float64 Float64 Float64
─────┼─────────────────────────────────────────────────────────────────
1 │ -2.56661 -2.34772 -2.22018 -2.09349 -1.86299
2 │ 0.00516392 0.00465319 0.00437521 0.00409877 0.00356173
How would I apply R^-1 * β
to all the β
in the chn_qr
object so that I can create a new “chn_qr_reconstructed
” object and perform all the methods that uses Chains
to it?
chn_qr
has the following dimensions (2000 samples, 24 parameters - which there are only 2 β
, and 4 chains)
size(chn_qr)
(2000, 24, 4)
BTW: QR reparameterization is 19.6s sampling and using X
is 135.6s sampling (with a way better ESS overall)