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

    % 
function  [psi, G1,G2]  = EMNP_compute_constr_violn_fn( weight_mat, covar_mat, betas_assets, Highvolassets )
[row_mat, col_mat]= size(weight_mat);
No_Highvolassets = length(Highvolassets);


  for i=1:row_mat
    
    x_chromo=weight_mat(i,:);
    portfolio_risk  =  (x_chromo * covar_mat * x_chromo');   
   
    %penalty function G1
    portfolio_beta_term = sum(betas_assets.* x_chromo); 
    g1_term = abs(portfolio_beta_term) - 0.1;
    g1_result = power(g1_term,2);
    if (g1_term <=0 )
        G1(i)=0;
    else
        G1(i)=1;
    end

                        
   %penalty function G2
   for k=1: No_Highvolassets 
   RiskLmt_ShortHighvol(k)= (x_chromo (1,Highvolassets(k)) ^2)  * covar_mat (Highvolassets(k), Highvolassets(k)) ;

   g2_term(i,k) = RiskLmt_ShortHighvol(k)-(portfolio_risk/5) ;
   g2_result(i,k)= power(g2_term(i,k), 2);
   if (g2_term <=0 )
        G2(i,k)=0;
   else
        G2(i,k)=1;
   end
   end

   %compute constraint violation function
    psi(i)= (G1(i)*g1_result + sum( G2(i, 1:No_Highvolassets).*g2_result(i,1:No_Highvolassets)) );
 end

        
end