1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use rand::distributions::Distribution;
use rand::thread_rng;
use rand_distr::Gamma;

use super::{ArgBounds, Bandit};

/// A bandit whose arms distribute rewards according to the gamma distributions.
pub struct GammaBandit {
    /// Vector of distribution shape parameters.
    alphas: Vec<f64>,

    /// Vector of distribution scale parameters.
    thetas: Vec<f64>,

    /// Number of arms on the bandit.
    arms: usize,

    /// The bandit arm with highest reward.
    best_arm: usize,

    /// Distributions of the arms.
    distributions: Vec<Gamma<f64>>,
}

impl GammaBandit {
    /// Initializes a new Bandit where each arm distributes rewards according to a gamma
    /// distribution.
    fn new(alphas: Vec<f64>, thetas: Vec<f64>) -> GammaBandit {
        assert_eq!(alphas.len(), thetas.len());
        assert!(alphas.val_min() > 0.0);
        assert!(thetas.val_min() > 0.0);
        let dist = alphas
            .iter()
            .zip(&thetas)
            .map(|(&a, &t)| Gamma::new(a, t).unwrap())
            .collect();
        let best_arm = alphas
            .iter()
            .zip(&thetas)
            .map(|(&a, &t)| a * t)
            .collect::<Vec<f64>>()
            .arg_max();
        let arms = thetas.len();
        GammaBandit {
            alphas,
            thetas,
            arms,
            best_arm,
            distributions: dist,
        }
    }
}

impl Bandit<f64> for GammaBandit {
    ///Returns the number of arms on the bandit.
    fn arms(&self) -> usize {
        self.arms
    }

    ///Returns the arm with highest average reward.
    fn best_arm(&self) -> usize {
        self.best_arm
    }

    /// The expected return of each arm.
    fn mean(&self, arm: usize) -> f64 {
        self.alphas[arm] * self.thetas[arm]
    }

    /// Determines the reward for pulling a given arm.
    fn reward(&self, arm: usize) -> f64 {
        self.distributions[arm].sample(&mut thread_rng())
    }

    /// The standard deviations of each arm.
    fn std(&self, arm: usize) -> f64 {
        self.alphas[arm].sqrt() * self.thetas[arm]
    }
}

#[cfg(test)]
mod tests {
    use assert_approx_eq::assert_approx_eq;

    use super::GammaBandit;
    use super::super::Bandit;

    #[test]
    fn test_arms() {
        let alphas_vec: Vec<f64> = vec![1.3, 9.5, 1.6, 1.9, 8.9];
        let thetas_vec: Vec<f64> = vec![8.0, 0.7, 3.3, 0.5, 5.9];
        let gamma: GammaBandit = GammaBandit::new(alphas_vec, thetas_vec);
        assert_eq!(gamma.arms(), 5)
    }

    #[test]
    fn test_best_arm() {
        let alphas_vec: Vec<f64> = vec![1.3, 9.5, 1.6, 1.9, 8.9];
        let thetas_vec: Vec<f64> = vec![8.0, 0.7, 3.3, 0.5, 5.9];
        let gamma: GammaBandit = GammaBandit::new(alphas_vec, thetas_vec);
        assert_eq!(gamma.best_arm(), 4)
    }

    #[test]
    fn test_max_reward() {
        let alphas_vec: Vec<f64> = vec![1.3, 9.5, 1.6, 1.9, 8.9];
        let thetas_vec: Vec<f64> = vec![8.0, 0.7, 3.3, 0.5, 5.9];
        let gamma: GammaBandit = GammaBandit::new(alphas_vec, thetas_vec);
        assert_approx_eq!(gamma.max_reward(), 52.51)
    }

    #[test]
    fn test_mean() {
        let alphas_vec: Vec<f64> = vec![1.3, 9.5, 1.6, 1.9, 8.9];
        let thetas_vec: Vec<f64> = vec![8.0, 0.7, 3.3, 0.5, 5.9];
        let gamma: GammaBandit = GammaBandit::new(alphas_vec, thetas_vec);
        assert_approx_eq!(gamma.mean(1), 6.65)
    }

    #[test]
    fn test_means() {
        let alphas_vec: Vec<f64> = vec![1.3, 9.5, 1.6, 1.9, 8.9];
        let thetas_vec: Vec<f64> = vec![8.0, 0.7, 3.3, 0.5, 5.9];
        let gamma: GammaBandit = GammaBandit::new(alphas_vec, thetas_vec);
        gamma
            .means()
            .iter()
            .zip(vec![10.4, 6.65, 5.28, 0.95, 52.51])
            .for_each(|(m1, m2)| assert_approx_eq!(m1, m2))
    }

    #[test]
    #[should_panic]
    fn test_new_neg_alphas() {
        let alphas_vec: Vec<f64> = vec![1.3, 9.5, 1.6, -1.9, 8.9];
        let thetas_vec: Vec<f64> = vec![8.0, 0.7, 3.3, 0.5, 5.9];
        GammaBandit::new(alphas_vec, thetas_vec);
    }

    #[test]
    #[should_panic]
    fn test_new_neg_thetas() {
        let alphas_vec: Vec<f64> = vec![1.3, 9.5, 1.6, 1.9, 8.9];
        let thetas_vec: Vec<f64> = vec![8.0, -0.7, 3.3, 0.5, 5.9];
        GammaBandit::new(alphas_vec, thetas_vec);
    }

    #[test]
    #[should_panic]
    fn test_new_wrong_size() {
        let alphas_vec: Vec<f64> = vec![1.3, 9.5, 1.6, 1.9, 8.9];
        let thetas_vec: Vec<f64> = vec![8.0, 0.7, 3.3, 0.5];
        GammaBandit::new(alphas_vec, thetas_vec);
    }

    #[test]
    fn test_reward() {
        let alphas_vec: Vec<f64> = vec![1.3, 9.5, 1.6, 1.9, 8.9];
        let thetas_vec: Vec<f64> = vec![8.0, 0.7, 3.3, 0.5, 5.9];
        let gamma: GammaBandit = GammaBandit::new(alphas_vec, thetas_vec);
        for _ in 0..1000 {
            gamma.reward(2);
        }
    }

    #[test]
    fn test_std() {
        let alphas_vec: Vec<f64> = vec![1.3, 9.5, 1.6, 1.9, 8.9];
        let thetas_vec: Vec<f64> = vec![8.0, 0.7, 3.3, 0.5, 5.9];
        let gamma: GammaBandit = GammaBandit::new(alphas_vec, thetas_vec);
        assert_approx_eq!(gamma.std(1), 2.1575449)
    }

    #[test]
    fn test_stds() {
        let alphas_vec: Vec<f64> = vec![1.3, 9.5, 1.6, 1.9, 8.9];
        let thetas_vec: Vec<f64> = vec![8.0, 0.7, 3.3, 0.5, 5.9];
        let gamma: GammaBandit = GammaBandit::new(alphas_vec, thetas_vec);
        gamma
            .stds()
            .iter()
            .zip(vec![
                9.1214034,
                2.1575449,
                4.1742065,
                0.68920243,
                17.60139199,
            ])
            .for_each(|(s1, s2)| assert_approx_eq!(s1, s2))
    }
}