-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathalgorithms-base.R
322 lines (235 loc) · 8.56 KB
/
algorithms-base.R
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#' @include KINOMOstd-class.R
#' @include KINOMOOffset-class.R
#' @include KINOMOns-class.R
#' @include registry-algorithms.R
NULL
KINOMO_update.brunet_R <- function(i, v, x, eps=.Machine$double.eps, ...)
{
# retrieve each factor
w <- .basis(x); h <- .coef(x);
# standard divergence-reducing KINOMO update for H
h <- R_std.divergence.update.h(v, w, h)
# standard divergence-reducing KINOMO update for W
w <- R_std.divergence.update.w(v, w, h)
#every 10 iterations: adjust small values to avoid underflow
if( i %% 10 == 0 ){
#precision threshold for numerical stability
#eps <- .Machine$double.eps
h[h<eps] <- eps;
w[w<eps] <- eps;
}
#return the modified model
.basis(x) <- w; .coef(x) <- h;
return(x)
}
KINOMO_update.brunet <- function(i, v, x, copy=FALSE, eps=.Machine$double.eps, ...)
{
# retrieve each factor
w <- .basis(x); h <- .coef(x);
# fixed terms
nb <- nbterms(x); nc <- ncterms(x)
# standard divergence-reducing KINOMO update for H
h <- std.divergence.update.h(v, w, h, nbterms=nb, ncterms=nc, copy=copy)
# standard divergence-reducing KINOMO update for W
w <- std.divergence.update.w(v, w, h, nbterms=nb, ncterms=nc, copy=copy)
#every 10 iterations: adjust small values to avoid underflow
# NB: one adjusts in place even when copy=TRUE, as 'h' and 'w' are local variables
if( i %% 10 == 0 ){
#eps <- .Machine$double.eps
h <- pmax.inplace(h, eps, icterms(x))
w <- pmax.inplace(w, eps, ibterms(x))
}
# update object if the updates duplicated the model
if( copy ){
#return the modified model
.basis(x) <- w;
.coef(x) <- h;
}
return(x)
}
KINOMOAlgorithm.brunet_R <- setKINOMOMethod('.R#brunet'
, objective='KL'
, Update=KINOMO_update.brunet_R
, Stop='connectivity')
KINOMOAlgorithm.brunet <- setKINOMOMethod('brunet', '.R#brunet', Update=KINOMO_update.brunet)
KINOMOAlgorithm.KL <- setKINOMOMethod('KL'
, objective='KL'
, Update=KINOMO_update.brunet
, Stop='stationary')
KINOMO_update.lee_R <- function(i, v, x, rescale=TRUE, eps=10^-9, ...)
{
# retrieve each factor
w <- .basis(x); h <- .coef(x);
#precision threshold for numerical stability
#eps <- 10^-9
# compute the estimate WH
#wh <- estimate(x)
# euclidean-reducing KINOMO iterations
# H_au = H_au (W^T V)_au / (W^T W H)_au
#h <- pmax(h * (t(w) %*% v),eps) / ((t(w) %*% w) %*% h + eps);
h <- R_std.euclidean.update.h(v, w, h, eps=eps)
# update H and recompute the estimate WH
#metaprofiles(x) <- h
#wh <- estimate(x)
# W_ia = W_ia (V H^T)_ia / (W H H^T)_ia and columns are rescaled after each iteration
#w <- pmax(w * (v %*% t(h)), eps) / (w %*% (h %*% t(h)) + eps);
w <- R_std.euclidean.update.w(v, w, h, eps=eps)
#rescale columns TODO: effect of rescaling? the rescaling makes the update with offset fail
if( rescale ) w <- sweep(w, 2L, colSums(w), "/", check.margin=FALSE)
#return the modified model
.basis(x) <- w; .coef(x) <- h;
return(x)
}
KINOMO_update.lee <- function(i, v, x, rescale=TRUE, copy=FALSE, eps=10^-9, weight=NULL, ...)
{
# retrieve each factor
w <- .basis(x); h <- .coef(x);
# fixed terms
nb <- nbterms(x); nc <- ncterms(x)
#precision threshold for numerical stability
#eps <- 10^-9
# compute the estimate WH
#wh <- estimate(x)
# euclidean-reducing KINOMO iterations
# H_au = H_au (W^T V)_au / (W^T W H)_au
h <- std.euclidean.update.h(v, w, h, eps=eps, nbterms=nb, ncterms=nc, copy=copy)
# update original object if not modified in place
if( copy ) .coef(x) <- h
# W_ia = W_ia (V H^T)_ia / (W H H^T)_ia and columns are rescaled after each iteration
w <- std.euclidean.update.w(v, w, h, eps=eps, weight=weight, nbterms=nb, ncterms=nc, copy=copy)
#rescale columns TODO: effect of rescaling? the rescaling makes the update with offset fail
if( rescale ){
w <- sweep(w, 2L, colSums(w), "/", check.margin=FALSE)
}
#return the modified model
.basis(x) <- w;
return(x)
}
KINOMOAlgorithm.lee_R <- setKINOMOMethod('.R#lee', objective='euclidean'
, Update=KINOMO_update.lee_R
, Stop='connectivity')
KINOMOAlgorithm.lee <- setKINOMOMethod('lee', '.R#lee', Update=KINOMO_update.lee)
#
KINOMOAlgorithm.Frobenius <- setKINOMOMethod('Frobenius', objective='euclidean'
, Update=KINOMO_update.lee
, Stop='stationary')
KINOMO_update.euclidean_offset.h <- function(v, w, h, offset, eps=10^-9, copy=TRUE){
.Call("offset_euclidean_update_H", v, w, h, offset, eps, copy, PACKAGE='KINOMO')
}
#' @export
#' @rdname offset-KINOMO
KINOMO_update.euclidean_offset.w <- function(v, w, h, offset, eps=10^-9, copy=TRUE){
.Call("offset_euclidean_update_W", v, w, h, offset, eps, copy, PACKAGE='KINOMO')
}
#' \code{KINOMO_update.offset_R} implements a complete single update step,
#' using plain R updates.
#' @export
#' @rdname offset-KINOMO
KINOMO_update.offset_R <- function(i, v, x, eps=10^-9, ...)
{
# retrieve each factor
w <- .basis(x); h <- .coef(x);
# retrieve offset and fill it if necessary (with mean of rows)
off <- offset(x)
if( i == 1 && length(off) == 0 )
off <- rowMeans(v)
#precision threshold for numerical stability
#eps <- 10^-9
# compute standard lee update (it will take the offset into account) without rescaling W's columns
h <- R_std.euclidean.update.h(v, w, h, wh=w%*%h + off, eps=eps)
w <- R_std.euclidean.update.w(v, w, h, wh=w%*%h + off, eps=eps)
#x <- KINOMO_update.lee(i, v, x, rescale=FALSE, ...)
# update the offset
# V0_i = V0_i ( sum_j V_ij ) / ( sum_j (V.off + W H)_ij )
x@offset <- off * pmax(rowSums(v), eps) / (rowSums(w%*%h + off) + eps)
#return the modified model
.basis(x) <- w; .coef(x) <- h;
return(x)
}
#' \code{KINOMO_update.offset} implements a complete single update step,
#' using C++-optimised updates.
#' @export
#' @rdname offset-KINOMO
KINOMO_update.offset <- function(i, v, x, copy=FALSE, eps=10^-9, ...)
{
# retrieve each factor
w <- .basis(x); h <- .coef(x);
# retrieve offset and fill it if necessary (with mean of rows)
off <- offset(x)
if( i == 1 && length(off) == 0 )
off <- rowMeans(v)
#precision threshold for numerical stability
#eps <- 10^-9
# compute standard offset updates
h <- KINOMO_update.euclidean_offset.h(v, w, h, off, eps=eps, copy=copy)
w <- KINOMO_update.euclidean_offset.w(v, w, h, off, eps=eps, copy=copy)
# update the offset
# V0_i = V0_i ( sum_j V_ij ) / ( sum_j (V.off + W H)_ij )
x@offset <- off * pmax(rowSums(v), eps) / (rowSums(w%*%h + off) + eps)
# update the original object if not modified in place
if( copy ){
.basis(x) <- w;
.coef(x) <- h;
}
return(x)
}
KINOMOAlgorithm.offset_R <- setKINOMOMethod('.R#offset', objective='euclidean'
, model = 'KINOMOOffset'
, Update=KINOMO_update.offset_R
, Stop='connectivity')
# KINOMO with offset (optimised version)
#' @rdname offset-KINOMO
KINOMOAlgorithm.offset <- setKINOMOMethod('offset', '.R#offset', Update=KINOMO_update.offset)
#' @rdname nsKINOMO-KINOMO
KINOMO_update.ns <- function(i, v, x, copy=FALSE, ...)
{
# retrieve and alter the factors for updating H
S <- smoothing(x)
w <- .basis(x)
h <- .coef(x);
# standard divergence-reducing update for H with modified W
h <- std.divergence.update.h(v, w %*% S, h, copy=copy)
# update H if not modified in place
if( copy ) .coef(x) <- h
# standard divergence-reducing update for W with modified H
w <- std.divergence.update.w(v, w, S %*% h, copy=copy)
# rescale columns of W
w <- sweep(w, 2L, colSums(w), '/', check.margin=FALSE)
#return the modified model
.basis(x) <- w;
return(x)
}
#' \code{KINOMO_update.ns_R} implements the same updates in \emph{plain R}.
#'
#' @export
#' @rdname nsKINOMO-KINOMO
KINOMO_update.ns_R <- function(i, v, x, ...)
{
# retrieve and alter the factors for updating H
S <- smoothing(x)
w <- .basis(x)
#w <- metagenes(x) %*% smoothing(fit(x)); # W <- WS
h <- .coef(x);
# compute the estimate WH
#wh <- estimate(x, W=w.init, H=h, S=S)
# standard divergence-reducing update for H with modified W
h <- R_std.divergence.update.h(v, w %*% S, h)
# update H and recompute the estimate WH
.coef(x) <- h
# retrieve and alter the factors for updating W
#w <- tmp;
#h <- smoothing(fit(x)) %*% metaprofiles(x); # H <- SH
#h <- S %*% h; # H <- SH
# standard divergence-reducing update for W with modified H
w <- R_std.divergence.update.w(v, w, S %*% h)
# rescale columns of W
w <- sweep(w, 2L, colSums(w), '/', check.margin=FALSE)
#return the modified model
.basis(x) <- w; #metaprofiles(x) <- h;
return(x)
}
KINOMOAlgorithm.nsKINOMO_R <- setKINOMOMethod('.R#nsKINOMO', objective='KL'
, model='KINOMOns'
, Update=KINOMO_update.ns_R
, Stop='connectivity')
KINOMOAlgorithm.nsKINOMO <- setKINOMOMethod('nsKINOMO', '.R#nsKINOMO', Update=KINOMO_update.ns)