forked from PRML/PRMLT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmixLogit.m
54 lines (47 loc) · 1.22 KB
/
mixLogit.m
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
function [model, llh] = mixLogit(X, t, k)
% mixture of logistic regression model
n = size(X,2);
X = [X; ones(1,n)];
d = size(X,1);
z = ceil(k*rand(1,n));
R = full(sparse(1:n,z,1,n,k,n)); % n x k
W = zeros(d,k);
w0 = zeros(1,k);
tol = 1e-6;
maxiter = 100;
llh = -inf(1,maxiter);
converged = false;
iter = 1;
t = t(:);
h = ones(n,1);
h(t==0) = -1;
A = bsxfun(@plus,X'*W,w0);
while ~converged && iter < maxiter
iter = iter+1;
% maximization
nk = sum(R,1);
alpha = nk/n;
Y = sigmoid(A);
for j = 1:k
W(:,j) = newtonStep(X, t, Y(:,j), W(:,j), R(:,j));
end
% expectation
A = bsxfun(@plus,X'*W,w0);
logRho = -log1pexp(-bsxfun(@times,A,h));
logRho = bsxfun(@plus,logRho,log(alpha));
T = logsumexp(logRho,2);
llh(iter) = sum(T)/n; % loglikelihood
logR = bsxfun(@minus,logRho,T);
R = exp(logR);
converged = (llh(iter)-llh(iter-1)) < tol*abs(llh(iter));
end
llh = llh(2:iter);
model.alpha = alpha; % mixing coefficient
model.W = W; % logistic model coefficent
function w = newtonStep(X, t, y, w, r)
% lambda = 1e-6;
v = y.*(1-y).*r;
H = bsxfun(@times,X,v')*X';%+lambda*eye(size(X,1));
s = (y-t).*r;
g = X*s;
w = w-H\g;