obp.utils

Useful Tools.

Functions

check_bandit_feedback_inputs(context, …[, …])

Check inputs for bandit learning or simulation.

check_is_fitted(estimator[, attributes, …])

Perform is_fitted validation for estimator.

convert_to_action_dist(n_actions, …)

Convert selected actions (output of run_bandit_simulation) to distribution over actions.

estimate_confidence_interval_by_bootstrap(samples)

Estimate confidence interval by nonparametric bootstrap-like procedure.

sigmoid(x)

Calculate sigmoid function.

softmax(x)

Calculate softmax function.

obp.utils.check_bandit_feedback_inputs(context: numpy.ndarray, action: numpy.ndarray, reward: numpy.ndarray, position: Optional[numpy.ndarray] = None, pscore: Optional[numpy.ndarray] = None, action_context: Optional[numpy.ndarray] = None) → Optional[AssertionError][source]

Check inputs for bandit learning or simulation.

Parameters
  • context (array-like, shape (n_rounds, dim_context)) – Context vectors in each round, i.e., \(x_t\).

  • action (array-like, shape (n_rounds,)) – Action sampled by a behavior policy in each round of the logged bandit feedback, i.e., \(a_t\).

  • reward (array-like, shape (n_rounds,)) – Observed rewards (or outcome) in each round, i.e., \(r_t\).

  • position (array-like, shape (n_rounds,), default=None) – Positions of each round in the given logged bandit feedback.

  • pscore (array-like, shape (n_rounds,), default=None) – Propensity scores, the probability of selecting each action by behavior policy, in the given logged bandit feedback.

  • action_context (array-like, shape (n_actions, dim_action_context)) – Context vectors characterizing each action.

obp.utils.check_is_fitted(estimator: sklearn.base.BaseEstimator, attributes=None, *, msg: str = None, all_or_any=<built-in function all>) → bool[source]

Perform is_fitted validation for estimator.

Note

Checks if the estimator is fitted by verifying the presence of fitted attributes (ending with a trailing underscore) and otherwise raises a NotFittedError with the given message. This utility is meant to be used internally by estimators themselves, typically in their own predict / transform methods.

Parameters
  • estimator (estimator instance.) – estimator instance for which the check is performed.

  • attributes (str, list or tuple of str, default=None) – Attribute name(s) given as string or a list/tuple of strings Eg.: ["coef_", "estimator_", ...], "coef_" If None, estimator is considered fitted if there exist an attribute that ends with a underscore and does not start with double underscore.

  • msg (string) – The default error message is, “This %(name)s instance is not fitted yet. Call ‘fit’ with appropriate arguments before using this estimator.” For custom messages if “%(name)s” is present in the message string, it is substituted for the estimator name. Eg. : “Estimator, %(name)s, must be fitted before sparsifying”.

  • all_or_any (callable, {all, any}, default all) – Specify whether all or any of the given attributes must exist.

Returns

is_fitted – Whether the given estimator is fitted or not.

Return type

bool

References

https://scikit-learn.org/stable/modules/generated/sklearn.utils.validation.check_is_fitted.html

obp.utils.convert_to_action_dist(n_actions: int, selected_actions: numpy.ndarray) → numpy.ndarray[source]

Convert selected actions (output of run_bandit_simulation) to distribution over actions.

Parameters
  • n_actions (int) – Number of actions.

  • selected_actions (array-like, shape (n_rounds, len_list)) – Sequence of actions selected by evaluation policy at each round in offline bandit simulation.

Returns

action_dist – Action choice probabilities (can be deterministic).

Return type

array-like, shape (n_rounds, n_actions, len_list)

obp.utils.estimate_confidence_interval_by_bootstrap(samples: numpy.ndarray, alpha: float = 0.05, n_bootstrap_samples: int = 10000, random_state: Optional[int] = None) → Dict[str, float][source]

Estimate confidence interval by nonparametric bootstrap-like procedure.

Parameters
  • samples (array-like) – Empirical observed samples to be used to estimate cumulative distribution function.

  • alpha (float, default=0.05) – P-value.

  • n_bootstrap_samples (int, default=10000) – Number of resampling performed in the bootstrap procedure.

  • random_state (int, default=None) – Controls the random seed in bootstrap sampling.

Returns

estimated_confidence_interval – Dictionary storing the estimated mean and upper-lower confidence bounds.

Return type

Dict[str, float]

obp.utils.sigmoid(x: Union[float, numpy.ndarray]) → Union[float, numpy.ndarray][source]

Calculate sigmoid function.

obp.utils.softmax(x: Union[float, numpy.ndarray]) → Union[float, numpy.ndarray][source]

Calculate softmax function.