-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid_search_cv.R
171 lines (144 loc) · 5.01 KB
/
grid_search_cv.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
# source pcp function
source("pcpr.R")
# Code for grid search, with necessary nested functions
# Rough way to determine the sparsity of a given matrix.
sparsity <- function(mat, tol = .00001) {
mat[abs(mat) < tol] <- 0
100 - (100 * sum((mat != 0) * 1) / prod(dim(mat)))
}
# Corrupts a data matrix
impute <- function(v, limit, fill="NA") {
q = quantile(v, probs = limit)
if (fill == "sqrt2") {
rep_val = q/sqrt(2)
} else if (fill == "-1") {
rep_val = -1
} else if (fill == "mean") {
rep_val = mean(v[v <= q])
} else {
rep_val = NA
}
# annoying edge cases with quantile
if (limit == 0) {
x = ifelse(v < q, rep_val, v)
} else {
x = ifelse(v <= q, rep_val, v)
}
if (fill == "status") {
if (limit == 0) {
x = ifelse(v < q, "< LOD", "above")
} else {
x = ifelse(v <= q, "< LOD", "above")
}
}
x
}
corrupt_mat <- function(mat, cols, limit, fill="NA") {
mat[, cols] <- apply(mat[, cols, drop=FALSE], FUN=impute, limit=limit, fill=fill, MARGIN=2)
mat
}
# Corrupts a data matrix by knocking out a random specified percentage of entries.
corrupt_mat_randomly <- function(mat, k, perc_b) {
nvals_to_corrupt <- floor(perc_b*prod(dim(mat)))
mat.vec <- as.vector(mat)
mask <- rep(0, length(mat.vec))
pool <- which(mat.vec >= 0)
if (nvals_to_corrupt > length(pool)) {
corrupted <- pool
} else {
set.seed(k)
corrupted <- sample(pool, nvals_to_corrupt, replace = FALSE)
}
mask[corrupted] <- 1
mat.vec[corrupted] <- NA
rows <- nrow(mat)
cols <- ncol(mat)
ret.mat <- matrix(mat.vec, nrow = rows, ncol = cols)
ret.mask <- matrix(mask, nrow = rows, ncol = cols)
list(cor.mat = ret.mat, cor.mask = ret.mask)
}
# Evaluates a given setting of lambda and mu on a given matrix with a given PCP function.
eval_params <- function(
seed,
mat,
pcp_func,
perc_b,
eval_params_index,
...
) {
pcp_args <- list(...)
cor_mat <- corrupt_mat_randomly(mat, k = seed, perc_b = perc_b) # a list containing cor.mat and cor.mask
mask <- cor_mat$cor.mask
pcp_out <- do.call(pcp_func, c(pcp_args, list(D = cor_mat$cor.mat, verbose = F)))
mat[is.na(mat)] <- 0
score <- norm((mat - pcp_out$L - pcp_out$S)*mask, "F") / norm(mat * mask, "F")
L.rank <- Matrix::rankMatrix(pcp_out$L, tol = 1e-04)
S.sparsity <- sparsity(pcp_out$S, tol = 1e-04)
its <- pcp_out$final_iter
c(as.numeric(pcp_args[eval_params_index]), seed, score, L.rank, S.sparsity, its)
}
# Conducts a cross-validated grid search of the parameters for Principle Component Pursuit (PCP).
grid_search_cv <- function(
mat,
pcp_func,
grid_df,
cores = NULL,
perc_b = 0.2,
runs = 100,
progress_bar = TRUE,
file = NULL,
...)
{
# setting up the parallel programming
if (is.null(cores)) {
cores <- parallel::detectCores()
}
cl <- snow::makeCluster(cores)
doSNOW::registerDoSNOW(cl)
# initialization
metrics <- c("value", "L.rank", "S.sparsity", "iterations")
for (metric in metrics) {
if (!metric %in% names(grid_df)) {
grid_df[, metric] <- as.numeric(NA)
}
}
param_names <- grid_df %>% dplyr::select(!tidyselect::all_of(metrics)) %>% colnames()
points_to_eval <- which(is.na(grid_df$value))
params <- data.frame(grid_df[points_to_eval, param_names], rep(1:runs, each = length(points_to_eval)), row.names = NULL)
colnames(params) <- c(param_names, "seed")
constant_params <- list(...)
# progress bar setup
if (progress_bar) {
pb <- txtProgressBar(min=0, max=nrow(params), width=50, style=3)
progress <- function(p) setTxtProgressBar(pb, p)
opts <- list(progress=progress)
} else {
opts <- list()
}
# grid search
cv <- foreach(i = iterators::icount(nrow(params)), .options.snow=opts, .combine = cbind,
.inorder = FALSE,
.export=c("eval_params", "corrupt_mat_randomly", "prox_nuclear", "prox_l1", "proj_rank_r",
"prox_fro", "sparsity")) %dopar% {
do.call(what = 'eval_params', c(as.list(params[i,]), constant_params, list(mat = mat, pcp_func = pcp_func, perc_b = perc_b, eval_params_index = param_names)))
}
# close the progress bar and stop cluster
if (progress_bar) close(pb)
snow::stopCluster(cl)
# format the output
rownames(cv) <- c(colnames(params), metrics)
cv <- as.data.frame(t(cv))
cv.formatted <- cv %>%
dplyr::group_by_at(all_of(param_names)) %>%
dplyr::summarise(value = mean(value), L.rank = mean(L.rank), S.sparsity = mean(S.sparsity), iterations = mean(iterations)) %>%
tidyr::unite(param_setting, all_of(param_names))
grid_df.formatted <- grid_df %>%
tidyr::unite(param_setting, all_of(param_names))
grid_df.formatted[match(cv.formatted$param_setting, grid_df.formatted$param_setting), ] <- cv.formatted
grid_df <- grid_df.formatted %>%
tidyr::separate(param_setting, into = param_names, sep = "_", convert = TRUE)
# save
if (!is.null(file)) save(cv, grid_df, file = file)
# return
list(raw = cv, formatted = grid_df, constants = constant_params)
}