-
Notifications
You must be signed in to change notification settings - Fork 0
/
dahl.R
195 lines (139 loc) · 5.19 KB
/
dahl.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#Dahl Split Merge 2003?
fn.dahl <- function(All.Stuff, data, parm, max.row.nbhd.size, max.col.nbhd.size, row.frac.probes, col.frac.probes, prob.compute.col.nbhd, true_parm, computeMode)
{
parm <- All.Stuff$parm
All.Stuff$dahl$min <- Inf
for (cc in 1:n.reps)
{
### Dahl calculations
tmp.mat <- array(0,c(p,p))
for (jj in 0:All.Stuff$G.v[cc])
{indx.jj <- which(All.Stuff$c.matrix[cc,]==jj)
tmp.mat[indx.jj,indx.jj] <- 1
}
dahl.dist <- sum((All.Stuff$pi.mt - tmp.mat)^2)
if (dahl.dist < All.Stuff$dahl$min)
{All.Stuff$dahl$min <- dahl.dist
All.Stuff$dahl$G.clust <- c.matrix[cc,]
All.Stuff$dahl$G <- All.Stuff$G.v[cc]
}
} # end for loop in cc
##########################################
## THEN get an estimated elementwise (K-cluster)
## run fewer iterations because takes longer to do Dahl calculations
##########################################
All.Stuff.1 <- All.Stuff
###########
All.Stuff <- All.Stuff.1
All.Stuff.2 <- NULL
###########
new.n.reps <- n.reps/2
new.n.burn <- n.burn/2
new.text <- paste("ROUND_2_",text,sep="")
#
All.Stuff.2$tau_0.v <- All.Stuff.2$tau.v <- All.Stuff.2$tau_int.v <- All.Stuff.2$G.v <- All.Stuff.2$K.v <- array(,new.n.reps)
All.Stuff.2$row.flip.v <- array(0,new.n.reps)
All.Stuff.2$merge.flip.v <- All.Stuff.2$split.changed.v <- array(0,new.n.reps)
parm <- fn.init(true, data, max.row.nbhd.size, row.frac.probes, col.frac.probes, true_parm, computeMode)
init.parm <- parm
parm <- fn.init(true, data, zero.hemming, entropy.prop, max.row.nbhd.size, max.col.nbhd.size, row.frac.probes, col.frac.probes, col.DP.flag=FALSE, dahl=All.Stuff$dahl, d=mean(All.Stuff$d.v), max.d=max.d)
init.parm <- parm
err <- fn.quality.check(parm)
if (err > 0)
{stop(paste("failed QC at fn.init: err=",err))
}
flip.count <- 0
All.Stuff.2$dahl <- NULL
#### K.matrix <- array(0,c(init.parm$N,init.parm$N))
r.matrix <- array(0,c(new.n.reps,init.parm$N))
if (new.n.burn > 0)
{
for (cc in 1:new.n.burn)
{parm <- fn.iter(data, parm, zero.hemming, entropy.prop, max.row.nbhd.size, max.col.nbhd.size, row.frac.probes, col.frac.probes, col.DP.flag=FALSE, split.merge.flag, max.d=max.d)
if (cc %% 10 == 0)
{print(paste(new.text, "BURN = ",cc,date(),"***********"))
}
}
}
for (cc in 1:new.n.reps)
{parm <- fn.iter(data, parm, zero.hemming, entropy.prop, max.row.nbhd.size, max.col.nbhd.size, row.frac.probes, col.frac.probes, col.DP.flag=FALSE, split.merge.flag, max.d)
All.Stuff.2$G.v[cc] <- parm$clust$G
All.Stuff.2$K.v[cc] <- parm$clust$K
All.Stuff.2$tau.v[cc] <- parm$tau
All.Stuff.2$tau_0.v[cc] <- parm$tau_0
All.Stuff.2$tau_int.v[cc] <- parm$tau_int
# summarizing elementwise DP in "fn.groupwise.updates"
All.Stuff.2$row.flip.v[cc] <- parm$clust$row.flip
r.matrix[cc,] <- parm$clust$s.v
### exact Dahl calculations: too expensive
if (FALSE)
{
tmp.mat <- array(0,c(parm$N,parm$N))
for (jj in 0:parm$clust$K)
{indx.jj <- which(parm$clust$s.v==jj)
tmp.mat[indx.jj,indx.jj] <- 1
}
K.matrix <- K.matrix + tmp.mat
}
if (cc %% 10 == 0)
{print(paste(new.text, "REPS = ",cc,date(),"***********"))
}
} # end for loop in cc
All.Stuff.2$parm <- parm
All.Stuff.2$init.parm <- init.parm
# normalize Dahl matrix
#### K.matrix <- K.matrix/new.n.reps
All.Stuff.2$dahl$min <- Inf
# sample only up to 20 replications for Dahl calculations
for (cc in sort(sample(new.n.reps,size=min(new.n.reps,20))))
{
### exact Dahl calculations: too expensive
if (FALSE)
{
tmp.mat <-array(0,c(parm$N,parm$N))
for (jj in 0:All.Stuff.2$K.v[cc])
{indx.jj <- which(r.matrix[cc,]==jj)
tmp.mat[indx.jj,indx.jj] <- 1
}
dahl.dist <- sum((K.matrix - tmp.mat)^2)
if (dahl.dist < All.Stuff.2$dahl$min)
{All.Stuff.2$dahl$min <- dahl.dist
All.Stuff.2$dahl$K.clust <- r.matrix[cc,]
All.Stuff.2$dahl$K <- All.Stuff.2$K.v[cc]
}
}
### approx Dahl calculations
if (TRUE)
{
dahl.dist <- 0
abort.flag <- FALSE
for (k in 1:max(r.matrix[cc,]))
{i.k <- sort(which(r.matrix[cc,]==k))
if (length(i.k) > 0)
{for (i in i.k)
for (j in i.k)
{if (j > i)
{K.ij <- mean(r.matrix[,i]==r.matrix[,j])
delta.ij <- as.numeric(r.matrix[cc,i] == r.matrix[cc,i])
dahl.dist <- dahl.dist + 2*(K.ij-delta.ij)^2
if (dahl.dist > All.Stuff.2$dahl$min)
{abort.flag <- TRUE
break
}
}
}
if (abort.flag) break
}
}
if (dahl.dist < All.Stuff.2$dahl$min)
{All.Stuff.2$dahl$min <- dahl.dist
All.Stuff.2$dahl$K.clust <- r.matrix[cc,]
All.Stuff.2$dahl$K <- All.Stuff.2$K.v[cc]
}
}
} # end for loop in cc
# just for the record, although All.Stuff.1 and All.Stuff.2
# have $d fixed at mean
All.Stuff.1$d.v <- All.Stuff.2$d.v <- All.Stuff$d.v
list(All.Stuff.1, All.Stuff.2)
}