-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathupdateUV.R
164 lines (146 loc) · 3.6 KB
/
updateUV.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
updateUV <- function(cc = 5,
inMat,
thisAlpha = 0.8,
thisBeta = 0.1,
Sd,
thisGamma = NULL,
St,
lamU,
lamV,
numLat = 50,
initMethod = "useNorm",
thisSeed = 123,
maxIter = 100) {
# INPUT
# cc:
# inMat:
# thisAlpha:
# thisBeta:
# Sd:
# thisGamma:
# St:
# lamU:
# lamV:
# numLat:
# initMethod:
# thisSeed:
# maxIter:
# OUTPUT:
# a list with two elements: U and V
if ((thisAlpha > 1) | (thisAlpha < 0)) {
stop("thisAlpha should be [0, 1]! \n")
}
if ((thisBeta > 1) | (thisBeta < 0)) {
stop("thisBeta should be [0, 1]! \n")
}
if (is.null(thisGamma)) {
thisGamma <- 1 - thisAlpha - thisBeta
}
numRow <- nrow(inMat)
numCol <- ncol(inMat)
if (initMethod == "useNorm") {
U <- matrix(NA, nrow = numRow, ncol = numLat)
U <- apply(U, 2, function(x) {
rnorm(x, mean = 0, sd = 1)
})
U <- 1 / numLat * U
V <- matrix(NA, nrow = numCol, ncol = numLat)
V <- apply(V, 2, function(x) {
rnorm(x, mean = 0, sd = 1)
})
V <- 1 / numLat * V
} else if (initMethod == "useSeed") {
set.seed(thisSeed)
U <- matrix(NA, nrow = numRow, ncol = numLat)
U <- apply(U, 2, function(x) {
rnorm(x, mean = 0, sd = 1)
})
U <- 1 / numLat * U
V <- matrix(NA, nrow = numCol, ncol = numLat)
V <- apply(V, 2, function(x) {
rnorm(x, mean = 0, sd = 1)
})
V <- 1 / numLat * V
} else {
stop("initMethod should be one of {useNorm, useSeed}\n")
}
sumGradU <- matrix(0, nrow = numRow, ncol = numLat)
sumGradV <- matrix(0, nrow = numCol, ncol = numLat)
# last log-likelihood
lastLog <- calcLogLik(
cc = cc,
inMat = inMat,
thisAlpha = thisAlpha,
U = U,
V = V,
thisBeta = thisBeta,
Sd = Sd,
thisGamma = thisGamma,
St = St,
lamU = lamU,
lamV = lamV)
currDeltaLL <- 1000
# main loop
for (i in 1:maxIter) {
# gradU
gradU <- calcDeriv(
cc = cc,
inMat = inMat,
thisAlpha = thisAlpha,
U = U,
V = V,
thisBeta = thisBeta,
Sd = Sd,
thisGamma = thisGamma,
St = St,
lamU = lamU,
lamV = lamV,
isGradU = TRUE)
sumGradU <- sumGradU + (gradU ^ 2)
stepSize <- 1 / sqrt(sumGradU)
U <- U + stepSize * gradU
# gradV
gradV <- calcDeriv(
cc = cc,
inMat = inMat,
thisAlpha = thisAlpha,
U = U,
V = V,
thisBeta = thisBeta,
Sd = Sd,
thisGamma = thisGamma,
St = St,
lamU = lamU,
lamV = lamV,
isGradU = FALSE
)
sumGradV <- sumGradV + (gradV ^ 2)
stepSize <- 1 / sqrt(sumGradV)
V <- V + stepSize * gradV
currLog <- calcLogLik(
cc = cc,
inMat = inMat,
thisAlpha = thisAlpha,
U = U,
V = V,
thisBeta = thisBeta,
Sd = Sd,
thisGamma = thisGamma,
St = St,
lamU = lamU,
lamV = lamV)
# delta log-likelihood
deltaLog <- (currLog - lastLog) / abs(lastLog)
# stop earlier
if (abs(deltaLog) < 1e-5) {
break
}
if ((i > 50) & (deltaLog > currDeltaLL)) {
break
}
currDeltaLL <- deltaLog
lastLog <- currLog
}
UV <- list(U = U, V = V)
return(UV)
}