Loop with updating of non-diagonal covariance matrix

Dear cmarch,
Thanks for your help last February.
I write again because I get an error when running the shock_loop.m (uploaded):

Attempt to execute SCRIPT dyn_main as a function:
G:shock_loop.m (1.7 KB) \Math11\dyn_main.m

Error in shock_loop (line 19)
eval(['dynare ',dyn_main])

Do you now what may be the cause? I uploaded the rest of file of my code in this forum line (on 6th of March).

Thank you so much

Hi asha1,

dyn_main needs to be a valid .mod file such that it can be parsed by Dynare’s internal routines.
From what I can see from the error reported here, your dyn_main is currently an .m file, which cannot be parsed by Dynare and therefore the eval call reports an error and Matlab cannot execute that command line.

Hi cmarch,
In my working folder I have a correctly running dyn_main.mod. However, when you run it, a dyn_main.m is created. I tried deleting it before running shock_loop.m . I get the error:

‘dyn_main’ is not found in the current folder or on the MATLAB path, but exists
in:
G:\Math11no_r

Change the MATLAB current folder or add its folder to the MATLAB path.

Error in shock_loop (line 19)
eval(['dynare ',dyn_main])

So, it seems it’s looking for the m-file… These are the relevant files:
dyn_main.mod (8.3 KB) dyn_extract.m (1.3 KB) dyn_main_steadystate.m (955 Bytes) dyn_stats.m (2.1 KB) dyn_table.m (3.8 KB) findss.m (503 Bytes) stst.m (1.9 KB)

Thank you!

No, the message

clearly shows that you are working in the wrong folder.

Dear Johannes,

No… Unfortunately, this is not the problem. dyn_main.mod is in the folder where I work. This error appears when I delete the file dyn_main.m from my working folder (this file is created when I run my code from dyn_table.m which calls dyn_main.mod without problems).
It is true that files with the same name are in the other folder too. But those are for another version of the model…

You need to call

eval(['dynare ',model_name])

and not

eval(['dynare ',dyn_main])

as the latter tries to call a dyn_main.m instead of treating it as a string.

1 Like

Thank you! This solved the problem.

Error using check_model (line 36)

I am not sure whether I should open a new line or go on here for this new error… Sorry.
Now when running shook_loop.m shock_loop.m (1.8 KB) I get the error:

Error using check_model (line 36)
The specified covariances for the structural errors are not consistent with the
variances as they imply a correlation larger than ±1

Error in stoch_simul (line 67) check_model(M_);

Error in shock_loop (line 65)
info=stoch_simul(var_list_); % loop over stoch_simul.

Where should I look to find the mistake? (I check a previous entrance in the forum -2014- but it did not apply to this case…)
Many thanks…

You defined correlations between shocks, but now you are only setting the diagonal of the covariance matrix. That cannot work. You also need to adjust the covariance terms to be consistent with the specified correlation

Sorry, I’m afraid, then, that I don’t understand what the loop exactly does…
Shock_loop.m creates a grid of different shock sizes (different standard deviations to the credit shock). And it retrieves the correspondent volatility (variance) for two variables (exports and imports). Am I correct?
If I “manually” change the standard deviation of the shock, dyn_main runs. Why, with the loop, it is not similar?
My final scope is to draw a 3D plot of exports and imports volatilities against shock size to check robustness in their behaviour and/or identify non-linearities…

Thank you so much…

(shock_loop.m (1.8 KB) dyn_main.mod (8.3 KB) dyn_extract.m (1.3 KB) dyn_main_steadystate.m (955 Bytes) dyn_stats.m (2.1 KB) dyn_table.m (3.8 KB) findss.m (503 Bytes) stst.m (1.9 KB))

When you run Dynare fully, it will set the covariance matrix based on the specified correlation and variances. Within your loop, you need to do that manually. Currently, you are only setting the variance. You need

          M_.Sigma_e(eps_pos,eps_pos) = SIGM_grid(ii); 
          M_.Sigma_e(eps_pos,enF_pos) = M_.Correlation_matrix(eps_pos,enF_pos)*sqrt(SIGM_grid(ii)*M_.Sigma_e(enF_pos,enF_pos)); 
          M_.Sigma_e(enF_pos,eps_pos) = M_.Sigma_e(eps_pos,enF_pos);

where

enF_pos=strmatch('enF',M_.exo_names,'exact');

Note that you need to alter this whenever you allow for other correlations than between enF and enH.

1 Like

Dear Johannes,
Once more, many thanks. Now the loop works and I understand a bit more how it does it.
Best.