function TEST_Filtered_Probabilities(predictability)
% This code calculates and writes the filtered probabilities in the worksheet "Data".

    tic;
    
%Variables setup
    predictability = 0;
    Period = 52; 
    folder_result = '../Result/';
    if predictability == 0
        parameters = load(strcat(folder_result, 'A_Parameters_No_Predictability.mat'));
    else 
        parameters = load(strcat(folder_result, 'A_Parameters_Predictability.mat')); 
    end
    
% The variables order below MUST be in the same as in the parameters structure (.mat file)
     [ESTIM, m, n, beta, r, folder_db, folder_result, FILE, period_beg_row, filename_data, sheet, A, B, var_cov, V, P, Q, D_p, sigma_p, vol_pi, ...
        pi_inf, V_pi_inf, p_bar] = v2struct(parameters);
     clear parameters;
    
    range = strcat('K2:', xlsColNum2Str(xlsColStr2Num({'K'})+ n - 1), num2str(period_beg_row + Period));
    returns = log(1 + xlsread(filename_data, sheet, range{1}));
    range{1} = strcat('CB2:', 'CB', num2str(period_beg_row + Period + 1));
    predictor = xlsread(filename_data, sheet, range{1});
    data_length = period_beg_row + Period-1;
    
    if predictability ~= 0 
        LR = [returns, predictor(1:end-1) - predictor(2:end)]';
        n_timeseries = n + 1;
    else
        LR = returns';
        n_timeseries = n;
    end
    
    MLR = zeros(n_timeseries, m, data_length);
    
    temp = zeros(n,m);
    for reg = 1:m
        temp(:,reg) = 1/2*diag(var_cov(:,:,reg));
    end
            
    for t = 1:data_length
         MLR(1:n,:,t) = A + B*ones(1,m)*predictor(t+1) - temp;    
        if predictability ~= 0
            MLR(end, :, t) = D_p;
        end
    end  
    
    eta = zeros(m, data_length);
    
    for reg = 1:m
        if predictability ~= 0
            sigma_i = [chol(var_cov(:,:,reg), 'lower'), zeros(n,1)];
            sigma_i = [sigma_i; sigma_p];
        else
            sigma_i = chol(var_cov(:,:,reg), 'lower');
        end
        for t = 1:data_length
          eta(reg,t) = 1/(2*pi)^(n_timeseries/2)/(det(sigma_i*sigma_i'))^0.5*exp(-1/2 * (LR(:,t) - MLR(:,reg,t))'*...
              ((sigma_i*sigma_i')^(-1))*(LR(:,t) - MLR(:,reg,t)));
        end
    end

    Y_t_1_t = zeros(m,data_length,2);
    Y_t_t = zeros(m,data_length,2);
    Y_t_1_t(:,end,1) = pi_inf;         % Initial values for the probabilities (ergodic probabilities)
    Y_t_1_t(:,end,2) = ones(m,1)/m;    % Initial values for the probabilities (equal probabilities)

    for j = data_length - 1:-1:1
        Y_t_t(:,j+1,1) = (Y_t_1_t(:,j+1,1).* eta(:,j+1))/(ones(1,m)*(Y_t_1_t(:,j+1,1).* eta(:,j+1)));
        Y_t_t(:,j+1,2) = (Y_t_1_t(:,j+1,2).* eta(:,j+1))/(ones(1,m)*(Y_t_1_t(:,j+1,2).* eta(:,j+1)));
        Y_t_1_t(:,j,1) = P'*Y_t_t(:,j+1,1);
        Y_t_1_t(:,j,2) = P'*Y_t_t(:,j+1,2);
    end
    
    Y_t_t(:,1,1) = (Y_t_1_t(:,1,1).* eta(:,1))/(ones(m,1)'*(Y_t_1_t(:,1,1).* eta(:,1)));
    Y_t_t(:,1,2) = (Y_t_1_t(:,1,2).* eta(:,1))/(ones(m,1)'*(Y_t_1_t(:,1,2).* eta(:,1)));
    
   % Calculating Smoothed Probabilities
    smoothed_prob = zeros(m,data_length,2);
    smoothed_prob(:,1,:) = Y_t_t(:,1,:);
    
    for t = 2:data_length
        smoothed_prob(:,t,1) = Y_t_t(:,t,1) .* (P*(smoothed_prob(:,t-1,1)./Y_t_1_t(:,t-1,1)));
        smoothed_prob(:,t,2) = Y_t_t(:,t,2) .* (P*(smoothed_prob(:,t-1,2)./Y_t_1_t(:,t-1,2)));
    end
    
    % Writing results
    T1 = table(Y_t_1_t(:,:,1)');
    writetable(T1,filename_data,'Sheet',sheet,'Range','S2','WriteVariableName',false);
            
    T2 = table(Y_t_1_t(:,:,2)');
    writetable(T2,filename_data,'Sheet',sheet,'Range','AA2','WriteVariableName',false);
            
	T3 = table(Y_t_t(:,:,1)');
    writetable(T3,filename_data,'Sheet',sheet,'Range','AM2','WriteVariableName',false);
            
    T4 = table(Y_t_t(:,:,2)');
    writetable(T4,filename_data,'Sheet',sheet,'Range','AU2','WriteVariableName',false);
            
    T5 = table(smoothed_prob(:,:,1)');
    writetable(T5,filename_data,'Sheet',sheet,'Range','BG2','WriteVariableName',false);
            
    T6 = table(smoothed_prob(:,:,2)');
    writetable(T6,filename_data,'Sheet',sheet,'Range','BO2','WriteVariableName',false);

    clear T1 T2 T3 T4 T5 T6
    toc;
end