fmincon nonlcon中的错误必须是一个函数

时间:2018-10-08 19:04:59

标签: matlab optimization constraints

使用fmincon时出现以下错误:

NONLCON must be a function.
Error in fmincon (line 409)
confcn = optimfcnchk(NONLCON,'fmincon',length(varargin),funValCheck,flags.gradconst,false,true);

这是我正在使用的代码:

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);  

这是我正在使用的nonlcon函数:

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

我不确定我做错了什么!谢谢!

0 个答案:

没有答案