我为MatLab编写了以下代码,但到目前为止,它显然仅能返回一行参数。该代码由主文件,功能文件(MLE_GARCH)和约束文件(nonlcon_GARCH)组成,如下所示:
clc
clear
%% Setup Data %%
% Input the price levels and dates.
Data = readtable('HSI.xlsx','Sheet','HSI Index');
Prices = Data.PX_LAST;
Dates = Data.Dates;
% Continuous risk free rate.
dt = 1/252; % Daily time steps
rfd = [0.01 0.02 0.03]*dt; % Duan risk-free rate
rfbs = rfd/dt; %BS risk-free rate
% Define the returns (pad first return with risk-free rate).
r = [rfd; repmat(diff(log(Prices)),1,3)];
residuals = r - mean(r);
size = length(r);
%% Duan (1995) Pricing Model Estimation %%
% Starting values for constrained optimization.
start = [0.1 0.1 0.1 0.1];
A = [0 1 1 0];
b = 1;
lb = [eps 0 0 -Inf]; % Lower bound for parameters
ub = [Inf Inf Inf Inf]; % Upper bound for parameters
[params,fval,exitflag,output,llambda,grad,hessian] = fmincon(@(p) ...
MLE_GARCH(p, Prices, rfd), start, A, b, [], [], lb, ub, @(c) Nonlcon_GARCH(c));
alpha0 = params(1);
alpha1 = params(2);
beta1 = params(3);
lambda = params(4);
上面的这是主文件。
function y = MLE_GARCH(params,S,rf)
% Finds log-likelihood for the GARCH option pricing model.
alpha0 = params(1);
alpha1 = params(2);
beta1 = params(3);
lambda = params(4);
N = length(S);
% Define the returns (pad first return with risk-free rate)
r(1,:) = [rf; price2ret(S)];
% Infer the other conditional instantaneous variances
h(1) = alpha0/(1 - alpha1 - beta1);
% Initialize the conditional variance recursion
for i=2:N
h(i) = alpha0 + alpha1*(r(i-1) - rf - lambda*sqrt(h(i-1)) + ...
0.5*h(i-1))^2 + beta1*h(i-1);
end
% Initialize the log-likelihood recursion
for i=1:N
LL(i) = - 0.5*log(h(i)) - 0.5/h(i)*(r(i) - rf - lambda*sqrt(h(i)) + ...
0.5*h(i))^2;
end
% Return the negative log-likelihood function
y = -sum(LL);
另一个是功能文件。
function [c, ceq] = Nonlcon_GARCH(params)
alpha0 = params(1);
alpha1 = params(2);
beta1 = params(3);
lambda = params(4);
% Set inequality constraints for parameters estimation.
c = alpha1*(1 + lambda^2) + beta1 - 1 - eps;
ceq = [];
最后这是fmincon的约束。我已经尝试更改主文件中的某些内容,并且在优化部分之前一切都很好。但是,当我开始更改功能文件时,fmincon无法运行,它告诉我“ A必须具有16列”。修改后的功能文件如下:
function y = MLE_GARCH_(params,S,rf)
% Finds log-likelihood for the GARCH option pricing model.
alpha0 = params(1,:);
alpha1 = params(2,:);
beta1 = params(3,:);
lambda = params(4,:);
N = length(S);
% Define the returns (pad first return with risk-free rate)
r = [rf; repmat(price2ret(S),1,3)];
% Infer the other conditional instantaneous variances
h(1,:) = alpha0./(1 - alpha1 - beta1);
% Initialize the conditional variance recursion
for i=2:N
h(i,:) = alpha0 + alpha1.*(r(i-1,:) - rf(:) - lambda.*sqrt(h(i-1,:)) + ...
0.5*h(i-1,:)).^2 + beta1.*h(i-1,:);
end
% Initialize the log-likelihood recursion
for i=1:N
LL(i,:) = - 0.5.*log(h(i,:)) - 0.5./h(i,:).*(r(i,:) - rf(:) - lambda.*sqrt(h(i,:)) + ...
0.5.*h(i,:))^2;
end
% Return the negative log-likelihood function
y = -sum(LL);
鉴于我的原始代码运行没有问题,如何更改它以使其返回3x4参数矩阵?
这是一个很长的问题,有很多代码无法复制,因为它在HDD上使用xls文件。您可能想为我们发布一个最低可行的示例(MVE)?
无论如何,简短的答案是:no。答案很长:为什么有人要这样做?更改从12x1向量构建3x4矩阵的成本函数,您就可以完成。