forked from huazhengwang/CF_Bandit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinUCB.py
216 lines (175 loc) · 7.42 KB
/
LinUCB.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import numpy as np
from YahooExp_util_functions import vectorize
class LinUCBUserStruct:
def __init__(self, featureDimension, lambda_, init="zero"):
self.d = featureDimension
self.A = lambda_*np.identity(n = self.d)
self.b = np.zeros(self.d)
self.AInv = np.linalg.inv(self.A)
if (init=="random"):
self.UserTheta = np.random.rand(self.d)
else:
self.UserTheta = np.zeros(self.d)
self.time = 0
def updateParameters(self, articlePicked_FeatureVector, click):
self.A += np.outer(articlePicked_FeatureVector,articlePicked_FeatureVector)
self.b += articlePicked_FeatureVector*click
self.AInv = np.linalg.inv(self.A)
self.UserTheta = np.dot(self.AInv, self.b)
self.time += 1
def getTheta(self):
return self.UserTheta
def getA(self):
return self.A
def getProb(self, alpha, article_FeatureVector):
if alpha == -1:
alpha = alpha = 0.1*np.sqrt(np.log(self.time+1))
mean = np.dot(self.UserTheta, article_FeatureVector)
var = np.sqrt(np.dot(np.dot(article_FeatureVector, self.AInv), article_FeatureVector))
pta = mean + alpha * var
return pta
def getProb_plot(self, alpha, article_FeatureVector):
mean = np.dot(self.UserTheta, article_FeatureVector)
var = np.sqrt(np.dot(np.dot(article_FeatureVector, self.AInv), article_FeatureVector))
pta = mean + alpha * var
return pta, mean, alpha * var
class Uniform_LinUCBAlgorithm(object):
def __init__(self, dimension, alpha, lambda_, init="zero"):
self.dimension = dimension
self.alpha = alpha
self.USER = LinUCBUserStruct(dimension, lambda_, init)
self.CanEstimateUserPreference = False
self.CanEstimateCoUserPreference = True
self.CanEstimateW = False
self.CanEstimateV = False
def decide(self, pool_articles, userID):
maxPTA = float('-inf')
articlePicked = None
for x in pool_articles:
x_pta = self.USER.getProb(self.alpha, x.contextFeatureVector[:self.dimension])
if maxPTA < x_pta:
articlePicked = x
maxPTA = x_pta
return articlePicked
def updateParameters(self, articlePicked, click, userID):
self.USER.updateParameters(articlePicked.contextFeatureVector[:self.dimension], click)
def getCoTheta(self, userID):
return self.USER.UserTheta
#---------------LinUCB(fixed user order) algorithm---------------
class N_LinUCBAlgorithm:
def __init__(self, dimension, alpha, lambda_, n, init="zero"): # n is number of users
self.users = []
#algorithm have n users, each user has a user structure
for i in range(n):
self.users.append(LinUCBUserStruct(dimension, lambda_ , init))
self.dimension = dimension
self.alpha = alpha
self.CanEstimateUserPreference = False
self.CanEstimateCoUserPreference = True
self.CanEstimateW = False
self.CanEstimateV = False
def decide(self, pool_articles, userID):
maxPTA = float('-inf')
articlePicked = None
for x in pool_articles:
x_pta = self.users[userID].getProb(self.alpha, x.contextFeatureVector[:self.dimension])
# pick article with highest Prob
if maxPTA < x_pta:
articlePicked = x
maxPTA = x_pta
return articlePicked
def getProb(self, pool_articles, userID):
means = []
vars = []
for x in pool_articles:
x_pta, mean, var = self.users[userID].getProb_plot(self.alpha, x.contextFeatureVector[:self.dimension])
means.append(mean)
vars.append(var)
return means, vars
def updateParameters(self, articlePicked, click, userID):
self.users[userID].updateParameters(articlePicked.contextFeatureVector[:self.dimension], click)
def getCoTheta(self, userID):
return self.users[userID].UserTheta
#-----------LinUCB select user algorithm-----------
class LinUCB_SelectUserAlgorithm(N_LinUCBAlgorithm):
def __init__(self, dimension, alpha, lambda_, n): # n is number of users
N_LinUCBAlgorithm.__init__(self, dimension, alpha, lambda_, n)
def decide(self, pool_articles, AllUsers):
maxPTA = float('-inf')
articlePicked = None
userPicked = None
for x in pool_articles:
for user in AllUsers:
x_pta = self.users[user.id].getProb(self.alpha, x.contextFeatureVector[:self.dimension])
# pick article with highest Prob
if maxPTA < x_pta:
articlePicked = x
userPicked = user
maxPTA = x_pta
return userPicked, articlePicked
class Hybrid_LinUCB_singleUserStruct(LinUCBUserStruct):
def __init__(self, userFeature, lambda_, userID):
LinUCBUserStruct.__init__(self, len(userFeature), lambda_)
self.d = len(userFeature)
self.B = np.zeros([self.d, self.d**2])
self.userFeature = userFeature
def updateParameters(self, articlePicked_FeatureVector, click):
additionalFeatureVector = vectorize(np.outer(self.userFeature, articlePicked_FeatureVector))
LinUCBUserStruct.updateParameters(self, articlePicked_FeatureVector, click)
self.B +=np.outer(articlePicked_FeatureVector, additionalFeatureVector)
def updateTheta(self, beta):
self.UserTheta = np.dot(self.AInv, (self.b- np.dot(self.B, beta)))
class Hybrid_LinUCBUserStruct:
def __init__(self, featureDimension, lambda_, userFeatureList):
self.k = featureDimension**2
self.A_z = lambda_*np.identity(n = self.k)
self.b_z = np.zeros(self.k)
self.A_zInv = np.linalg.inv(self.A_z)
self.beta = np.dot(self.A_zInv, self.b_z)
self.users = []
for i in range(len(userFeatureList)):
self.users.append(Hybrid_LinUCB_singleUserStruct(userFeatureList[i], lambda_ , i))
def updateParameters(self, articlePicked_FeatureVector, click, userID):
z = vectorize( np.outer(self.users[userID].userFeature, articlePicked_FeatureVector))
temp = np.dot(np.transpose(self.users[userID].B), self.users[userID].AInv)
self.A_z += np.dot(temp, self.users[userID].B)
self.b_z +=np.dot(temp, self.users[userID].b)
self.users[userID].updateParameters(articlePicked_FeatureVector, click)
temp = np.dot(np.transpose(self.users[userID].B), self.users[userID].AInv)
self.A_z = self.A_z + np.outer(z,z) - np.dot(temp, self.users[userID].B)
self.b_z =self.b_z+ click*z - np.dot(temp, self.users[userID].b)
self.A_zInv = np.linalg.inv(self.A_z)
self.beta =np.dot(self.A_zInv, self.b_z)
self.users[userID].updateTheta(self.beta)
def getProb(self, alpha, article_FeatureVector,userID):
x = article_FeatureVector
z = vectorize(np.outer(self.users[userID].userFeature, article_FeatureVector))
temp =np.dot(np.dot(np.dot( self.A_zInv , np.transpose( self.users[userID].B)) , self.users[userID].AInv), x )
mean = np.dot(self.users[userID].UserTheta, x)+ np.dot(self.beta, z)
s_t = np.dot(np.dot(z, self.A_zInv), z) + np.dot(np.dot(x, self.users[userID].AInv), x)
-2* np.dot(z, temp)+ np.dot(np.dot( np.dot(x, self.users[userID].AInv) , self.users[userID].B ) ,temp)
var = np.sqrt(s_t)
pta = mean + alpha * var
return pta
class Hybrid_LinUCBAlgorithm(object):
def __init__(self, dimension, alpha, lambda_, userFeatureList):
self.dimension = dimension
self.alpha = alpha
self.USER = Hybrid_LinUCBUserStruct(dimension, lambda_, userFeatureList)
self.CanEstimateUserPreference = False
self.CanEstimateCoUserPreference = False
self.CanEstimateW = False
self.CanEstimateV = False
def decide(self, pool_articles, userID):
maxPTA = float('-inf')
articlePicked = None
for x in pool_articles:
x_pta = self.USER.getProb(self.alpha, x.contextFeatureVector[:self.dimension], userID)
if maxPTA < x_pta:
articlePicked = x
maxPTA = x_pta
return articlePicked
def updateParameters(self, articlePicked, click, userID):
self.USER.updateParameters(articlePicked.contextFeatureVector, click, userID)
def getCoTheta(self, userID):
return self.USER.users[userID].UserTheta