def test_add_one_success(self): gen = BetaPrior() gen.add_one(mean=0.5, variance=0.2, effective_size=10, label="option1") assert len(gen.priors) == 1 assert isinstance(gen.priors, dict) assert isinstance(gen.priors["option1"], dict) assert gen.priors == {"option1": {"a": 5, "b": 5}}
def test_add_one_value_errors(self): gen = BetaPrior() mean_errors = [-1, 0, 1.2] variance_errors = [-1, 0, 1] effective_size_errors = [-1, 0] with pytest.raises(ValueError): gen.add_one(mean=0.5, variance=0.5, effective_size=10, label="option1") for item in mean_errors: gen.add_one(mean=item, variance=0.1, effective_size=10, label="option1") for item in variance_errors: gen.add_one(mean=0.5, variance=item, effective_size=10, label="option1") for item in effective_size_errors: gen.add_one(mean=0.5, variance=0.2, effective_size=item, label="option1")
class ThompsonSampling: def __init__(self): self.beta = BetaPrior() self.experimento = BernoulliExperiment(priors=self.beta) def atualizar(self, reward): self.experimento.add_rewards(reward) def inicializar(self, estrategias): for opcao in estrategias: self.beta.add_one(mean=0.5, variance=0.2, effective_size=10, label=opcao) def escolher(self): return self.experimento.choose_arm()
from thompson_sampling.bernoulli import BernoulliExperiment experiment = BernoulliExperiment(arms=2) from thompson_sampling.bernoulli import BernoulliExperiment from thompson_sampling.priors import BetaPrior pr = BetaPrior() pr.add_one(mean=0.5, variance=0.2, effective_size=10, label="option1") pr.add_one(mean=0.6, variance=0.3, effective_size=30, label="option2") experiment = BernoulliExperiment(priors=pr) experiment.choose_arm() #Update rewards rewards = [{"label":"option1", "reward":1}, {"label":"option2", "reward":0}] experiment.add_rewards(rewards)