www.gusucode.com > GAVPai_Book_MathworksCntrlFileEx_May2019 > GAVPai_Book_MathworksCntrlFileEx_May2019/normalize_weight.m
%normalizes weights to lie within buy/sell bounds function [mutat_popln_norm] = normalize_weight(mutat_popln_raw,sell_low , sell_high, buy_low, buy_high) [popln_rows, popln_cols] = size(mutat_popln_raw); Pos_Weights = mutat_popln_raw .*( mutat_popln_raw >=0); % Negative weights are marked 0 Neg_Weights = -(mutat_popln_raw.*( mutat_popln_raw <0));% Positive weights are marked 0 % normalize positive and negative weights to [0,1] Max_Pos = max(max(Pos_Weights)); Max_Neg = max(max(Neg_Weights)); Min_Pos = min(Pos_Weights (Pos_Weights ~=0)); range_Positive = Max_Pos - Min_Pos; Min_Neg = min(Neg_Weights(Neg_Weights ~=0)); range_Negative = Max_Neg - Min_Neg; for i=1:popln_rows for j =1:popln_cols a = Pos_Weights(i,j); b = Neg_Weights(i,j); if (a >0) Pos_Weights_01norm (i,j) = (a-Min_Pos)/range_Positive; else Pos_Weights_01norm (i,j) =0; end if (b > 0) Neg_Weights_01norm (i,j) = (b-Min_Neg)/range_Negative; else Neg_Weights_01norm (i,j) =0; end end end rangenew_Pos = buy_high - buy_low; rangenew_Neg = sell_high - sell_low; for i=1:popln_rows for j =1:popln_cols a = Pos_Weights_01norm(i,j); b = Neg_Weights_01norm(i,j); if (a >0) Pos_Weights_newnorm (i,j) = buy_low(j)+( a*rangenew_Pos(j)); else Pos_Weights_newnorm (i,j) =0; end if (b > 0) Neg_Weights_newnorm (i,j) = sell_low(j)+( b*rangenew_Neg(j)); else Neg_Weights_newnorm (i,j) =0; end end end mutat_popln_norm = Pos_Weights_newnorm - Neg_Weights_newnorm; end