
%%%%%%%%%% Simulation of a factor model with block structure %%%%%%%%%%%%%%
 
% clearing

clear
clc

%%%%%%

t_vec = [100,150]
n_vec = [500,1000,2000]
com_fac_vec = [1]
q_vec = 1
dist_type_vec = [2,3] % 0 = 'normal', 1 = 'student_1', 2 = 'student_3', 3 = 'laplace' 
delta_vec = 0
tau_vec = [.1,.5]
n_blocks_vec = [5]
factor_per_block_vec = [1]
type_vec = [2]   %% 1 = homogeneous , 2 = heterogeneous
theta_vec = [0.5]%,0.7,0.9]
share_vec = [0,0.5]
k_vec = 5

iterator = makeTuples(t_vec,n_vec,...
                      tau_vec, dist_type_vec, ...
                      type_vec,...
                      theta_vec,share_vec);


T = iterator(:,1);
n = iterator(:,2);
tau = iterator(:,3);
dist = iterator(:,4);
type = iterator(:,5);
theta = iterator(:,6);
share = iterator(:,7);


res_table = table(T,n,tau,dist,type,theta,share);
res_table(:,'dist_verb') = {'null'};
res_table(ismember(res_table.dist, [0]),'dist_verb') = {'normal'};   
res_table(ismember(res_table.dist, [1]),'dist_verb') = {'student_1'};   
res_table(ismember(res_table.dist, [2]),'dist_verb') = {'student_3'};   
res_table(ismember(res_table.dist, [3]),'dist_verb') = {'laplace'};   
res_table.dist_verb = string(res_table.dist_verb);

parfor s = 1:100
    
    Solution = arrayfun(@(TT,nn,ta,dt,bd,th,sh) ...
                        EB_vs_BQML(TT,nn,... %% T,n
                                   1,1,0.5,...  %% r_c, q, mu
                                   'normal',dt,0,...   %% load_dist, idio_dist, idio autocorr
                                   ta,...                  %% idio cross corr
                                   5,1,... %% blocks number, factor per block
                                   bd, ...%% homogeneous or heterogenous block dimensions
                                   th,sh, ...%% noise to signal, share of missings
                                   5),...
                       res_table.T,res_table.n,res_table.tau,...
                       res_table.dist_verb,res_table.type,res_table.theta,res_table.share) %%%

    abc = struct2table(Solution)

    location = strcat('simulation_results',filesep,'Exp_04',filesep,'table',string(s),'.csv')
    writetable(abc,location)
    
    
end






