00001 %% @file model0.m 00002 %% @author weaves 00003 %% The first model used for GARCH systems. 00004 % 00005 % Revision 1.0, Mar-19-2010 00006 % - File created 00007 00008 % MODEL0 00009 % obj = model0(spec) 00010 % creates a container for an MA-GARCH model. 00011 % This follows the strategy pattern. 00012 % 00013 % Example: 00014 % spec = garchset('M',1,'P',1,'Q',1); 00015 % spec = garchset(spec, 'Display','off', 'Distribution', 'T'); 00016 % 00017 % model0_ = model0(spec); 00018 % 00019 % As new data arrives and a calibrate is requested. 00020 % model0_.update( collector0_ ); 00021 % disp(model0_.coeff); 00022 % 00023 00024 classdef model0 < handle 00025 properties (Constant) 00026 samples = 10; 00027 epsilon = 1e-10; 00028 UnityThreshold = 0.999; 00029 end 00030 00031 properties (SetAccess = protected, GetAccess = public) 00032 Description = 'GARCH model container'; 00033 Date = date; % When started 00034 00035 Type = 'model0'; 00036 name = 'unassigned'; 00037 00038 expiry_; % 30 sec ticks 00039 issane; 00040 logger_; 00041 logReturn_; 00042 volatility0_; 00043 end 00044 00045 properties 00046 spec; % container for the GARCH model 00047 coeff; % GARCH co-efficients 00048 returns; 00049 volatility0; 00050 logReturn; 00051 meanForecast0; 00052 InterpFactor0; 00053 maxTrend = 1e-5; % Maximum trend. 00054 00055 % An indication of the regime. 00056 volRegime = -1; 00057 00058 % Indexes into conversion vectors 00059 idxes = []; 00060 end 00061 00062 methods 00063 00064 %% GARCH model 00065 % spec_ is the result of a call to garchset(). 00066 function obj = model0(spec_) 00067 obj.spec = spec_.model; 00068 obj.maxTrend = spec_.maxTrend; 00069 obj.name = spec_.name; 00070 obj.issane = -1; 00071 obj.logger_ = settings0.instance(); 00072 end 00073 00074 function delete(obj) 00075 % delete(obj.spec); 00076 end 00077 00078 function p0_ = p0(obj, expected_, actual_, volatility0, volf) 00079 p0_ = []; 00080 if isempty(obj.coeff) 00081 return 00082 end 00083 % Estimated probability using standard cdf from probability 00084 % theory and the degrees of freedom of the t-test. 00085 at_ = (expected_ - actual_ - obj.logReturn) / ... 00086 volf; 00087 at_ = (expected_ - actual_) / ... 00088 volf; 00089 p0_ = tcdf( at_ / volatility0, obj.coeff.DoF); 00090 if ~isreal(p0_) || p0_ < 0 00091 obj.issane = 4; 00092 end 00093 end 00094 00095 function update(obj, returner) 00096 obj.issane = -1; 00097 returns = returner.returns0; 00098 if length(returns) <= obj.samples || ... 00099 rtolerance(sum(abs(returns)), 0) 00100 return; 00101 end 00102 00103 obj.returns = returns; 00104 00105 [coeff] = garchfit(obj.spec, returns); 00106 00107 if coeff.K <= 0, coeff.K = obj.epsilon; end 00108 if coeff.GARCH < 0, coeff.GARCH = 0; end 00109 if coeff.ARCH < 0, coeff.ARCH = 0; end 00110 00111 % Sum of ARCH + GARCH + 0.5 * Leverage coefficients must be < 1 for 00112 % GJR(P,Q) models. 00113 % The current model of choice is MA-GARCH (not GJR) because it 00114 % scored best among all competing models. 00115 Sum = coeff.ARCH + coeff.GARCH; 00116 if ~(Sum < 1) 00117 obj.UnityThreshold = 0.999; 00118 coeff.ARCH = obj.UnityThreshold * coeff.ARCH/Sum; 00119 coeff.GARCH = obj.UnityThreshold * coeff.GARCH/Sum; 00120 end 00121 00122 obj.coeff = coeff; 00123 end 00124 00125 end 00126 00127 00128 end