Bayesian Models¶
Bernoulli distribution¶
-
class
cprior.BernoulliModel(alpha=1, beta=1)¶ Bases:
cprior.cdist.beta.BetaModelBayesian model with a Bernoulli likelihood and a beta prior distribution.
Given data samples \(\mathbf{x} = (x_1, \ldots, x_n)\) from a Bernoulli distribution with parameter \(p\), the posterior distribution is
\[p | \mathbf{x} \sim \mathcal{B}(\alpha + \sum_{i=1}^n x_i, \beta + n - \sum_{i=1}^n x_i).\]with prior parameters \(\alpha\) and \(\beta\).
Parameters: - alpha (int or float (default=1)) – Prior parameter alpha.
- beta (int or float (default=1)) – Prior parameter beta.
-
n_success_¶ int – Number of successes.
-
n_samples_¶ int – Number of samples.
-
alpha_posterior¶ Posterior parameter alpha.
Returns: alpha Return type: float
-
beta_posterior¶ Posterior parameter beta.
Returns: beta Return type: float
-
cdf(x)¶ Cumulative distribution function of the posterior distribution.
Parameters: x (array-like) – Quantiles. Returns: cdf – Cumulative distribution function evaluated at x. Return type: numpy.ndarray
-
credible_interval()¶ Credible interval of the posterior distribution.
Parameters: interval_length (float (default=0.9)) – Compute interval_length% credible interval. This is a value in [0, 1].Returns: interval – Lower and upper credible interval limits. Return type: tuple
-
mean()¶ Mean of the posterior distribution.
-
pdf(x)¶ Probability density function of the posterior distribution.
Parameters: x (array-like) – Quantiles. Returns: pdf – Probability density function evaluated at x. Return type: numpy.ndarray
-
ppf(q)¶ Percent point function (quantile) of the posterior distribution.
Parameters: x (array-like) – Lower tail probability. Returns: ppf – Quantile corresponding to the lower tail probability q. Return type: numpy.ndarray
-
ppmean()¶ Posterior predictive mean.
If \(X\) is a Bernoulli trial with parameter \(p \sim Beta(\alpha, \beta)\), then the posterior predictive expected value is given by
\[\mathrm{E}[X] = \frac{\alpha}{\alpha + \beta},\]where \(\alpha\) and \(\beta\) are the posterior values of the parameters.
Returns: mean Return type: float
-
pppdf(x)¶ Posterior predictive probability density function.
If \(X\) is a Bernoulli trial with parameter \(p \sim Beta(\alpha, \beta)\), then the posterior predictive probability density function is given by
\[\begin{split}f(x; \alpha, \beta) = \begin{cases} \frac{\alpha}{\alpha+ \beta} & \text{if $x = 1$}\\ \frac{\beta}{\alpha+ \beta} & \text{if $x = 0$} \end{cases}\end{split}\]where \(\alpha\) and \(\beta\) are the posterior values of the parameters.
Returns: pdf – Probability density function evaluated at x. Return type: float
-
ppvar()¶ Posterior predictive variance.
If \(X\) is a Bernoulli trial with parameter \(p \sim Beta(\alpha, \beta)\), then the posterior predictive variance is given by
\[\mathrm{Var}[X] = \frac{\alpha \beta}{(\alpha + \beta)^2},\]where \(\alpha\) and \(\beta\) are the posterior values of the parameters.
Returns: var Return type: float
-
rvs(size=1, random_state=None)¶ Random variates of the posterior distribution.
Parameters: - size (int (default=1)) – Number of random variates.
- random_state (int or None (default=None)) – The seed used by the random number generator.
Returns: rvs – Random variates of given size.
Return type: numpy.ndarray or scalar
-
std()¶ Standard deviation of the posterior distribution.
-
update(data)¶ Update posterior parameters with new data samples.
Parameters: data (array-like, shape = (n_samples)) – Data samples from a Bernoulli distribution.
-
var()¶ Variance of the posterior distribution.
-
class
cprior.BernoulliABTest(modelA, modelB, simulations=1000000, random_state=None)¶ Bases:
cprior.cdist.beta.BetaABTestBernoulli A/B test.
Parameters: - modelA (object) – The control model.
- modelB (object) – The variation model.
- simulations (int or None (default=1000000)) – Number of Monte Carlo simulations.
- random_state (int or None (default=None)) – The seed used by the random number generator.
-
expected_loss(method='exact', variant='A', lift=0)¶ Compute the expected loss. This is the expected uplift lost by choosing a given variant.
- If
variant == "A", \(\mathrm{E}[\max(B - A - lift, 0)]\) - If
variant == "B", \(\mathrm{E}[\max(A - B - lift, 0)]\) - If
variant == "all", both.
If
liftis positive value, the computation method must be Monte Carlo sampling.Parameters: - method (str (default="exact")) – The method of computation. Options are “exact” and “MC”.
- variant (str (default="A")) – The chosen variant. Options are “A”, “B”, “all”.
- lift (float (default=0.0)) – The amount of uplift.
- If
-
expected_loss_ci(method='MC', variant='A', interval_length=0.9)¶ Compute credible intervals on the difference distribution of \(Z = B-A\) and/or \(Z = A-B\).
- If
variant == "A", \(Z = B - A\) - If
variant == "B", \(Z = A - B\) - If
variant == "all", both.
Parameters: - method (str (default="MC")) – The method of computation. Options are “asymptotic” and “MC”.
- variant (str (default="A")) – The chosen variant. Options are “A”, “B”, “all”.
- interval_length (float (default=0.9)) – Compute
interval_length% credible interval. This is a value in [0, 1].
- If
-
expected_loss_relative(method='exact', variant='A')¶ Compute expected relative loss for choosing a variant. This can be seen as the negative expected relative improvement or uplift.
- If
variant == "A", \(\mathrm{E}[(B - A) / A]\) - If
variant == "B", \(\mathrm{E}[(A - B) / B]\) - If
variant == "all", both.
Parameters: - method (str (default="exact")) – The method of computation. Options are “exact” and “MC”.
- variant (str (default="A")) – The chosen variant. Options are “A”, “B”, “all”.
- If
-
expected_loss_relative_ci(method='MC', variant='A', interval_length=0.9)¶ Compute credible intervals on the relative difference distribution of \(Z = (B-A)/A\) and/or \(Z = (A-B)/B\).
- If
variant == "A", \(Z = (B-A)/A\) - If
variant == "B", \(Z = (A-B)/B\) - If
variant == "all", both.
Parameters: - method (str (default="MC")) – The method of computation. Options are “asymptotic”, “exact” and “MC”.
- variant (str (default="A")) – The chosen variant. Options are “A”, “B”, “all”.
- interval_length (float (default=0.9)) – Compute
interval_length% credible interval. This is a value in [0, 1].
- If
-
probability(method='exact', variant='A', lift=0)¶ Compute the error probability or chance to beat control.
- If
variant == "A", \(P[A > B + lift]\) - If
variant == "B", \(P[B > A + lift]\) - If
variant == "all", both.
If
liftis positive value, the computation method must be Monte Carlo sampling.Parameters: - method (str (default="exact")) – The method of computation. Options are “exact” and “MC”.
- variant (str (default="A")) – The chosen variant. Options are “A”, “B”, “all”.
- lift (float (default=0.0)) – The amount of uplift.
- If
Binomial distribution¶
Geometric distribution¶
-
class
cprior.GeometricModel(alpha=1, beta=1)¶ Bases:
cprior.cdist.beta.BetaModelBayesian model with geometric likelihood and a beta prior distribution.
Parameters: - alpha (int or float (default=1)) – Prior parameter alpha.
- beta (int or float (default=1)) – Prior parameter beta.
-
alpha_posterior¶ Posterior parameter alpha.
Returns: alpha Return type: float
-
beta_posterior¶ Posterior parameter beta.
Returns: beta Return type: float
-
cdf(x)¶ Cumulative distribution function of the posterior distribution.
Parameters: x (array-like) – Quantiles. Returns: cdf – Cumulative distribution function evaluated at x. Return type: numpy.ndarray
-
credible_interval()¶ Credible interval of the posterior distribution.
Parameters: interval_length (float (default=0.9)) – Compute interval_length% credible interval. This is a value in [0, 1].Returns: interval – Lower and upper credible interval limits. Return type: tuple
-
mean()¶ Mean of the posterior distribution.
-
pdf(x)¶ Probability density function of the posterior distribution.
Parameters: x (array-like) – Quantiles. Returns: pdf – Probability density function evaluated at x. Return type: numpy.ndarray
-
ppf(q)¶ Percent point function (quantile) of the posterior distribution.
Parameters: x (array-like) – Lower tail probability. Returns: ppf – Quantile corresponding to the lower tail probability q. Return type: numpy.ndarray
-
rvs(size=1, random_state=None)¶ Random variates of the posterior distribution.
Parameters: - size (int (default=1)) – Number of random variates.
- random_state (int or None (default=None)) – The seed used by the random number generator.
Returns: rvs – Random variates of given size.
Return type: numpy.ndarray or scalar
-
std()¶ Standard deviation of the posterior distribution.
-
update(data)¶ Update posterior parameters with new data samples.
Parameters: data (array-like, shape = (n_samples)) – Data samples from a geometric distribution.
-
var()¶ Variance of the posterior distribution.
-
class
cprior.GeometricABTest(modelA, modelB, simulations=1000000, random_state=None)¶ Bases:
cprior.cdist.beta.BetaABTestGeometric A/B test.
Parameters: - modelA (object) – The control model.
- modelB (object) – The variation model.
- simulations (int or None (default=1000000)) – Number of Monte Carlo simulations.
- random_state (int or None (default=None)) – The seed used by the random number generator.
-
expected_loss(method='exact', variant='A', lift=0)¶ Compute the expected loss. This is the expected uplift lost by choosing a given variant.
- If
variant == "A", \(\mathrm{E}[\max(B - A - lift, 0)]\) - If
variant == "B", \(\mathrm{E}[\max(A - B - lift, 0)]\) - If
variant == "all", both.
If
liftis positive value, the computation method must be Monte Carlo sampling.Parameters: - method (str (default="exact")) – The method of computation. Options are “exact” and “MC”.
- variant (str (default="A")) – The chosen variant. Options are “A”, “B”, “all”.
- lift (float (default=0.0)) – The amount of uplift.
- If
-
expected_loss_ci(method='MC', variant='A', interval_length=0.9)¶ Compute credible intervals on the difference distribution of \(Z = B-A\) and/or \(Z = A-B\).
- If
variant == "A", \(Z = B - A\) - If
variant == "B", \(Z = A - B\) - If
variant == "all", both.
Parameters: - method (str (default="MC")) – The method of computation. Options are “asymptotic” and “MC”.
- variant (str (default="A")) – The chosen variant. Options are “A”, “B”, “all”.
- interval_length (float (default=0.9)) – Compute
interval_length% credible interval. This is a value in [0, 1].
- If
-
expected_loss_relative(method='exact', variant='A')¶ Compute expected relative loss for choosing a variant. This can be seen as the negative expected relative improvement or uplift.
- If
variant == "A", \(\mathrm{E}[(B - A) / A]\) - If
variant == "B", \(\mathrm{E}[(A - B) / B]\) - If
variant == "all", both.
Parameters: - method (str (default="exact")) – The method of computation. Options are “exact” and “MC”.
- variant (str (default="A")) – The chosen variant. Options are “A”, “B”, “all”.
- If
-
expected_loss_relative_ci(method='MC', variant='A', interval_length=0.9)¶ Compute credible intervals on the relative difference distribution of \(Z = (B-A)/A\) and/or \(Z = (A-B)/B\).
- If
variant == "A", \(Z = (B-A)/A\) - If
variant == "B", \(Z = (A-B)/B\) - If
variant == "all", both.
Parameters: - method (str (default="MC")) – The method of computation. Options are “asymptotic”, “exact” and “MC”.
- variant (str (default="A")) – The chosen variant. Options are “A”, “B”, “all”.
- interval_length (float (default=0.9)) – Compute
interval_length% credible interval. This is a value in [0, 1].
- If
-
probability(method='exact', variant='A', lift=0)¶ Compute the error probability or chance to beat control.
- If
variant == "A", \(P[A > B + lift]\) - If
variant == "B", \(P[B > A + lift]\) - If
variant == "all", both.
If
liftis positive value, the computation method must be Monte Carlo sampling.Parameters: - method (str (default="exact")) – The method of computation. Options are “exact” and “MC”.
- variant (str (default="A")) – The chosen variant. Options are “A”, “B”, “all”.
- lift (float (default=0.0)) – The amount of uplift.
- If