Exemplo n.º 1
0
 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}}
Exemplo n.º 2
0
 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")
Exemplo n.º 3
0
 def test_add_multiple_error(self):
     gen = BetaPrior()
     means = Series([0.5, 0.6])
     variances = Series([0.24, 0.34, None])
     effective_sizes = Series([20, 10, 50])
     labels = Series(["option1", "option2", "option3"])
     with pytest.raises(ValueError):
         gen.add_multiple(means, variances, effective_sizes, labels)
Exemplo n.º 4
0
 def test_init_custom(self):
     prior = BetaPrior()
     means = Series([0.2, 0.5])
     variances = Series([0.02, 0.2])
     effective_sizes = Series([10, 10])
     labels = Series(["option1", "option2"])
     prior.add_multiple(means, variances, effective_sizes, labels)
     exper = BernoulliExperiment(priors=prior)
     assert len(exper.posteriors) == 2
Exemplo n.º 5
0
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()
Exemplo n.º 6
0
 def test_add_multiple(self):
     gen = BetaPrior()
     means = Series([0.2, 0.5])
     variances = Series([0.02, 0.2])
     effective_sizes = Series([10, 10])
     labels = Series(["option1", "option2"])
     gen.add_multiple(means, variances, effective_sizes, labels)
     assert len(gen.priors) == 2
     assert gen.priors == {
         "option1": {
             "a": 2.0,
             "b": 8.0
         },
         "option2": {
             "a": 5.0,
             "b": 5.0
         },
     }
Exemplo n.º 7
0
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)
Exemplo n.º 8
0
 def __init__(self):
     self.beta = BetaPrior()
     self.experimento = BernoulliExperiment(priors=self.beta)