Validate

tsbootstrap.utils.validate.add_newaxis_if_needed(X: ndarray, model_is_var: bool) ndarray[source]

Add a new axis to the given array if it’s needed.

tsbootstrap.utils.validate.check_are_1d_integer_arrays(input_list, input_name: str)[source]

Check if all NumPy arrays in the input list are 1D and contain integer values.

tsbootstrap.utils.validate.check_are_2d_arrays(input_list, input_name: str)[source]

Check if all NumPy arrays in the input list are 2D.

tsbootstrap.utils.validate.check_are_finite(input_list, input_name: str)[source]

Check if all elements in the NumPy arrays in the input list are finite.

tsbootstrap.utils.validate.check_are_nonnegative(input_array: ndarray, input_name: str) ndarray[source]

Check if all elements in the input NumPy array are nonnegative.

tsbootstrap.utils.validate.check_are_np_arrays(input_list, input_name: str)[source]

Check if all elements in the input list are NumPy arrays.

tsbootstrap.utils.validate.check_are_real(input_array: ndarray, input_name: str) ndarray[source]

Check if all elements in the input NumPy array are real.

tsbootstrap.utils.validate.check_array_shape(X: ndarray, model_is_var: bool, allow_multi_column: bool) ndarray[source]

Check if the given array meets the required shape constraints.

Parameters:
  • X (np.ndarray) – The input array to be checked.

  • model_is_var (bool) – Flag indicating if the model is a VAR (Vector Autoregression) model.

  • allow_multi_column (bool) – Flag indicating if multiple columns are allowed in the array.

Returns:

The original array if it meets the constraints.

Return type:

np.ndarray

Raises:

ValueError – If the array does not meet the required shape constraints.

Examples

>>> check_array_shape(np.array([[1, 2], [3, 4]]), True, True)
array([[1, 2], [3, 4]])
>>> check_array_shape(np.array([1, 2, 3]), False, False)
array([1, 2, 3])
tsbootstrap.utils.validate.check_array_size(X: ndarray) ndarray[source]

Check if the given array contains at least two elements.

tsbootstrap.utils.validate.check_array_type(X: ndarray) ndarray[source]

Check if the given array is a NumPy array of floats.

tsbootstrap.utils.validate.check_have_at_least_one_element(input_list, input_name: str)[source]

Check if all NumPy arrays in the input list have at least one element.

tsbootstrap.utils.validate.check_have_at_least_one_feature(input_list, input_name: str)[source]

Check if all NumPy arrays in the input list have at least one feature.

tsbootstrap.utils.validate.check_have_at_least_one_index(input_list, input_name: str)[source]

Check if all NumPy arrays in the input list have at least one index.

tsbootstrap.utils.validate.check_have_same_num_of_features(input_list, input_name: str)[source]

Check if all NumPy arrays in the input list have the same number of features.

tsbootstrap.utils.validate.check_indices_within_range(input_list, input_length: Integral, input_name: str)[source]

Check if all indices in the NumPy arrays in the input list are within the range of the input length.

tsbootstrap.utils.validate.check_is_1d_or_2d_single_column(input_array: ndarray, input_name: str) ndarray[source]

Check if the input NumPy array is a 1D array or a 2D array with a single column.

tsbootstrap.utils.validate.check_is_finite(input_array: ndarray, input_name: str) ndarray[source]

Check if all elements in the input NumPy array are finite.

tsbootstrap.utils.validate.check_is_list(input_list: list, input_name: str) list[source]

Check if the input is a list.

tsbootstrap.utils.validate.check_is_nonempty(input_list: list, input_name: str) list[source]

Check if the input list is nonempty.

tsbootstrap.utils.validate.check_is_not_all_zero(input_array: ndarray, input_name: str) ndarray[source]

Check if the input NumPy array is not all zeros.

tsbootstrap.utils.validate.check_is_np_array(input_array: ndarray, input_name: str) ndarray[source]

Check if the input is a NumPy array.

tsbootstrap.utils.validate.validate_X(X: ndarray, model_is_var: bool, allow_multi_column: bool | None = None) ndarray[source]

Validate the input array X based on the given model type.

Parameters:
  • X (np.ndarray) – The input array to be validated. It must be a NumPy array of floats (i, u, or f type).

  • model_is_var (bool) – A flag to determine whether the model is of VAR (Vector Autoregression) type. If True, the function will validate it as a VAR array. If False, the function will validate it as a non-VAR array.

  • allow_multi_column (bool, optional) – A flag to determine whether the array is allowed to have more than one column. If not specified, it defaults to the value of model_is_var.

Returns:

A validated array.

Return type:

np.ndarray

Raises:
  • TypeError – If X is not a NumPy array or its data type is not float.

  • ValueError – If X contains fewer than two elements, or does not meet the dimensionality requirements.

tsbootstrap.utils.validate.validate_X_and_y(X: ndarray, y: ndarray | None, model_is_var: bool = False, model_is_arch: bool = False)[source]

Validate and reshape input data and exogenous variables.

This function uses validate_X() and validate_exog() to perform detailed validation.

Parameters:
  • X (np.ndarray) – The input array to be validated.

  • y (Optional[np.ndarray]) – The exogenous variable array to be validated. Can be None.

  • model_is_var (bool, optional) – A flag to determine if the model is of VAR type. Default is False.

  • model_is_arch (bool, optional) – A flag to determine if the model is of ARCH type. Default is False.

Returns:

A tuple containing the validated X array and optionally the validated exog array.

Return type:

Tuple[np.ndarray, Optional[np.ndarray]]

See also

validate_X

Function for validating the input array X.

validate_exog

Function for validating the exogenous variable array.

tsbootstrap.utils.validate.validate_block_indices(block_indices: List[ndarray], input_length: Integral) None[source]

Validate the input block indices. Each block index must be a 1D NumPy array with at least one index and all indices must be within the range of X.

Parameters:
  • block_indices (List[np.ndarray]) – The input block indices.

  • input_length (Integral) – The length of the input data.

Raises:
  • TypeError – If block_indices is not a list or if it contains non-NumPy arrays.

  • ValueError – If block_indices is empty or if it contains NumPy arrays with non-integer values, or if it contains NumPy arrays with no indices, or if it contains NumPy arrays with indices outside the range of X.

tsbootstrap.utils.validate.validate_blocks(blocks: List[ndarray]) None[source]

Validate the input blocks. Each block must be a 2D NumPy array with at least one element.

Parameters:

blocks (List[np.ndarray]) – The input blocks.

Raises:
  • TypeError – If blocks is not a list or if it contains non-NumPy arrays.

  • ValueError – If blocks is empty or if it contains NumPy arrays with non-finite values, or if it contains NumPy arrays with no elements, or if it contains NumPy arrays with no features, or if it contains NumPy arrays with different number of features.

tsbootstrap.utils.validate.validate_exog(exog: ndarray) ndarray[source]

Validate the exogenous variable array exog, ensuring its dimensionality and dtype.

Parameters:

exog (np.ndarray) – The exogenous variable array to be validated. Must be a NumPy array of floats.

Returns:

A validated exogenous variable array.

Return type:

np.ndarray

Raises:
  • TypeError – If exog is not a NumPy array or its data type is not float.

  • ValueError – If exog contains fewer than two elements.

tsbootstrap.utils.validate.validate_fitted_model(fitted_model) None[source]

Validate the input fitted model. It must be an instance of a fitted model class.

Parameters:

fitted_model (FittedModelTypes) – The input fitted model.

Raises:

TypeError – If fitted_model is not an instance of a fitted model class.

tsbootstrap.utils.validate.validate_integer_array(value: ndarray, min_value: Integral | None = None, max_value: Integral | None = None) None[source]

Validate a 1D numpy array of integers against an optional minimum value.

tsbootstrap.utils.validate.validate_integers(*values, min_value: Integral | None = None, max_value: Integral | None = None) None[source]

Validates that all input values are integers and optionally, above a minimum value.

Each value can be an integer, a list of integers, or a 1D numpy array of integers. If min_value is provided, all integers must be greater than or equal to min_value.

Parameters:
  • *values (Union[Integral, List[Integral], np.ndarray]) – One or more values to validate.

  • min_value (Integral, optional) – If provided, all integers must be greater than or equal to min_value.

  • max_value (Integral, optional) – If provided, all integers must be less than or equal to max_value.

Raises:

TypeError – If a value is not an integer, list of integers, or 1D array of integers, or if any integer is less than min_value or greater than max_value.

tsbootstrap.utils.validate.validate_list_of_integers(value, min_value: Integral | None = None, max_value: Integral | None = None) None[source]

Validate a list of integer values against an optional minimum value.

tsbootstrap.utils.validate.validate_literal_type(input_value: str, literal_type: Any) None[source]

Validate the type of input_value against a Literal type or dictionary keys.

Parameters:
  • input_value (str) – The value to validate.

  • literal_type (type, or list) – if type: Literal type or dictionary against which to validate the input_value. if list: list of valid values against which to validate the input_value.

Raises:
  • TypeError – If input_value is not a string.

  • ValueError – If input_value is not among the valid types in literal_type or dictionary keys.

Examples

>>> validate_literal_type("a", Literal["a", "b", "c"])
>>> validate_literal_type("x", {"x": 1, "y": 2})
>>> validate_literal_type("z", Literal["a", "b", "c"])
ValueError: Invalid input_value 'z'. Expected one of 'a', 'b', 'c'.
>>> validate_literal_type("z", {"x": 1, "y": 2})
ValueError: Invalid input_value 'z'. Expected one of 'x', 'y'.
tsbootstrap.utils.validate.validate_order(order) None[source]

Validates the type of the resids_order order.

Parameters:

order (Any) – The order to validate.

Raises:
  • TypeError – If the order is not of the expected type (Integral, list, or tuple).

  • orderError – If the order is an integral but is negative. If the order is a list/tuple and not all elements are positive integers.

tsbootstrap.utils.validate.validate_rng(rng: Generator | Integral | None, allow_seed: bool = True) Generator[source]

Validate the input random number generator.

This function validates if the input random number generator is an instance of the numpy.random.Generator class or an integer. If allow_seed is True, the input can also be an integer, which will be used to seed the default random number generator.

Parameters:
  • rng (RngTypes) – The input random number generator.

  • allow_seed (bool, optional) – Whether to allow the input to be an integer. Default is True.

Returns:

The validated random number generator.

Return type:

Generator

Raises:
  • TypeError – If rng is not an instance of the numpy.random.Generator class or an integer.

  • ValueError – If rng is an integer and it is negative or greater than or equal to 2**32.

tsbootstrap.utils.validate.validate_single_integer(value: Integral, min_value: Integral | None = None, max_value: Integral | None = None) None[source]

Validate a single integer value against an optional minimum value.

tsbootstrap.utils.validate.validate_weights(weights: ndarray) None[source]

Validate the input weights. Each weight must be a non-negative finite value.

Parameters:

weights (np.ndarray) – The input weights.

Raises:
  • TypeError – If weights is not a NumPy array.

  • ValueError – If weights contains any non-finite values, or if it contains any negative values, or if it contains any complex values, or if it contains all zeros, or if it is a 2D array with more than one column.