clear all
%cd 'C:\Users\Daniel Jones\Desktop\Projekt Statistisches Landesamt\Code'
Include%_linux

path='../Daten_aufbereitet/';

file_names={
'Krankenversicherung';
'Landwirtschaft';
'Geburten_Groesse';
'Geburten_Gewicht';
%'KH-Diagnose';
'Umsatzsteuer';
%'Baumassnahmen_Dauer';
'Baumassnahmen_Kosten';
'Wohngeld';
'Versorgungsempfaenger';
%'Unfaelle_Unfallstelle';
'Unfaelle_Fahrer';
%'Mikrozensus_Wohnflaeche';
'Mikrozensus_Einkommen';
'Pruefungen2000'};

estimate_names={
'simple_mean';
'simple_linear';
'kernel';
'partitioning';
'nearest_neighbour';
'neural_networks';
'regression_tree';
'random_forest'
'additive_b_splines';
'local_polynomial_kernel';
'smoothing_spline';
};

parameter_list={
    2.^(0:5);
    2.^(0:5);
    2.^(-10:0);
    2.^(-10:0);
    2.^(0:11);
    2.^(0:7);
    2.^(0:11);
    11;
    2.^(-10:0);
    2.^(-10:0);
    2.^(-20:20);
    };

number_estimates=length(estimate_names);
number_files=length(file_names);

estimates_list=cell(number_estimates,1);

for k=1:number_estimates
estimates_list{k}=str2func(estimate_names{k});
end

results=cell(number_files,number_estimates);
mse=zeros(number_files,number_estimates);
time=zeros(number_files,number_estimates);

param_samplerate=0.5;   % Aufteilung zwischen Training und testen

% try
% matlabpool open
% 
% catch me
%     matlabpool close
%     matlabpool open    
% end

for i=1:size(file_names,1)
    %display(i)
    %extract data from .csv-files
    [X,Y,X_eval,Y_eval]=get_data(path,file_names{i});
    
% Statt der ursprünglichen Aufteilung Evaluationsdaten ca. 1/2 wird hier
% gewährleistet, dass nur 1/3 der Daten zum Evaluieren verwendet wird.
    [n,d]=size(X);
    N=ceil(n*1/3);
    
    X=[X;X_eval(1:N,:)];
    X_eval=X_eval((N+1):size(X_eval,1),:);
    
    Y=[Y;Y_eval(1:N,:)];
    Y_eval=Y_eval((N+1):size(Y_eval,1),:);
    
    [n,d]=size(X);
    
    %different preprocessing for partitioning estimate
    [X_p X_eval_p]=preproc(X,X_eval,[0.01,0.99]);
    %[X_p2 X_eval_p2]=preproc(X,X_eval,[0.02,0.98]);
    [X X_eval]=preproc(X,X_eval,[0,1]);
    
    %data has already been randomly permuted, so we just split
    N=ceil(n*param_samplerate);
    X_test=X((N+1):n,:);
    X_test_p=X_p((N+1):n,:);
    %X_test_p2=X_p2((N+1):n,:);
    X_train=X(1:N,:);
    X_train_p=X_p(1:N,:);
    %X_train_p2=X_p2(1:N,:);
    Y_test=Y((N+1):n,:);
    Y_train=Y(1:N);
    
    clear X X_p X_p2
    
    for k=1:number_estimates
        
        if (k>=10 && i>=15) || (k>10 && i>=14)  % Speicherüberlauf
        
%             matlabpool close
%             
%             time_dummy=tic;
%         
%             try 
%                 if strcmp(estimate_names{k},'partitioning1') || strcmp(estimate_names{k},'partitioning1a')
%                     results{i,k}=estimates_list{k}(X_train_p, Y_train, X_test_p, Y_test , X_eval_p, Y_eval, parameter_list{k});
%     %             elseif strcmp(estimate_names{k},'partitioning2') || strcmp(estimate_names{k},'partitioning2a')
%     %                 results{i,k}=estimates_list{k}(X_train_p2, Y_train, X_test_p2, Y_test , X_eval_p2, Y_eval, parameter_list{k});
%                 else
%                      results{i,k}=estimates_list{k}(X_train, Y_train, X_test, Y_test , X_eval, Y_eval, parameter_list{k});
%                 end 
% 
%                 mse(i,k)=results{i,k}.mse;
%                 %display(mse(i,k))
%             catch me
%             results{i,k}=me;
%             mse(i,k)=0;
%             end
% 
%             time(i,k)=toc(time_dummy);
% 
%             save('Alles_mit_ss2','results','mse','time','file_names','estimate_names')
%             
%             
%             
%             matlabpool open
        
            
        else
            
        time_dummy=tic;
        
        try 
            if strcmp(estimate_names{k},'partitioning1') || strcmp(estimate_names{k},'partitioning1a')
                results{i,k}=estimates_list{k}(X_train_p, Y_train, X_test_p, Y_test , X_eval_p, Y_eval, parameter_list{k});
%             elseif strcmp(estimate_names{k},'partitioning2') || strcmp(estimate_names{k},'partitioning2a')
%                 results{i,k}=estimates_list{k}(X_train_p2, Y_train, X_test_p2, Y_test , X_eval_p2, Y_eval, parameter_list{k});
            else
                 results{i,k}=estimates_list{k}(X_train, Y_train, X_test, Y_test , X_eval, Y_eval, parameter_list{k});
            end 
            
            mse(i,k)=results{i,k}.mse;
            %display(mse(i,k))
        catch me
        results{i,k}=me;
        mse(i,k)=0;
        end

        time(i,k)=toc(time_dummy);
        
        save('Alles_mit_ss_NEU','results','mse','time','file_names','estimate_names')
        
        end
        
        if i<size(file_names,1) && sum(k==[8 10 11])
        matlabpool close
        matlabpool open  
        end
       
    end
      
    
end
matlabpool close      
        
% try
% run_analyse('..\data_bachelor_theses\Umsatzsteuer')
% catch exception
% end
% 
% try
% run_analyse('..\data_bachelor_theses\KH-Diagnose')
% catch exception
% end
% 
% try
% run_analyse('..\data_bachelor_theses\Geburten_Gewicht')
% catch exception
% end
% 
% try
% run_analyse('..\data_bachelor_theses\Geburten_Groesse')
% catch exception
% end
% 
% try
% run_analyse('..\data_bachelor_theses\Landwirtschaft',11)
% catch exception
% end
% 
% try
% run_analyse('..\data_bachelor_theses\Pruefung2000')
% catch exception
% end

