/
expectMaxClust.go
198 lines (184 loc) · 4.41 KB
/
expectMaxClust.go
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
package cophycollapse
import (
"fmt"
"math"
"math/rand"
)
/*
type HCSearch struct {
Tree *Node
PreorderNodes []*Node
Clusters map[int]*Cluster
SiteAssignments map[int]int
Gen int
Threads int
Workers int
ClustOutFile string
LogOutFile string
K int
PrintFreq int
}
*/
func (s *HCSearch) RunEM() {
for i := 0; i < s.Gen; i++ {
if i%s.PrintFreq == 0 {
fmt.Println("ITERATION", i)
}
s.updateClusters()
s.updateMixtureBranchLengths()
}
fmt.Println(len(s.Clusters), s.ClusterString())
}
func (s *HCSearch) SplitEM() {
clen := len(s.Clusters)
if clen > 1 {
for i := 0; i < s.SplitGen; i++ {
s.updateClusters()
s.updateMixtureBranchLengths()
//fmt.Println(i)
//fmt.Println(s.ClusterString())
}
//fmt.Println(clen, s.ClusterString())
}
}
func (s *HCSearch) updateMixtureBranchLengths() {
for _, v := range s.Clusters {
if len(v.Sites) == 0 {
continue
}
assignClusterLengths(s.PreorderNodes, v)
IterateLengthsWeighted(s.Tree, v, 10)
}
}
func (s *HCSearch) updateClusters() {
var weights map[int]float64
for k, v := range s.SiteAssignments {
weights = s.siteClusterUpdate(k, v)
for l, c := range s.Clusters {
c.SiteWeights[k] = weights[l]
//fmt.Println(k, l, weights[l])
}
}
}
func (s *HCSearch) clusterLL() {
for _, c := range s.Clusters {
curll := 0.0
for site := range c.Sites {
curll += SingleSiteLL(s.Tree, site)
}
c.LogLike = curll
}
}
func (s *HCSearch) siteClusterUpdate(site int, siteClusterLab int) (weights map[int]float64) {
siteCluster := s.Clusters[siteClusterLab]
bestLL := -1000000000000.
var bestClustLab int
var bestClust *Cluster
llsum := 0.0
llmap := make(map[int]float64)
weights = map[int]float64{}
for k, v := range s.Clusters {
for i, n := range s.PreorderNodes { //assign current cluster's branch lengths
n.LEN = v.BranchLengths[i]
}
var constrain float64
constrain = (math.Log(float64(len(v.Sites))+(s.Alpha/float64(len(s.Clusters)))) / (s.NumPoints + s.Alpha - 1.))
//constrain = 0
curll := SingleSiteLL(s.Tree, site) + constrain
llmap[k] = curll
llsum += curll
if curll > bestLL {
bestLL = curll
bestClustLab = k
bestClust = v
}
}
for k, v := range llmap {
weights[k] = v / llsum
}
if bestClustLab != siteClusterLab { //move the site to its new cluster if the best cluster has changed
var newSlice []int
for _, s := range siteCluster.Sites {
if s != site {
newSlice = append(newSlice, s)
}
}
siteCluster.Sites = newSlice
bestClust.Sites = append(bestClust.Sites, site)
s.SiteAssignments[site] = bestClustLab
}
return
}
func InitEMSearch(tree *Node, gen int, k int, pr int, alpha float64) *HCSearch {
s := new(HCSearch)
s.Tree = tree
s.PreorderNodes = tree.PreorderArray()
s.Gen = gen
s.K = k
//s.startingClustersEMOnly()
s.singleStartingCluster()
s.perturbAndUpdate(3)
s.PrintFreq = pr
s.Alpha = alpha
s.ExpandPenalty = math.Log(s.Alpha / (s.Alpha + s.NumPoints))
return s
}
/*
this gives starting clusters when K is unknown (for basically a prior-free DPP-style mixture model)
func (search *HCSearch) startingClusters() {
clus := make(map[int]*Cluster)
lab := 0
siteClust := make(map[int]int)
for k := range search.Tree.CONTRT {
cur := new(Cluster)
cur.Sites = append(cur.Sites, k)
ClusterMissingTraitsEM(search.Tree, cur, 10)
clus[lab] = cur
siteClust[k] = lab
lab++
}
search.Clusters = clus
search.SiteAssignments = siteClust
}
*/
func (search *HCSearch) startingClustersEMOnly() {
clus := make(map[int]*Cluster)
siteClust := make(map[int]int)
var clustLabs []int
for i := 0; i < search.K; i++ { //create clusters
cur := new(Cluster)
clus[i] = cur
clustLabs = append(clustLabs, i)
cur.SiteWeights = map[int]float64{}
}
for k := range search.Tree.CONTRT {
lab := rand.Intn(search.K)
/*
var lab int
if k < 50 {
lab = 0
} else {
lab = 1
}
*/
cur := clus[lab]
cur.Sites = append(cur.Sites, k)
siteClust[k] = lab
}
stWt := 1.0 / float64(search.K)
search.Clusters = clus
search.SiteAssignments = siteClust
for _, cur := range search.Clusters {
if len(cur.Sites) == 0 {
for range search.PreorderNodes {
cur.BranchLengths = append(cur.BranchLengths, rand.Float64())
}
continue
}
for i := range search.SiteAssignments {
cur.SiteWeights[i] = stWt
}
//ClusterMissingTraitsEM(search.Tree, cur, 10)
IterateLengthsWeighted(search.Tree, cur, 40)
}
}