function TEST_Single_Regime_Solution_No_Predictability
 
% Single-regime model: loading the parameters
    format bank;
    n = 4; 
    gamma = 5;
    r = 0; 
    folder.db = '../db/';
    folder.result = '../Result/';
    FILE = 'Data.xlsx';
    sheet = 'Parameters';
    filename = strcat(folder.db,FILE);
    diary(strcat(folder.result,'H_Single_Regime_Solution_No_Predictability.txt'));

    
    range = strcat('C2:', xlsColNum2Str(xlsColStr2Num({'C'})+ n - 1), '2');
    means = xlsread(filename, sheet, range{1})';
    
    excess_means = means - r;
    
    range = strcat('C3:', xlsColNum2Str(xlsColStr2Num({'C'})+ n - 1), num2str(2+n));
    var_cov = xlsread(filename, sheet, range{1});

% The myopic optimal (unconstrained) portfolio under a single regime is then:
    unc_portfolio = 1/gamma * (var_cov)^(-1) * excess_means;
    unc_portfolio = [unc_portfolio; 1 - sum(unc_portfolio)];
    diary on
    disp(' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ');
    disp('           Unconstrained Portfolio');
    disp(100*unc_portfolio');
    diary off
    
% Using our methdology to get constrained portfolios:
    con_portfolio = Local_Convert(n, unc_portfolio, gamma, r, means, var_cov);
    
    diary on
    disp(' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ');
    disp('           Constrained Portfolio');
    disp(100*con_portfolio');
    diary off
    
% Calculating the tangent portfolio:
    A = ones(size(var_cov,1),1)'*var_cov^(-1)*ones(size(var_cov,1),1);
    B = ones(size(var_cov,1),1)'*var_cov^(-1)*means;

    tangent_weights = var_cov^(-1)*excess_means/(B-A*r);
    diary on
    disp(' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ');
    disp('           Tangent Portfolio');
    disp(100*tangent_weights');
    diary off
    
    format short;

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function constrained = Local_Convert(n, unconstrained, gamma, r, means, var_cov)
    
    constrained = unconstrained;
    if length(constrained) ~= n+1
        error('The strategy must have (n+1) asset weights !!!');
    end
    
    means_temp = means;
    var_cov_temp = var_cov;
    n_temp = n;
    no_ss_flag = 0;
    position = 1:n;
    new_strategy = constrained;

    while no_ss_flag == 0
        upper_bound = n_temp;
        flag = 0;
        for j=upper_bound:-1:1
            if new_strategy (j) < 0
                constrained(position(j))=0;
                position(j) = [];
                means_temp(j,:) = [];
                var_cov_temp(:,j) = [];
                var_cov_temp(j,:) = [];
                n_temp = n_temp - 1;
                flag = 1;
            end
        end   
        if flag == 1
            if n_temp ~= 0 
                new_strategy  = 1/gamma * (var_cov_temp)^(-1) * (means_temp - r);
                new_strategy  = [new_strategy; 1 - sum(new_strategy)];
                if new_strategy(1,end)<0
                   new_strategy(1,end)=0;
                   total = sum(new_strategy);
                   for j=1:length(new_strategy)-1
                        new_strategy(1,j) = new_strategy(1,j)/total;
                   end
                end
                counter = 1;
                for j=1:length(position)
                        constrained(position(j))=new_strategy(counter);
                        counter = counter + 1;
                end
                constrained(n+1)=new_strategy(counter);
            elseif n_temp==0
                constrained(n+1)=1;
                constrained(1:n)=0;
                no_ss_flag = 1;
            end
        elseif flag == 0
            no_ss_flag = 1;
            if constrained(n+1)<0
                total = 1 - constrained(n+1);
                constrained(n+1)=0;
                constrained = constrained/total;
            end
        end
    end
end