-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathestW_sim.m
85 lines (75 loc) · 3.06 KB
/
estW_sim.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
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
%% Run simulation, generate data, Cross-validation, perform regularization method and original method
function estW_sim(n,m,d,repprop,L_noise,X_noise,W_noise,nsim)
%alphas = [0:0.01:0.1,0.2,0.5,0.8,1];
%X_bysim = cell(nsim,1);
%X_input_bysim = cell(nsim,1);
%W_bysim = cell(nsim,1);
%W_init_bysim = cell(nsim,1);
Wl_bysim = cell(nsim,1);
%Wl_cv_bysim = cell(nsim,1);
%best_alpha_bysim = zeros(nsim,1);
%criterion_cv_onfulldata_bysim = zeros(nsim,1);
%criterion_old_onfulldata_bysim = zeros(nsim,1);
b = m/d;
for l = 1:nsim
%Set the seed to be iterations number at the beginning of each iteration.
rng(l);
X = zeros(n,m);
W = zeros(m,d);
% Simulate the dataset
for i = 1:d
ind = (1+(i-1)*b):(i*b);
if rand > repprop || i==1
sig = randn(n,1);
end
X(:,ind) = sig*ones(1,b) + 0.1*randn(n,b);
W(ind,i) = rand(b,1);
end
% change W_init here
% W_init = rand(m,d);
% W_init = W+randn(m,d);
% !!!WORONG!!! !!LACK BRACKET!! : W_init = double(W + (rand(m,d)>(1-W_noise))~=0);
W_init = double((W + (rand(m,d)>(1-W_noise)))~=0);
% W_init = double(W~=0);
% W_init = double(W>0.25);
L = X*W;
%S = corr(L);
L_input = L + L_noise .* randn(n,d);
S_input = corr(L_input);
X_input = X + X_noise .* rand(n,m);
% Perform Cross-validation
%mean_criterion_byalpha = zeros(length(alphas),1);
% Cross-validation Partition
%CVO = cvpartition(n,'k',nfolds);
%criterion_byfold = zeros(CVO.NumTestSets,1);
%for k = 1:length(alphas)
% for i = 1:CVO.NumTestSets
% trIdx = CVO.training(i);
% teIdx = CVO.test(i);
% [~,criterion] = estW_smooth(X_input(trIdx,:),X_input(teIdx,:),S_input,W_init,alphas(k));
% criterion_byfold(i) = criterion;
% end
% mean_criterion_byalpha(k) = mean(criterion_byfold);
%end
%[~, Idx_max_criteirion] = max(mean_criterion_byalpha);
%best_alpha = alphas(Idx_max_criteirion);
%fprintf('Best alpha is found to be %0.2f\n', best_alpha);
% Runing on the full data with best tuning parameter found by CV
%[Wl_cv,criterion_cv_onfulldata] = estW_smooth(X_input,X_input,S_input,W_init,best_alpha);
[Wl] = estW(X_input,S_input,W_init);
fprintf('Completed for Simulation %i\n', l);
%X_bysim{l} = X;
%X_input_bysim{l} = X_input;
%W_bysim{l} = W;
%W_init_bysim{l} = W_init;
Wl_bysim{l} = Wl;
%Wl_cv_bysim{l} = Wl_cv;
%best_alpha_bysim(l) = best_alpha;
%criterion_cv_onfulldata_bysim(l) = criterion_old_onfulldata;
%criterion_old_onfulldata_bysim(l) = criterion_cv_onfulldata;
disp('Results saved.');
end
fname = sprintf('results/estW/simres_estW_n%d_m%d_d%d_repprop%.2f_Lnoise%.2f_Xnoise%0.2f_Wnoise%0.2f_nsim%d.mat', n, m, d, repprop, L_noise, X_noise, W_noise, nsim);
save(fname,'Wl_bysim','-v7.3');
disp('Success, All Results saved!');
end