1 | import numpy as np
|
---|
2 | from scipy.stats import norm
|
---|
3 |
|
---|
4 | class StrategyModel():
|
---|
5 | def __init__(self, alphas: list, betas: list, accepts: list):
|
---|
6 | self.alphas = alphas
|
---|
7 | self.betas = betas
|
---|
8 | self.accepts = accepts
|
---|
9 |
|
---|
10 | self.SIGMA = 0.1
|
---|
11 | self.LINE_FACTOR = 0.1
|
---|
12 |
|
---|
13 | def u(self, starting_util: float, alpha: float):
|
---|
14 | return self.p(alpha) * (starting_util + (1.0 - starting_util) * alpha)
|
---|
15 |
|
---|
16 | def p(self, alpha: float):
|
---|
17 | line_value = self.linear(alpha)
|
---|
18 | gauss_factor = self.gauss(np.repeat(alpha, len(self.alphas)), self.alphas, self.SIGMA)
|
---|
19 | gauss_value = np.array(self.accepts) * gauss_factor
|
---|
20 | prob = (np.sum(gauss_value) + self.LINE_FACTOR * line_value) / (np.sum(gauss_factor) + self.LINE_FACTOR)
|
---|
21 | return prob
|
---|
22 |
|
---|
23 | def gauss(self, x, mu, sig):
|
---|
24 | return np.exp(-np.power((x - mu) / sig, 2.) / 2 )
|
---|
25 |
|
---|
26 | def linear(self, x: float):
|
---|
27 | return 1.0 - x
|
---|
28 |
|
---|
29 | #call with mag = desired degrees of precision
|
---|
30 | def max_u(self, starting_util: float, min_u: float, max_u: float, mag: int):
|
---|
31 | if mag > 0:
|
---|
32 | step = float(max_u - min_u)/10
|
---|
33 | start = min_u
|
---|
34 | maxi = start
|
---|
35 | best_u = None
|
---|
36 | while start <= max_u:
|
---|
37 | new_u = self.u(starting_util, start)
|
---|
38 | if best_u is None or new_u > best_u:
|
---|
39 | maxi = start
|
---|
40 | best_u = new_u
|
---|
41 | start += step
|
---|
42 | return self.max_u(starting_util, min(max_u - 2 * step, max(maxi-step, min_u)), min(max_u, max(maxi+step, min_u + 2 * step)), mag-1)
|
---|
43 | else:
|
---|
44 | return min_u
|
---|