The crepes.martingales module#
- class crepes.martingales.EpsilonBettingFunction(E=[-1, -0.5, 0, 0.5, 1])[source]#
An epsilon betting function generates rewards for a sequence of p-values.
Methods
apply(p_values)Obtain rewards from p-values.
- Parameters:
E (list of floats or integers, default=[-1, -0.5, 0, 0.5, 1]) – epsilons, where each epsilon is in the range (-2, 2)
Examples
An epsilon betting function using default values is obtained by:
bf = EpsilonBettingFunction()
An epsilon betting function using the values -1, 0 and 1 is obtained by:
bf = EpsilonBettingFunction(E = [-1, 0, 1])
Note
A runtime error is reported if not all values in E are within the range (-2, 2).
Methods
apply(p_values)Obtain rewards from p-values.
- apply(p_values)[source]#
Obtain rewards from p-values.
- Parameters:
p_values (array-like of shape (n_values,)) – p-values
- Returns:
rewards – rewards for each p-value and epsilon, where n_epsilons is the number of values in E (provided during initialization)
- Return type:
ndarray of shape (n_values, n_epsilons)
Examples
Assuming that
p_valuesis a vector of p-values, then rewards using the default epsilon values are generated by:rewards = EpsilonBettingFunction().apply(p_values)
Rewards using the epsilon values [-1, 0 and 1] are obtained by:
rewards = EpsilonBettingFunction(E = [-1, 0, 1]).apply(p_values)
- class crepes.martingales.StepBettingFunction(G=10, Gs=None, A=None, s=None)[source]#
A step betting function generates rewards for a sequence of p-values.
Methods
apply(p_values[, s])Obtain rewards from p-values.
- Parameters:
G (integer, default=10) – grid size, ignored if Gs is not None
Gs (list of floats, default=None) – grid values, set to [1/G, …, (G-1)/G] if Gs is None
A (real or array-like of shape (a_values,), default=None) – threshold values, set to Gs if A is None
s (integer, default=None) – start index, used for step betting function with drift
Examples
A step betting function using default values is obtained by:
bf = StepBettingFunction()
A step betting function using the threshold values [0.2, 0.4, 0.6] is obtained by:
bf = EpsilonBettingFunction(A = [0.2, 0.4, 0.6])
Methods
apply(p_values[, s])Obtain rewards from p-values.
- apply(p_values, s=None)[source]#
Obtain rewards from p-values.
- Parameters:
p_values (array-like of shape (n_values,)) – p-values
- Returns:
rewards – rewards for each p-value and epsilon, where grid_size is the number pairs in the grid (specified during initialization)
- Return type:
ndarray of shape (n_values, grid_size)
Examples
Assuming that
p_valuesis a vector of p-values, then rewards using the default step betting function are generated by:rewards = StepBettingFunction().apply(p_values)
where
rewardswill be an ndarray with the same number of rows as the length ofp_valuesand with 81 columns, which follows from that the default grid size (G) is 10, resulting in a grid that consists of the pairs formed from the grid values [1/G, …, (G-1)/G].
- class crepes.martingales.SimpleJumper(J=0.01, bf=<crepes.martingales.EpsilonBettingFunction object>)[source]#
SimpleJumper computes martingale values given a vector of p-values.
Methods
apply(p_values[, c])Generate a vector of martingale values, given a vector of p-values.
- Parameters:
J (float, 0 < J < 1) – jumping rate
bf (
Martingale) – betting function
Examples
SimpleJumper with default settings can be obtained by:
m = SimpleJumper()
We may initialize SimpleJumper with a specific jumping rate and betting function:
m = SimpleJumper(J = 0.1, bf = StepBettingFunction())
Note
The specified betting function must have a method apply that given a vector of p-values (ndarray of shape (n_values,)), returns an ndarray of shape (n_values, n_accounts), where n_accounts >= 1.
Note
SimpleJumper is a generalization of Alg. 8.1: Simple Jumper betting martingale in ALRW, 2ed, p., 232.
Methods
apply(p_values[, c])Generate a vector of martingale values, given a vector of p-values.
- apply(p_values, c=None)[source]#
Generate a vector of martingale values, given a vector of p-values.
- Parameters:
p_values (array-like of shape (n_values,)) – p-values
c (integer, default=None) – threshold value
- Returns:
martingales (ndarray of shape (n_values,)) – martingale values, returned only if c is None
index (integer, 0 <= index <= n_values) – index of the first martingale value that is equal to or greater than c, if any, and index = n_values, otherwise, returned only if c is not None
Examples
Assuming that
p_valuesis a vector of p-values, then martingale values can be generated by:v = SimpleJumper().apply(p_values)
The following gives the index of the first martingale value that is equal to or exceeds 100:
i = SimpleJumper().apply(p_values, c=100)
Note
If a threshold (c) is specified, the computation is terminated as soon as a martingale value reaches this, which may save some time compared to computing the full sequence.
- class crepes.martingales.SleeperStayer(R=0.001, bf=<crepes.martingales.StepBettingFunction object>)[source]#
SleeperStayer computes martingale values given a vector of p-values.
Methods
apply(p_values[, c])Generate a vector of martingale values, given a vector of p-values.
- Parameters:
R (float, 0 < R < 1) – investment rate
bf (
Martingale) – betting function
Examples
SleeperStayer with default settings can be obtained by:
m = SleeperStayer()
We may initialize SleeperStayer with a specific investment rate and betting function:
m = SleeperStayer(J = 0.1, bf = EpsilonBettingFunction())
Note
The specified betting function must have a method apply that given a vector of p-values (ndarray of shape (n_values,)), returns an ndarray of shape (n_values, n_accounts), where n_accounts >= 1.
Note
SleeperStayer is a generalization of Alg. 9.4: Sleeper/Stayer in ALRW, 2ed, p., 283.
Methods
apply(p_values[, c])Generate a vector of martingale values, given a vector of p-values.
- apply(p_values, c=None)[source]#
Generate a vector of martingale values, given a vector of p-values.
- Parameters:
p_values (array-like of shape (n_values,)) – p-values
c (integer, default=None) – threshold value
- Returns:
martingales (ndarray of shape (n_values,)) – martingale values, returned only if c is None
index (integer, 0 <= index <= n_values) – index of the first martingale value that is equal to or greater than c, if any, and index = n_values, otherwise, returned only if c is not None
Examples
Assuming that
p_valuesis a vector of p-values, then martingale values can be generated by:v = SleeperStayer().apply(p_values)
The following gives the index of the first martingale value that is equal to or exceeds 100:
i = SleeperStayer().apply(p_values, c=100)
Note
If a threshold (c) is specified, the computation is terminated as soon as a martingale value reaches this, which may save some time compared to computing the full sequence.
- class crepes.martingales.SleeperDrifter(R=0.1, M=100, bf=<crepes.martingales.StepBettingFunction object>, drift=True)[source]#
SleeperDrifter computes martingale values given a vector of p-values.
Methods
apply(p_values[, c])Generate a vector of martingale values, given a vector of p-values.
- Parameters:
R (float, 0 < R < 1) – investment rate
M (integer, 0 < M, default=100) – wake-up frequency
bf (
Martingale) – betting function
Examples
SleeperDrifter with default settings can be obtained by:
m = SleeperDrifter()
We may initialize SleeperDrifter with a specific investment rate, wake-up frequency and betting function:
m = SleeperStayer(J = 0.1, M = 50, bf = EpsilonBettingFunction())
Note
The specified betting function must have a method apply that given a vector of p-values (ndarray of shape (n_values,)), returns an ndarray of shape (n_values, n_accounts), where n_accounts >= 1.
Note
SleeperDrifter is a generalization of Alg. 9.5: Sleeper/Drifter in ALRW, 2ed, p., 284 with a slightly modified initialization, by which rewards greater than 1 may be obtained for the first M p-values.
Methods
apply(p_values[, c])Generate a vector of martingale values, given a vector of p-values.
- apply(p_values, c=None)[source]#
Generate a vector of martingale values, given a vector of p-values.
- Parameters:
p_values (array-like of shape (n_values,)) – p-values
c (integer, default=None) – threshold value
- Returns:
martingales (ndarray of shape (n_values,)) – martingale values, returned only if c is None
index (integer, 0 <= index <= n_values) – index of the first martingale value that is equal to or greater than c, if any, and index = n_values, otherwise, returned only if c is not None
Examples
Assuming that
p_valuesis a vector of p-values, then martingale values can be generated by:v = SleeperDrifter().apply(p_values)
The following gives the index of the first martingale value that is equal to or exceeds 100:
i = SleeperDrifter().apply(p_values, c=100)
Note
If a threshold (c) is specified, the computation is terminated as soon as a martingale value reaches this, which may save some time compared to computing the full sequence.
- class crepes.martingales.CompositeMartingale(Martingales)[source]#
A composite martingale averages the output of multiple conformal test martingales given at initialization.
Methods
apply(p_values[, c])Generate a vector of martingale values, given a vector of p-values.
- Parameters:
Martingales (list of conformal test martingales) – conformal test martingales, used for averaging
Examples
An composite martingale can be obtained by:
cm = CompositeMartingale([SimpleJumper(), SleeperStayer()])
Note
Each object in the provided list of conformal test martingales is required to have a method apply that given a vector of p-values returns an equally long vector of martingale values.
Methods
apply(p_values[, c])Generate a vector of martingale values, given a vector of p-values.
- apply(p_values, c=None)[source]#
Generate a vector of martingale values, given a vector of p-values.
- Parameters:
p_values (array-like of shape (n_values,)) – p-values
c (integer, default=None) – threshold value
- Returns:
martingales (ndarray of shape (n_values,)) – martingale values, returned only if c is None
index (integer, 0 <= index <= n_values) – index of the first martingale value that is equal to or greater than c, if any, and index = n_values, otherwise, returned only if c is not None
Examples
Assuming that
p_valuesis a vector of p-values, then martingale values can be generated by:cm = CompositeMartingale([SimpleJumper(), SleeperStayer()]) v = cm.apply(p_values)
The following gives the index of the first martingale value that is equal to or exceeds 100:
i = cm.apply(p_values, c=100)
- crepes.martingales.semi_online_p_values(alphas, seed=None)[source]#
Computes semi-online p-values from a vector of non-conformity scores.
- Parameters:
alphas (array-like of shape (n_values,)) – non-conformity scores
seed (int, default=None) – set random seed
- Returns:
p_values – p-values
- Return type:
array-like of shape (n_values,)
Examples
Assuming that alphas is a vector of non-conformity scores, p-values are obtained by:
from crepes.martingales import semi_online_p_values p_values = semi_online_p_values(alphas)
The above results in that
p_valuesis assigned a vector of the same length asalphasand where the p-value for each non-conformity score is computed with respect to the preceding non-conformity scores only.Note
The output p-values are smoothed and generated in the semi-online mode according to the terminology in in ALRW, 2ed, p., 111.