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
use rand::distributions::Distribution;
use rand::thread_rng;
use rand_distr::Exp;

use super::{ArgBounds, Bandit};

/// A bandit whose arms distribute rewards according to the exponential distributions.
pub struct ExponentialBandit {
    /// Vector of the inverses of the distribution means.
    lambdas: 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<Exp<f64>>,
}

impl ExponentialBandit {
    /// Initializes a new Bandit where each arm distributes rewards according to an exponential
    /// distribution.
    pub fn new(lambdas: Vec<f64>) -> ExponentialBandit {
        assert!(lambdas.val_min() > 0.0);
        let dist = lambdas.iter().map(|&l| Exp::new(l).unwrap()).collect();
        let arms = lambdas.len();
        let best_arm = lambdas.arg_min();
        ExponentialBandit {
            lambdas,
            arms,
            best_arm,
            distributions: dist,
        }
    }
}

impl Bandit<f64> for ExponentialBandit {
    ///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 {
        1.0 / self.lambdas[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 {
        1.0 / self.lambdas[arm]
    }
}

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

    use super::ExponentialBandit;
    use super::super::Bandit;

    #[test]
    fn test_arms() {
        let lambdas_vec: Vec<f64> = vec![6.1, 0.6, 5.4, 9.1, 2.5];
        let exp: ExponentialBandit = ExponentialBandit::new(lambdas_vec);
        assert_eq!(exp.arms(), 5)
    }

    #[test]
    fn test_best_arm() {
        let lambdas_vec: Vec<f64> = vec![6.1, 0.6, 5.4, 9.1, 3.5];
        let exp: ExponentialBandit = ExponentialBandit::new(lambdas_vec);
        assert_eq!(exp.best_arm(), 1)
    }

    #[test]
    fn test_max_reward() {
        let lambdas_vec: Vec<f64> = vec![6.1, 0.6, 5.4, 9.1, 3.5];
        let exp: ExponentialBandit = ExponentialBandit::new(lambdas_vec);
        assert_approx_eq!(exp.max_reward(), 1.6666667)
    }

    #[test]
    fn test_mean() {
        let lambdas_vec: Vec<f64> = vec![6.1, 0.6, 5.4, 9.1, 2.5];
        let exp: ExponentialBandit = ExponentialBandit::new(lambdas_vec);
        assert_eq!(exp.mean(4), 0.4)
    }

    #[test]
    fn test_means() {
        let lambdas_vec: Vec<f64> = vec![6.1, 0.6, 5.4, 9.1, 3.5];
        let exp: ExponentialBandit = ExponentialBandit::new(lambdas_vec);
        exp.means()
            .iter()
            .zip(vec![6.1, 0.6, 5.4, 9.1, 3.5])
            .for_each(|(m1, m2)| assert_approx_eq!(1.0 / m1, m2))
    }

    #[test]
    #[should_panic]
    fn test_new_neg_lambda() {
        let lambdas_vec: Vec<f64> = vec![6.1, 0.6, -5.4, 9.1, 3.5];
        ExponentialBandit::new(lambdas_vec);
    }

    #[test]
    fn test_reward() {
        let lambdas_vec: Vec<f64> = vec![6.1, 0.6, 5.4, 9.1, 3.5];
        let exp: ExponentialBandit = ExponentialBandit::new(lambdas_vec);
        for _ in 0..1000 {
            exp.reward(2);
        }
    }

    #[test]
    fn test_std() {
        let lambdas_vec: Vec<f64> = vec![6.1, 0.6, 5.4, 9.1, 3.5];
        let exp: ExponentialBandit = ExponentialBandit::new(lambdas_vec);
        assert_approx_eq!(exp.std(1), 1.6666667)
    }

    #[test]
    fn test_stds() {
        let lambdas_vec: Vec<f64> = vec![6.1, 0.6, 5.4, 9.1, 3.5];
        let exp: ExponentialBandit = ExponentialBandit::new(lambdas_vec);
        exp.stds()
            .iter()
            .zip(vec![6.1, 0.6, 5.4, 9.1, 3.5])
            .for_each(|(s1, s2)| assert_approx_eq!(1.0 / s1, s2))
    }
}