mojolearn.resample#
Resampling statistics: bootstrap intervals, permutation tests, Monte Carlo integration and scikit-learn style row resampling.
The public functions are bootstrap(), permutation_test(), monte_carlo_integrate(), resample() and resample_indices(). Their parameters mean what SciPy’s (or scikit-learn’s) parameters of the same name mean. The random draws and the per-replicate statistics run on the device, one block per replicate; the point estimate, interval, standard error and p-value are then computed from those replicates in compiled code.
Inputs are converted to float32. Unknown statistic, method, alternative or
integrand names raise ValueError, as does a sample that is not 1-D or
2-D, a non-finite value, a confidence_level outside (0, 1), a statistic
that reads a column the sample does not have, 'std' on fewer than two
rows, or an n_resamples too large to address.
r_first (and i_first for Monte Carlo) selects where a run starts in
the replicate stream: replicates [r_first, r_first + n_resamples) are
bit-identical to the same slice of one whole run, so a long run can be split
into batches.
Every function takes numeric_mode ('identical', 'deterministic',
or 'fast' on Apple); None uses the package default.
Examples
>>> import numpy as np, mojolearn
>>> x = np.random.default_rng(0).random(1000, dtype=np.float32)
>>> res = mojolearn.resample.bootstrap(x, statistic="mean", n_resamples=999)
>>> low, high = res.confidence_interval
Module Attributes
Statistic names accepted by bootstrap() and permutation_test(), with their integer codes. |
|
Confidence-interval methods accepted by bootstrap() ( |
|
Alternative hypotheses accepted by bootstrap() and permutation_test(). |
|
Integrands accepted by monte_carlo_integrate(). |
Functions
|
Bootstrap confidence interval for a statistic. |
|
Permutation test of a statistic. |
|
Monte Carlo estimate of a 2-D integral over a box. |
|
Resample arrays consistently along the first axis. |
|
Row indices for a scikit-learn style resample. |
Classes
|
Result of bootstrap(), SciPy's |
|
Result of permutation_test(), SciPy's |
|
Result of monte_carlo_integrate(). |
- mojolearn.resample.bootstrap(data, statistic='mean', n_resamples=9999, confidence_level=0.95, method='percentile', alternative='two-sided', random_state=0, q_or_prop=0.5, r_first=0, numeric_mode=None, paired=True)[source]#
Bootstrap confidence interval for a statistic.
Follows
scipy.stats.bootstrap((data,), statistic, n_resamples=..., rng=random_state, method=..., confidence_level=..., alternative=...). The statistic is chosen by name from a compiled set rather than passed as a callable. The resampling and the per-replicate statistic run on the device.- Parameters:
data (array-like of shape (n,) or (n, 2), or a pair (x, y)) – The sample, converted to float32. A two-column sample keeps its row pairing (SciPy’s
paired=True), which'pearson'and'diff_means'read. Withpaired=False, a pair of 1-D samples of any lengths.statistic (str or int, default=’mean’) –
'mean','std','quantile','pearson','diff_means'or'trimmed_mean', or its code inSTATISTICS.n_resamples (int, default=9999) – Number of bootstrap replicates.
confidence_level (float, default=0.95) – Confidence level of the interval, in (0, 1).
method ({‘percentile’, ‘basic’, ‘BCa’}, default=’percentile’) – Interval method, case-insensitive as in SciPy.
'BCa'is available for'mean','std'and'diff_means'and raisesValueErrorfor the other statistics or a degenerate interval.alternative ({‘two-sided’, ‘less’, ‘greater’}, default=’two-sided’) – Which side(s) of the interval to bound.
random_state (int, default=0) – Seed of the replicate stream.
q_or_prop (float, default=0.5) –
qfor'quantile'andproportiontocutfor'trimmed_mean'; ignored otherwise.r_first (int, default=0) – Index of the first replicate. Replicates
[r_first, r_first + n_resamples)equal the same slice of one whole run, bit for bit.numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default=None) – Numeric mode (
'fast'is Apple only);Noneuses the package default.paired (bool, default=True) –
Falsewithdata=(x, y)andstatistic='diff_means'runs SciPy’s unpaired two-sample bootstrap, each sample resampled independently; any other statistic raisesValueError. A single sample ignorespaired, as SciPy does.
- Returns:
BootstrapResult
- Raises:
ValueError – For an unknown name, a sample that is not 1-D or 2-D, a non-finite value, a
confidence_leveloutside (0, 1),'std'on fewer than two rows, or an unsupportedmethod/statisticcombination.TypeError – For a parameter of the wrong type.
Examples
>>> import numpy as np, mojolearn >>> x = np.random.default_rng(0).random(1000, dtype=np.float32) >>> res = mojolearn.resample.bootstrap(x, statistic="mean", method="BCa") >>> low, high = res.confidence_interval
- mojolearn.resample.permutation_test(x, y=None, statistic='diff_means', n_resamples=9999, alternative='two-sided', random_state=0, r_first=0, numeric_mode=None, permutation_type='independent')[source]#
Permutation test of a statistic.
Follows
scipy.stats.permutation_test((x, y), statistic, permutation_type=..., n_resamples=..., rng=random_state, alternative=...), with the statistic chosen by name. Unlike SciPy, the null distribution is always sampled withn_resamplespermutations, never enumerated exhaustively; the p-value is conservative.- Parameters:
x (array-like of shape (n_x,)) – First sample, converted to float32.
y (array-like of shape (n_y,) or None, default=None) – Second sample. Required for
permutation_type='independent'. With'samples',yis the paired second sample, orNonefor a one-sample sign-flip test.statistic (str or int, default=’diff_means’) – The statistic, by name or by its code in
STATISTICS. Withpermutation_type='samples'it must be'diff_means'for two paired samples and'mean'for one sample.n_resamples (int, default=9999) – Number of permutations.
alternative ({‘two-sided’, ‘less’, ‘greater’}, default=’two-sided’) – The alternative hypothesis.
random_state (int, default=0) – Seed of the permutation stream.
r_first (int, default=0) – Index of the first permutation; a slice of permutations equals the same slice of one whole run, bit for bit.
numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default=None) – Numeric mode (
'fast'is Apple only);Noneuses the package default.permutation_type ({‘independent’, ‘samples’}, default=’independent’) –
'independent'reassigns pooled observations betweenxandy.'samples'swaps the two observations of each pair at random (two paired samples), or flips the sign of each observation (one sample), as in SciPy.'pairings'raisesValueError.
- Returns:
PermutationTestResult
- Raises:
ValueError – For an unknown name, a missing
y, a refusedpermutation_typeorstatisticcombination, or a non-finite value.
Examples
>>> import numpy as np, mojolearn >>> rng = np.random.default_rng(0) >>> x = rng.random(200, dtype=np.float32) >>> y = rng.random(300, dtype=np.float32) + 0.1 >>> res = mojolearn.resample.permutation_test(x, y, n_resamples=999) >>> res.pvalue
- mojolearn.resample.monte_carlo_integrate(integrand, lower, upper, n_samples, random_state=0, i_first=0, numeric_mode=None)[source]#
Monte Carlo estimate of a 2-D integral over a box.
Computes
volume * mean(f(x_i))overn_samplesuniform draws from the two-dimensional box[lower, upper). The exact value of the same integral is returned beside the estimate.- Parameters:
integrand ({‘const’, ‘sum’, ‘product’} or int) –
'const'(f = 1),'sum'(x0 + x1) or'product'(x0 * x1), or its code inINTEGRANDS.lower, upper (sequence of 2 floats) – Lower and upper corners of the box;
ValueErrorif either does not hold exactly two values.n_samples (int) – Number of uniform draws.
random_state (int, default=0) – Seed of the draw stream.
i_first (int, default=0) – Index of the first draw, so a long run can be split into batches.
numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default=None) – Numeric mode (
'fast'is Apple only);Noneuses the package default.
- Returns:
MonteCarloResult
Examples
>>> import mojolearn >>> res = mojolearn.resample.monte_carlo_integrate("product", [0, 0], [1, 2], 100000) >>> round(res.closed_form, 6) 1.0
- mojolearn.resample.resample(*arrays, replace=True, n_samples=None, random_state=0, stratify=None, sample_weight=None, numeric_mode=None)[source]#
Resample arrays consistently along the first axis.
Follows
sklearn.utils.resample(*arrays, replace=..., n_samples=..., random_state=...): every array is indexed by the same rows, from resample_indices(). One array returns the resampled array, several return a list, as in scikit-learn. The rows drawn differ from scikit-learn’s for the samerandom_state.- Parameters:
*arrays (sequence of array-like) – Arrays with the same first dimension. NumPy arrays are indexed as arrays; other sequences return lists.
replace (bool, default=True) – Draw with replacement.
n_samples (int or None, default=None) – Number of rows to draw;
Nonemeans the length of the arrays. Withreplace=Falseit must be at most that length.random_state (int, default=0) – Seed.
stratify (None) – Not supported; any other value raises
ValueError.sample_weight (None) – Not supported; any other value raises
ValueError.numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default=None) – Numeric mode (
'fast'is Apple only);Noneuses the package default.
- Returns:
array or list of arrays –
Nonewhen no arrays are given.- Raises:
ValueError – If the arrays have different lengths, or
stratifyorsample_weightis given.
Examples
>>> import numpy as np, mojolearn >>> X = np.random.default_rng(0).random((1000, 8), dtype=np.float32) >>> y = np.arange(1000, dtype=np.float32) >>> Xb, yb = mojolearn.resample.resample(X, y, n_samples=500)
- mojolearn.resample.resample_indices(n, n_samples=None, replace=True, random_state=0, numeric_mode=None)[source]#
Row indices for a scikit-learn style resample.
Returns the indices resample() gathers. With
replace=Trueeach position draws a row independently; withreplace=Falsethe result is the firstn_samplesentries of a random permutation ofrange(n). The draw is integer only, so the same seed gives the same indices on every device. The indices differ from scikit-learn’s for the samerandom_state.- Parameters:
n (int) – Number of rows to draw from.
n_samples (int or None, default=None) – Number of indices;
Nonemeansn. Withreplace=Falseit must be at mostn.replace (bool, default=True) – Draw with replacement.
random_state (int, default=0) – Seed.
numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default=None) – Numeric mode (
'fast'is Apple only);Noneuses the package default.
- Returns:
Array of int32, shape (n_samples,)
Examples
>>> import mojolearn >>> idx = mojolearn.resample.resample_indices(1000, n_samples=100, replace=False)
- class mojolearn.resample.BootstrapResult(point_estimate, distribution, sorted_distribution, standard_error, low, high, order_low, order_high)[source]#
Bases:
objectResult of bootstrap(), SciPy’s
BootstrapResultwith extra fields.- Variables:
point_estimate (float) – The statistic on the original sample.
distribution (Array of float32, shape (n_resamples,)) – The bootstrap replicates, in replicate order.
sorted_distribution (Array of float32, shape (n_resamples,)) – The same replicates sorted ascending.
standard_error (float) – Standard deviation of the bootstrap distribution.
confidence_interval (ConfidenceInterval) –
(low, high); also available as.lowand.high.order_high (order_low,) – Positions in
sorted_distributionused for the interval ends (formethod='BCa', the positions at its adjusted levels).
- class mojolearn.resample.PermutationTestResult(statistic, null_distribution, pvalue, count_less, count_greater)[source]#
Bases:
objectResult of permutation_test(), SciPy’s
PermutationTestResultwith extra fields.- Variables:
statistic (float) – The statistic on the observed samples.
null_distribution (Array of float32, shape (n_resamples,)) – The statistic on each permutation.
pvalue (float) – The p-value for the requested
alternative.count_greater (count_less,) – The two counts the p-value was computed from: null values on the low side and on the high side of the observed statistic.
- class mojolearn.resample.MonteCarloResult(integral, mean, volume, closed_form)[source]#
Bases:
objectResult of monte_carlo_integrate().
- mojolearn.resample.STATISTICS = {'diff_means': 4, 'mean': 0, 'pearson': 3, 'quantile': 2, 'std': 1, 'trimmed_mean': 5}#
Statistic names accepted by bootstrap() and permutation_test(), with their integer codes.
- mojolearn.resample.METHODS = {'basic': 1, 'bca': 2, 'percentile': 0}#
Confidence-interval methods accepted by bootstrap() (
methodis case-insensitive).
- mojolearn.resample.ALTERNATIVES = {'greater': 2, 'less': 1, 'two-sided': 0}#
Alternative hypotheses accepted by bootstrap() and permutation_test().
- mojolearn.resample.INTEGRANDS = {'const': 0, 'product': 2, 'sum': 1}#
Integrands accepted by monte_carlo_integrate().