-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreduce_subset.r
More file actions
241 lines (212 loc) · 7.59 KB
/
Copy pathreduce_subset.r
File metadata and controls
241 lines (212 loc) · 7.59 KB
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
##
# reduce_subset.r
#
# ,--.,--.
# ,-| |`--' ,---. ,---. ,--,--. ,---. ,---.
# ' .-. |,--.( .-' | .-. |' ,-. || .--'| .-. :
# \ `-' || |.-' `)| '-' '\ '-' |\ `--.\ --.
# `---' `--'`----' | |-' `--`--' `---' `----'
# `--'
# "To wander, to roam, move about."
#
# Copyright 2014-2017 Harm Brouwer <me@hbrouwer.eu>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##
## REQUIRES:
##
## model: path to a DSS model.
##
## DEFINES:
##
## reduce(dims, epochs, write): reduces the dimensionality of a DSS using
## subset sampling.
##
## dims: desired number of dimensions;
## epochs: number of subsetting epochs;
## write: boolean flagging whether reduced vectors should;
## be saved (TRUE) or not (FALSE).
###########################################################################
###########################################################################
if (!exists("model"))
stop("'model' not set")
if (!exists("model_fb")) {
model_nm <- tail(strsplit(model, "/")[[1]], 1)
model_fb <- paste(model, model_nm, sep = "/")
}
###########################################################################
###########################################################################
file.obs <- paste(model_fb, ".observations", sep = "")
file.vec <- paste(model_fb, ".vectors", sep = "")
# read observations
cat("Reading observations ...\n", file = stderr())
df.mtx <- as.matrix(read.csv(file.obs, sep = " ", head = TRUE, check.names = FALSE))
###########################################################################
#### S U B S E T S A M P L I N G ####
###########################################################################
##
# Reduce the dimensionality of an m x n observation matrix X using subset
# sampling. The following procedure is repeated for "epochs" epochs to
# arrive at an k x n matrix X' (where k < m) that maximally reflects the
# knowledge encoded in the original matrix X:
#
# (1) Take a subset of k rows of matrix X, and call it X';
#
# (2) Check if all columns of matrix X' are informative, and if the reduced
# matrix encodes the same hard constraints (if enforced) as the
# unreduced matrix, otherwise skip to the next epoch;
#
# (3) Compute the similarity between X and X' on the basis of the
# comprehension scores in X and X';
#
# (4) If X' is the best approximation of X so far, store it;
#
# (5) Run next epoch, and rerun from step (1);
#
# (6) If we have reached "epochs", return the best X' found.
##
reduce <- function(
dims,
epochs,
enforce_pos_inferences = TRUE,
enforce_neg_inferences = TRUE,
write = FALSE)
{
cv <- comprh_vector(df.mtx)
reduced.mtx <- matrix()
reduced.sim <- 0
epoch <- 1
while (epoch <= epochs) {
cat(paste("Epoch:", epoch), file = stderr())
# (1): Take a subset of k rows of matrix X, and call it X';
trial.mtx <- df.mtx[sample(1 : nrow(df.mtx), dims, replace = FALSE),]
# (2): Check if all columns of matrix X' are informative, and
# if the reduced matrix encodes the same hard constraints
# (if enforced) as the unreduced matrix, otherwise skip to
# the next epoch;
if (!all_informative_vectors(trial.mtx)) {
cat("\tBad sample ... Skipping epoch!\n", file = stderr())
next
}
trial.cv <- comprh_vector(trial.mtx)
if ( (enforce_pos_inferences && !equal_positive_inferences(cv, trial.cv))
| (enforce_neg_inferences && !equal_negative_inferences(cv, trial.cv)) ) {
cat("\tBad sample ... Skipping epoch!\n", file = stderr())
next
}
# (3): Compute the similarity between X and X' on the basis of
# the comprehension scores in X and X';
trial.sim <- cor(cv, trial.cv)
cat(paste("\tSim:", trial.sim), file = stderr())
# (4): If X' is the best approximation of X so far, store it;
if (epoch == 1 | trial.sim > reduced.sim) {
reduced.mtx <- trial.mtx
reduced.sim <- trial.sim
cat(" ***", file = stderr())
}
cat("\n", file = stderr())
# (5): Run next epoch, and rerun from step (1);
epoch <- epoch + 1
}
cat(paste("Sim: ", reduced.sim, "\n"), file = stderr())
# write matrix (if required)
if (write) {
colnames(reduced.mtx) <- colnames(df.mtx)
write.table(reduced.mtx, file.vec, quote = FALSE, sep = " ",
col.names = TRUE, row.names = FALSE)
cat(paste("Wrote:", file.vec, "\n"), file = stderr())
}
reduced.mtx
}
###########################################################################
###########################################################################
##
# Returns TRUE if all columns are non-zero vectors.
##
all_informative_vectors <- function(mtx)
{
all(colSums(mtx) > 0)
}
##
# Returns TRUE if comprehension vectors cv1 and cv2 have extreme positive
# comprehension values (+1) in the exact same positions.
##
equal_positive_inferences <- function(cv1, cv2)
{
all(replace(cv1, cv1 < 1, 0) == replace(cv2, cv2 < 1, 0))
}
##
# Returns TRUE if comprehension vectors cv1 and cv2 have extreme negative
# comprehension values (+1) in the exact same positions.
##
equal_negative_inferences <- function(cv1, cv2)
{
all(replace(cv1, cv1 > -1, 0) == replace(cv2, cv2 > -1, 0))
}
##
# Construct a vector of comprehension scores cs(a,b) for each combination of
# events a and b.
##
comprh_vector <- function(mtx)
{
cv <- c()
for (i in 1 : ncol(mtx)) {
for (j in 1 : ncol(mtx)) {
cv <- c(cv, comprh_score(mtx[,i], mtx[,j]))
}
}
cv
}
##
# Computes the prior probability of a situation vector v.
##
prior_prob <- function(v)
{
sum(v) / length(v)
}
##
# Computes the conjuction probability of v1 and v2. In case v1 and v2 are
# identical, the conjunction probability is their prior probability.
##
conj_prob <- function(v1, v2)
{
pr.conj <- 0
if (all(v1 == v2)) {
pr.conj <- prior_prob(v1)
} else {
pr.conj <- sum(v1 * v2) / length(v1)
}
pr.conj
}
##
# Computes the conditional probablity of v1 given v2.
##
cond_prob <- function(v1, v2)
{
conj_prob(v1, v2) / prior_prob(v2)
}
##
# Computes the comprehension scores cs(v1,v2).
##
comprh_score <- function(v1, v2)
{
pr.cond <- cond_prob(v1, v2)
pr.prior <- prior_prob(v1)
cs <- 0
if (pr.cond > pr.prior) {
cs <- (pr.cond - pr.prior) / (1.0 - pr.prior)
} else {
cs <- (pr.cond - pr.prior) / pr.prior
}
cs
}