Converting mixed empty/non-empty cells into a numeric matrix

467 views Asked by At

I am working on a code to extract my AR(1)-GARCH(1) parameter, which I estimated using an AR(1)-GJR(1,1) model to individual matrices so that I can use them as variables in my calculations. As I have 16 time series variables, I combine the code with a loop in the following way:

for i=1:nIndices
AA_ARCH(:,i) = cell2mat(fit{i}.Variance.ARCH)';
end; 

My problem is that for some variables is are no for AA_ARCH(:,i) the dimension is lower than nIndices. Naturally, when I try to export the estimates in the loop which specified the dimension of (:,i) and nIndices matlab reports a dimension mismatch. I would like to tell Matlab to replace the NaN with 0 instead of leaving the spot empty so that it is able to produce a (1,nIndices) matrix from AA_ARCH.

I thought of something like the this:

fit{i}.Variance.Leverage(isnan(fit{i}.Variance.Leverage))=0

but I wasn't able to combine this part with the previous code.

I would be very happy about any hints!

Best, Carolin

UPDATE:

Here is a fully a runnable version of my code which produces my problem. Notice that the code produces a dimension mismatch error because there is no ARCH and GARCH estimate in the fit.gjr(1,1) for time series 1. For these missing values I would like to have 0 as a placeholder in the extracted matrix.

returns = randn(2,750)'; 

T       = size(returns,1);
nIndices = 2; 

model     = arima('AR', NaN, 'Variance', gjr(1,1));
residuals = NaN(T, nIndices);    
variances = NaN(T, nIndices);
fit       = cell(nIndices,1);

options   = optimset('fmincon');
options   = optimset(options, 'Display'  , 'off', 'Diagnostics', 'off', ...
                              'Algorithm', 'sqp', 'TolCon'     , 1e-7);

for i = 1:nIndices
    fit{i} = estimate(model, returns(:,i), 'print', false, 'options', options);
    [residuals(:,i), variances(:,i)] = infer(fit{i}, returns(:,i));
end

for i=1:nIndices
AA_beta(:,i) = cell2mat(fit{i}.AR)';
AA_GARCH(:,i) = cell2mat(fit{i}.Variance.GARCH)';
AA_ARCH(:,i) = cell2mat(fit{i}.Variance.ARCH)';
AA_Leverage(:,i) = cell2mat(fit{i}.Variance.Leverage)';
end; 
1

There are 1 answers

1
Dev-iL On BEST ANSWER

I have some general things to say about the code, but first a solution to your problem:

You can put a simple if/else structure in your loop to handle the case of an empty array:

for ind1=1:nIndices
    AA_beta(:,ind1) = cell2mat(fit{ind1}.AR)'; %//'
    %// GARCH    
    if isempty(cell2mat(fit{ind1}.Variance.GARCH)') %//'
        AA_GARCH(1,ind1) = 0;
    else
        AA_GARCH(:,ind1) = cell2mat(fit{ind1}.Variance.GARCH)'; %//'
    end
    %// ARCH (same exact code, should probably be exported to a function)
    if isempty(cell2mat(fit{ind1}.Variance.ARCH)') %//'
        AA_ARCH(1,ind1) = 0;
    else
        AA_ARCH(:,ind1) = cell2mat(fit{ind1}.Variance.ARCH)'; %//'
    end
    AA_Leverage(:,ind1) = cell2mat(fit{ind1}.Variance.Leverage)'; %//'
end; 

Side note: I initially tried something like this: soz = @(A)isempty(A)*0+~isempty(A)*A; as an inline replacement for the if/else, but it turns out that MATLAB doesn't handle [] + 0 the way I wanted (it results in [] instead of 0; unlike other languages like JS).

As for the other things I have to say:

  • I am a firm supporter of the notion that one shouldn't use i,j as loop indices, as this may cause compatibility problems in some cases where complex numbers are involved (e.g. if you loop index is i then 1*i now refers to the loop index instead of to the square root of -1).
  • Part of your problem was that the arrays you were writing into weren't preallocated - which also means the correct datatype was unknown to MATLAB at the time of their creation. Besides the obvious performance hit this entails, it could also result in errors like the one you encountered here. If, for example, you used cells for AA_beta etc. then they could contain empty values, which you could later replace with whichever placeholder your heart desired using a combination of cellfun and isempty. Bottom line: lint (aka the colorful square on the top right of the editor window) is your friend - don't ignore it :)