-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdkl.r
56 lines (46 loc) · 1.33 KB
/
dkl.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
args = commandArgs(trailingOnly=TRUE)
library(stringr)
library(dplyr)
divergence <- function(path) {
print(paste("The data to analyze is", path))
x = read.delim(path, header = FALSE)
m = length(x[[1]])
print(paste("Number of lines is", m))
my_tab <- table(x)
print(paste("max occurence is", max(as.vector(my_tab))))
my_tab_prob <- my_tab / sum(my_tab) # Create proportion table
P <- as.vector(my_tab_prob)
times = length(P)
print(paste("Number of distinct element is occurence is", times))
v = 1/times
Q <- rep(v, times)
sum <- 0
for (i in 1:times){
sum = sum + P[i] * log(P[i]/Q[i])
}
print(paste("The KL divergence is ", sum))
return(sum)
}
dKL <- function(args){
expe= args[1]
path= args[2]
parameters = args[3]
d2 <- divergence(path)
filename = paste("result/", parameters, sep="")
filename
file.info(filename)$size
if (!file.exists(filename)) {
head <- "Expe dKL"
write(head, append=TRUE, file = filename)
}
separator = " "
sol = paste(expe, d2, sep = separator)
print(sol)
write(sol, append=TRUE, file = filename)
}
#d1 <- divergence("data/resultJul95")
"---------------------"
"---------------------"
#d2 <- divergence("data/output")
#G = 1 - d2/d1
#print(paste("The Gain is ", G))