|
bi-kappa 3.0.0
Monte Carlo particle samplers for plasma velocity and position distributions
|
Functions | |
| np.ndarray | log_radius_quantile (float kappa, exceed) |
| np.ndarray | log_shell_edges (float kappa) |
| tuple[np.ndarray, np.ndarray] | radius_shells (float kappa, int n) |
| np.ndarray | field_basis (np.ndarray bhat) |
| float | mad_relative_sigma (float kappa, int n) |
| dict | _holm_adjusted (dict pvalues) |
| float | _finite_number (str name, x) |
| dict | validate_sample (v, float kappa, float theta_perp, float theta_par, bhat=None, int|None n_attempts=None, float alpha=0.01) |
| str | format_report (dict report) |
| tuple[np.ndarray, str|None] | _read_text (str path, delimiter) |
| tuple[np.ndarray, str|None] | _load (str path, bool binary) |
| np.ndarray | load_sample (str path, bool binary=False) |
| argparse.ArgumentParser | _parser () |
| int | main (argv=None) |
Variables | |
| int | MIN_SAMPLES = 1000 |
| tuple | QUANTILE_PROBES = (0.5, 0.9, 0.99, 0.999) |
| int | N_RADIAL_BINS = 4 |
| int | N_COS_BINS = 5 |
| int | N_PHI_BINS = 8 |
| int | N_SHELLS = 10 |
| float | MIN_EXPECTED_PER_CELL = 5.0 |
| tuple | TAIL_EXCEEDANCE = (1e-2, 1e-3, 1e-4, 1e-5) |
| tuple | RAW_EXTENSIONS = (".bin", ".f64") |
| str | EPILOG |
Distributional validation of a bi-Kappa velocity sample.
This module tests a *sample*, not a sampler. Nothing in it assumes the
Gamma-ratio construction, so it applies to velocities produced by any bi-Kappa
loader -- inverse transform, acceptance-rejection, or scale mixture.
Input contract
--------------
``v`` (N, 3) array of velocities, in the simulation's global frame,
with N >= MIN_SAMPLES (1000).
``kappa`` shape index, > 1/2.
``theta_perp`` perpendicular thermal speed, > 0, in the units of ``v``.
``theta_par`` parallel thermal speed, > 0, in the units of ``v``.
``bhat`` magnetic-field direction, or ``None`` if the sample is already
field-aligned (i.e. the third column is the parallel one).
``n_attempts`` total draws attempted, if the loader enforced a velocity bound
by discarding and redrawing. Supplying it lets the report
quote the rejected fraction, which for a hard bound equals the
total-variation distance from the uncapped distribution.
``alpha`` family-wise significance level, 0 < alpha < 1.
The thermal speeds follow the convention of Eq. (2) of the accompanying paper,
f(v) ~ [1 + v_perp^2/(kappa theta_perp^2) + v_par^2/(kappa theta_par^2)]^-(kappa+1),
normalizable for every kappa > 1/2. A thermal speed is a speed, not a
temperature. A temperature-linked convention such as
theta^2 = 2[(kappa-3/2)/kappa](k_B T/m) is defined only for kappa > 3/2 and is
*not* interchangeable with this one; convert before calling.
The target is the uncapped distribution above. A sample from a loader that
rejects draws outside a velocity bound follows a different, capped distribution;
the tests then measure the effect of the bound.
What the tests establish, and what they do not
----------------------------------------------
The cell-count test of the accompanying paper (Sec. VI B) and the radial,
directional and independence tests below together pin down the
three-dimensional distribution, because a spherically symmetric density is
determined by its radial distribution plus a uniform, radius-independent
direction. They are distribution-free and use no moments, so they remain valid
on 1/2 < kappa <= 3/2 where the second moment does not exist. A pass is
evidence that the sample is consistent with the target at the given size; it is
not a proof of correctness, and it says nothing about a bound the loader may
have applied -- for that, supply ``n_attempts`` and read the tail quantiles.
Verdicts
--------
The nine distributional tests (seven when the sample is too small for the
cell-count test) form one family. Each has a p-value, and the family is
corrected with the Holm-Bonferroni step-down procedure: a test fails when its
Holm-adjusted p-value is at most ``alpha``, which keeps the probability that a
correct sample fails any of them at or below ``alpha`` (to the accuracy of the
asymptotic p-values). Two further tests have
their own criteria: the anisotropy ratio fails when it is more than four
standard errors from its expected value, and the finiteness test fails on any
non-finite draw. The tail quantiles and the largest normalized speeds are
diagnostics and carry no verdict.
Usage
-----
from bikappa_validate import validate_sample, format_report
report = validate_sample(v, kappa=2.0, theta_perp=1.0, theta_par=2.0)
print(format_report(report))
or from the command line (``--help`` lists the options and input formats):
python bikappa_validate.py sample.npy --kappa 2 --theta-perp 1 --theta-par 2
Exit status is 0 if every test passes, 1 if at least one test fails, and 2 if
the input or the options are invalid.
|
protected |
Definition at line 230 of file bikappa_validate.py.
Referenced by validate_sample().
|
protected |
Holm-Bonferroni adjusted p-values over a family of p-values.
With the m p-values sorted as p_(1) <= ... <= p_(m), the adjusted value of
p_(i) is max_{j <= i} min(1, (m - j + 1) p_(j)). Rejecting every test whose
adjusted p-value is at most alpha is the Holm step-down procedure at
family-wise level alpha.
Definition at line 213 of file bikappa_validate.py.
Referenced by validate_sample().
|
protected |
Definition at line 710 of file bikappa_validate.py.
References _read_text().
Referenced by load_sample(), and main().
|
protected |
Definition at line 780 of file bikappa_validate.py.
Referenced by main().
|
protected |
Read a text table, skipping one non-numeric header line if there is one. Blank lines and lines starting with '#' are ignored. If the first other line does not parse as numbers it is taken as a column header and skipped; any later non-numeric line is an error.
Definition at line 682 of file bikappa_validate.py.
Referenced by _load().
| np.ndarray field_basis | ( | np.ndarray | bhat | ) |
Orthonormal basis (e1, e2, e3) with e3 = bhat, as columns. Built the same way as the released C++ loader: start from (1,1,1), replace the component of largest |b_k| so the result is orthogonal to bhat. Picking the largest component keeps the division safe -- for a unit vector |b_k| >= 1/sqrt(3) -- including the axis-aligned cases. Any orthonormal basis with e3 = bhat serves for the tests below, since they depend on the perpendicular directions only through rotationally invariant statistics.
Definition at line 162 of file bikappa_validate.py.
Referenced by validate_sample().
| str format_report | ( | dict | report | ) |
Render a report as a short human-readable table. The distributional tests are listed with their statistic, raw p-value and Holm-adjusted p-value; each verdict is FAIL exactly when the adjusted p-value is at most alpha. The tests with their own criteria and the diagnostics without a verdict follow.
Definition at line 590 of file bikappa_validate.py.
Referenced by main().
| np.ndarray load_sample | ( | str | path, |
| bool | binary = False ) |
Read an (N, 3) velocity sample from a file. The format follows the extension: ``.bin`` and ``.f64`` are raw float64 values in the machine's byte order, three per draw (``binary=True`` forces this for any extension); ``.npy`` is a NumPy array of shape (N, 3); anything else is text with three columns, separated by whitespace, or by commas for ``.csv``. In text, blank lines and lines starting with '#' are ignored, and a first line that is not numeric (a header such as ``vx vy vz``) is skipped. Raises ``ValueError`` if the file does not hold an (N, 3) array of numbers, and ``OSError`` if it cannot be read.
Definition at line 732 of file bikappa_validate.py.
References _load().
| np.ndarray log_radius_quantile | ( | float | kappa, |
| exceed ) |
log of the radius r with P(R > r) = exceed under the target, elementwise. P(R > r) = P(W < w) with W = 1/(1+R^2) ~ Beta(kappa-1/2, 3/2), and R^2 = (1-w)/w. Near kappa = 1/2 the edge w lies below the smallest double -- w ~ 10^-500 for the 10^-5 edge at kappa = 0.51, and even the deciles underflow once kappa - 1/2 < 3e-3 -- so Beta.ppf returns 0 there. For such w the leading term of I_w(a, b) = w^a / (a B(a, b)) (1 + O(w)) is exact to double precision and gives log w directly. Comparing log R with these edges then needs no bounded transform of either, so nothing underflows however large R is.
Definition at line 121 of file bikappa_validate.py.
Referenced by log_shell_edges(), and radius_shells().
| np.ndarray log_shell_edges | ( | float | kappa | ) |
log of the radii splitting R into N_SHELLS shells of equal probability.
Definition at line 141 of file bikappa_validate.py.
References log_radius_quantile().
Referenced by radius_shells(), and validate_sample().
| float mad_relative_sigma | ( | float | kappa, |
| int | n ) |
Asymptotic relative standard error of a sample MAD, per component.
Each Cartesian marginal of Eq. (2) is a scaled Student-t distribution with
nu = 2 kappa - 1 degrees of freedom, so in units of its own scale the MAD is
t0 = ppf(3/4, nu). For a symmetric distribution the MAD is the median of |X|, whose
density at t0 is 2 f(t0), giving
sqrt(n) (MAD_n - t0) -> N(0, 1 / (16 f(t0)^2)).
The relative error therefore scales as 1/(4 sqrt(n) t0 f(t0)), which blows up
as kappa -> 1/2 because the marginal flattens: a fixed percentage tolerance
would be far too tight there and far too loose at large kappa.
Definition at line 193 of file bikappa_validate.py.
Referenced by validate_sample().
| int main | ( | argv = None | ) |
Definition at line 812 of file bikappa_validate.py.
References _load(), _parser(), format_report(), main(), and validate_sample().
| tuple[np.ndarray, np.ndarray] radius_shells | ( | float | kappa, |
| int | n ) |
log edges and probabilities of the shells for the test of the radius alone. The N_SHELLS decile shells, with the outermost divided at those TAIL_EXCEEDANCE edges beyond which n draws expect at least MIN_EXPECTED_PER_CELL.
Definition at line 146 of file bikappa_validate.py.
References log_radius_quantile(), and log_shell_edges().
Referenced by validate_sample().
| dict validate_sample | ( | v, | |
| float | kappa, | ||
| float | theta_perp, | ||
| float | theta_par, | ||
| bhat = None, | |||
| int | None | n_attempts = None, | ||
| float | alpha = 0.01 ) |
Run the tests on an (N, 3) velocity sample. See the module docstring. Returns a nested dict of per-test statistics and verdicts, plus a top-level ``passed`` flag. Raises ``ValueError`` on a malformed input contract.
Definition at line 240 of file bikappa_validate.py.
References _finite_number(), _holm_adjusted(), field_basis(), log_shell_edges(), mad_relative_sigma(), and radius_shells().
Referenced by main().
| str bikappa_validate.EPILOG |
Definition at line 748 of file bikappa_validate.py.
| float bikappa_validate.MIN_EXPECTED_PER_CELL = 5.0 |
Definition at line 111 of file bikappa_validate.py.
| int bikappa_validate.MIN_SAMPLES = 1000 |
Definition at line 91 of file bikappa_validate.py.
| int bikappa_validate.N_COS_BINS = 5 |
Definition at line 103 of file bikappa_validate.py.
| int bikappa_validate.N_PHI_BINS = 8 |
Definition at line 104 of file bikappa_validate.py.
| int bikappa_validate.N_RADIAL_BINS = 4 |
Definition at line 102 of file bikappa_validate.py.
| int bikappa_validate.N_SHELLS = 10 |
Definition at line 110 of file bikappa_validate.py.
| tuple bikappa_validate.QUANTILE_PROBES = (0.5, 0.9, 0.99, 0.999) |
Definition at line 95 of file bikappa_validate.py.
| tuple bikappa_validate.RAW_EXTENSIONS = (".bin", ".f64") |
Definition at line 679 of file bikappa_validate.py.
| tuple bikappa_validate.TAIL_EXCEEDANCE = (1e-2, 1e-3, 1e-4, 1e-5) |
Definition at line 118 of file bikappa_validate.py.