Repository navigation
Fix the Laplace covariance for solver 2 and after a solver fallthrough - #3436
Merged
Merged
Conversation
Contributor
Jenkins Console Log Machine informationDistributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focal CPU: Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian Address sizes: 46 bits physical, 48 bits virtual CPU(s): 80 On-line CPU(s) list: 0-79 Thread(s) per core: 2 Core(s) per socket: 20 Socket(s): 2 NUMA node(s): 2 Vendor ID: GenuineIntel CPU family: 6 Model: 85 Model name: Intel(R) Xeon(R) Gold 6248 CPU @ 2.50GHz Stepping: 7 CPU MHz: 1000.172 CPU max MHz: 3900.0000 CPU min MHz: 1000.0000 BogoMIPS: 5000.00 Virtualization: VT-x L1d cache: 1.3 MiB L1i cache: 1.3 MiB L2 cache: 40 MiB L3 cache: 55 MiB NUMA node0 CPU(s): 0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78 NUMA node1 CPU(s): 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59,61,63,65,67,69,71,73,75,77,79 Vulnerability Gather data sampling: Mitigation; Microcode Vulnerability Indirect target selection: Mitigation; Aligned branch/return thunks Vulnerability Itlb multihit: KVM: Mitigation: Split huge pages Vulnerability L1tf: Not affected Vulnerability Mds: Not affected Vulnerability Meltdown: Not affected Vulnerability Mmio stale data: Mitigation; Clear CPU buffers; SMT vulnerable Vulnerability Old microcode: Not affected Vulnerability Reg file data sampling: Not affected Vulnerability Retbleed: Mitigation; Enhanced IBRS Vulnerability Spec rstack overflow: Not affected Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization Vulnerability Spectre v2: Mitigation; Enhanced / Automatic IBRS; IBPB conditional; PBRSB-eIBRS SW sequence; BHI SW loop, KVM SW loop Vulnerability Srbds: Not affected Vulnerability Tsa: Not affected Vulnerability Tsx async abort: Mitigation; TSX disabled Vulnerability Vmscape: Mitigation; IBPB before exit to userspace Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req vnmi pku ospke avx512_vnni md_clear flush_l1d arch_capabilities G++: g++ (Ubuntu 9.4.0-1ubuntu1~20.04) 9.4.0 Copyright (C) 2019 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Clang: clang version 10.0.0-4ubuntu1 Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This was assisted by Claude
Summary
laplace_base_rng()(stan/math/mix/functor/laplace_base_rng.hpp) computes the covariance of the Laplace approximation.laplace_latent_rng(),laplace_latent_tol_rng(),laplace_latent_solve(),laplace_latent_solve_tol()andlaplace_latent[_tol]_{bernoulli_logit,poisson_log,neg_binomial_2_log}_rng()return this covariance or sample from it.laplace_base_rng()selected the formula withoptions.solver, the solver that the user requested. But the content ofmd_est.W_r,md_est.L,md_est.K_rootandmd_est.LUdepends on the solver that producedmd_est, which ismd_est.solver_used.laplace_marginal_density()already selects bymd_est.solver_used.This gave two errors:
Solver 2 returned a wrong covariance. The branch
options.solver == 1 || options.solver == 2used the solver-1 formulaSigma = K - V^T VwithV = L^-1 W_r K. This is correct for solver 1, whereW_r = sqrt(W)andL L^T = I + W_r K W_r. Solver 2 (CholeskyKSolver) storesW_r = WandL L^T = B = I + K_root^T W K_root, so the result wasK - K W B^-1 W Kinstead ofK_root B^-1 K_root^T. The wrong matrix can be positive definite, and then there is no error message (example, case 1).After a solver fallthrough, the formula did not match the solver. With
allow_fallthrough = 1, a failed solver 1 continues with solver 2, and a failed solver 2 continues with solver 3. The formula stayed the one foroptions.solver:cholesky_decompose()throws;md_est.Lis 0 x 0. The triangular solve fails an Eigen assertion (SolveTriangular.h:177), and the process aborts. Because this is not an exception, Stan cannot reject the draw, and all output is lost.solver = 1andallow_fallthrough = 1are the defaults (stan/math/prim/fun/generate_laplace_options.hpp). So the functions without control arguments took this path whenever solver 1 failed, that is, when the negative Hessian of the log-likelihood was not positive definite at a Newton iterate. This happens for likelihoods that are not log-concave (example, case 2). Thelaplace_marginal*functions do not calllaplace_base_rng()and were not affected.I found this while using
laplace_latent_solve_tol()to construct importance-sampling proposals for a nonlinear joint model with 4 latent variables per group. Its log-likelihood is not concave at the mode for about half of the groups.Changes
In
stan/math/mix/functor/laplace_base_rng.hpp:md_est.solver_used.Sigma = C^T CwithC = L^-1 K_root^T, that isSigma = K_root B^-1 K_root^T. This is the form thatlaplace_marginal_density()uses for solver 2.No other file in
stan/changes.Example
Two latent variables with prior covariance
K = [1 0.5; 0.5 2]:normal_lpdf(y | theta, s),y = (0.3, -0.2),s = 2. The Laplace approximation is exact, with covariance(K^-1 + I / s^2)^-1.student_t_lpdf(y | 1, theta, s),s = 0.5, (a)y = (4, -3)and (b)y = (2.5, -2). At the mode,W = -d^2/dtheta^2 log p(y | theta)has one negative entry (-0.104 and -0.353), whileK^-1 + Wis positive definite. The reference is the mode fromoptim()and the analyticW.Covariance
Sigma = L L^Treturned by the call,[row 1; row 2], 4 decimals:_tol, solver 1 or 3[0.7731 0.2689; 0.2689 1.3109]_tol, solver 2[0.9462 0.4442; 0.4442 1.8226][0.7731 0.2689; 0.2689 1.3109]_tol, solver 3[0.9784 0.0564; 0.0564 0.2050]_tol, solver 1, fallthrough[0.9784 0.0564; 0.0564 0.2050]_tol, solver 3[1.2855 0.0559; 0.0559 0.1547]_tol, solver 1, fallthroughcholesky_decompose: Matrix m is not positive definite[1.2855 0.0559; 0.0559 0.1547]The values in the column "this PR" are the closed-form covariances. For case 1 with solver 2, the
developvalue is exactlyK - K W B^-1 W KwithW = I / 4andB = I + K_root^T W K_root. The default-options calls agree to about 1e-4 because of the default convergence tolerance.Stan program
R code (cmdstanr)
Tests
Four new tests in
test/unit/math/laplace/laplace_latent_solve_test.cpp. They use the two cases of the example and compare with closed forms:laplace_latent_solve.all_solvers_match_closed_form_normal: case 1. For solvers 1, 2 and 3 and Hessian block sizes 1 and 2, the mean andL L^Tmust equalSigma y / s^2andSigma = (K^-1 + I / s^2)^-1.laplace_latent_solve.fallthrough_matches_closed_form_cauchy: case 2, with the Cauchy likelihood written withcauchy_lpdf(). The test asserts thatWhas a negative entry at the mode. Solver 1 and solver 2 withallow_fallthrough = 1must give the mode of solver 3 and the covariance(K^-1 + W)^-1, withWcomputed analytically at the mode. In the example, the fallthrough ends in solver 3 for case 2a and in solver 2 for case 2b.laplace_latent_solve.default_options_cauchy: the same check forlaplace_latent_solve()without control arguments. The test tolerance is 1e-3, because the default convergence tolerance (1.49e-8 on the change of the objective) gives a mode error of order 1e-4.laplace_latent_tol_rng.solver_2_draw_uses_laplace_covariance: case 1. With the same seed, the draw oflaplace_latent_tol_rng()with solver 2 equals the draw ofmulti_normal_rng()with the exact mean and covariance.Without the change in
laplace_base_rng.hpp, all four tests fail:cholesky_decompose: Matrix m is not positive definite(case 2b comes first in the tests, so the assertion failure of case 2a does not occur);With the change, these tests pass. The existing tests in
laplace_latent_solve_test.cpp,laplace_bernoulli_logit_rng_test.cpp,laplace_poisson_log_rng_test.cpp,laplace_neg_binomial_2_log_rng_test.cppandlaplace_solver_fallback_test.cppalso pass. The current tests did not cover these cases:laplace_latent_solve_test.cppused only solver 1, andlaplace_solver_fallback_test.cppuses onlylaplace_marginal_tol().Side Effects
Results change only where they were wrong: solver 2, and solver 1 or 2 when a fallthrough occurs. Solver 1 without a fallthrough and solver 3 give the same values as before. The
laplace_marginal*functions do not change.Release notes
Fixed the covariance of the Laplace approximation in
laplace_latent_rng(),laplace_latent_solve()and the related functions for solver 2 and after a solver fallthrough. Before this fix, the covariance was wrong, or the program stopped with an exception or a failed assertion. With the default options, a fallthrough occurs when the log-likelihood is not log-concave.Checklist
Copyright holder: Aalto University
The copyright holder is typically you or your assignee, such as a university or company. By submitting this pull request, the copyright holder is agreeing to the license the submitted work under the following licenses:
- Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause)
- Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)
the basic tests are passing
./runTests.py test/unit)make test-headers)make test-math-dependencies)make doxygen)make cpplint)the code is written in idiomatic C++ and changes are documented in the doxygen
the new changes are tested