我想最大化GARCH模型的对数似然函数,我在MATLAB中使用fminsearch
来最小化负对数似然函数,此问题有两个约束。我尝试使用fmin
或minimize
函数(来自scipy),但我认为转换约束的方法不正确。
以下是我在python 3.6中的尝试:
def computeNegLLH(parameters):
sigma2 = np.zeros(len(returns));
omega = parameters[0];
alpha = parameters[1];
beta = parameters[2];
sigma2[1] = parameters[3];
# parameters restrictions: omega>0; alpha,beta>=0; alpha + beta >= 1
if (omega<=0 or min(alpha,beta)<0 or alpha + beta >= 1 or sigma2[0]<=0):
negLLH = sys.maxsize;
sys.exit();
# calculate squared returns
returns2 = returns**2;
for t in range(1,len(returns)):
sigma2[t]= omega + alpha*returns2[t-1] + beta*sigma2[t-1];
# calculate (-1)xLogLikelihood for a GARCH(1,1) process with parameters
negLLH = 0.5*sum(log(sigma2) + returns2/sigma2);
return negLLH
result = minimize(fun = computeNegLLH, x0 = startingValues, method = 'Nelder-
Mead', options = {'xtol': 1e-8})
我的MATLAB代码:
function param = calibrate_GARCH(returns, startingValues)
% computes maximum likelhood estimates for a GARCH(1,1) process give
% timeseries "returns" using fminsearch function
options = optimset('TolX',1e-12,'MaxFunEvals',3000,'Maxiter',3000,
'Display','off','LargeScale','off');
param = fminsearch(@(param)computeNegLLH(param,returns), startingValues,
options);
end
function negLLH = computeNegLLH(param,returns)
% param for the given timeseries returns
sigma2 = zeros(size(returns));
omega = param(1);
alpha = param(2);
beta = param(3);
sigma2(1) = param(4);
% parameter restrictions: omega>0 alpha,beta>=0
% ensure that GARCH(1,1) process is covariance-stationary
if omega<=0 || min(alpha,beta)<0 || alpha + beta >= 1 || sigma2(1)<=0;
negLLH = intmax;
return;
end
% calculate squared returns
returns2 = returns.^2;
for t=2:length(returns)
sigma2(t)= omega + alpha*returns2(t-1) + beta*sigma2(t-1);
end
% computes (-1)xLogLikelihood for a GARCH(1,1) process with parameters
negLLH = 0.5*sum(log(sigma2) + returns2./sigma2);
end