Turn For Loop into Vectorization for Genetic Algorithm
Afficher commentaires plus anciens
I am using a genetic algorithm with the objective function below. Its actually pretty slow even with the Parallelism Enabled and a few constraints. I believe vectorization would help in replacing the nested for loops, but I am not confident with the indexing here in combination with the ga, and I have had many attempts at trying to vectorize. Any pointers on this? Thanks!
function [f] = CalcBestStratPart6ModforPosition(x,Delta,Vega,Gamma,Theta,Rho,Da,IV,DVIVPriceTime,NumberOpts,CurrentPriceRange,SizeCurrentP,SizeCurrentT,IVPriceTime,PercentageRange)
%
%Simulate Time Changes
for k=1:SizeCurrentT;
%
%Simulate Price Changes
for i=1:SizeCurrentP;
%
ThetaAllPrices=-(Theta(i,k,x(1)).*x(3))+(Theta(i,k,x(2)).*x(3))+-(Theta(i,k,x(4)).*x(6))+(Theta(i,k,x(5)).*x(6))+(Theta(i,k,x(7)).*x(9))+(Theta(i,k,x(8)).*x(9))+(Theta(i,k,x(10)).*x(12))+(Theta(i,k,x(11)).*x(12))+-(Theta(i,k,x(13)).*x(15))+(Theta(i,k,x(14)).*x(15))+-(Theta(i,k,x(16)).*x(18))+(Theta(i,k,x(17)).*x(18))+-(Theta(i,k,x(19)).*x(21))+(Theta(i,k,x(20)).*x(21))+-(Theta(i,k,x(22)).*x(24))+(Theta(i,k,x(23)).*x(24));
DeltaAllPrices=-(Delta(i,k,x(1)).*x(3))+(Delta(i,k,x(2)).*x(3))+-(Delta(i,k,x(4)).*x(6))+(Delta(i,k,x(5)).*x(6))+(Delta(i,k,x(7)).*x(9))+(Delta(i,k,x(8)).*x(9))+(Delta(i,k,x(10)).*x(12))+(Delta(i,k,x(11)).*x(12))+-(Delta(i,k,x(13)).*x(15))+(Delta(i,k,x(14)).*x(15))+-(Delta(i,k,x(16)).*x(18))+(Delta(i,k,x(17)).*x(18))+-(Delta(i,k,x(19)).*x(21))+(Delta(i,k,x(20)).*x(21))+-(Delta(i,k,x(22)).*x(24))+(Delta(i,k,x(23)).*x(24));
VegaAllPrices=-(Vega(i,k,x(1)).*x(3))+(Vega(i,k,x(2)).*x(3))+-(Vega(i,k,x(4)).*x(6))+(Vega(i,k,x(5)).*x(6))+(Vega(i,k,x(7)).*x(9))+(Vega(i,k,x(8)).*x(9))+(Vega(i,k,x(10)).*x(12))+(Vega(i,k,x(11)).*x(12))+-(Vega(i,k,x(13)).*x(15))+(Vega(i,k,x(14)).*x(15))+-(Vega(i,k,x(16)).*x(18))+(Vega(i,k,x(17)).*x(18))+-(Vega(i,k,x(19)).*x(21))+(Vega(i,k,x(20)).*x(21))+-(Vega(i,k,x(22)).*x(24))+(Vega(i,k,x(23)).*x(24));
GammaAllPrices=-(Gamma(i,k,x(1)).*x(3))+(Gamma(i,k,x(2)).*x(3))+-(Gamma(i,k,x(4)).*x(6))+(Gamma(i,k,x(5)).*x(6))+(Gamma(i,k,x(7)).*x(9))+(Gamma(i,k,x(8)).*x(9))+(Gamma(i,k,x(10)).*x(12))+(Gamma(i,k,x(11)).*x(12))+-(Gamma(i,k,x(13)).*x(15))+(Gamma(i,k,x(14)).*x(15))+-(Gamma(i,k,x(16)).*x(18))+(Gamma(i,k,x(17)).*x(18))+-(Gamma(i,k,x(19)).*x(21))+(Gamma(i,k,x(20)).*x(21))+-(Gamma(i,k,x(22)).*x(24))+(Gamma(i,k,x(23)).*x(24));
%
ECIV = DVIVPriceTime(i,k).*((Da.* (5./7)).^0.5);
%
EPC = CurrentPriceRange(i).*(exp(IVPriceTime(i,k).*((Da./365).^0.5))-1);
%
DeltaEffect(i,k)=(-abs(DeltaAllPrices).*EPC).*PercentageRange(i); %Multiply by some factor - probability weighted
%
GammaEffect(i,k) = (GammaAllPrices.*(EPC.^ 2)./2).*PercentageRange(i);
%
VegaEffect(i,k) = (-abs(VegaAllPrices).* ECIV).*PercentageRange(i);
%
ThetaEffect(i,k) = (ThetaAllPrices.*Da).*PercentageRange(i);
%
% DeltaEffect=-abs(DeltaAllPrices).*EPC;
%
% GammaEffect = GammaAllPrices.*(EPC.^ 2)./2;
%
% VegaEffect = -abs(VegaAllPrices).* ECIV;
%
% ThetaEffect = ThetaAllPrices.*Da;
%
% f=-(DeltaEffect+GammaEffect+VegaEffect+ThetaEffect);
end
end
% f=-(sum(DeltaEffect(:))+sum(GammaEffect(:))+sum(VegaEffect(:))+sum(ThetaEffect(:)));
%
end
Réponses (0)
Catégories
En savoir plus sur Genetic Algorithm dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!