lf2i.critical_values package#

Submodules#

lf2i.critical_values.nn_qr_algorithm module#

class lf2i.critical_values.nn_qr_algorithm.QuantileLoss(quantiles: Sequence[float])[source]#

Bases: Module

Quantile loss as a PyTorch module.

Parameters:

quantiles (Sequence[float]) – Target quantiles. Values must be in the range (0, 1).

forward(predictions: Tensor, targets: Tensor) Tensor[source]#

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool#
class lf2i.critical_values.nn_qr_algorithm.QuantileNN(n_quantiles: int, input_d: int, hidden_layer_shapes: Sequence[int], hidden_activation: Module = ReLU(), dropout_p: float = 0.0)[source]#

Bases: Module

Fully connected neural network for quantile regression.

Parameters:
  • n_quantiles (int) – Number of target quantiles.

  • input_d (int) – Dimensionality of the input.

  • hidden_layer_shapes (Sequence[int]) – The i-th element represents the number of neurons in the i-th hidden layer.

  • dropout_p (float, optional) – Probability for the dropout layers, by default 0.0

build_model() None[source]#
init_weights() None[source]#
forward(x: Tensor) Tensor[source]#

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool#
class lf2i.critical_values.nn_qr_algorithm.Learner(model: ~torch.nn.modules.module.Module, optimizer: ~torch.optim.optimizer.Optimizer, loss: ~torch.nn.modules.module.Module = <class 'lf2i.critical_values.nn_qr_algorithm.QuantileLoss'>, device: str = 'cpu')[source]#

Bases: object

Learner for the quantile neural network. Implements methods for training and inference.

Parameters:
  • model (torch.nn.Module) – Quantile Neural Network architecture.

  • optimizer (torch.optim.Optimizer) – Chosen optimizer.

  • loss (torch.nn.Module) – Quantile Loss.

  • device (str, optional) – Device on which to perform computations, by default “cpu”. Use “cuda” for GPU.

fit(X: ndarray, y: ndarray, epochs: int, batch_size: int) None[source]#
predict(X: ndarray) ndarray[source]#

lf2i.critical_values.quantile_regression module#

lf2i.critical_values.quantile_regression.train_qr_algorithm(test_statistics: ndarray | Tensor, parameters: ndarray | Tensor, algorithm: str | Callable, alpha: float | Sequence[float], param_dim: int, algorithm_kwargs: Dict[str, Any] | Dict[str, Dict[str, Any]] = {}) Any[source]#

Dispatcher to train different quantile regressors.

Parameters:
  • test_statistics (Union[np.ndarray, torch.Tensor]) – Test statistics used to train the quantile regressor (i.e., outputs).

  • parameters (Union[np.ndarray, torch.Tensor]) – Parameters used to train the quantile regressor (i.e., inputs).

  • algorithm (Union[str, Any]) – Either ‘gb’ for gradient boosted trees, ‘nn’ for a feed-forward neural network, or a custom algorithm (Any). The latter must implement a fit(X=…, y=…) method.

  • alpha (Union[float, Sequence[float]] # TODO: update this to include sequences, but only with models monotonic in inputs.) – The alpha quantile of the test statistic to be estimated. E.g., for 90% confidence intervals, it should be 0.1 if the acceptance region of the test statistic is on the right of the critical value. Must be in the range (0, 1).

  • param_dim (int) – Dimensionality of the parameter.

  • algorithm_kwargs (Union[Dict[str, Any], Dict[str, Dict[str, Any]]], optional) –

    Keywork arguments for the desired algorithm, by default {}. If algorithm == ‘nn’, then ‘hidden_layer_shapes’, ‘epochs’ and ‘batch_size’ must be present.

    If algorithm == ‘gb’, pass {‘cv’: hp_dist} to do a randomized search over the hyperparameters in hp_dist (a Dict) via 5-fold cross validation. Include ‘n_iter’ as a key to decide how many hyperparameter setting to sample for randomized search. Defaults to 10.

Returns:

Fitted quantile regressor.

Return type:

Any

Raises:

ValueError – Only one of ‘gb’, ‘nn’ or an instantiated custom quantile regressor (Any) is currently accepted as algorithm.

Module contents#