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 PR was assisted by Claude
Summary
This PR fixes the underflow of #3329, and other accuracy defects of the Wiener functions that I found while I tested a fix for it. It is an alternative to #3435 (comparison: #3435 (comment)).
Functions and files (in
stan/math/prim/prob; each function has an optional last argument for the precision):wiener_lpdf(y | alpha, tau, beta, delta)wiener_lpdf.hppwiener_lpdf(y | a, t0, w, v, sv)wiener5_lpdf.hppwiener_lpdf(y | a, t0, w, v, sv, sw, st0)wiener_full_lpdf.hpphcubatureintegrals of the 5-parameter density over sw and st0; the 5-parameter lpdf if sw = st0 = 0wiener_lcdf_unnorm(y, a, t0, w, v)wiener4_lcdf_unnorm.hpplogMillwiener_lccdf_unnorm(y, a, t0, w, v)wiener4_lccdf_unnorm.hppwiener_lcdf_unnorm(y, a, t0, w, v, sv, sw, st0)wiener_full_lcdf_unnorm.hpphcubatureintegrals of the 4-parameter cdf over sv, sw and st0; the 4-parameter lcdf if sv = sw = st0 = 0wiener_lccdf_unnorm(y, a, t0, w, v, sv, sw, st0)wiener_full_lccdf_unnorm.hppBecause of these calls, fixes in the 5-parameter functions also reach the 7-parameter lpdf, and fixes in the 4-parameter cdf also reach the 4-parameter lccdf and the 7-parameter lcdf and lccdf (table below).
7-parameter lpdf, #3329. The integrands are 5-parameter densities on the natural scale, which underflow to 0 below a log density of about -708. This PR scales them per observation by exp(-c), with c the largest 5-parameter log density on a 3 x 3 grid of the integration box (17 x 17 if the scaled integral is outside [1e-100, 1e100]). The log density is c + log(integral); the partials are ratios of integrals, so c cancels, and the series tolerances are relative to exp(c).
wiener5_densityandwiener5_grad_*get an optionallog_shiftargument (default 0). All autodiff types use the same code path with the existinghcubature.hcubature (used only by the three 7-parameter functions). Two defects: the loop condition used the error and value of the first box, so once that box failed the test, the loop ran to the evaluation limit; and a split box stayed in the list with error 0 and could be split and counted again. The integration now stops at the requested tolerance, which 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, with value errors up to 1e-2 at ordinary points:
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; and the gradient series of t, a and w used an absolute tolerance.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, and up to 3e-3 in the 7-parameter lcdf and lccdf.
logMillnow useserfcxfor x >= 0.4-parameter lccdf. P - F loses all digits where F is close to P (-inf at 8 of 300 ordinary points). Where F >= P/2 the function now uses the large-time series of P - F and its derivatives.
7-parameter lcdf and lccdf. The integration tolerance is at most 0.9e-6, and the d/dt series tolerance is relative to the value. The lccdf now includes the trials whose non-decision time ends after y; they contribute P, and without them the log ccdf was off by up to 1.4 where st0 > y - t0. The lccdf integrates the 4-parameter ccdf, 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 log ccdf -39, -120 and -299). Where the value integral underflows to 0, the value is -inf and the gradient integrals are skipped.
Fixes in shared code 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 (sw = st0 = 0) or the 4-parameter lcdf (sv = sw = st0 = 0).
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, my own adaptive Gauss-Kronrod integration of the 5-parameter density (not
hcubature), with gradients under the integral or by finite differences. Test 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, 16 points with y - t0 from 3e-6 to 3e-5; 240 points with sw or st0 from 1e-8 to 0.1.Run time with gradients, this PR / develop: 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 and return -inf below a cdf or ccdf of 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 integrals reach the evaluation limit of 6000 (errors up to 4.3e-5, as in develop).
Diff against develop (43bc568): code 7 files, +542/−149 (most in
wiener4_lccdf_unnorm.hpp+227/−49 andwiener_full_lpdf.hpp+162/−35); tests 6 files, +454/−10 (4 new files, 198 lines;hcubature_test.cpp+47;wiener_full_test.cpp+209/−10).Tests
Each new test fails with the develop versions of the 7 headers:
hcubature_test: a step integrand, where develop counts split boxes twice (99.5 instead of 0.5); sqrt(x) and a 2-D Gaussian, which must stop at the tolerance.wiener4_lcdf_unnorm_test(prim:logMill; rev: gradients),wiener4_lccdf_unnorm_test(small ccdf) andwiener5_lpdf_test(small densities), against mpmath.wiener_full_test: the wiener_lpdf full model returns -inf for valid-support inputs in variability branches #3329 point and scan (value, d/dy, d/dst0, d/dsw); d/dsw with st0 > y - t0; the 7-parameter lccdf with st0 > y - t0, with a small ccdf, and at log ccdf -120 and -299 (develop: -inf); the 7-parameter lcdf and lccdf below the smallest double (-inf, without the gradient integrals).Changed test: the third point of
mathRevCorrectValues_wiener_lccdf_unnormcame from WienR, which computes cdf(inf) - cdf and loses digits there (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