-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathnktest.m
More file actions
473 lines (338 loc) · 13.4 KB
/
Copy pathnktest.m
File metadata and controls
473 lines (338 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
function [tstat, pval] = nktest(u,parallel,family1,theta1,A1,family2,theta2,A2,varargin)
% Conducts a model comparison test of k models according to
% Nikoloulopoulos & Karlis (2008) adapted for vine copulas.
%
% call: [tstat, pval] = nktest(u,parallel,family1,theta1,A1,family2,theta2,A2[,family3,theta3,A3,...])
%
% input u - nxd data matrix of
% pseudo-observations
% parallel - switch parallelization on (=1) or
% off (=0)
% family1 - a (d-1)x(d-1) cell variable
% determining the copula families for
% model1; possible families: 'gumbel',
% 'clayton', 'frank', 't', 'gauss',
% 'ind', 'amhaq', 'tawn', 'fgm', 'joe',
% 'plackett', 'surclayton', 'surjoe',
% 'surgumbel'
% theta1 - a (d-1)x(d-1) cell variable of copula
% parameters for model1; for t-copula
% insert [rho nu] in cell element
% A1 - vine array for mode11; note that a
% feasible structure has to be used,
% since the function will not check
% this
% family2 - a (d-1)x(d-1) cell variable
% determining the copula families for
% model2; possible families: 'gumbel',
% 'clayton', 'frank', 't', 'gauss',
% 'ind', 'amhaq', 'tawn', 'fgm', 'joe',
% 'plackett', 'surclayton', 'surjoe',
% 'surgumbel'
% theta2 - a (d-1)x(d-1) cell variable of copula
% parameters for model2; for t-copula
% insert [rho nu] in cell element
% A2 - vine array for mode12; note that a
% feasible structure has to be used,
% since the function will not check
% this
% (optional) if more than 2 models are compared:
% family3,...,familiyk - a (d-1)x(d-1) cell variable
% determining the copula families;
% possible families: 'gumbel',
% 'clayton', 'frank', 't', 'gauss',
% 'ind', 'amhaq', 'tawn', 'fgm', 'joe',
% 'plackett', 'surclayton', 'surjoe',
% 'surgumbel'
% theta3,...,thetak - a (d-1)x(d-1) cell variable of copula
% parameters; for t-copula insert
% [rho nu] in cell element
% A3,...,Ak - a vine array; note that a feasible
% structure has to be used, since the
% function will not check this
%
% output tstat - kx1 vector of test statistic values
% pval - kx1 vector of corresponding p values
%
%
% How does it work?
% The function conducts a model comparison test according to
% Nikoloulopoulos & Karlis (2008) adapted for vine copulas. Note that the
% models have to be fitted in advance. The minimum number of models
% compared is two. The function can compare k models at once. The null
% hypothesis is that model k is correct. For each model beyond the first
% two, specify the family, theta and A variables analoguesly to the
% first two.
%
% Note that for the function to work, the vine arrays provided by the user
% have to be a feasible vine arrays in the first place. The function will
% not check feasibilty on its own! For c- and d-vines the function
% cdvinearray can be used to generate a feasible vine array.
%
% Structure of the input is demonstrated for a 5-dimensional r-vine copula:
%
% Let the sample r-vine structure be
%
% 4
% /
% 1 - 2 - 3
% \
% 5
%
% 12 - 23 - 34 - 35
% .
% .
% .
%
% , where the numbers correspond to the columns of the matrix u. In this
% case
%
% 1 1 2 3 3
% 2 1 2 4
% A = 3 1 2
% 4 1
% 5
%
% is the corresponding vine array.
%
% In order for the function to work, the user has to input information on
% the following bivariate copulas: 12, 23, 34, 35, 13|2, 24|3, 45|3, 14|23,
% 25|34, 15|234, where '|' represents conditioning. Note that this system
% corresponds to the appearance of the copula in the vine array from left
% to right. Input family and theta cell variables for the copulas like
% this:
%
% family12 family23 family34 family35
% family = family13|2 family24|3 family45|3 0
% family14|23 family25|34 0 0
% family15|234 0 0 0
%
% Matlab syntax:
% family = {'family12','family23','family34','family35'; 'family13|2','family24|3','family45|3',0; 'family14|23','family25|34',0,0;'familiy15|234',0,0,0}
%
% theta12 theta23 theta34 theta35
% theta = theta13|2 theta24|3 theta45|3 0
% theta14|23 theta25|34 0 0
% theta15|234 0 0 0
%
% Matlab syntax:
% theta = {theta12,theta23,theta34,theta35; theta13|2,theta24|3,theta45|3,0; theta14|23,theta25|34,0,0;theta15|234,0,0,0}
%
% Additionally, input the vine array A as a matrix as stated above.
%
%
% References:
% Allcroft & Glasbey (2003), A Simulation-Based Method for Model
% Evaluation, Statistical Modelling, Vol. 3, 1-13.
% Nikoloulopoulos & Karlis (2008), Copula Model Evaluation Based on
% Parametric Bootstrap, Computational Statistics and Data Analysis, Vol.
% 52, 3342-3353.
%
%
% Copyright 2020, Maximilian Coblenz
% This code is released under the 3-clause BSD license.
%
% some parsing
p = inputParser;
p.addRequired('x',@ismatrix);
p.addRequired('para',@isscalar);
p.addRequired('family1',@iscell);
p.addRequired('theta1',@iscell);
p.addRequired('A1',@ismatrix);
p.addRequired('family2',@iscell);
p.addRequired('theta2',@iscell);
p.addRequired('A2',@ismatrix);
p.parse(u,parallel,family1,theta1,A1,family2,theta2,A2);
% sanity checks
if mod(nargin-2,3) ~= 0
error('nktest:InvalidNumberOfArguments','please specify the correct number of optional input arguments (3 per model)');
end
for ii = 1:1:size(family1,1)
for jj = 1:1:size(family1,2)-ii+1
if ~cpcheck(family1{ii,jj},theta1{ii,jj})
error('nktest:InvalidParameter',['invalid parameter for ',family1{ii,jj},' copula at (',num2str(ii),',',num2str(jj),') in family1']);
end
end % jj
end % ii
for ii = 1:1:size(family2,1)
for jj = 1:1:size(family2,2)-ii+1
if ~cpcheck(family2{ii,jj},theta2{ii,jj})
error('nktest:InvalidParameter',['invalid parameter for ',family2{ii,jj},' copula at (',num2str(ii),',',num2str(jj),') in family2']);
end
end % jj
end % ii
% some sanity checks for vine array A1
if (size(A1,1) ~= size(A1,2))
error('nktest:InvalidVineArray','vine array A1 has to be a quadratic matrix');
end
for jj = 1:1:size(A1,1)
if (length(unique(A1(1:jj,jj))) ~= jj)
error('nktest:InvalidVineArray','input A1 is not a vine array');
end
end % jj
% some sanity checks for vine array A2
if (size(A2,1) ~= size(A2,2))
error('nktest:InvalidVineArray','vine array A2 has to be a quadratic matrix');
end
for jj = 1:1:size(A2,1)
if (length(unique(A2(1:jj,jj))) ~= jj)
error('nktest:InvalidVineArray','input A2 is not a vine array');
end
end % jj
if nargin > 8
m = 3;
for ii = 1:3:nargin-8
for kk = 1:1:size(varargin{ii},1)
for jj = 1:1:size(varargin{ii},2)-kk+1
if ~cpcheck(varargin{ii}{kk,jj},varargin{ii+1}{kk,jj})
error('nktest:InvalidParameter',['invalid parameter for ',varargin{ii}{kk,jj},' copula at (',num2str(kk),',',num2str(jj),') for family',num2str(m)]);
end
end % jj
end % kk
% some sanity checks for vine arrays A3...Ak
aux = varargin{ii+2};
if (size(aux,1) ~= size(aux,2))
error('nktest:InvalidVineArray',['vine array A', num2str(m), ' has to be a quadratic matrix']);
end
for jj = 1:1:size(aux,1)
if (length(unique(aux(1:jj,jj))) ~= jj)
error('nktest:InvalidVineArray',['input A', num2str(m), ' is not a vine array']);
end
end % jj
m = m+1;
end % ii
end
% parallelization mode
if parallel ~= 1 && parallel ~= 0
error('nktest:InvalidParallelizationMode','input argument parallel has to be 0 or 1')
end
if parallel
ppool = gcp;
poolsize = ppool.NumWorkers;
end
% initialize some variables
n = size(u,1);
k = (nargin-2)/3;
loglik = zeros(k,1);
tstat = zeros(k,1);
B = 100; % number of bootstraps
bsample = cell(k,B);
lambda = zeros(k,B,k);
% 1 - compute log-likelihood of data x for each model
% model 1
[~,loglik(1)] = llrvine(u,A1,family1,theta1);
% model 2
[~,loglik(2)] = llrvine(u,A2,family2,theta2);
if k > 2 % models 3 to k
m = 1;
for ii = 3:1:k
[~,loglik(ii)] = llrvine(u,varargin{2+m},varargin{m},varargin{1+m});
m = m + 3;
end % ii
end
% 2 - Simulate B samples from each fitted model
% At the end of this step there is a series of B samples for each of the k
% models:
% model1: sample1 sample2 ... sampleB
% model2: sample1 sample2 ... sampleB
% ...
% modelk: sample1 sample2 ... sampleB
if parallel
parfor (jj = 1:B,poolsize) % model 1
bsample{1,jj} = simrvine(n,A1,family1,theta1);
end % jj
parfor (jj = 1:B,poolsize) % model 2
bsample{2,jj} = simrvine(n,A2,family2,theta2);
end % jj
else
for jj = 1:1:B % models 1 & 2
bsample{1,jj} = simrvine(n,A1,family1,theta1);
bsample{2,jj} = simrvine(n,A2,family2,theta2);
end % jj
end
if k > 2 % models 3 to k
m = 1;
for ii = 3:1:k
if parallel
parfor (jj = 1:B,poolsize)
bsample{ii,jj} = simrvine(n,varargin{2+m},varargin{m},varargin{1+m});
end % jj
else
for jj = 1:1:B
bsample{ii,jj} = simrvine(n,varargin{2+m},varargin{m},varargin{1+m});
end % jj
end
m = m + 3;
end % ii
end
% 3 - For each of the series of B samples 1) either refit all the k
% competing models, 2) or evaluate the log-likelihood function of the k
% models at the sample. Here the models are refitted and the loglikelihoods
% evaluated.
% At the end of this step there is a kxB matrix of
% log-likelihoods for each of the k series.
if parallel
parfor (jj = 1:B,poolsize) % model 1
lambda_aux = zeros(k,1);
for ll = 1:1:k
[~,~,lambda_aux(ll),~] = ssp(bsample{ll,jj},A1,family1);
end % ll
lambda(:,jj,1) = lambda_aux;
end % jj
else
for jj = 1:1:B % model 1
for ll = 1:1:k
[~,~,lambda(ll,jj,1),~] = ssp(bsample{ll,jj},A1,family1);
end % ll
end % jj
end
if parallel
parfor (jj = 1:B,poolsize) % model 2
lambda_aux = zeros(k,1);
for ll = 1:1:k
[~,~,lambda_aux(ll),~] = ssp(bsample{ll,jj},A2,family2);
end % ll
lambda(:,jj,2) = lambda_aux;
end % jj
else
for jj = 1:1:B % model 2
for ll = 1:1:k
[~,~,lambda(ll,jj,2),~] = ssp(bsample{ll,jj},A2,family2);
end % ll
end % jj
end
if k > 2 % models 3 to k
m = 1;
for ii = 3:1:k
if parallel
parfor (jj = 1:B,poolsize)
lambda_aux = zeros(k,1);
for ll = 1:1:k
[~,~,lambda_aux(ll),~] = ssp(bsample{ll,jj},varargin{m+2},varargin{m});
end % ll
lambda(:,jj,ii) = lambda_aux;
end % jj
else
for jj = 1:1:B
for ll = 1:1:k
[~,~,lambda(ll,jj,ii),~] = ssp(bsample{ll,jj},varargin{m+2},varargin{m});
end % ll
end % jj
end
m = m + 3;
end % ii
end
% 4 - test statistics and p values
if parallel
parfor (ii = 1:k,poolsize)
Sinv = inv(cov(lambda(:,:,ii)'));
tstat(ii) = (loglik-mean(lambda(:,:,ii),2))'*Sinv*(loglik-mean(lambda(:,:,ii),2))/k;
end % ii
else
for ii = 1:1:k
Sinv = inv(cov(lambda(:,:,ii)'));
tstat(ii) = (loglik-mean(lambda(:,:,ii),2))'*Sinv*(loglik-mean(lambda(:,:,ii),2))/k;
end % ii
end
pval = fcdf(tstat,k,B-1);
end