|
bi-kappa 3.0.0
Monte Carlo particle samplers for plasma velocity and position distributions
|
Sampling primitives used to build the bi-Kappa radius. More...
Functions | |
| template<typename GeneratorType> | |
| int | engineBits () |
| Whole bits of entropy the engine delivers per call (32 for std::mt19937). | |
| template<typename RealType, typename GeneratorType> | |
| RealType | openCanonical (GeneratorType &gen) |
| Uniform on the open interval (0,1), by an explicit mapping from engine bits. | |
| template<typename RealType, typename GeneratorType> | |
| RealType | polarNormal (GeneratorType &gen, RealType &cache, bool &cached) |
| Standard normal by the Marsaglia polar method. | |
| template<typename RealType, typename GeneratorType> | |
| RealType | marsagliaTsangGamma (RealType shape, GeneratorType &gen, RealType &ncache, bool &ncached) |
| Gamma(shape, 1) for shape >= 1, by Marsaglia and Tsang (2000), ACM TOMS 26(3), 363-372. | |
| template<typename RealType, typename GeneratorType> | |
| RealType | logGamma (RealType a, GeneratorType &gen, RealType &ncache, bool &ncached) |
| log X for X ~ Gamma(a, 1), for every a > 0, without ever forming X. | |
| template<typename RealType> | |
| bool | materializeComponents (RealType logR, const std::array< RealType, 3 > &g, std::array< RealType, 3 > &point) |
| Materialize V_j = R g_j from log R, and report whether any component overflowed. | |
Sampling primitives used to build the bi-Kappa radius.
These live here, rather than being delegated to <random>, because the radius is a Gamma ratio whose denominator shape goes to zero as kappa -> 1/2. In that limit the denominator variate underflows to exactly zero and the draw is lost, so the loader carries log X2 and never materializes X2. Taking a logarithm forces three requirements that <random> does not meet:
Fixing all three here makes the sampled sequence a function of the engine alone.
|
inline |
Whole bits of entropy the engine delivers per call (32 for std::mt19937).
Definition at line 56 of file bi_kappa_distribution.H.
Referenced by openCanonical().
| RealType bikappa_detail::logGamma | ( | RealType | a, |
| GeneratorType & | gen, | ||
| RealType & | ncache, | ||
| bool & | ncached ) |
log X for X ~ Gamma(a, 1), for every a > 0, without ever forming X.
Uses the shape-boosting relation of Ahrens and Dieter (1974), X = Y U^{1/a} with Y ~ Gamma(a+1, 1) and U ~ Uniform(0,1), written additively:
log X = log Y + (log U) / a.
The product form is what underflows: at a = 0.01, U^{1/a} = U^{100} is already zero in double for U < 7.2e-4. The sum has no such loss, because log Y is order unity and (log U)/a is a moderate negative number. The relation holds for every positive shape, so this is one code path with no domain split and no fallback, and it is rejection-free at the boosting step: exactly one Gamma and one uniform per call.
log X is a stable representation of X, not an exact one. log Y carries about one unit in the last place, and (log U)/a about one unit in the last place of |log U|/a, so the absolute error of log X grows like eps * |log X| – equivalently the relative error of the radius grows like eps * |log R|, losing roughly one decimal digit per decade of dynamic range. Near the largest finite value |log R| is about 709.8 in double and 88.7 in float, so the relative error of a radius there is about 1e-13 in double and about 1e-5 in float. The uniform also has finite resolution, so |log U| cannot exceed about 36.7 (double) or 16.6 (float), and the sampled law is truncated below exp(-36.7/a). That region carries about 1.1e-16 of the probability and lies far outside the range in which any velocity component is representable.
Everything here is computed in RealType, the type of the instantiation.
Definition at line 184 of file bi_kappa_distribution.H.
References marsagliaTsangGamma(), and openCanonical().
Referenced by bi_kappa_distribution< RealType >::operator()().
| RealType bikappa_detail::marsagliaTsangGamma | ( | RealType | shape, |
| GeneratorType & | gen, | ||
| RealType & | ncache, | ||
| bool & | ncached ) |
Gamma(shape, 1) for shape >= 1, by Marsaglia and Tsang (2000), ACM TOMS 26(3), 363-372.
Expected attempts stay below 1.01 for every shape >= 1, so the cost does not depend on the shape – which is why the boosted shape below is always taken above one.
Definition at line 130 of file bi_kappa_distribution.H.
References openCanonical(), and polarNormal().
Referenced by logGamma(), and bi_kappa_distribution< RealType >::operator()().
| bool bikappa_detail::materializeComponents | ( | RealType | logR, |
| const std::array< RealType, 3 > & | g, | ||
| std::array< RealType, 3 > & | point ) |
Materialize V_j = R g_j from log R, and report whether any component overflowed.
This is the one place the loader decides what is representable, and it decides it from the value it is about to return rather than from that value's logarithm.
The obvious test – compare log|V_j| = log R + log|g_j| against log(max()) – is wrong by up to a rounding step, in both directions, and neither error is academic:
Comparing the materialized magnitude against max() cannot have either defect: it is the number that will be returned, so the test and the result agree by construction. It is also cheaper, because the common path takes no logarithm at all – one per component is needed only where R alone overflows, which is where the log domain is doing the work anyway and where there is no accuracy left to lose.
The decision is exact for the number returned, but that number carries the error of log R (see bikappa_detail::logGamma) and of g_j. A component whose exact value lies within that error of the largest finite value may therefore be returned as a finite number or as an infinity. In float that band is about 1e-5 relative, wider than the spacing of neighbouring floats at FLT_MAX (6e-8 relative); in double it is about 1e-13.
| logR | logarithm of the radius. |
| g | the order-unity vector, already rotated into the frame the caller returns. |
| point | receives the materialized components, +/-inf where one has no representation, with the sign the mathematics gives it. |
RealType. Definition at line 231 of file bi_kappa_distribution.H.
Referenced by bi_kappa_distribution< RealType >::operator()().
| RealType bikappa_detail::openCanonical | ( | GeneratorType & | gen | ) |
Uniform on the open interval (0,1), by an explicit mapping from engine bits.
b = digits - 1 random bits give an integer k uniform on [0, 2^b), and the value returned is (k + 1/2) / 2^b. Spending one bit fewer than the significand is what makes the arithmetic exact: k + 1/2 is then exactly representable and the division by a power of two is exact, so the result lies strictly inside (0,1) for every k – the smallest is 2^-(b+1), the largest 1 - 2^-(b+1). No endpoint is ever produced, so none is redrawn and none is clamped. Clamping is not a neutral choice: it replaces the end of the sampled law with an atom, which is what one standard library does for the value 1.
The bit costs one part in 2^53 (double) or 2^24 (float) of resolution, far below what log(U)/a can carry at any shape.
Definition at line 83 of file bi_kappa_distribution.H.
References engineBits().
Referenced by logGamma(), marsagliaTsangGamma(), bi_kappa_distribution< RealType >::operator()(), and polarNormal().
| RealType bikappa_detail::polarNormal | ( | GeneratorType & | gen, |
| RealType & | cache, | ||
| bool & | cached ) |
Standard normal by the Marsaglia polar method.
The second variate of each generated pair is cached, so cached must be cleared whenever the engine is reseeded – otherwise a reseeded stream opens with a variate belonging to the previous one.
Definition at line 104 of file bi_kappa_distribution.H.
References openCanonical().
Referenced by marsagliaTsangGamma().