-
Notifications
You must be signed in to change notification settings - Fork 19
/
threshold.py
123 lines (93 loc) · 3.27 KB
/
threshold.py
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
# abcpmc is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# abcpmc is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with abcpmc. If not, see <http://www.gnu.org/licenses/>.
'''
Created on Jan 19, 2015
author: jakeret
'''
from __future__ import print_function, division, absolute_import, unicode_literals
import numpy as np
class EpsProposal(object):
def __init__(self, T):
self.T = T
self.reset()
def __iter__(self):
return self
def __next__(self):
return self.next()
def next(self):
if(self.t>=self.T):
raise StopIteration()
eps_val = self(self.t)
self.t += 1
return eps_val
def reset(self):
self.t = 0
class ListEps(EpsProposal):
def __init__(self, T, eps_vals):
super(ListEps, self).__init__(T)
self.eps_vals = eps_vals
def __call__(self, t):
return self.eps_vals[t]
class ConstEps(EpsProposal):
"""
Constant threshold. Can be used to apply alpha-percentile threshold decrease
:param eps: epsilon value
"""
def __init__(self, T, eps):
super(ConstEps, self).__init__(T)
self.eps = eps
def __call__(self, t):
return self.eps
class LinearEps(EpsProposal):
"""
Linearly decreasing threshold
:param max: epsilon at t=0
:param min: epsilon at t=T
:param T: number of iterations
"""
def __init__(self, T, max, min):
super(LinearEps, self).__init__(T)
self.eps_vals = np.linspace(max, min, T)
def __call__(self, t):
return self.eps_vals[t]
class LinearConstEps(EpsProposal):
"""
Linearly decreasing threshold until T1, then constant until T2
:param max: epsilon at t=0
:param min: epsilon at t=T
:param T1: number of iterations for decrease
:param T2: number of iterations for constant behavior
"""
def __init__(self, max, min, T1, T2):
super(LinearConstEps, self).__init__(T1+T2)
self.eps_vals = np.r_[np.linspace(max, min, T1), [min]*T2]
def __call__(self, t):
return self.eps_vals[t]
class ExponentialEps(EpsProposal):
"""
Exponentially decreasing threshold
:param max: epsilon at t=0
:param min: epsilon at t=T
:param T: number of iterations
"""
def __init__(self, T, max, min):
super(ExponentialEps, self).__init__(T)
self.eps_vals = np.logspace(np.log10(max), np.log10(min), T)
def __call__(self, t):
return self.eps_vals[t]
class ExponentialConstEps(EpsProposal):
def __init__(self, max, min, T1, T2):
super(ExponentialConstEps, self).__init__(T1+T2)
self.eps_vals = np.r_[np.logspace(np.log10(max), np.log10(min), T1), [min]*T2]
def __call__(self, t):
return self.eps_vals[t]