-
Notifications
You must be signed in to change notification settings - Fork 0
/
PerformanceTests.Rmd
212 lines (157 loc) · 4.66 KB
/
PerformanceTests.Rmd
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
---
jupyter:
jupytext:
formats: ipynb,Rmd
text_representation:
extension: .Rmd
format_name: rmarkdown
format_version: '1.2'
jupytext_version: 1.4.1
kernelspec:
display_name: R
language: R
name: ir
---
# Performance tests
Here we try to understand the power of the K2 algorithm, tesiting its scaling with the data size and the network size. We will also look at the difference between K2 and other structural algorithms.
This notebook requires the packages "bnstruct" and "bnlearn" <br>
`install.packages('bnstruct')`
`install.packages('bnlearn')`
```{r}
library('bnstruct')
library('bnlearn')
```
```{r}
f <- function(index, parents, database) {
#Compute N_{ijk}
p <- length(parents)
todec <- 2 ** (0:max(0,p-1)) # (2, 4, 8)
#(1, 2, 4, 8)
qi <- 2**p #can be adapted to generic r
N <- matrix(data = 0, nrow = qi, ncol = 2)
for (i in 1:nrow(database)) {
index.k <- database[i, index] + 1 #(0, 1) -> (1, 2)
if (p > 0)
index.j <- sum(database[i, parents] * todec) + 1 # 10 -> 2
else
index.j <- 1
N[index.j, index.k] <- N[index.j, index.k] + 1
}
#Compute N_{ij}
Nj <- rowSums(N)
# print(N)
# print(Nj)
#Compute the log(factorials)
logN <- matrix(data = factorials[N+1], ncol=2) #+1 because indices start from 1
# print(logN)
factor.first <- factorials[2] #2-1+1
factor.second <- factorials[Nj+2] #+r_i -1 +1 (because of the indices) = +r_i = +2
factor.third <- rowSums(logN)
result <- exp(sum(factor.first - factor.second + factor.third)) #factor.first could be brought outside to gain precision (and multiply by qi)
return(result)
}
#0 is first, 1 is second
```
```{r}
K2 <- function(database, u, ordering) {
# database : dataframe m x n, containing no missing values. Entries must be binary. (could be extended to integers 1 to r_i)
# u : maximum allowed number of parents for each node
# ordering : a vector containing a permutation of the first n integers
n <- ncol(database)
structure <- matrix(data = 0, nrow = n, ncol = n)
#structure[i,j] = 1 if j is a parent of i, 0 otherwise (it is not symmetric)
for (i in 1:n) {
pi.i <- c()
p.old <- f(i, pi.i, database)
OKToProceed <- TRUE
#Compute possible parents of i
i.pos <- which(ordering == i) #Position of i in the ordering
if (i.pos == 1) {
next
} else {
precedents <- ordering[1:i.pos-1]
}
while (OKToProceed & length(pi.i) < u) {
p.new <- 0
p.new.index <- NA
#cat(i,' - ', precedents, '\n')
for (j in precedents) { # 1-3
# can it be vectorized?
fz <- f(i, c(pi.i, j), database)
if (fz > p.new) {
p.new <- fz
p.new.index <- j
}
}
# cat(i, ' ', p.old, ' - ', p.new, '\n')
if (p.new > p.old) {
p.old <- p.new
pi.i <- c(pi.i, p.new.index)
#cat('Taking ' , p.new.index, ' as parent of ', i, '\n')
structure[i, p.new.index] = 1
precedents <- precedents[-p.new.index]
} else {
OKToProceed <- FALSE
}
}
}
return(structure)
}
```
```{r}
datab <- learning.test
n <- ncol(datab)
m <- nrow(datab)
df <- matrix(data=0, nrow = m, ncol = n)
one <- c('b')#, 'wide', 'high')
two <- c('c')
for( i in 1:m){
for(j in 1:n){
if(datab[i, j] %in% one) df[i,j] <- 1
if(datab[i, j] %in% two) df[i,j] <- 2
}
}
df <- data.frame(df)
```
```{r}
df
```
```{r}
#Precompute all needed factorials (1 to m + r -1)
n <- ncol(df)
m <- nrow(df)
r <- 2 #number of possible values
log.fact <- function(m, r){
# Return the logarithm of the first m+r-1 factorials
# m: int, number of cases in the dataset
# r: int, number of possible values a variable can assume
fact <- log( c(1, 1:(m+r-1)) )
for (i in 4:length(fact)) { #first 3 are already correct
fact[i] <- fact[i] + fact[i-1]
}
return(fact)
}
factorials <- log.fact(m, 2)
```
```{r}
#Print the structure
structure <- K2(df[-6], 3, c(1,3,2 ))
print(structure)
for (i in 1:nrow(structure)) {
cat("Node ", i, " : [")
for (j in 1:ncol(structure)) {
if (structure[i,j]) {
cat(j)
}
}
cat("]\n")
}
```
```{r}
data.alarm <- factor(alarm)
```
```{r}
?factor
```
```{r}
```