Odds and Ends

tsbootstrap.utils.odds_and_ends.assert_arrays_compare(a: ndarray, b: ndarray, rtol=1e-05, atol=1e-08, check_same=True) bool[source]

Assert that two arrays are almost equal.

This function compares two arrays for equality, allowing for NaNs and Infs in the arrays. The arrays are considered equal if the following conditions are satisfied: 1. The locations of NaNs and Infs in both arrays are the same. 2. The signs of the infinite values in both arrays are the same. 3. The finite values are almost equal.

Parameters:
  • a (np.ndarray) – The arrays to be compared.

  • b (np.ndarray) – The arrays to be compared.

  • rtol (float, optional) – The relative tolerance parameter for the np.allclose function. Default is 1e-5.

  • atol (float, optional) – The absolute tolerance parameter for the np.allclose function. Default is 1e-8.

  • check_same (bool, optional) – If True, raise an AssertionError if the arrays are not almost equal. If False, return True if the arrays are not almost equal and False otherwise. Default is True.

Returns:

If check_same is False, returns True if the arrays are not almost equal and False otherwise. If check_same is True, returns True if the arrays are almost equal and False otherwise.

Return type:

bool

Raises:
  • AssertionError – If check_same is True and the arrays are not almost equal.

  • ValueError – If check_same is True and the arrays have NaNs or Infs in different locations. If check_same is True and the arrays have Infs with different signs.

tsbootstrap.utils.odds_and_ends.check_generator(seed_or_rng: Generator | Integral | None, seed_allowed: bool = True) Generator[source]

Turn seed into a np.random.Generator instance.

Parameters:
  • seed_or_rng (int, Generator, or None) – If seed_or_rng is None, return the Generator singleton used by np.random. If seed_or_rng is an int, return a new Generator instance seeded with seed_or_rng. If seed_or_rng is already a Generator instance, return it. Otherwise raise ValueError.

  • seed_allowed (bool, optional) – If True, seed_or_rng can be an int. If False, seed_or_rng cannot be an int. Default is True.

Returns:

A numpy.random.Generator instance.

Return type:

Generator

Raises:

ValueError – If seed_or_rng is not None, an int, or a numpy.random.Generator instance. If seed_or_rng is an int and seed_allowed is False. If seed_or_rng is an int and it is not between 0 and 2**32 - 1.

tsbootstrap.utils.odds_and_ends.generate_random_indices(num_samples: Integral, rng: Generator | Integral | None = None) ndarray[source]

Generate random indices with replacement.

This function generates random indices from 0 to num_samples-1 with replacement. The generated indices can be used for bootstrap sampling, etc.

Parameters:
  • num_samples (Integral) – The number of samples for which the indices are to be generated. This must be a positive integer.

  • rng (Integral, optional) – The seed for the random number generator. If provided, this must be a non-negative integer. Default is None, which does not set the numpy’s random seed and the results will be non-deterministic.

Returns:

A numpy array of shape (num_samples,) containing randomly generated indices.

Return type:

np.ndarray

Raises:

ValueError – If num_samples is not a positive integer or if random_seed is provided and it is not a non-negative integer.

Examples

>>> generate_random_indices(5, random_seed=0)
array([4, 0, 3, 3, 3])
>>> generate_random_indices(5)
array([2, 1, 4, 2, 0])  # random
tsbootstrap.utils.odds_and_ends.suppress_output(verbose: int = 2)[source]

A context manager for controlling the suppression of stdout and stderr.

Parameters:

verbose (int, optional) – Verbosity level controlling suppression. 2 - No suppression (default) 1 - Suppress stdout only 0 - Suppress both stdout and stderr

Return type:

None

Examples

with suppress_output(verbose=1):

print(‘This will not be printed to stdout’)

tsbootstrap.utils.odds_and_ends.time_series_split(X: ndarray, test_ratio: float)[source]

Splits a given time series into training and test sets.

Parameters:
  • X (np.ndarray) – The input time series.

  • test_ratio (float) – The ratio of the test set size to the total size of the series.

Returns:

A tuple containing the training set and the test set.

Return type:

Tuple[np.ndarray, np.ndarray]