Skip to content

tea_tasting.config #

Global configuration.

config_context(*, alpha=None, alternative=None, confidence_level=None, equal_var=None, n_obs=None, n_resamples=None, power=None, ratio=None, use_t=None, **kwargs) #

A context manager that temporarily modifies the global configuration.

Parameters:

Name Type Description Default
alpha float | None

Significance level. Default is 0.05.

None
alternative Literal['two-sided', 'greater', 'less'] | None

Alternative hypothesis:

  • "two-sided": the means are unequal,
  • "greater": the mean in the treatment variant is greater than the mean in the control variant,
  • "less": the mean in the treatment variant is less than the mean in the control variant.

Default is "two-sided".

None
confidence_level float | None

Confidence level for the confidence interval. Default is 0.95.

None
equal_var bool | None

Defines whether equal variance is assumed. If True, pooled variance is used for the calculation of the standard error of the difference between two means. Default is False.

None
n_obs int | Sequence[int] | None

Number of observations in the control and in the treatment together. Default is None.

None
n_resamples int | None

The number of resamples performed to form the bootstrap distribution of a statistic. Default is 10_000.

None
power float | None

Statistical power. Default is 0.8.

None
ratio float | int | None

Ratio of the number of observations in the treatment relative to the control. Default is 1.

None
use_t bool | None

Defines whether to use the Student's t-distribution (True) or the Normal distribution (False) by default. Default is True.

None
**kwargs Any

User-defined global parameters.

{}

Examples:

>>> import tea_tasting as tt

>>> with tt.config_context(equal_var=True, use_t=False):
...     experiment = tt.Experiment(
...         sessions_per_user=tt.Mean("sessions"),
...         orders_per_session=tt.RatioOfMeans("orders", "sessions"),
...         orders_per_user=tt.Mean("orders"),
...         revenue_per_user=tt.Mean("revenue"),
...     )
>>> print(experiment.metrics["orders_per_user"])
Mean(value='orders', covariate=None, alternative='two-sided', confidence_level=0.95, equal_var=True, use_t=False, alpha=0.05, ratio=1, power=0.8, effect_size=None, rel_effect_size=None, n_obs=None)
Source code in src/tea_tasting/config.py
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
@contextlib.contextmanager
def config_context(
    *,
    alpha: float | None = None,
    alternative: Literal["two-sided", "greater", "less"] | None = None,
    confidence_level: float | None = None,
    equal_var: bool | None = None,
    n_obs: int | Sequence[int] | None = None,
    n_resamples: int | None = None,
    power: float | None = None,
    ratio: float | int | None = None,
    use_t: bool | None = None,
    **kwargs: Any,
) -> Iterator[Any]:
    """A context manager that temporarily modifies the global configuration.

    Args:
        alpha: Significance level. Default is 0.05.
        alternative: Alternative hypothesis:

            - `"two-sided"`: the means are unequal,
            - `"greater"`: the mean in the treatment variant is greater than the mean
                in the control variant,
            - `"less"`: the mean in the treatment variant is less than the mean
                in the control variant.

            Default is `"two-sided"`.

        confidence_level: Confidence level for the confidence interval.
            Default is `0.95`.
        equal_var: Defines whether equal variance is assumed. If `True`,
            pooled variance is used for the calculation of the standard error
            of the difference between two means. Default is `False`.
        n_obs: Number of observations in the control and in the treatment together.
            Default is `None`.
        n_resamples: The number of resamples performed to form the bootstrap
            distribution of a statistic. Default is `10_000`.
        power: Statistical power. Default is 0.8.
        ratio: Ratio of the number of observations in the treatment
            relative to the control. Default is 1.
        use_t: Defines whether to use the Student's t-distribution (`True`) or
            the Normal distribution (`False`) by default. Default is `True`.
        **kwargs: User-defined global parameters.

    Examples:
        ```pycon
        >>> import tea_tasting as tt

        >>> with tt.config_context(equal_var=True, use_t=False):
        ...     experiment = tt.Experiment(
        ...         sessions_per_user=tt.Mean("sessions"),
        ...         orders_per_session=tt.RatioOfMeans("orders", "sessions"),
        ...         orders_per_user=tt.Mean("orders"),
        ...         revenue_per_user=tt.Mean("revenue"),
        ...     )
        >>> print(experiment.metrics["orders_per_user"])
        Mean(value='orders', covariate=None, alternative='two-sided', confidence_level=0.95, equal_var=True, use_t=False, alpha=0.05, ratio=1, power=0.8, effect_size=None, rel_effect_size=None, n_obs=None)

        ```
    """  # noqa: E501
    new_config = {k: v for k, v in locals().items() if k != "kwargs"} | kwargs
    old_config = get_config()
    set_config(**new_config)

    try:
        yield
    finally:
        _global_config.update(**old_config)

get_config(option=None) #

Retrieve the current settings of the global configuration.

Parameters:

Name Type Description Default
option str | None

The option name.

None

Returns:

Type Description
Any

The specified option value if its name is provided, or a dictionary containing all options otherwise.

Examples:

>>> import tea_tasting as tt

>>> print(tt.get_config("equal_var"))
False
Source code in src/tea_tasting/config.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def get_config(option: str | None = None) -> Any:
    """Retrieve the current settings of the global configuration.

    Args:
        option: The option name.

    Returns:
        The specified option value if its name is provided,
            or a dictionary containing all options otherwise.

    Examples:
        ```pycon
        >>> import tea_tasting as tt

        >>> print(tt.get_config("equal_var"))
        False

        ```
    """
    if option is not None:
        return _global_config[option]
    return _global_config.copy()

set_config(*, alpha=None, alternative=None, confidence_level=None, equal_var=None, n_obs=None, n_resamples=None, power=None, ratio=None, use_t=None, **kwargs) #

Update the global configuration with specified settings.

Parameters:

Name Type Description Default
alpha float | None

Significance level. Default is 0.05.

None
alternative Literal['two-sided', 'greater', 'less'] | None

Alternative hypothesis:

  • "two-sided": the means are unequal,
  • "greater": the mean in the treatment variant is greater than the mean in the control variant,
  • "less": the mean in the treatment variant is less than the mean in the control variant.

Default is "two-sided".

None
confidence_level float | None

Confidence level for the confidence interval. Default is 0.95.

None
equal_var bool | None

Defines whether equal variance is assumed. If True, pooled variance is used for the calculation of the standard error of the difference between two means. Default is False.

None
n_obs int | Sequence[int] | None

Number of observations in the control and in the treatment together. Default is None.

None
n_resamples int | None

The number of resamples performed to form the bootstrap distribution of a statistic. Default is 10_000.

None
power float | None

Statistical power. Default is 0.8.

None
ratio float | int | None

Ratio of the number of observations in the treatment relative to the control. Default is 1.

None
use_t bool | None

Defines whether to use the Student's t-distribution (True) or the Normal distribution (False) by default. Default is True.

None
**kwargs Any

User-defined global parameters.

{}

Examples:

>>> import tea_tasting as tt

>>> tt.set_config(equal_var=True, use_t=False)
>>> experiment = tt.Experiment(
...     sessions_per_user=tt.Mean("sessions"),
...     orders_per_session=tt.RatioOfMeans("orders", "sessions"),
...     orders_per_user=tt.Mean("orders"),
...     revenue_per_user=tt.Mean("revenue"),
... )
>>> tt.set_config(equal_var=False, use_t=True)
>>> print(experiment.metrics["orders_per_user"])
Mean(value='orders', covariate=None, alternative='two-sided', confidence_level=0.95, equal_var=True, use_t=False, alpha=0.05, ratio=1, power=0.8, effect_size=None, rel_effect_size=None, n_obs=None)
Source code in src/tea_tasting/config.py
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
def set_config(
    *,
    alpha: float | None = None,
    alternative: Literal["two-sided", "greater", "less"] | None = None,
    confidence_level: float | None = None,
    equal_var: bool | None = None,
    n_obs: int | Sequence[int] | None = None,
    n_resamples: int | None = None,
    power: float | None = None,
    ratio: float | int | None = None,
    use_t: bool | None = None,
    **kwargs: Any,
) -> None:
    """Update the global configuration with specified settings.

    Args:
        alpha: Significance level. Default is 0.05.
        alternative: Alternative hypothesis:

            - `"two-sided"`: the means are unequal,
            - `"greater"`: the mean in the treatment variant is greater than the mean
                in the control variant,
            - `"less"`: the mean in the treatment variant is less than the mean
                in the control variant.

            Default is `"two-sided"`.

        confidence_level: Confidence level for the confidence interval.
            Default is `0.95`.
        equal_var: Defines whether equal variance is assumed. If `True`,
            pooled variance is used for the calculation of the standard error
            of the difference between two means. Default is `False`.
        n_obs: Number of observations in the control and in the treatment together.
            Default is `None`.
        n_resamples: The number of resamples performed to form the bootstrap
            distribution of a statistic. Default is `10_000`.
        power: Statistical power. Default is 0.8.
        ratio: Ratio of the number of observations in the treatment
            relative to the control. Default is 1.
        use_t: Defines whether to use the Student's t-distribution (`True`) or
            the Normal distribution (`False`) by default. Default is `True`.
        **kwargs: User-defined global parameters.

    Examples:
        ```pycon
        >>> import tea_tasting as tt

        >>> tt.set_config(equal_var=True, use_t=False)
        >>> experiment = tt.Experiment(
        ...     sessions_per_user=tt.Mean("sessions"),
        ...     orders_per_session=tt.RatioOfMeans("orders", "sessions"),
        ...     orders_per_user=tt.Mean("orders"),
        ...     revenue_per_user=tt.Mean("revenue"),
        ... )
        >>> tt.set_config(equal_var=False, use_t=True)
        >>> print(experiment.metrics["orders_per_user"])
        Mean(value='orders', covariate=None, alternative='two-sided', confidence_level=0.95, equal_var=True, use_t=False, alpha=0.05, ratio=1, power=0.8, effect_size=None, rel_effect_size=None, n_obs=None)

        ```
    """  # noqa: E501
    params = {k: v for k, v in locals().items() if k != "kwargs"} | kwargs
    for name, value in params.items():
        if value is not None:
            _global_config[name] = tea_tasting.utils.auto_check(value, name)