fmincon error nonlcon must be a function

20 views (last 30 days)
I get the following error when using fmincon:
NONLCON must be a function.
Error in fmincon (line 409)
confcn = optimfcnchk(NONLCON,'fmincon',length(varargin),funValCheck,flags.gradconst,false,true);
This is the code I am using:
nAssets = size(ExpCovariance,2);
%Constraints and data for fmincon
options = optimoptions(@fmincon,'Algorithm','interior-point');
nFrontierPts = 20;
pWts = zeros(nFrontierPts, nAssets);
pExitFlag = zeros(nFrontierPts, 1);
StaticFXhedgeReturns = AssetReturns(t-59:t,1)+AssetReturns(t-59:t,2:7)*exp1';
StaticVol5y = std(StaticFXhedgeReturns)*sqrt(12);
StaticTE5y = std(AssetReturns(t-59:t,2:7)*exp1')*sqrt(12);
teCap=StaticTE5y;
covMat=cov(equRet(t-2-59:t-2),AssetReturns(t-59:t,2:7)*exp1')*12; %Covariance of exposure resulting from country specific hedging with equities
covCap=covMat(1,2);
iniWt = ones(1,nAssets).*(1/nAssets);
Aud6040ret = AssetReturns(t-x-59:t-x,1);
FXret_1 = AssetReturns(t-x-59:t-x,2:7);
equRet_1 = equRet(t-59-2:t-2);
%min variance portfolio
minVarWts = fmincon(@(pWts) -pWts*ExpCovariance'*pWts', iniWt, [], [], [], [],...
LowerBound', UpperBound',nonlconFr(pWts,StaticVol5y,teCap,Aud6040ret,FXret_1,equRet_1,covCap), options);
and this is the nonlcon function I am using:
function [c, ceq] = nonlconFr(w,volcap,teCap,Aud6040ret,FXret,equRet,covCap)
% nonlcon1: add TE constraint
% nonlcon2: add equity beta/correlation constraint
w = w(2:7)'; %Need extra line for frontier code
ret_w = Aud6040ret+FXret*w;
covMat=cov(equRet,FXret*w)*12;
%Inequality Constraints (Less than zero)
c(1) = std(ret_w)*sqrt(12) - volcap; %Vol to be less than 60/40 with country specific hedging strategy
c(2) = std(FXret*w)*sqrt(12) - teCap; %Tracking error to exogenous 60/40 asset allocation constraint
c(3) = covMat(1,2) - covCap; %Covariance of FX overlay to be less than covariance of FX exposure resulting from benchmark hedging strategy
%Equality Constraint
ceq = [];
end
I'm not sure what I've done wrong! Thanks!

Accepted Answer

Walter Roberson
Walter Roberson on 9 Oct 2018
In your fmincon call start that parameter with @(pWts)
The @(pWts) you have for the first parameter to fmincon only applies to the first parameter.

More Answers (0)

Categories

Find more on Genomics and Next Generation Sequencing in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!