forked from MattNolanLab/Inter_Intra_Variation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMAPDP.R
161 lines (133 loc) · 3.68 KB
/
MAPDP.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
# Functions for implementing MAP-DP clusering.
# From https://github.com/thorstenwagner/R-ClustMAPDP/blob/master/PACKAGE/R/clustMapDP.R
library(matrixStats);
library(compiler)
enableJIT(3)
#' Compute Stundent-t log likeligood
stnll <- function(x,mu,a,c,B,D) {
nu <- a-D+1;
Lambda <- c*nu/(c+1)*B;
nl <- (nu+D)/2*log(1+t(x-mu)%*%Lambda%*%(x-mu)/nu)-0.5*log(det(Lambda))+lgamma(nu/2)-lgamma((nu+D)/2)+D/2*log(nu*pi);
return(nl)
}
#' Update Normal-Wishart hyper parameters
nwupd <- function(Nki,xki,m0,a0,c0,B0) {
xmki <- 0;
if(!is.matrix(xki)){
xmki <- mean(xki);
}else{
xmki <- rowMeans(xki);
}
xmcki <-0;
b = kronecker(matrix(1,1,Nki),xmki);
if(dim(b)[1]==1 && dim(b)[2]==1){
xmcki <- xki- b[1,1];
}
else{
xmcki <- xki-b;
}
Ski <- tcrossprod(xmcki)
cki <- c0+Nki;
mki <- (c0*m0+Nki*xmki)/cki;
Bki <- solve(solve(B0)+Ski+c0*Nki/cki*(xmki-m0)%*%t(xmki-m0));
aki <- a0+Nki;
resultList <- list("mki"=mki,"aki"=aki,"cki"=cki,"Bki"=Bki);
return(resultList);
}
#' MAP-DP Clustering
#'
#'It implements the algorithm descriped by
#'Raykov YP, Boukouvalas A, Baig F, Little MA (2016)
#'What to Do When K-Means Clustering Fails: A Simple yet Principled Alternative Algorithm.
#'PLOS ONE 11(9): e0162259. https://doi.org/10.1371/journal.pone.0162259
#'
#' @param X DxN matrix of data
#' @param N0 prior count (DP concentration paramter)
#' @param m0 cluster prior mean
#' @param a0 cluster prior scale
#' @param c0 cluster prior degrees of freedom
#' @param B0 cluster prior precision (inverse covariance)
#' @export
clustMapDP <- function(X,N0,m0,a0,c0,B0) {
D <- dim(X)[1];
N <- dim(X)[2];
epsilon <- 1e-6;
#Initialization
K <- 1;
z <- matrix(1,N,1);
Enew <- Inf;
dE <- Inf;
iter <- 0;
E <- c();
while (abs(dE) > epsilon) {
Eold <- Enew;
dik <- matrix(Inf,N,1);
for(i in 1:N){
dk <- matrix(Inf,K+1,1);
Nki <- matrix(Inf,K+1,1);
for(k in 1:K){
zki <- (z==k);
zki[i] <- FALSE;
Nki[k] <- sum(zki);
# Updates meaningless for Nki=0
if(Nki[k]==0){
next;
}
# Update NW cluster hyper parameters
nwupdList <- nwupd(Nki[k],X[,zki],m0,a0,c0,B0);
mki <- nwupdList$mki;
aki <- nwupdList$aki;
cki <- nwupdList$cki;
Bki <- nwupdList$Bki;
# Computer Student-t NLL, existing cluster
help <-stnll(X[,i],mki,aki,cki,Bki,D);
dk[k] <- help;
}
if(iter==0){
Nki[1]<-1;
}
Nki[K+1] = N0;
# Computer Student-t NLL, new cluster
dk[K+1] <- stnll(X[,i],m0,a0,c0,B0,D);
# Computer MAP Assignment
v = dk-log(Nki);
z[i] <- which.min(v);
dik[i] <- v[z[i]];
# Create new cluster if required
if(z[i] == (K+1)){
K <- K+1;
}
}
# Remove any empty clusters and re-assign
Knz = 0;
for(k in 1:K){
i <- (z==k);
Nk <- sum(i);
if (Nk>0){
Knz <- Knz + 1;
z[i] <- Knz;
}
}
K <- Knz;
Nk <- hist(z,1:K,plot=FALSE)$counts
# Compute updated NLL
Enew <- sum(dik)-K*log(N0)-sum(lgamma(Nk));
dE <- Eold - Enew;
iter <- iter +1;
E <- c(E,Enew);
}
# Compute cluster centroids
mu <- matrix(NaN,D,K);
for(k in 1:K){
if (k %in% unique(z) & length(z[z==k]) > 1){
xk <- X[,z==k];
mu[,k] <- rowMeans(xk);
} else if (k %in% unique(z) & length(z[z==k]) == 1){
mu[,k] <- X[z==k]
} else {
stop('Something went terribly wrong, check cluster centroids assignation')
}
}
resultList <- list("mu"=mu,"z"=z,"K"=K,"E"=E);
return(resultList);
}