📄 garchset.m
字号:
end
%
% Check LLF distribution for 'Gaussian' & set defaults.
% Currently, a 'Gaussian' distribution is the only distribution
% allowed. The field is kept in as a visual reminder.
%
options.Distribution = options.Distribution(~isspace(options.Distribution));
if ~isempty(options.Distribution)
if ~strcmpi(options.Distribution , 'GAUSSIAN')
error(' Distribution of innovations must be ''Gaussian''.')
end
else
options.Distribution = 'Gaussian';
end
options.Distribution(1) = upper(options.Distribution(1));
%
% Check Boolean equality constraints.
%
if ~isempty(options.FixC)
if isempty(options.C)
error(' Mean model constant ''C'' must be specified along with ''FixC''.');
end
if (prod(size(options.FixC)) ~= 1) | ((sum((options.FixC == 0) + (options.FixC == 1))) ~= 1)
error(' ''FixC'' must be a Boolean (0,1) scalar.');
end
end
if ~isempty(options.FixK)
if isempty(options.K)
error(' Variance model constant ''K'' must be specified along with ''FixK''.');
end
if (prod(size(options.FixK)) ~= 1) | ((sum((options.FixK == 0) + (options.FixK == 1))) ~= 1)
error(' ''FixK'' must be a Boolean (0,1) scalar.');
end
end
%
% Check the equality constraints.
%
if ~isempty(options.FixAR)
if isempty(options.AR)
error(' ''AR'' coefficients must be specified along with ''FixAR''.');
end
if (prod(size(options.FixAR)) ~= length(options.FixAR)) | ...
((sum((options.FixAR == 0) + (options.FixAR == 1))) ~= length(options.FixAR))
error(' ''FixAR'' must be a Boolean (0,1) vector.');
end
if ~isempty(options.R) & (options.R ~= length(options.FixAR))
error(' Length of Boolean ''FixAR'' vector must equal model order ''R''.');
else
options.R = length(options.FixAR);
end
options.FixAR = options.FixAR(:)';
end
if ~isempty(options.FixMA)
if isempty(options.MA)
error(' ''MA'' coefficients must be specified along with ''FixMA''.');
end
if (prod(size(options.FixMA)) ~= length(options.FixMA)) | ...
((sum((options.FixMA == 0) + (options.FixMA == 1))) ~= length(options.FixMA))
error(' ''FixMA'' must be a Boolean (0,1) vector.');
end
if ~isempty(options.M) & (options.M ~= length(options.FixMA))
error(' Length of Boolean ''FixMA'' vector must equal model order ''M''.');
else
options.M = length(options.FixMA);
end
options.FixMA = options.FixMA(:)';
end
if ~isempty(options.FixARCH)
if isempty(options.ARCH)
error(' ''ARCH'' coefficients must be specified along with ''FixARCH''.');
end
if (prod(size(options.FixARCH)) ~= length(options.FixARCH)) | ...
((sum((options.FixARCH == 0) + (options.FixARCH == 1))) ~= length(options.FixARCH))
error(' ''FixARCH'' must be a Boolean (0,1) vector.');
end
if ~isempty(options.Q) & (options.Q ~= length(options.FixARCH))
error(' Length of Boolean ''FixARCH'' vector must equal model order ''Q''.');
else
options.Q = length(options.FixARCH);
end
options.FixARCH = options.FixARCH(:)';
end
if ~isempty(options.FixGARCH)
if isempty(options.GARCH)
error(' ''GARCH'' coefficients must be specified along with ''FixGARCH''.');
end
if (prod(size(options.FixGARCH)) ~= length(options.FixGARCH)) | ...
((sum((options.FixGARCH == 0) + (options.FixGARCH == 1))) ~= length(options.FixGARCH))
error(' ''FixGARCH'' must be a Boolean (0,1) vector.');
end
if ~isempty(options.P) & (options.P ~= length(options.FixGARCH))
error(' Length of Boolean ''FixGARCH'' vector must equal model order ''P''.');
else
options.P = length(options.FixGARCH);
end
options.FixGARCH = options.FixGARCH(:)';
end
if ~isempty(options.Regress)
if prod(size(options.Regress)) ~= length(options.Regress)
error(' Regression coefficients ''Regress'' must be a vector.');
end
options.Regress = options.Regress(:)';
end
if ~isempty(options.FixRegress)
if isempty(options.Regress)
error(' ''Regress'' coefficients must be specified along with ''FixRegress''.');
end
if (prod(size(options.FixRegress)) ~= length(options.FixRegress)) | ...
((sum((options.FixRegress == 0) + (options.FixRegress == 1))) ~= length(options.FixRegress))
error(' ''FixRegress'' must be a Boolean (0,1) vector.');
end
options.FixRegress = options.FixRegress(:)';
end
if ~isempty(options.Regress) & ~isempty(options.FixRegress) & ...
(length(options.Regress) ~= length(options.FixRegress))
error(' Vectors ''Regress'' and ''FixRegress'' must be the same length.');
end
%
% Set model orders of conditional mean & variance if unspecified.
%
if isempty(options.R) , options.R = 0; end
if isempty(options.M) , options.M = 0; end
if isempty(options.P) , options.P = 0; end
if isempty(options.Q) , options.Q = 0; end
%
% Prevent GARCH(P>0,Q=0) models. It's OK for both P = Q = 0, but
% it makes no sense to have P > 0 when Q = 0.
%
if (options.P > 0) & (options.Q == 0)
error(' When model order ''Q'' is 0, order ''P'' must also be 0.');
end
%
% Construct the summary comment from the model orders if unspecified OR if the
% summary comment string was originally constructed from the input specification
% automatically. The comment is assumed to have been constructed automatically
% when the presence of 2 NULLs is found buried in the string. This feature is
% here so that the comment, when inferred from the input specification, is
% updated when the mean or variance model orders are updated.
%
if isempty(options.Comment) | (length(find(options.Comment == char(0))) == 2)
meanString = ['ARMAX(' num2str(options.R) ',' num2str(options.M) ',?)'];
varianceString = ['GARCH(' num2str(options.P) ',' num2str(options.Q) ')'];
options.Comment = ['Mean:' char(0) meanString '; Variance:' char(0) varianceString];
end
%
% Check the parameters associated with FMINCON of the Optimization Toolbox.
%
if ~isempty(options.MaxFunEvals)
if prod(size(options.MaxFunEvals)) > 1
error(' ''MaxFunEvals'' must be a scalar.');
end
if (round(options.MaxFunEvals) ~= options.MaxFunEvals) | (options.MaxFunEvals <= 0)
error(' ''MaxFunEvals'' must be a positive integer.');
end
end
if ~isempty(options.MaxIter)
if prod(size(options.MaxIter)) > 1
error(' ''MaxIter'' must be a scalar.');
end
if (round(options.MaxIter) ~= options.MaxIter) | (options.MaxIter <= 0)
error(' ''MaxIter'' must be a positive integer.');
end
end
if ~isempty(options.TolCon)
if (prod(size(options.TolCon)) > 1) | (options.TolCon <= 0)
error(' ''TolCon'' must be a positive scalar.');
end
end
if ~isempty(options.TolFun)
if (prod(size(options.TolFun)) > 1) | (options.TolFun <= 0)
error(' ''TolFun'' must be a positive scalar.');
end
end
if ~isempty(options.TolX)
if (prod(size(options.TolX)) > 1) | (options.TolX <= 0)
error(' ''TolX'' must be a positive scalar.');
end
end
%
% Get optimization fields:
%
% Allow for the case when the first input is an existing OPTIONS structure
% to update. In this case, retain the sub-structure parameters associated
% with the optmizations function FMINCON.
%
Optimization = optimset('fmincon');
Optimization = optimset(Optimization , argSave);
if ~isempty(options.MaxFunEvals)
Optimization = optimset(Optimization , 'MaxFunEvals' , options.MaxFunEvals);
end
if ~isempty(options.MaxIter)
Optimization = optimset(Optimization , 'MaxIter' , options.MaxIter);
end
if ~isempty(options.TolFun)
Optimization = optimset(Optimization , 'TolFun' , options.TolFun);
end
if ~isempty(options.TolCon)
Optimization = optimset(Optimization , 'TolCon' , options.TolCon);
end
if ~isempty(options.TolX)
Optimization = optimset(Optimization , 'TolX' , options.TolX);
end
Optimization = optimset(Optimization , 'LargeScale' , 'off');
options.Display = lower(options.Display(~isspace(options.Display)));
%
% At this point, if the first input is an existing OPTIONS structure, then
% 'argSave' contains its 'Optimization' sub-structure. If the input argument
% list was strictly parameter/value pairs (i.e., no existing OPTIONS structure
% to update), then argSave = [].
%
if ~isempty(options.Display)
if strcmp(options.Display , 'off')
Optimization = optimset(Optimization , 'Display' , 'off');
Optimization = optimset(Optimization , 'Diagnostics' , 'off');
else
Optimization = optimset(Optimization , 'Display' , 'iter');
Optimization = optimset(Optimization , 'Diagnostics' , 'on');
end
else
if isempty(argSave)
Optimization = optimset(Optimization , 'Display' , 'iter');
Optimization = optimset(Optimization , 'Diagnostics' , 'on');
end
end
options.Optimization = Optimization;
%
% The following segment just removes the fields related to the FMINCON
% optimization function from the top level, since they are now in the
% nested structure 'Optimization'. RMFIELD could have been used, but
% is substantially slower than direct assignment.
%
output.Comment = options.Comment;
output.R = options.R;
output.M = options.M;
output.P = options.P;
output.Q = options.Q;
output.Distribution = options.Distribution;
output.C = options.C;
output.AR = options.AR;
output.MA = options.MA;
output.Regress = options.Regress;
output.K = options.K;
output.GARCH = options.GARCH;
output.ARCH = options.ARCH;
output.FixC = options.FixC;
output.FixAR = options.FixAR;
output.FixMA = options.FixMA;
output.FixRegress = options.FixRegress;
output.FixK = options.FixK;
output.FixGARCH = options.FixGARCH;
output.FixARCH = options.FixARCH;
output.Optimization = options.Optimization;
options = output;
function errorCode = errorCheck(scalar , vector , codes)
%ERRORCHECK Check parameter values and sizes of paired scalars/vectors.
% Given a scalar and a corresponding vector input, this function implements
% a library of error checks. The checks involve testing the scalar in
% isolation, testing the vector in isolation, and testing for consistency
% between them. Either SCALAR or VECTOR may be empty ([]).
%
% errorCode = errorCheck(scalar , vector , codes)
%
%Inputs:
% scalar - A scalar input; for example, an integer model order number or a
% continuous parameter coefficient.
%
% vector - A vector input; for example, a vector parameter coefficients or
% flags.
%
% codes - A vector of error codes of interest.
%
%Output:
% errorCode - The error codes specified in CODES that were found to exist.
%
% Several tests are performed on the SCALAR and VECTOR, and the function
% tests against a library of conditions. Once the library of checks is made,
% the actual errors of interest in CODES are retained if the error condition
% occurred. For example, if CODES = [1 4 5], and error conditions 1 and 5
% are found, the ERRORCODE = [1 5].
%
% The current library of error codes is:
% 1 = SCALAR is not an integer
% 2 = SCALAR is negative
% 3 = SCALAR is not positive
% 4 = SCALAR is not a scalar
% 5 = VECTOR is not a vector (i.e., is a matrix)
% 6 = VECTOR of polynomial coefficients is non-stationary
% 7 = VECTOR has negative elements
% 8 = VECTOR of polynomial coefficients is non-stationary (GARCH sum)
% 9 = SCALAR/VECTOR dimension inconsistency
%
errorCode = zeros(1,9); % Initialize codes to no error condition.
codes = codes(:)';
n = [~isempty(scalar) ~isempty(vector)];
if any(n)
%
% Check the scalar input.
%
if n(1)
if any(round(scalar) ~= scalar) % non-integer
errorCode(1) = 1;
end
if any(scalar < 0) % negativity
errorCode(2) = 2;
end
if any(scalar <= 0) % non-positivity
errorCode(3) = 3;
end
if prod(size(scalar)) ~= 1 % non-scalar
errorCode(4) = 4;
end
end
%
% Check the vector input.
%
if n(2)
V = vector(:);
if prod(size(vector)) ~= length(vector) % non-vector
errorCode(5) = 5;
end
if any(abs(roots([1 ; V])) >= 1) % non-stationary
errorCode(6) = 6;
end
if any(vector < 0) % negativity
errorCode(7) = 7;
end
if sum(vector) >= 1 % non-stationary
errorCode(8) = 8;
end
end
%
% Check for consistency between the scalar and vector inputs.
%
if all(n)
if scalar ~= length(vector) % dimension inconsistency
errorCode(9) = 9;
end
end
end
%
% Retain only the errors we actually care about (i.e., the 'real' errors!).
%
errorCode = errorCode(find(errorCode));
if ~isempty(errorCode)
[C , E] = meshgrid(codes , errorCode);
delta = C - E;
delta(delta == 0) = NaN;
errorCode = codes(isnan(sum(delta,1)));
end
%
% If no error codes of interest remain, then at least pad it with a zero
% so the SWITCH statement outside will not error out on an empty matrix [].
%
if isempty(errorCode)
errorCode = 0;
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -