I am estimating a linear model with highly nonlinear constraints on coefficients.
These constraints can be written as function(coefficients)=function_name(parameters) as an external (MATLAB) function.
Because parameters are to be estimated I need to call it within the model(linear) block.
However, when I try to do so, by declaring external_function(name=function_name) and inserting line
inside the model block, dynare tells me that I have more equations than variables and refuses to work.
What is the way around it?
Additionally, because dynare seems to be uncomfortable with many arguments of an external function, I pass them as globals, by including lines
global PARAMETERS COEFFICIENTS;
parameters PARAMETERS COEFFICIENTS;
into both dynare file and into the corresponding MATLAB function. The function is then written as functionscalar_out) = function_name(scalar_in)
Dynare seems to forget globals half way. What is the way around it?
Not sure I can help you much but have you tried “evalin”? I also call an external function like this:
[x,fval, exitflag] = evalin(‘base’, ‘fsolve(@myfun,x0)’) ;
But I do not use it in the model block.
I am not sure I understand your problem completely. But if your coefficients are a function of the parameters you want to estimate and need to be updated in every iteration, the best way to go is to write a steady state file. In the steady state file you can call every Matlab function you like and assign computed values to parameters (which is what your coefficients are for Dynare).
how this can be done?
See the code below. I need to (Bayesian) estimate parameters a1,a2,a3, but my linear system is written using parameters b. There is an external matlab function which is written
function[b11,b12,b21,b22] = namefunction(a1,a2,a3). In the code below where exactly I call this steady state thing? I would have thought it should be called within the model block, but dynare does not eat this (Version 3 did everything fine). How this external function can be called in the steady state file and how this steady state file can be included such that the estimation of a1,a2,a3 would become possible
parameters a1 a2 a3 b11 b12 b21 b22
variables x1 x2
exogenous variables shocks
x1 = b11x1(-1) + b12x2(-1) + shock1
x2 = b21x1(-1) + b22x2(-1) + shock2
block of priors on parameters a1, a2, a3 and shocks
PS. I am sure I am not the only one with this problem: this is a very standard problem of imposing (non-linear) restrictions on coefficients. I am either blind or I cannot find anything in the documentation.
Many thanks for the help.
It can be done by supplying a steady state-file ([Steady state file)), which computes the values for b given the values of a (which can be accessed in the steady state file). As the steady state file is a Matlab file, it follows Matlab-syntax.
I managed to run a simulation, but I need to estimate parameters.
All I got was the message “This version of Dynare cannot estimate non linearized models!” Obviously, my model is linearised, coefficients are non-linear in parameters. Is this a real problem, or is this just a random diagnostics?
I attach the files: the model and the state file. In principle, I need something much more complicated than this, but even this does not work. What is wrong here?
US_data_80.m (7.07 KB)
rule_X4.mod (1.96 KB)
rule_X4_steadystate.m (1.14 KB)
This is a warning, not an error. By default, the check command sets order=2, but estimation currently only works at order=1 and hence sets it to 1. It has nothing to do with the parameters you want to estimate. If the warning bothers you, set order=1 in the estimation command.
I realize it is a warning, but it crashes after that warning. It crashes in doing something with inverse gamma, which is the first declared distribution it sees. I appreciate that dynare error messages do not correspond to the problem very often, but I find it extremely annoying that dynare 4 does not do what dynare 3 used to do easily and in an intuitive way.
Even if I make this steady state file work, how can I be sure that it calls it every time inside the loop? I would have thought that in linear models the steady state is zero and it should not be updated at every draw. If it does update it by default, it means that a lot of machine time is spent on it, even if it is explicitly declared that the model is linear. If this is the programming standard then I should just forget that dynare exists… Which is a pity.
I understand that I should stop complaining but just program all I need. I was sure that I was doing something very standard: imposing restrictions on estimated coefficients, and wanted to use the advantage dynare 4 might give, like paralelling the MH algorithm.
I would really want to see a single working example of bayesian estimation of a linear model with constraints on coefficients…
You should always post the exact error message. Guessing from your description, you are running Dynare 4.2.5. There is a documented bug in the inverse gamma function dynare.org/DynareWiki/KnownBugs. The fix can be found here: [Smets/Wouters (2007) in Dynare 4.2.5). Just replace the one file and it should run. It did on my computer, which indicates your steady state file works.
A couple more things:
- You seem to misinterpret what linear means. There could always be an intercept term in the linear specification. Such mod-files are rare, but they exist. Hence, the option to supply a steady state file. And as the name says, it is only an option. If no steady state file exists (which Dynare checks once), a steady state of 0 is used and everything runs faster. In the latter case, there is no updating every period.
- If your nonlinear constraints have a closed form solution, you can use the #-operator to define restrictions on the parameters. This is numerically more efficient than always calling a steady state file. But your initial problem description said something about “fsolve” from which I inferred that you don’t have an analytical solution. In this case, updating the parameters in the steady state file is the easiest way to do it and hardly inefficient as you would need to do the parameter updating even if you program everything yourself.
Many many thanks! It did produce something! I hope that I can now deal with my “complications”.
Many thanks again!
I managed to replicate the result from dynare 3 for one of my big toys. So, some confidence is there…
Can I, using the opportunity ask about the “output file” which is “not found”. I guess this is not essential, but maybe it is something useful and nice to have?..
What do you mean and what are you trying to do? Could you please post the exact message and, if necessary, the mod-file.
The mod file is the one attached above. Dynare estimates and then gives on the screen the following (see below)
It also produces two graphs: smoothed shocks and “Historical and smoothd variables”. In the second graps I can only see historical variables. I suspect can also be something to do with the way the output is handled.
Please do not spend too much time on this. At the very end I could do it manually. I just thought it might be convenient.
eps_A 0.250 16.3980 3.4786 4.7140 invg 2.0000
eps_MU 0.050 0.0230 0.0094 2.4500 invg 2.0000
eps_XI 0.250 4.1266 0.6845 6.0287 invg 2.0000
Log data density [Laplace approximation] is -475.194057.
Warning: File ‘rule_X4/Output’ not found.
In CheckPath at 42
In dynare_estimation_1 at 665
In dynare_estimation at 62
In rule_X4 at 196
In dynare at 120
Total computing time : 0h00m17s
To get correct smoothed observable plots, you need to replace two files in the matlab folder of Dynare 4.2.5 with the attached two files.
dynare_estimation_init.m (13.2 KB)
dynare_estimation_1.m (65.3 KB)
Many many thanks! It works!
Note that the init-file above has been updated to also work without prefiltering.
Please need help. Using above posts, as exercise, I introduced in the example model fs2000 a some counter. For it I initialized parameter counter=0 in fs2000.mod and in entered equation counter = counter + 1 in _steadstate.m. However, at the end of estimation counter takes a small number, such as 8. I expected a large number. Please explain why the counter takes a small number, where is mistake (I use 4.4.3).
best regards, mak
fs2000_steadystate.m (1.84 KB)
fs2000.mod (3.26 KB)
The reason is that Dynare estimates a different set of parameters that does not include counter. While counter is updated within every step of estimation, the value for counter is not stored and returned to the next iteration, because there is no reason for doing so. counter is not a function of the estimated parameters. As counter is not estimated, its value is basically kept at the level set at the beginning of estimation.