Reproducible random sampling in PmagPy#
Many PmagPy functions involve random number generation — drawing directions from a Fisher distribution, bootstrap resampling, Monte Carlo simulations, and generating synthetic datasets from the TK03.GAD secular variation model. By default, these functions produce different results each time they are called. The optional random_seed parameter makes any of these analyses exactly reproducible.
Quick start#
Pass an integer to random_seed and the output is deterministic:
import pmagpy.ipmag as ipmag
# Two calls with the same seed produce identical results
data_a = ipmag.fishrot(k=30, n=50, dec=0, inc=45, random_seed=42)
data_b = ipmag.fishrot(k=30, n=50, dec=0, inc=45, random_seed=42)
# data_a and data_b are identical
# Without random_seed (or random_seed=None), each call gives different results
data_c = ipmag.fishrot(k=30, n=50, dec=0, inc=45)
data_d = ipmag.fishrot(k=30, n=50, dec=0, inc=45)
# data_c and data_d differ
Functions that accept random_seed#
ipmag (high-level, notebook-friendly)#
Function |
Purpose |
|---|---|
|
Fisher-distributed random directions |
|
Resample directions from a Fisher mean |
|
Kent-distributed random directions |
|
TK03.GAD statistical field model |
|
Bootstrap fold test (Tauxe and Watson, 1994) |
|
Bootstrap confidence region for a mean direction |
|
Bootstrap test for a common mean |
|
Bootstrap common mean test (Heslop et al., 2023) |
|
Watson common mean test with Monte Carlo |
|
Bootstrap reversal test |
|
Bootstrap reversal test (Heslop et al., 2023) |
|
Secular variation of the Earth’s magnetic field |
|
Elongation/inclination inclination-shallowing correction with bootstrap |
|
E/I correction with a Kent confidence ellipse on the corrected pole |
|
Pole correction by resampling a compilation of flattening factors |
|
McFadden and McElhinny (1990) reversal test with Monte Carlo critical values |
|
Bogue and Coe (1981) probabilistic correlation via simulation |
|
Bogue and Coe (1981) probabilistic correlation via random directions |
|
Monte Carlo plate rate estimation |
pmag (low-level)#
Function |
Purpose |
|---|---|
|
Fisher-distributed deviate (scalar or array kappa) |
|
Kent-distributed deviate |
|
Gaussian random samples |
|
Uniformly distributed random directions |
|
Bootstrap resample of a directional dataset |
|
Bootstrap resample of a generic list |
|
Bootstrap means for directional data |
|
Bootstrap means for a directional DataFrame |
|
Bootstrap resample of anisotropy tensors |
|
Bootstrap parameters for anisotropy data |
|
VGP scatter (Sf) with bootstrap or within-site correction |
|
Generate Gauss coefficients from TK03 |
How it works#
The random_seed parameter accepts three types of input:
Input |
Behavior |
|---|---|
|
Fresh entropy on every call — non-reproducible, same as previous PmagPy behavior |
|
Creates a seeded |
|
Uses the provided Generator directly — for threading RNG state through call chains |
Internally, PmagPy uses the modern numpy.random.Generator API (numpy.random.default_rng) rather than the legacy numpy.random.seed approach. This means:
No global state is modified — setting
random_seedin one function cannot affect other unrelated code.Multiple seeded analyses can run concurrently without interfering with each other.
The default behavior (
random_seed=None) is identical to previous PmagPy versions.
Examples#
Reproducible Fisher directions#
data = ipmag.fishrot(k=50, n=200, dec=340, inc=60, random_seed=99)
ipmag.plot_net(1)
ipmag.plot_di(di_block=data)
Reproducible bootstrap common mean test#
data1 = ipmag.fishrot(k=40, n=20, dec=40, inc=60, random_seed=0)
data2 = ipmag.fishrot(k=45, n=20, dec=42, inc=58, random_seed=1)
result = ipmag.common_mean_bootstrap(data1, data2, random_seed=2)
Reproducible TK03.GAD secular variation model#
directions = ipmag.tk03(n=500, dec=0, lat=45, rev='no', random_seed=7)