% Minimizing sum-of-fi loss function using stochastic gradient descent (SGD).
% Example 1 
% load X2; load X7; load T2; load T7; X = [X2 X7]; 
% y = [ones(2000,1); -ones(2000,1)];
% [ws,f,rtm] = sgd_rlog(X,y,T2,T7,0.001,13,0.009,8,9,3452);
% Example 2 
% load X3; load X5; load T3; load T5; X = [X3 X5]; 
% y = [ones(2000,1); -ones(2000,1)];
% [ws,f,rtm] = sgd_rlog(X,y,T3,T5,0.00175,9.5,0.012,12,9,1007);
function [ws,f,rtm] = sgd_rlog(X,y,Xte1,Xte2,mu,bt,gm,m,st,iter)
format long
format compact
[N,P] = size(X);
Xh = [X; ones(1,P)];
t1 = size(Xte1,2);
t2 = size(Xte2,2);
A1 = [Xte1; ones(1,t1)];
A2 = [Xte2; ones(1,t2)];
w0 = zeros(N+1,1);
wk = w0;
fk = f_rlog(wk,Xh,y,0);
f = fk;
y1 = wk'*A1;
y2 = wk'*A2;
C = zeros(2,2);
C(1,1) = length(find(y1 > 0));
C(2,1) = t1 - C(1,1);
C(2,2) = length(find(y2 < 0));
C(1,2) = t2 - C(2,2);
rtm = (1-((C(1,1)+C(2,2))/sum(sum(C))))*100;
aw = zeros(1,iter);
for k = 1:iter
    aw(k) = bt/(1+gm*k);
end
k = 0;
rand('state',st)
while k < iter
  r = randperm(P);
  Xw = Xh(:,r(1:m));
  yw = y(r(1:m));
  gk = g_rlog(wk,Xw,yw,mu);
  dk = -gk;
  adk = aw(k+1)*dk;
  wk = wk + adk;
  k = k + 1;
  fk = f_rlog(wk,Xh,y,0);
  f = [f; fk];
  y1 = wk'*A1;
  y2 = wk'*A2;
  C(1,1) = length(find(y1 > 0));
  C(2,1) = t1 - C(1,1);
  C(2,2) = length(find(y2 < 0));
  C(1,2) = t2 - C(2,2);
  rk = (1-((C(1,1)+C(2,2))/sum(sum(C))))*100;
  rtm = [rtm rk];
end
disp('Objective function at the solution point:')
fs = f(end)
ws = wk;
disp('confusion matrix')
C
disp('rate of misclassification in percentage:')
rk
figure(1)
set(gca,'fontsize',14,'fontname','times')
semilogy(0:1:iter,f,'b-','linewidth',1.4)
xlabel('Iterations \itk','fontsize',13,'fontname','times')
ylabel('Objective  {\itE_L}{\rm(}{\itw_k}{\rm)}','fontsize',13,'fontname','times')
axis([0 iter+1 1e-2 35])
grid
format short