-
Notifications
You must be signed in to change notification settings - Fork 27
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
Numerically stable computation of invariant mass #188
Comments
That would be great to add! Worse than not being numerically stable, Vector doesn't yet have a function for computing the invariant mass of two 4-vectors independently of the other three components. That is, users are currently computing (vector1 + vector2).mass which computes all four components of mass = sqrt(2*pt1*pt2*(cosh(eta1 - eta2) - cos(phi1 - phi2))) but that has the numerically unstable subtraction (as well as an approximation that the mass components of Adding a function to Vector is a bit of a process. It supports many backends by duck-typing: the source code in the These functions are currently being used on Python scalars, NumPy arrays, Awkward Arrays, and in Numba (all of which use Another dimension that scales Vector functions is that we have a set of 2 × 3 × 2 = 12 coordinate systems:
That is, the azimuthal coordinates are independent of all others, longitudinal components depend only on the longitudinal direction and azimuthal, and temporal components depend on all three. This makes it impossible to use "r" (3D spatial distance) as a stored coordinate, but everything else that we typically want is available (e.g. proper time with any combination of Cartesian, polar, and pseudorapidity). Unfortunately, that blows up the number of formulas that in principle need to be written in the compute submodule, especially for 4D vectors, especially for multiple inputs (an invariant mass calculation has two vector inputs). For 2D and to a lesser extent, 3D, we often write out specialized formulas when they're available in a combination of coordinate systems, but most 4D calculations either use 2D and 3D functions directly or do coordinate transformations before computing if necessary. Here's an example: the vector/src/vector/_compute/planar/dot.py Lines 28 to 41 in b9c60a8
has an explicit formula when both vectors are Cartesian or when both vectors are polar, but if one is Cartesian and the other is polar, it converts to Cartesian. Here, vector/src/vector/_compute/planar/dot.py Line 16 in b9c60a8
and the Methods on vectors look up compute functions through a dict, which is keyed on combinations of coordinate systems and includes signature information (this function returns vector/src/vector/_compute/planar/dot.py Lines 44 to 49 in b9c60a8
Some of the 3D and 4D functions use the lookup programmatically to generate all of the coordinate system combinations. ( The 2D implementation has 4 functions because there are 2 azimuthal coordinate systems and 2 input vectors. The 3D implementation has 6 coordinate systems (raised to the power of) 2 input vectors = 36 functions. vector/src/vector/_compute/planar/dot.py Lines 44 to 49 in b9c60a8
Four of these are specialized and the others are defined in terms of them. The 4D implementation has 12 coordinate systems (raised to the power of) 2 input vectors = 144 functions. We don't actually write out those 144 functions; they're generated at startup: vector/src/vector/_compute/lorentz/dot.py Lines 38 to 139 in b9c60a8
This loop fills the That's not to say that you need to write all of that—I just wanted to make you aware of it so that I can ask, "In what coordinate systems would it be pragmatic/worthwhile to derive your numerically stable formula?" |
I recently developed a formula to compute the invariant mass for a two-body decay in a numerically stable way from the momenta of the daughters and their assumed masses. The algorithm is described and benchmarked here:
https://github.com/HDembinski/essays/blob/master/Numerically%20stable%20calculation%20of%20invariant%20mass.ipynb
The numerical instability arises from the subtractive cancellation in this formula
for m1,m2 << |p1|, |p2|.
I am not sure whether this fits in here, but I wanted to advertise it. As you can see in my notebook, my formula improves the accuracy dramatically over the naive formula. It further allows one to perform the whole calculation in single precision instead of double precision, which frees CPU registers and should accelerate code that benefits from SIMD instructions. Single precision is sufficiently accurate to describe the resolution of experimental data, we only habitually convert to double precision because we usually do not use numerically stable formulas.
The text was updated successfully, but these errors were encountered: