k-Armed Bandit 1.0.0
A collection of k-armed bandits and assoicated agents for reinforcement learning
Loading...
Searching...
No Matches
test_normal.py
Go to the documentation of this file.
1from bandit import Normal
2import unittest
3
4
5class TestNormalBandit(unittest.TestCase):
6 """
7 Tests the methods for a normal distribution bandit.
8 """
9
11 """
12 Ensure that arm distributions have means on the range [-1, 1) and
13 standard deviations of 1.
14 """
15 # As these are randomly selected, do this several times to
16 # somewhat increase confidence.
17 for i in range(100):
18 K = 100
19 bandit = Normal(k=K)
20 (mean, std) = bandit.trueValues()
21 # All means should be between [-1, 1), so use the all() to alert if
22 # there are any that aren't.
23 self.assertTrue((mean >= -1.0).all(),
24 msg='Selected means have a value below -1.0.')
25 self.assertTrue((mean < 1.0).all(),
26 msg='Selected means have a value above 1.0.')
27 # Standard deviations are fixed at 1.0.
28 self.assertTrue((std == 1.0).all(),
29 msg='Standard deviations are not all 1.0.')
30
32 """
33 Test that rewards of the correct shape are produced.
34
35 Since any numeric value is technically possible, only check that the
36 right number of rewards are returned.
37 """
38 K = 10
39 bandit = Normal(K)
40 # Integers should return single values, as long as they are within
41 # range
42 for arm in (-3, 0, 2, 9):
43 reward = bandit.select(arm)
44 self.assertTrue(isinstance(reward, float))
45 # Ranges should return the same number of elements.
46 reward = bandit.select(range(K))
47 self.assertEqual(len(reward), K)
48 # None should return None.
49 self.assertIsNone(bandit.select(None))
50 # Other values should produce an error.
51 for i in (0.5, K, '1', 'the'):
52 with self.subTest(i=i):
53 with self.assertRaises(Exception, msg='Incorrect indices not rejected.'):
54 reward = bandit.select(i)
This bandit draws a reward from a set normal distribution each time an arm is chosen.
Definition normal.py:5
Tests the methods for a normal distribution bandit.
Definition test_normal.py:5
test_distribution_generation(self)
Ensure that arm distributions have means on the range [-1, 1) and standard deviations of 1.
test_reward_selection(self)
Test that rewards of the correct shape are produced.