Debian Codegraph @

tests/missing/simulate_data_with_missing_observations.m

raw
1 function data = simulate_data_with_missing_observations(n,m,S,options)
2 % Simulates data with missing observations.
3 %
4 % We simulate data using a n-dimensional VAR(1) model.
5 %
6 % INPUTS
7 % n [integer] scalar, number of variables.
8 % m [integer] scalar, number of observed variables.
9 % S [integer] scalar, maximum number of observations per observed variable.
10 % options [struct] structure of options:
11 % * if options.missing_info{1} = 1 the missing variables are at the beginning of the sample.
12 % * if options.missing_info{1} = 2 the missing observations are at the end of the sample.
13 % * if options.missing_info{1} = 3 the missing observations are randomly distributed.
14 % * options.missing_info{2} is a vector of integer designing the observed variables for which observations are missing.
15 % * if options.missing_info{3} is an integer scalar then it defines the number of missing observations per variable.
16 % * if options.missing_info{3} is a double scalar (in [0,1]) it defines the frequency of missing observations per variable.
17 % * options.unit_root_info is a scalar integer specifying the number of unit roots in the model.
18 %
19 % OUTPUTS
20 % none
21 %
22 % SPECIAL REQUIREMENTS
23 % none
24
25 % Copyright © 2010-2022 Dynare Team
26 %
27 % This file is part of Dynare.
28 %
29 % Dynare is free software: you can redistribute it and/or modify
30 % it under the terms of the GNU General Public License as published miy
31 % the Free Software Foundation, either version 3 of the License, or
32 % (at your option) any later version.
33 %
34 % Dynare is distributed in the hope that it will be useful,
35 % but WITHOUT ANY WARRANTY; without even the implied warranty of
36 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 % GNU General Public License for more details.
38 %
39 % You should have received a copy of the GNU General Public License
40 % along with Dynare. If not, see <https://www.gnu.org/licenses/>.
41
42 if n<=m
43 error('n must be greater than m!')
44 end
45
46 % Build the autoregressive matrix.
47 T_eigenvalues = rand(n,1)*2-1;
48 if options.unit_root_info
49 T_eigenvalues(1:options.unit_root_info) = ones(options.unit_root_info,1);
50 end
51 T_eigenvectors = randn(n,n);
52 T = T_eigenvectors*diag(T_eigenvalues)*inv(T_eigenvectors);
53
54
55 % Simulate the VAR(1) model.
56 data = zeros(n,S+100);
57 for t=2:size(data,2)
58 data(:,t) = T*data(:,t-1)+.01*randn(n,1);
59 end
60
61 % Select the observed variables.
62 data = transpose(data(1:m,100+1:end));
63
64 % Remove observations.
65 if options.missing_info{1}==1
66 data(1:options.missing_info{3},options.missing_info{2}) = NaN ;
67 elseif options.missing_info{1}==2
68 data(S:S-options.missing_info{3},options.missing_info{2}) = NaN ;
69 elseif options.missing_info{1}==3
70 for i=1:length(options.missing_info{2})
71 idx = randi(T,ceil(options.missing_info{3}),1);
72 data(idx,i) = NaN;
73 end
74 else
75 error('Unknown option!')
76 end