n.sim=100;
sample=400;
dim=50;
imp_dim=3;
val=zeros(dim-1,n.sim);
ranks_corr=zeros(imp_dim,n.sim);
val_reg=zeros(dim-1,n.sim);
ranks_corr_reg=zeros(imp_dim,n.sim);
misclass=zeros(n.sim,1);
mes_err=zeros(n.sim,1);

% population=[-1,-0.5,0.5,1];
% coeff=randsample(population,imp_dim,true);
% coef=[1,-1,-0.5,0.5]; % SVM with Gauss d=15, d0=4;
% coeff=[-0.5,1,0.5,-1]; % SVR with linear d=15, d0=4;
% coef=[-0.5,-0.5,-1,0.5,-0.5,0.5,-1]; % SVM with Gauss d=30, d0=7;
% coeff=[1,1,-1,0.5,0.5,0.5,-0.5]; % SVR with linear d=30, d0=7;
 coef=[-0.5,-1,1]; % SVM with Gauss d=50, d0=3;
 coeff=[1,0.5,-1]; % SVR with Linear d=50, d0=3;

for i=1:n.sim;
    

 corrset=[1:imp_dim];
 loss_name='hinge_loss';
 loss_gridsel='class_loss';
 algo=svm(kernel('rbf',1));
 algo.optimizer='andre';
 
 example_object=toy_n;
 example_object.l=sample;
 example_object.n=dim;
 example_object.relevant_n=imp_dim;
 example_object.coef=[coef';zeros(example_object.n-example_object.relevant_n,1)];
 example_object.seed=i;
 
 test_object=toy_n;
 test_object.l=sample;
 test_object.n=dim;
 test_object.relevant_n=imp_dim;
 test_object.coef=[coef';zeros(example_object.n-example_object.relevant_n,1)];
 test_object.seed=100+i;

 
 [d,example_object]=train(example_object);
[d_test,test_object]=train(test_object);

%% classification
% logistic regression with LASSO

Cov=get_X(d); Y=double(get_Y(d)==1)+1;
lasso_fit = glmnet(Cov,Y,'binomial'); % classification
%lasso_fit = glmnet(Cov, Y); % regression

beta=lasso_fit.beta;
for j=1:size(beta,2);
    l=find(beta(:,j)~=0);
    if length(l)>=imp_dim; 
        break;
    end;
end;

lasso_set=find(beta(:,j)~=0);
ranks_corr(:,i)=lasso_set(1:imp_dim);

Cov_lasso=Cov(:,lasso_set(1:imp_dim));
p = glmfit(Cov_lasso,(get_Y(d)==1),'binomial');
Cov_lasso_test=d_test.X(:,lasso_set);
y_hat = glmval(p, Cov_lasso_test, 'logit');
class=(y_hat > 0.5)-(y_hat <= 0.5);
misclass(i) = mean(d_test.Y ~= class);







%% Regression

corrset=[1:imp_dim];    
loss_name='epsilon_insensitive_loss'; 
loss_gridsel='linear_loss';
algo=svr(kernel('linear')); 
algo.optimizer='andre';

example_object=example_reg; 
example_object.seed=i;
example_object.n=sample;
example_object.dim=dim;
example_object.corrdim=imp_dim;
example_object.coef=[coeff';zeros(example_object.dim-example_object.corrdim,1)];

test_object=example_reg; 
test_object.seed=100+j;
test_object.n=sample;
test_object.dim=dim;
test_object.corrdim=imp_dim;
test_object.coef=[coeff';zeros(example_object.dim-example_object.corrdim,1)];


% regression with LASSO

Cov=get_X(d); Y=get_Y(d);
model_lasso=glmnet(Cov, Y);
lasso_fit = glmnet(Cov, Y); % regression

beta=lasso_fit.beta;
for j=1:size(beta,2);
    l=find(beta(:,j)~=0);
    if length(l)>=imp_dim; 
        break;
    end;
end;

lasso_set=find(beta(:,j)~=0);
ranks_corr_reg(:,i)=lasso_set(1:imp_dim);

    Cov_lasso=Cov(:,lasso_set(1:imp_dim));
    p = regress(Y,[ones(size(Cov_lasso,1),1),Cov_lasso]);
    Cov_lasso_test=d_test.X(:,lasso_set);
    yfit = [ones(size(Cov_lasso,1),1),Cov_lasso_test]*p;
    yresid = d_test.Y - yfit;
    mes_err(i) = mean(yresid.^2);




end;

ind1=zeros(n.sim,1);
ind2=zeros(n.sim,1);
ind3=zeros(n.sim,1);
ind4=zeros(n.sim,1);
ind5=zeros(n.sim,1);
ind6=zeros(n.sim,1);

for j=1:n.sim
    ind1(j)=isempty(setdiff(corrset,ranks_corr(:,j)));
    ind2(j)=length(setdiff(corrset,ranks_corr(:,j)))==1;
    ind3(j)=length(setdiff(corrset,ranks_corr(:,j)))>1;
    ind4(j)=isempty(setdiff(corrset,ranks_corr_reg(:,j)));
    ind5(j)=length(setdiff(corrset,ranks_corr_reg(:,j)))==1;
    ind6(j)=length(setdiff(corrset,ranks_corr_reg(:,j)))>1;

end
mean(ind1)
mean(ind2)
mean(ind3)
mean(ind4)
mean(ind5)
mean(ind6)

mean(misclass)
mean(mes_err)




