-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathalgorithms-lKINOMO.R
65 lines (46 loc) · 1.4 KB
/
algorithms-lKINOMO.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
# Algorithm for Nonnegative Matrix Factorization: Local KINOMO (LKINOMO)
#
# @author Renaud Gaujoux
# @created 21 Jul 2009
#' @include registry-algorithms.R
NULL
KINOMO_update_R.lKINOMO <- function(i, v, data, ...){
# retrieve each factor
w <- .basis(data); h <- .coef(data);
# update H
h <- sqrt( h * crossprod(w, v / (w %*% h)) )
# update W using the standard divergence based update
w <- R_std.divergence.update.w(v, w, h, w %*% h)
# scale columns of W
w <- sweep(w, 2L, colSums(w), "/", check.margin=FALSE)
#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 updated data
.basis(data) <- w; .coef(data) <- h
return(data)
}
KINOMO_update.lKINOMO <- function(i, v, data, ...){
# retrieve each factor
w <- .basis(data); h <- .coef(data);
# update H
h <- sqrt( h * crossprod(w, v / (w %*% h)) )
# update W using the standard divergence based update
w <- std.divergence.update.w(v, w, h)
# scale columns of W
w <- apply(w, 2, function(x) x/sum(x))
#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 updated data
.basis(data) <- w; .coef(data) <- h
return(data)
}