geoml.stats
The package generator
One seed governs everything: call set_seed()
before the objects are built, and parameter initialization, the
training draws and the simulation stream all follow from it. A model’s
options draw their own seed from this generator at construction, and a
saved model keeps the number it drew, so its simulations replay on reload.
The generator that every random initialization draws from.
Parameters are initialized at random when an object is built — a latent node, a kernel’s transform, an orthonormal matrix — which happens before any model exists to carry options.seed. So the draws come from one generator held here, seeded with set_seed before the objects are built:
import geoml geoml.set_seed(1234)
latent = geoml.latent.BasicGP(data, kernel=kernel) model = geoml.models.VGPNetwork(data, variables, likelihoods, latent)
Building the same objects again after the same call gives the same starting parameters. Left alone, the generator is seeded from the operating system, so each run differs — as it did before.
Training and prediction reach this generator through one more step: a model’s options draw their seed from here when built, and stateless TensorFlow sampling turns that one number into every training draw and simulation. So the call above is the single knob — it fixes the initial parameters, the training trajectory and the simulation stream alike, and a saved model keeps the seed it drew.
- geoml.stats.random.set_seed(seed)[source]
Seeds the generator used to initialize parameters.
The one seed a model has: parameter starts, the data-dependent starts of the warpings, and the seed a model’s options draw for training and simulation all come from this generator. Called before a model is built, it makes the whole run reproducible.
- Parameters:
seed (int) – The seed. None draws a fresh one from the operating system, which is how the generator starts out.
Notes
This must be called before the objects are built, since that is when the initial values are drawn. It replaces the generator, so anything already built keeps the values it was given.
- geoml.stats.random.rng()[source]
The generator every random initialization draws from.
- Returns:
numpy.random.Generator
- geoml.stats.random.sobol_engine(dimension, seed)[source]
A scrambled Sobol engine, however SciPy spells its seed argument.
SciPy is renaming that argument from seed to rng (SPEC 7): the new name works today and the old one is on its way out, so both are tried here and the same call runs either side of the change. The sequence a given SciPy produces is unaffected – the scramble is drawn from the same seed by the same construction under either name.
- Parameters:
dimension (int) – How many dimensions the points have.
seed (int or numpy.random.Generator) – What the scramble is drawn from, so that the rule is fixed rather than random.
- Returns:
scipy.stats.qmc.Sobol
The censored simplex
The row forms behind likelihood.CensoredSimplex, on whole batches and in
log space: a row’s positive parts on the fibre of the warping’s way back,
its zeros below their threshold, and its missing parts integrated over the
faces of what the observed parts leave of the whole.
The censored likelihood of a composition on the simplex.
A row of shares x is the image of a latent row w under a simplex
warping’s way back, x_k = m_k [1 + alpha (w_k - tau)]_+ ** (1/alpha),
tau making the parts sum to one; after it, increasing links psi_k take
w_k to the space where the model’s latent f_k and Gaussian noise live,
psi_k(w_k) = f_k + sigma_k xi_k. The way back ignores a shift of the
whole row, so the likelihood of x integrates that shift out in one
dimension. A positive part sits on the fibre, w_k = b_k + tau (b_k
the warping’s forward value); a zero part lies below the point where it
vanishes, w_k <= tau + t0 (t0 = -1/alpha), and scores a probability;
a part not assayed is integrated over every value it could take, given
what the observed parts leave of the whole (R).
Everything here is in log space and in float64, and works on whole batches: n rows, S latent realizations, T nodes in tau.
References: Butler, A. and Glasbey, C. (2008), A latent Gaussian model for
compositional data with zeros, JRSS C 57(5), 505-520 (the model family);
Farinhas, A., Aziz, W., Niculae, V. and Martins, A. F. T. (2022), Sparse
communication via mixed distributions, ICLR, arXiv:2108.02658, Appendix B,
eqs. 27-28 (this likelihood at alpha = 1, a uniform scale and Gaussian
noise); the research report scaled_simplex_entmax.tex (project
“Alternative CoDA transformations”), equations (eq:loglik), (eq:missing)
and (eq:general), for the weighted Box-Cox family, parts not assayed and
other noise families.
- class geoml.stats.censored.Simplex(warping)[source]
Bases:
objectWhat the likelihood reads off a simplex warping, part by part.
columns in every method names the part each value belongs to, with the shape of the values; the scale and the power are gathered for them, so a value may be of any part.
- parts(x, columns)[source]
b, the forward value of positive shares x, and log g, the log of
dx / db.
The share a part with forward value b holds: the way back for one part, its threshold already taken out.
- class geoml.stats.censored.Links(links, size)[source]
Bases:
objectThe increasing elementwise links after the simplex warping, read part by part.
links is a warping, a list of them applied in order, or None. An affine chain is read off two points per part, so a value of any part costs one multiplication; any other chain is evaluated on whole rows, each value placed in its own column of a matrix as tall as the most values one part holds.
- class geoml.stats.censored.Rule(n_tau=32, n_legendre=24, n_sobol=1024, seed=0, n_guide=8, n_scan=256)[source]
Bases:
objectHow many nodes each integral takes.
- geoml.stats.censored.patterns(y, observed, max_hidden=6)[source]
Each row’s parts as positive, zero and hidden, the remainder the hidden parts share, and which rows are scored.
A row whose observed parts already make up the whole has its hidden parts at zero. A row with no observed part, or more than max_hidden hidden ones, is not scored.
- geoml.stats.censored.log_likelihood(y, observed, latent, noise_sd, simplex, links, rule, max_hidden=6)[source]
The log-likelihood of each row at each latent realization.
- Parameters:
y (tensor) – (n, D), shares of the whole, anything where not observed.
observed (tensor) – (n, D), boolean or 0/1.
latent (tensor) – (n, D, S), the latent realizations, on the links’ side.
noise_sd (tensor) – (D,), the noise’s standard deviation per part.
simplex (Simplex)
links (Links)
rule (Rule)
max_hidden (int) – The most hidden parts a scored row may have.
- Returns:
log_lik (tensor) – (n, S), zero on the rows not scored.
scored (tensor) – (n,), boolean.
Distributions
Custom TensorFlow-Probability distributions, used by the likelihoods and the spline warpings.
- class geoml.stats.probability.EpsilonInsensitive(loc, scale, epsilon, validate_args=False, allow_nan_stats=True, name='EpsilonInsensitive')[source]
Bases:
DistributionA custom implementation of the Epsilon Insensitive distribution (Gonçalves et al., 2022).
- __init__(loc, scale, epsilon, validate_args=False, allow_nan_stats=True, name='EpsilonInsensitive')[source]
Initialize the distribution.
- Args:
loc: The mean (mu) of the distribution. scale: The rate (c) of the distribution. epsilon: The insensitivity parameter.
- property loc
Distribution parameter for the mean.
- property scale
Distribution parameter for the standard deviation.
- property epsilon
Distribution parameter for the insensitivity.
- property parameters
Returns a dict of parameters.
- class geoml.stats.probability.Huber(loc, scale, epsilon, validate_args=False, allow_nan_stats=True, name='Huber')[source]
Bases:
EpsilonInsensitiveBased on the Huber loss.
- geoml.stats.probability.hazen_plotting_positions(n, dtype=tf.float64)[source]
Generates plotting positions using the Hazen formula. p_i = (i - 0.5) / n for i = 1, …, n
- class geoml.stats.probability.SplineBased(x_sorted, prob, validate_args=False, allow_nan_stats=True, name='SplineBased')[source]
Bases:
DistributionA continuous distribution based on a sample ECDF.
The distribution is defined by: - A MonotonicSpline for the body (between min/max samples) - Exponential tails for extrapolation (below min / above max)
The tails are ‘stitched’ to the spline by matching the value and slope at the min/max sample points.
- class geoml.stats.probability.SmoothEmpirical(samples, name='SmoothEmpirical')[source]
Bases:
SplineBasedA continuous distribution based on a sample ECDF.
The distribution is defined by: - A MonotonicSpline for the body (between min/max samples) - Exponential tails for extrapolation (below min / above max)
The tails are ‘stitched’ to the spline by matching the value and slope at the min/max sample points.
- geoml.stats.probability.hazen_binned_plotting_points(samples, num_bins, margin=0.05, pseudo_counts=1, dtype=tf.float64)[source]
Calculates K plotting points based on binned data.
- Args:
samples: A 1D tensor of raw data. num_bins: The number of bins (K) to create. margin: The margin to use. pseudo_counts: The number of pseudo counts to use. dtype: The data type to use.
- Returns:
(x_prime, p_prime): A tuple of K x-coordinates and K p-coordinates.
- class geoml.stats.probability.BinnedEmpirical(samples, num_bins, margin=0.05, pseudo_counts=1, name='BinnedEmpirical')[source]
Bases:
SplineBased
- class geoml.stats.probability.EmpiricalGaussianMixture(samples, num_knots, batch_size=1000, epochs=None)[source]
Bases:
object
Note
The distribution methods every TensorFlow-Probability distribution has –
sample, log_prob, quantile and the rest – are left out above: their
docstrings come from TFP’s own base class, which copies them into each
subclass, and they document TFP rather than geoML. Read them in
the TFP reference.