Repository navigation
Conversation
…egral for small sw and st0
…er wiener_lcdf_unnorm and wiener_lccdf_unnorm
4 tasks done
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: 52 bits physical, 57 bits virtual CPU(s): 192 On-line CPU(s) list: 0-191 Thread(s) per core: 2 Core(s) per socket: 48 Socket(s): 2 NUMA node(s): 2 Vendor ID: AuthenticAMD CPU family: 25 Model: 17 Model name: AMD EPYC 9474F 48-Core Processor Stepping: 1 Frequency boost: enabled CPU MHz: 1496.938 CPU max MHz: 4114.4229 CPU min MHz: 1500.0000 BogoMIPS: 7189.39 Virtualization: AMD-V L1d cache: 3 MiB L1i cache: 3 MiB L2 cache: 96 MiB L3 cache: 512 MiB NUMA node0 CPU(s): 0-47,96-143 NUMA node1 CPU(s): 48-95,144-191 Vulnerability Gather data sampling: Not affected Vulnerability Indirect target selection: Not affected Vulnerability Itlb multihit: Not affected Vulnerability L1tf: Not affected Vulnerability Mds: Not affected Vulnerability Meltdown: Not affected Vulnerability Mmio stale data: Not affected Vulnerability Old microcode: Not affected Vulnerability Reg file data sampling: Not affected Vulnerability Retbleed: Not affected Vulnerability Spec rstack overflow: Mitigation; Safe RET 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; STIBP always-on; PBRSB-eIBRS Not affected; BHI Not affected Vulnerability Srbds: Not affected Vulnerability Tsa: Mitigation; Clear CPU buffers Vulnerability Tsx async abort: Not affected 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 mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good amd_lbr_v2 nopl xtopology nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba perfmon_v2 ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local user_shstk avx512_bf16 clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin cppc arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif x2avic v_spec_ctrl vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid overflow_recov succor smca fsrm flush_l1d debug_swap 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 branch has not been deployed
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.
Fixes #3329.
This PR improves
wiener_lpdf(5- and 7-parameter),wiener_lcdf_unnormandwiener_lccdf_unnorm(4- and 7-parameter).This was assisted by Claude
Summary
This PR fixes the underflow of #3329 and other accuracy defects of the Wiener functions that I found while testing a fix for it. It is an alternative to #3435 (comparison: #3435 (comment)).
Functions and files. The Stan functions, the C++ files that compute them (in
stan/math/prim/prob), and the internal functions that they call. Each function has an optional last argument for the precision.wiener_lpdf(y | alpha, tau, beta, delta), 4-parameterwiener_lpdf.hppwiener_lpdf(y | a, t0, w, v, sv), 5-parameterwiener5_lpdf.hppinternal::wiener5_density,internal::wiener5_grad_*)wiener_lpdf(y | a, t0, w, v, sv, sw, st0), 7-parameterwiener_full_lpdf.hpphcubatureintegrals of the 5-parameter density and gradients over sw and st0 (internal::wiener7_integrate); with sw = st0 = 0 it calls the 5-parameterwiener_lpdfwiener_lcdf_unnorm(y, a, t0, w, v), 4-parameterwiener4_lcdf_unnorm.hppinternal::wiener4_distribution,internal::wiener4_cdf_grad_*,logMill)wiener_lccdf_unnorm(y, a, t0, w, v), 4-parameterwiener4_lccdf_unnorm.hppwiener_lcdf_unnorm(y, a, t0, w, v, sv, sw, st0), 7-parameterwiener_full_lcdf_unnorm.hpphcubatureintegrals of the 4-parameter cdf and gradients over sv, sw and st0 (internal::wiener7_integrate_cdf); with sv = sw = st0 = 0 it calls the 4-parameterwiener_lcdf_unnormwiener_lccdf_unnorm(y, a, t0, w, v, sv, sw, st0), 7-parameterwiener_full_lccdf_unnorm.hppinternal::wiener4_ccdf,internal::wiener4_ccdf_grad_*); with sv = sw = st0 = 0 it calls the 4-parameterwiener_lccdf_unnormIn the rest of this text, "4-parameter lcdf" means
wiener_lcdf_unnormwith 4 parameters, "7-parameter lpdf" meanswiener_lpdfwith 7 parameters, and so on. Because of these calls, a fix in the 5-parameter functions also changes the 7-parameter lpdf, and a fix in the 4-parameter cdf functions also changes the 4-parameter lccdf and the 7-parameter lcdf and lccdf (see the table "Several fixes are in shared code" below).7-parameter lpdf, #3329. The integrands of the 7-parameter density are natural-scale 5-parameter densities, which underflow to 0 when the log density is below about -708. Per observation, the integrands are now scaled by exp(-c), where c is the largest 5-parameter log density on a 3 x 3 grid of the integration box (a 17 x 17 grid if the scaled integral is still outside [1e-100, 1e100]). The log density is c + log(integral); the partials are ratios of integrals, so c cancels. The series tolerances are relative to exp(c).
wiener5_densityandwiener5_grad_*get an optionallog_shiftargument (default 0); existing callers are unchanged. The existinghcubatureis used; all autodiff types use the same code path.hcubature.
hcubatureis used only by the 7-parameterwiener_lpdf,wiener_lcdf_unnormandwiener_lccdf_unnorm, so this fix changes only these three functions. Two bugs: (1) the loop condition used the error and value of the first box, so once the first box failed the test, the loop ran to the evaluation limit; (2) a split box stayed in the list with error 0 and could be split and counted again. With the fix the integration stops at the requested tolerance. This makes the 7-parameter lpdf 11 to 14 times faster when st0 > 0.d/dsw and d/dst0 of the 7-parameter lpdf.
5-parameter lpdf. Three faults on the path for small densities:
assign_errtook its argument by value, so the re-estimate inestimate_with_err_checkused the old tolerance; withLogResult == falsethe check compared |log density| with the log tolerance and never fired; the gradient series of t, a and w used an absolute tolerance. Value errors were up to 1e-2 at ordinary points.4-parameter lcdf. The small-time series of the derivatives with respect to a and w had sign errors (gradient errors up to 1.6e-4; through these functions also the 7-parameter lcdf and lccdf gradients, up to 3e-3).
logMilluseserfcx(recently merged) for x >= 0.4-parameter lccdf. The ccdf P - F loses all digits where F is close to P (-inf at 8 of 300 ordinary points). Where F >= P/2 it now uses the large-time series of P - F, with its derivatives.
7-parameter lcdf and lccdf. The integration tolerance is at most 0.9e-6. The lccdf now includes the trials whose non-decision time ends after y (they contribute P; errors in log ccdf up to 1.4 where st0 > y - t0). The lccdf integrates the 4-parameter ccdf above, so it is finite down to a ccdf of about 1e-308; develop formed P - F and returned -inf below about 1e-16 of P (for example at a log ccdf of -39, -120 and -299). The d/dt series tolerance is relative to the cdf or ccdf. Where the value integral underflows to 0, the value is -inf and the gradient integrals are skipped.
Several fixes are in shared code, so they reach more than one function:
hcubature: loop condition, split box counted twiceassign_errby referenceestimate_with_err_checkwithLogResult == falselogMillwitherfcx(x): only through the call to the 5-parameter lpdf (7-parameter lpdf with sw = st0 = 0) or to the 4-parameter lcdf (7-parameter lcdf with sv = sw = st0 = 0). The 4-parameter lccdf uses the 4-parameter cdf and its gradients where F < P/2; the 7-parameter lcdf and lccdf use them in their integrands. The older 4-parameter
wiener_lpdf(5 arguments) is not changed.Accuracy, develop -> this PR (largest error over the test points; log scale for values, relative to max(|ref|, 1) for gradients; default precision):
References: mpmath (50 digits) for the 4- and 5-parameter functions; for the 7-parameter functions an independent adaptive Gauss-Kronrod integration of the 5-parameter density (own code, not
hcubature), with gradients by differentiation under the integral or by finite differences of the reference. Ordinary points: sets of 300 random points (a 0.5-3, v -5..5 or -3..3, w 0.2-0.8, sv 0-1.5, sw up to 0.8 of its range, st0 up to 0.3, y - t0 0.05-3); the #3329 scan has 16 points with y - t0 from 3e-6 to 3e-5; the small sw and st0 set has 240 points with sizes from 1e-8 to 0.1.Time, this PR / develop (gradients): 4-parameter lcdf 0.89, lccdf 0.62; 5-parameter lpdf 1.09-1.13; 7-parameter lpdf 0.07 (sw and st0), 0.09 (st0 only), 1.05 (sw only); 7-parameter lcdf 0.04-0.67, lccdf 0.06-0.87.
Not fixed (follow-up PR): the 7-parameter lcdf and lccdf integrate natural-scale values, so they return -inf where the cdf or ccdf is below about 1e-308 (the #3329 mechanism; for the lccdf this PR moves the limit from about 1e-16 of P to 1e-308); with sv, sw and st0 all non-zero, the 3-dimensional lcdf/lccdf integrals reach the evaluation limit of 6000 (errors up to 4.3e-5, as in develop).
Diff size against develop (43bc568), code:
stan/math/prim/functor/hcubature.hppstan/math/prim/prob/wiener4_lcdf_unnorm.hppstan/math/prim/prob/wiener4_lccdf_unnorm.hppstan/math/prim/prob/wiener5_lpdf.hppstan/math/prim/prob/wiener_full_lpdf.hppstan/math/prim/prob/wiener_full_lcdf_unnorm.hppstan/math/prim/prob/wiener_full_lccdf_unnorm.hppTests combined: 6 files, +454/−10. Four of these files are new (198 lines); the other two are
hcubature_test.cpp(+47) andwiener_full_test.cpp(+209/−10).Tests
New tests (each fails with the develop versions of the 7 headers):
hcubature_test(a step integrand, for which develop splits finished boxes again and counts them twice: 99.5 instead of 0.5; sqrt(x) and a 2-D Gaussian, which must stop at the tolerance, not at the evaluation limit); primwiener4_lcdf_unnorm_test(logMill); revwiener4_lcdf_unnorm_test(gradients, mpmath); revwiener4_lccdf_unnorm_test(small ccdf, mpmath); revwiener5_lpdf_test(small densities, mpmath); revwiener_full_test: the #3329 point and scan (values, d/dy, d/dst0, d/dsw), d/dsw with st0 > y - t0, 7-parameter lccdf with st0 > y - t0 and with a small ccdf, 7-parameter lccdf at log ccdf -120 and -299 (independent references; develop -inf), and the 7-parameter lcdf and lccdf below the smallest double (-inf, without the gradient integrals).Changed test: in
mathRevCorrectValues_wiener_lccdf_unnormthe reference of the third point (WienR) computes cdf(inf) - cdf and loses digits (the ccdf is 1.6e-14 of P): WienR -33.8123, mpmath -33.7956003. The row now has the mpmath values.All Wiener prim, rev and mix suites and the
hcubaturesuite pass.Side Effects
None.
Release notes
Improved the accuracy and speed of
wiener_lpdf(5- and 7-parameter),wiener_lcdf_unnormandwiener_lccdf_unnorm(4- and 7-parameter).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