Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/issue 2966 add 7 parameter ddm cdf and ccdf #3042

Open
wants to merge 58 commits into
base: develop
Choose a base branch
from

Conversation

Franzi2114
Copy link
Collaborator

@Franzi2114 Franzi2114 commented Mar 29, 2024

Summary

With this PR the CDF and the CCDF of the 7-parameter diffuion model are added.
See issue #2966
Relates to issue #2822

Tests

We implemented analogous tests as for the PDF

Side Effects

no

Release notes

CDF and CCDF for the 7-parameter diffusion model. Allows modeling truncated and censored data.

Checklist

  • Copyright holder: Franziska Henrich, Christoph Klauer

    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

    • unit tests pass (to run, use: ./runTests.py test/unit)
    • header checks pass, (make test-headers)
    • dependencies checks pass, (make test-math-dependencies)
    • docs build, (make doxygen)
    • code passes the built in C++ standards checks (make cpplint)
  • the code is written in idiomatic C++ and changes are documented in the doxygen

  • the new changes are tested

@Franzi2114
Copy link
Collaborator Author

Few Qs

You can continue. I changed some aspects regarding the structure.

@stan-buildbot
Copy link
Contributor


Name Old Result New Result Ratio Performance change( 1 - new / old )
arma/arma.stan 0.19 0.18 1.07 6.12% faster
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.01 0.01 1.15 13.19% faster
gp_regr/gen_gp_data.stan 0.02 0.02 1.08 7.25% faster
gp_regr/gp_regr.stan 0.11 0.1 1.06 5.93% faster
sir/sir.stan 76.72 74.37 1.03 3.06% faster
irt_2pl/irt_2pl.stan 3.84 3.97 0.97 -3.56% slower
eight_schools/eight_schools.stan 0.05 0.05 1.01 1.3% faster
pkpd/sim_one_comp_mm_elim_abs.stan 0.25 0.24 1.04 3.42% faster
pkpd/one_comp_mm_elim_abs.stan 18.02 17.71 1.02 1.72% faster
garch/garch.stan 0.45 0.44 1.03 2.53% faster
low_dim_gauss_mix/low_dim_gauss_mix.stan 2.76 2.73 1.01 1.29% faster
arK/arK.stan 1.63 1.6 1.02 2.1% faster
gp_pois_regr/gp_pois_regr.stan 2.48 2.47 1.0 0.45% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 9.16 8.85 1.04 3.43% faster
performance.compilation 175.17 176.97 0.99 -1.02% slower
Mean result: 1.0341338404824363

Jenkins Console Log
Blue Ocean
Commit hash: fb02dc7240781e4fe7ea4bc1fb4e06e70079765a


Machine information No LSB modules are available. Distributor 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 6148 CPU @ 2.40GHz
Stepping: 4
CPU MHz: 2400.000
CPU max MHz: 3700.0000
CPU min MHz: 1000.0000
BogoMIPS: 4800.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 Itlb multihit: KVM: Mitigation: VMX disabled
Vulnerability L1tf: Mitigation; PTE Inversion; VMX conditional cache flushes, SMT vulnerable
Vulnerability Mds: Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Meltdown: Mitigation; PTI
Vulnerability Mmio stale data: Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Retbleed: Mitigation; 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; IBRS, IBPB conditional, STIBP conditional, RSB filling, PBRSB-eIBRS Not affected
Vulnerability Srbds: Not affected
Vulnerability Tsx async abort: Mitigation; Clear CPU buffers; SMT vulnerable
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 invpcid_single pti intel_ppin ssbd mba ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm 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 pku ospke 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

Copy link
Collaborator

@SteveBronder SteveBronder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a few Qs about the design of some of the functions. I'm seeing a lot of if statements on conditions. It's unclear which exist as an optimization and which exist to stop NaN or Infinite.

If it's an optimization then tbh I'd rather we remove it and just have one path. Often these branches are very expensive for the compiler to take since they can change each iteration. Having one linear path makes things simpler for the compiler even if it involves a extra computation.

If there are places where there are massive saves from a branch then I'm open to it if they have greater than or less than as those are not unlikely. But the odds of a parameter being an exact value are very low so those branches have a very low odds of being taken

Comment on lines 123 to 124
inline auto wiener_prob_grad_a(const T_a& a, const T_v& v,
const T_w& w) noexcept {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used once so I'd inline this as well. I think it would be fine to just comment above the section

// derivative of the wiener probability w.r.t. 'a' (on log-scale)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I inlined the three partial derivatives.

*/
template <typename T_a, typename T_w, typename T_v>
inline auto wiener_prob(const T_a& a, const T_v& v_value,
const T_w& w_value) noexcept {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd remove the noexcepts on these. It's unlikely, but for any function that can hold an autodiff type we call malloc sometimes and our code to call malloc can throw an exception if the user runs out of memory. The exception is a program terminating one, but with noexcept here the program would just crash if it hit the exception instead of reporting the out of memory issue.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. For which functions shall I delete the noexcept? I now deleted it for wiener_prob. When do we use noexcept?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You only want to use noexcept when the function is guranteed to not throw an exception. It just tells the compiler, "I promise I cannot throw an exception here" so then the compiler can do optimizations.

For anything that uses Eigen matrices we can possibly throw an exception. It's good for simpler functions where you can say for certain there cannot be an exception thrown.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. But in this wiener_prob function I do not see a term like Eigen or Matrix. And in the other functions in this file neither. So, every function can keep its noexcept?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but it can call malloc and that would cause an exception

Comment on lines +33 to +37
if (exponent < 0) {
return ret_t(log1m_exp(exponent) - log_diff_exp(2 * v * a * w, exponent));
} else {
return ret_t(log1m_exp(-exponent) - log1m_exp(2 * v * a));
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm kind of confused by these cutpoints. Is this because the derivative is ill defined at certain areas or is this a math optimization

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is a math optimization and should stay.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The if statement here could be more more expensive than the ops that are saved. I'd remove all of these and just keep things simple. It also just becomes really hard to read and maintain with all of these if statements in the code

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Results are more robust when we have this case distinction. They both shall compute the same result, but when exponent < 0 then the upper case is more robust and when exponent >=0 the lower case is more robust. We could insert a comment on this to make the case distinction clear.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by robust here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Numerically robust.

Comment on lines 126 to 128
if (fabs(v) == 0.0) {
return ret_t(0.0);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the odds v will be exactly 0? I think this path is unlikely and would remove it.

In general, the odds a parameter takes on any given single value is very low so I would remove if statements like this. 99.9% of the time they won't be taken and forces the compiler to do a check and branch that is often unnecessary

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user can call the model with a fixed value for v. In some cases the user could want to set v=0 for his tests. Then, v will not be estimated and is constant at 0. For this case we need these if-statements.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would a value of 0 for v cause the rest of the code to error out? If not then remove these if statements

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed all spots where it is not necessary. One spot is still needed in wiener_prob to not cause an error.

inline auto wiener_prob_derivative_term(const T_a& a, const T_v& v_value,
const T_w& w_value) noexcept {
using ret_t = return_type_t<T_a, T_w, T_v>;
const auto exponent_m1 = log1p(-1.1 * 1.0e-8);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does this hard coded value come from?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hard coded value is connected to the internal precision of this computation.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it have 1e-8 precision? I'm asking where that number comes from

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, I have to correct myself.
This term serves also numerical stability. We test whether the exponents are larger than this small value. If this is the case, then they are negative and very near to 0. In the limit, when the exponents go to 0, the result is -w. As we later have to divide by the exponents, we would have to divide by nealry zero. Therefore, we do this check and return -w for exponents very near to zero to make compuatations more stable.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going to have to tag @bob-carpenter here as I'm unsure of how we usually handle things like this.

Bob the value exponent_m1 is essentially -1.1e-08. When we do the calculations starting on line 62 we check that each of the exponents are greater than this value. If any of those values are less than -1.1e-08 then the code returns -w.

As we later have to divide by the exponents, we would have to divide by nearly zero.

Sorry where in this code is the divide happening? Everything is on the log scale here so division is just turning into subtraction

@stan-buildbot

This comment was marked as off-topic.

@Franzi2114
Copy link
Collaborator Author

@SteveBronder: Is the code better now?
Why does the stan-buildbot comment after each push?

@stan-buildbot

This comment was marked as off-topic.

@stan-buildbot

This comment was marked as off-topic.

@Franzi2114
Copy link
Collaborator Author

Hey @SteveBronder, is the code ok now or shall I change some more things?

@Franzi2114
Copy link
Collaborator Author

Hey @SteveBronder, which changes would you like to have before continuing?

Copy link
Collaborator

@SteveBronder SteveBronder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had another look today. @andrjohns can you look at this code? I'm not super familiar with why a lot of the branches here exist so it's pretty difficult for me to review

stan/math/prim/prob/wiener4_lccdf.hpp Outdated Show resolved Hide resolved
stan/math/prim/prob/wiener4_lccdf.hpp Outdated Show resolved Hide resolved
inline auto wiener_prob_derivative_term(const T_a& a, const T_v& v_value,
const T_w& w_value) noexcept {
using ret_t = return_type_t<T_a, T_w, T_v>;
const auto exponent_m1 = log1p(-1.1 * 1.0e-8);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going to have to tag @bob-carpenter here as I'm unsure of how we usually handle things like this.

Bob the value exponent_m1 is essentially -1.1e-08. When we do the calculations starting on line 62 we check that each of the exponents are greater than this value. If any of those values are less than -1.1e-08 then the code returns -w.

As we later have to divide by the exponents, we would have to divide by nearly zero.

Sorry where in this code is the divide happening? Everything is on the log scale here so division is just turning into subtraction

stan/math/prim/prob/wiener4_lccdf.hpp Outdated Show resolved Hide resolved
stan/math/prim/prob/wiener4_lccdf.hpp Outdated Show resolved Hide resolved
Comment on lines +16 to +19
template <typename T_x>
inline auto rexp(T_x&& x) noexcept {
return (x <= 700) ? exp(x) : exp(700);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tagging @andrjohns do we do things like this anywhere else in Stan math? tmk we just check if it would overflow and if so we normally just return +/- inf or we let it overflow

stan/math/prim/prob/wiener4_lcdf.hpp Outdated Show resolved Hide resolved
stan/math/prim/prob/wiener4_lcdf.hpp Outdated Show resolved Hide resolved
stan/math/prim/prob/wiener4_lcdf.hpp Outdated Show resolved Hide resolved
inline auto log_probability_distribution(const T_a& a, const T_v& v,
const T_w& w) noexcept {
using ret_t = return_type_t<T_a, T_w, T_v>;
auto nearly_one = ret_t(1.0 - 1.0e-6);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bob-carpenter this is another spot where I'm getting a bit confused. Here the code is checking if we are within 1.0e-6 of 1 and then branching off of that. How do we normally handle cases like this?

@Franzi2114
Copy link
Collaborator Author

Hey @SteveBronder, I worked through your suggestions and pushed a new version. Is it good for these points?
@andrjohns and @bob-carpenter, what do you think on the points above?

@stan-buildbot
Copy link
Contributor


Name Old Result New Result Ratio Performance change( 1 - new / old )
arma/arma.stan 0.35 0.35 1.01 1.37% faster
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.01 0.01 0.99 -0.98% slower
gp_regr/gen_gp_data.stan 0.03 0.02 1.21 17.51% faster
gp_regr/gp_regr.stan 0.1 0.1 1.01 1.23% faster
sir/sir.stan 70.03 70.0 1.0 0.04% faster
irt_2pl/irt_2pl.stan 4.13 4.11 1.01 0.52% faster
eight_schools/eight_schools.stan 0.06 0.06 0.98 -2.11% slower
pkpd/sim_one_comp_mm_elim_abs.stan 0.25 0.25 0.98 -2.24% slower
pkpd/one_comp_mm_elim_abs.stan 19.14 18.6 1.03 2.84% faster
garch/garch.stan 0.43 0.41 1.05 4.87% faster
low_dim_gauss_mix/low_dim_gauss_mix.stan 2.68 2.59 1.04 3.53% faster
arK/arK.stan 1.78 1.7 1.05 4.6% faster
gp_pois_regr/gp_pois_regr.stan 2.82 2.72 1.04 3.62% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 8.65 8.34 1.04 3.66% faster
performance.compilation 182.65 181.36 1.01 0.7% faster
Mean result: 1.029312367432566

Jenkins Console Log
Blue Ocean
Commit hash: 1e343a702bf5d5690bf6d77b9ea703b43463162b


Machine information No LSB modules are available. Distributor 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 6148 CPU @ 2.40GHz
Stepping: 4
CPU MHz: 2400.000
CPU max MHz: 3700.0000
CPU min MHz: 1000.0000
BogoMIPS: 4800.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 Itlb multihit: KVM: Mitigation: VMX disabled
Vulnerability L1tf: Mitigation; PTE Inversion; VMX conditional cache flushes, SMT vulnerable
Vulnerability Mds: Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Meltdown: Mitigation; PTI
Vulnerability Mmio stale data: Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Retbleed: Mitigation; 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; IBRS, IBPB conditional, STIBP conditional, RSB filling, PBRSB-eIBRS Not affected
Vulnerability Srbds: Not affected
Vulnerability Tsx async abort: Mitigation; Clear CPU buffers; SMT vulnerable
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 invpcid_single pti intel_ppin ssbd mba ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm 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 pku ospke 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants