How do I solve this maximisation with two unknown variables?
显示 更早的评论
Hi,
I am not sure about how to solve the following maximisation problem in Matlab. Is there a built in function that can solve this? I have attached the maximisation problem as a picture. I have tried to answer this for days now and have not found a solution yet. Here is the code I made. It works until I get to confidence 1 - this is where I try to solve the max-problem using fmincon. The two unknown variables are w_a and w_b. The thing is that in the equations for the two Sigma-hats in the document, w_a and w_b are present. Here is what I have tried to do so far:
classdef Modelusikkerhedmk < handle
properties (Access=private)
Prisermk;
FVmk;
WMUmk1;
AfkastMUmk1;
StdMUmk1;
SRMUmk1;
end
methods (Access=public)
%%Modelusikkerhed med kortsalg tilladt
function MUsikkerhedmk(obj, filename,ark,antal,interval)
format shortg
[obj.Prisermk,~,~] = xlsread(filename,ark);
disp('Historiske priser:')
disp(obj.Prisermk)
for i10=2:(size(obj.Prisermk,2));
for i20 = 1:(size(obj.Prisermk,1)-1);
obj.FVmk(i20,i10-1)=(obj.Prisermk(i20+1,i10)-obj.Prisermk(i20,i10))/obj.Prisermk(i20,i10);
end
end
disp('Forventede afkast alle')
disp(obj.FVmk);
Afkast=obj.FVmk;
seed1=500;
rng(seed1);
n=1000;
E=rand(n,1);
E;
Factor = repmat(E,1,10);
disp(Factor);
mu1=mean(Afkast);
mu2=mean(Factor);
Sigma1=cov(Afkast,Afkast)
Sigmainv1=inv(Sigma1);
Korr=corrcoef(Afkast);
Sigma2=cov(Factor,Factor);
Sigmainv1=inv(Sigma2);
Sigma3=cov(Afkast,Factor);
Sigmainv1=inv(Sigma3);
Sigma4=cov(Factor,Afkast);
Sigmainv1=inv(Sigma4);
Gamma=1;
N=(size(obj.Prisermk,2)-1);
T=interval-N;
disp(T);
disp(N);
%%Her starts confidence 1
disp('konfidens 1')
konfidens=1;
Ambi1=0;
Epsilon1=Ambi1*((T-1)*N)/(T*(T-N));
Ambi2=0;
Epsilon2=Ambi2*((T-1)*N)/(T*(T-N));
Sigmahat1=1+(2/Gamma)*(sqrt(Epsilon1)/sqrt(w(1)*Sigma1*w(1)));
Sigmahat2=1+(2/Gamma)*(sqrt(Epsilon2)/sqrt(w(2)*Sigma2*w(2)));
options = optimoptions('fmincon','display','off');
w0=[0.5 0.5;0.5 0.5;0.5 0.5;0.5 0.5;0.5 0.5;0.5 0.5;0.5 0.5;0.5 0.5;0.5 0.5;0.5 0.5];
lb1=[0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0];
ub1=[1 1,1 1,1 1,1 1,1 1,1 1,1 1,1 1,1 1,1 1];
Aeq=[1 1,1 1,1 1,1 1,1 1,1 1,1 1,1 1,1 1,1 1];
Sigmahat1=1+(2/Gamma)*(sqrt(Epsilon1)/sqrt(w(1)*Sigma1*w(1)));
Sigmahat2=1+(2/Gamma)*(sqrt(Epsilon2)/sqrt(w(2)*Sigma2*w(2)));
obj.WMUmk1(:,1)=fmincon(@(w)-1*(w(1)'*mu1+w(2)'*mu2-(Gamma/2)*(w(1)'*Sigmahat1*w(1)-w(1)'*Sigma3*w(2)-w(2)'*Sigma4*w(1)-w(2)'*Sigmahat2*w(2))),w0,[],[],Aeq,1,lb1,ub1,[],options);
obj.AfkastMUmk1(:,1)=obj.WMUmk1(:,1)'*obj.FVmk(interval+1,:)';
obj.StdMUmk1(:,1)=sqrt(obj.WMUmk1(:,1)'*Sigma*obj.WMUmk1(:,1));
obj.SRMUmk1(:,1)=obj.AfkastMUmk1(:,1)/obj.StdMUmk1(:,1);
disp(obj.WMUmk1)
disp(obj.WMUmk1)
disp(obj.WMUmk1)
disp(obj.WMUmk1)
%%Løkken starter her
disp('Udregner stadig!')
N=antal-2-interval;
for i3=1:N
for i4=2:(size(obj.Prisermk,2));
for i5=i3:(interval+i3)
Afkastmk(i5,i4-1)=(obj.Prisermk(i5+1,i4)-obj.Prisermk(i5,i4))/obj.Prisermk(i5,i4);
end
end
Afkastmk(1:i3,:)=[];
mu1=mean(Afkastmk);
mu2=mean(Factor)
Sigma1=cov(Afkastmk,Afkastmk);
Sigmainv1=inv(Sigma1);
Korr=corrcoef(Afkastmk);
Sigma2=cov(Factor,Factor);
Sigmainv1=inv(Sigma2);
Sigma3=cov(Afkastmk,Factor);
Sigmainv1=inv(Sigma3);
Sigma4=cov(Factor,Afkastmk);
Sigmainv1=inv(Sigma4);
Gamma=1;
N=(size(obj.Prisermk,2)-1);
T=interval-N;
%%Her starter konfidens 1
konfidens=1;
Ambi1=0;
Epsilon1=Ambi1*((T-1)*N)/(T*(T-N));
Ambi2=0;
Epsilon2=Ambi2*((T-1)*N)/(T*(T-N));
g(1)=mu1-Gamma*Sigma3*obj.vaegt2;
h(1)=mu2-Gamma*Sigma4*obj.vaegt1;
Sigmahat1=1+(2/Gamma)*(sqrt(Epsilon1)/sqrt(w(1)*Sigma1*w(1)));
Sigmahat2=1+(2/Gamma)*(sqrt(Epsilon2)/sqrt(w(2)*Sigma2*w(2)));
options = optimoptions('fmincon','display','off');
w0=[0.5 0.5;0.5 0.5;0.5 0.5;0.5 0.5;0.5 0.5;0.5 0.5;0.5 0.5;0.5 0.5;0.5 0.5;0.5 0.5];
lb1=[0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0];
ub1=[1 1,1 1,1 1,1 1,1 1,1 1,1 1,1 1,1 1,1 1];
Aeq=[1 1,1 1,1 1,1 1,1 1,1 1,1 1,1 1,1 1,1 1];
obj.WMUmk1(:,i3+1)=fmincon(@(w)-1*(w(1)'*mu1+w(2)'*mu2-(Gamma/2)*(w(1)'*Sigmahat1*w(1)-w(1)'*Sigma3*w(2)-w(2)'*Sigma4*w(1)-w(2)'*Sigmahat2*w(2))),w0,[],[],Aeq,1,lb1,ub1,[],options);
obj.AfkastMUmk1(:,i3+1)=obj.WMUmk1(:,i3+1)'*obj.FVmk(interval+i3+1,:)';
obj.StdMUmk1(:,i3+1)=sqrt(obj.WMUmk1(:,i3+1)'*Sigma*obj.WMUmk1(:,i3+1));
obj.SRMUmk1(:,i3+1)=obj.AfkastMUmk1(:,i3+1)/obj.StdMUmk1(:,i3+1);
end
disp('Konfi 1')
mean(obj.AfkastMUmk1)
mean(obj.StdMUmk1)
mean(obj.SRMUmk1)
end
end
end
In the Excel file, I have 10 columns and 1000 rows containing different prices of assets.
Thanks,
Amer
3 个评论
John D'Errico
2017-7-16
编辑:per isakson
2017-8-11
How to do this?
1. You learn how to use the optimization tools in MATLAB.
2. You write this mess as a function of the parameter vector that can be called.
WTP?
Is it that your two unknown variables are separate variables in that expression? Surely it is possible to combine TWO variables into one vector, then inside your objective function, split the vector apart, then using it in any way you need? The very first lines of your m-file should split the passed in parameter vector apart.
If you want more help than this, you need to be far more forthcoming about your problem. What are knowns? Unknowns? What size and shape are they? What toolboxes do you have? The optimization toolbox is a good start, or the global optimization toolbox.
But if you just expect someone to take that picture and provide code to you with no effort made on your part, I'm not sure you should expect an answer soon.
Amer Skaljic
2017-7-16
编辑:Amer Skaljic
2017-7-16
Prashant Arora
2017-7-18
编辑:Prashant Arora
2017-7-18
Hi Amer,
I can see that you have defined w0 as a matrix. To solve the maximization problem, you'll need to create a linear array containing [w_a w_b] (row vectors) or [w_a;w_b] (column vectors). Then index accordingly in your objective function. For example if w_a = 5X1 and w_b = 6X1. In your objective function use w(1:5) for w_a and w(6:11) for w_b. Hope this helps!
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!