Matlab:fmincon抛出错误

时间:2014-11-25 08:09:52

标签: matlab optimization

我正在实现图像中给出的表达式,它是AR(p)模型的对数似然。 Log likelihood

在这种情况下,p=2。我使用fmincon作为优化工具。我在互联网上查看了有关此命令语法的文档和其他示例。不过,我无法缓解这个问题。有人可以帮助消除这个问题吗?

以下是错误

Warning: Options LargeScale = 'off' and Algorithm = 'trust-region-reflective' conflict.
Ignoring Algorithm and running active-set algorithm. To run trust-region-reflective, set
LargeScale = 'on'. To run active-set without this warning, use Algorithm = 'active-set'. 
> In fmincon at 456
  In MLE_AR2 at 20 
Error using ll_AR2 (line 6)
Not enough input arguments.

Error in fmincon (line 601)
      initVals.f = feval(funfcn{3},X,varargin{:});

Error in MLE_AR2 (line 20)
   [theta_hat,likelihood] =
   fmincon(@ll_AR2,theta0,[],[],[],[],low_theta,up_theta,[],opts);

Caused by:
    Failure in initial user-supplied objective function evaluation. FMINCON cannot
    continue.

未知参数的矢量,

theta_hat = [c, theta0, theta1, theta2] where c = intercept in the original model which is zero ; theta0 = phi1 = 0.195 ; theta1 = -0.95; theta2 = variance of the noise sigma2_epsilon.

代码:

     clc
    clear all
    global ERS
    var_eps = 1;
    epsilon = sqrt(var_eps)*randn(5000,1); % Gaussian signal exciting the AR model
    theta0 = ones(4,1);  %Initial values of the parameters
    low_theta = zeros(4,1); %Lower bound of the parameters
    up_theta = 100*ones(4,1); %upper bound of the parameters
    opts=optimset('DerivativeCheck','off','Display','off','TolX',1e-6,'TolFun',1e-6,...
   'Diagnostics','off','MaxIter', 200, 'LargeScale','off');
    ERS(1) = 0.0;
    ERS(2) = 0.0;
    for t= 3:5000
    ERS(t)=  0.1950*ERS(t-1) -0.9500*ERS(t-2)+ epsilon(t); %AR(2) model y
    end


   [theta_hat,likelihood,exit1] = fmincon(@ll_AR2,theta0,[],[],[],[],low_theta,up_theta,[],opts);

exit(1,1)=exit1;

format long;disp(num2str([theta_hat],5))


function L = ll_AR2(theta,Y)
rho0 = theta(1); %c
rho1 = theta(2); %phi1
rho2 = theta(3); %phi2
sigma2_epsilon = theta(4);
T= size(Y,1);
p=2;
mu_p = rho0./(1-rho1-rho2); %mean of Y for the first p samples
%changed sign of the log likelihood expression    
cov_p = xcov(Y);
L1 = (Y(3:end) - rho0 - rho1.*Y(1:end-1) - rho2.*Y(1:end-2)).^2;
 L = (p/2).*(log(2*pi)) + (p/2).*log(sigma2_epsilon) - 0.5*log(det(inv(cov_p))) + 0.5*(sigma2_epsilon^-1).*(Y(p) - mu_p)'.*inv(cov_p).*(Y(p) - mu_p)+...
     (T-p).*0.5*log(2*pi) + 0.5*(T-p).*log(sigma2_epsilon) + 0.5*(sigma2_epsilon^-1).*L1;
L = sum(L);
end

1 个答案:

答案 0 :(得分:1)

除了优化变量(Y)之外,您还试图将常量参数传递给目标函数(theta)。
正确的方法是使用匿名函数:

 Y = ...; %// define your parameter here
 fmincon( @(theta) ll_AR2(theta, Y), theta0, [],[],[],[],low_theta,up_theta,[],opts);

现在,就fmincon问题而言,目标函数仅取决于theta

有关详细信息,请参阅anonymous functionspassing const parameters