-
Notifications
You must be signed in to change notification settings - Fork 7
/
proportional.go
61 lines (49 loc) · 1.56 KB
/
proportional.go
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
package mab
import (
"fmt"
"math"
)
func NewProportional() *Proportional {
return &Proportional{}
}
// Proportional is a trivial bandit strategy that returns arm-selection probabilities proportional to the mean reward estimate for each arm.
// This can be used when a bandit service wants to provide selection weights rather than reward estimates.
// Proportional treats Point(0) and Null() the same way, assigning them zero selection probability.
type Proportional struct {
meanRewards, probs []float64
}
// ComputeProbs computes probabilities proportional to the mean reward of each arm.
// Returns an error if any arm has a negative finite mean reward.
// A mean reward of negative infinity is treated as zero, so that a Null() distribution is treated the same as Point(0).
func (p *Proportional) ComputeProbs(rewards []Dist) ([]float64, error) {
p.meanRewards = make([]float64, len(rewards))
for i, dist := range rewards {
mean := dist.Mean()
switch {
default:
p.meanRewards[i] = mean
case mean > math.Inf(-1) && mean < 0:
return nil, fmt.Errorf("negative mean reward")
case math.IsInf(mean, -1): // indicates a Null distribution
p.meanRewards[i] = 0
}
}
return p.computeProbs()
}
func (p Proportional) computeProbs() ([]float64, error) {
norm := 0.0
for _, r := range p.meanRewards {
if r < 0 {
return nil, fmt.Errorf("negative mean reward: %+v", r)
}
norm += r
}
p.probs = make([]float64, len(p.meanRewards))
if norm == 0 {
return p.probs, nil
}
for i, mean := range p.meanRewards {
p.probs[i] = mean / norm
}
return p.probs, nil
}