rolling optimization with nonlinear constraints
1 次查看(过去 30 天)
显示 更早的评论
Hi,friends, I recently encountered one problem and tried many times without success, is there anyone who can give me some hints, thanks in adance. here is the question:
I want to solve a optimization with nonlinear constraints, the difficulty lies that I want to solve it with a rolling window, say, using the first 60 datapoints to derive a solution, and then, using the second to 61 datapoints to derive the second solution, etc.
the code is:
MyCov=cell(NofRepetitions,1);%cell array
W=cell(NofRepetitions,1);%cell array
dMean=cell(NofRepetitions,1);
EndIndx=RollingWindow;
eMean=cell(1,NofRepetitions);
for i=1:NofRepetitions;
%calculating mean of each repetition
eMean{i}=(RetSeries(i:EndIndx+i-1,:)'*P)';
%calculating the deviation of each repetition
dMean{i}=RetSeries(i:EndIndx+i-1,:)-repmat(eMean{i},RollingWindow,1);
%calculating covariance
MyCov{i}=dMean{i}'*diag(P)*dMean{i};
end
RiskTarget=0.1/sqrt(12);% SETTING RISK TARGET.......REVISABLE
RiskMultiple=ones(c,1)/c;%
%RiskMultiple=[0.2 0.2 0.1]';%suppose 3 assets here
w0=ones(c,1)/c;
options=optimset('Algorithm','interior-point','LargeScale','off');
A=ones(c,1)';
b=1;% LEVERAGE CONSTRAINT
W=fmincon(@(W)-1*RiskMultiple'*log(W),w0,A,b,[],[],zeros(c,1),[],@(W)deal(W'*MyCov*W-RiskTarget^2),[],options);
but after I run the code, it returns error message,
"Error using @(W)-1*RiskMultiple'*log(W)
Too many input arguments.
Error in fmincon (line 601)
initVals.f = feval(funfcn{3},X,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. FMINCON cannot continue."
any friend can give me some hints? thanks a lot
0 个评论
采纳的回答
Alan Weiss
2013-4-29
You seem to have a syntax error in your nonlinear constraint:
W=fmincon(@(W)-1*RiskMultiple'*log(W),w0,A,b,[],[],zeros(c,1),[],@(W)deal(W'*MyCov*W-RiskTarget^2),[],options);
I think this should be
W=fmincon(@(W)-1*RiskMultiple'*log(W),w0,A,b,[],[],zeros(c,1),[],@(W)deal(W'*MyCov*W-RiskTarget^2,[]),options);
In any case, it would be easier to read and debug if you wrote it as
nonlcon = @(W)deal(W'*MyCov*W-RiskTarget^2,[]);
But fmincon is complaining that your objective function is not well defined at the initial point. Try running
fcn = @(W)-1*RiskMultiple'*log(W)
fval = fcn(w0)
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
0 个评论
更多回答(2 个)
Peihong
2013-4-29
2 个评论
Alan Weiss
2013-4-30
Please let me know the result of the test I asked you to run previously:
fcn = @(W)-1*RiskMultiple'*log(W)
fval = fcn(w0)
Alan Weiss
MATLAB mathematical toolbox documentation
Matt J
2013-4-30
编辑:Matt J
2013-4-30
You still haven't corrected the syntax error in your call to FMINCON that Alan mentioned. Please show your call to FMINCON so that we can see what you are now doing.
It is clear from the messages that you're not calling FMINCON with the correct number/order of input arguments, because
(1) FMINCON does not see your "options" input and is therefore not using the interior-point algorithm as you specified, but rather the default trust-region alg.
(2) FMINCON still thinks you are passing it more than 10 arguments. It is therefore passing the 11th (probably your options struct) and higher argument as extra parameters to the objective function, which the objective cannot accept because it is only written to accept a single input arg 'W'. Hence the "too many input arguments" error message.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!