www.gusucode.com > GAVPai_Book_MathworksCntrlFileEx_May2019 > GAVPai_Book_MathworksCntrlFileEx_May2019/EMNP_eqconstr_wgtstd.m

    % Repair strategy to standardize weights of long/short positions, to
% satisfy their linear constraints

function std_weight_mat = EMNP_eqconstr_wgtstd(weight_mat,  Begin_col, End_col, Low_bound, Up_bound, signal)

[row_mat, col_mat]=size(weight_mat);
if (signal ==0)    %short positions
    weight_mat(1:row_mat, Begin_col:End_col) = abs(weight_mat(1:row_mat, Begin_col:End_col) );
end

% standardize lower bounds for long/short positions for each population
% individual
for i=1: row_mat
    
                    
                    % R: those weights which are less than their lower
                    % bounds
                    R=[];
                    c_R=0;
                    for j=Begin_col:End_col
                            if (weight_mat(i,j)< Low_bound)
                                weight_mat(i,j)= Low_bound;
                                c_R = c_R+1;
                                R(c_R) = j;
                            end
                    end
                    
                    %Q: weights which satisfy their lower bounds
                    Q = setdiff([Begin_col:End_col], R); 
                    
                    % F: free proportion of weights
                    F = 1 - Low_bound *(End_col-Begin_col+1); 
                    
                    SUM = sum(weight_mat(i,Q));
                    if (SUM==0)
                        if (~isempty(Q))
                        term = F / length(Q);
                        weight_mat(i,Q)= Low_bound+ term;
                        else
                        term = F / length(R);
                        weight_mat(i,R)= Low_bound+ term;
                        end
                    else
                        term = F / SUM;
                        weight_mat(i,Q) = Low_bound+ weight_mat(i,Q)* term;
                    end
                   
    
end
% standardize upper bounds for long/short positions, for each population
% individual

for i = 1: row_mat 
    
                    kr = 1;
                    r = [];     
                    ex_flag = true;
                    Q = setdiff([Begin_col:End_col], r);
                    while (ex_flag == true) 
                    ex_flag = false; 

                    for j = 1: length(Q)
                            if ( weight_mat(i, Q(j)) <= Up_bound )
                            continue;
                            else
                            ex_flag = true;
                            r(kr) = Q(j);
                            kr = kr+1;
                            end
                    end
                   

                    Q = setdiff([Begin_col:End_col], r);

                    if (ex_flag == true)
                    SUM = sum(weight_mat(i,Q));
                    F = 1 - ( length(Q)*Low_bound) -(length(r)*Up_bound);
                    if (SUM==0) 
                            term = F;
                            weight_mat(i,Q(1))= term;
                    else
                        term = F/SUM;
                        weight_mat(i,Q) = Low_bound+ weight_mat(i,Q)* term;
                    end
                    weight_mat(i,r)=Up_bound;
                    end

                    end 
                
end
if (signal ==0) 
       weight_mat (1:row_mat, Begin_col:End_col) = -weight_mat (1:row_mat, Begin_col:End_col);
end
std_weight_mat = weight_mat;
end