Loss in Dynare and Loss in Matlab

Dear community,

I programmed the same model in Dynare and Matlab. The model is the Hybrid NKM under Commitment.
The plots and outputs are equal but there is a difference in the loss.

In Dynare I calculated the loss by:

variance.x=oo_.var(x_pos,x_pos);
variance.pi=oo_.var(pi_pos,pi_pos);
L= (0.5*variance.pi)+ (0.25*variance.x)

which is quite easy. The loss is 13.0859.

In Matlab it was a bit more difficult:
The input arguments are:

data = struct('v_solution', [1:1000; randn(1, 1000)]);   
w_pi = 0.5 ;   
w_x = 0.25;   
[ LossVal,vol_pi, vol_x ] = Loss_fun( data,w_pi, w_x )

and the Loss_fun.m

function [ LossVal,vol_pi, vol_x ] = Loss_fun( data,w_pi, w_x)
 
vol_pi = (mean(data.v_solution(2,:).^2));
vol_x = (mean(data.v_solution(1,:).^2));



LossVal =   w_pi * vol_pi ...
          + w_x  * vol_x 
      
end

In Matlab I got two Loss values instead of one.
Where could be the mistake?

Dear Faro,

  1. Dynare computes the exact theoretical variance from the linear approximation, but in Matlab you compute empirical moment. This will introduce some differences
  2. In Matlab you compute empirical second moment but assume that first moment (the mean) is zero which is the theoretical mean
  3. you get two value because your definition of data implies two columns: an index and a random value

Best

Michel

Thanks Michel,

About 1: Yes, your are right about the differences.
But my differences are to large. Dynare says 13 and Matlab says 8. I think this can not be.

I think somewhere here is the mistake:
data = struct(‘v_solution’, [1:1000; randn(1, 1000)]);
Because Iam not sure about this randn (1, 1000)

About 3: Thank you. I changed it

What are you trying to do with

data = struct('v_solution', [1:1000; randn(1, 1000)]);

?

My first thought was to write the code as follows:

data = struct(‘v_solution’, 1:1000);
w_pi = 0.5 ;
w_x = 0.25;

But I get the error:

Index in position 1 exceeds array bounds (must not exceed 1).
Error in Loss_fun (line 4)
vol_pi = (mean(data.v_solution(2,:).^2));
Error in COMMITMENT (line 98)
[ LossVal,vol_pi, vol_x ] = Loss_fun( data,w_pi, w_x )

But it does not work. I want matlab to take the mean of pi and x from a simulation lengt T=1000. My code for the model also include these simulation lenght. So, I wanted to find a way to give the input argument of T=1000. But I think this randn part is wrong because the data is not random. That was my intention with data struct

I meant in terms of the bigger logic. Are you trying to generate one or two series of normally distributed random numbers?

you’ve got me there. I would say one series. But iam anyways not sure about random numbers? Are they really random numbers?

They are pseudo-random numbers.

Sorry, I checked it again.
Now I think I dont need this part because, for the first row iam taking the numbers 1 to 1000 and in the second row iam taking 1000 random numbers of the normal distribution.
So no need for this line. I think it makes no sense anymore.

data = struct(‘v_solution’, [1:1000; randn(1, 1000)]);

I think I should expand my period lenght too.
My period lenght is defined in my main script with T=1000.
I think I should make it much larger.
These are my approaches to get the correct code and loss.

I corrected my code to:

data = struct(‘v_solution’,[1,T;2,T]);
T=1000000;
w_pi = 0.5 ;
w_x = 0.25;
[ LossVal,vol_pi, vol_x ] = Loss_fun( data,w_pi, w_x )

function [ LossVal,vol_pi, vol_x ] = Loss_fun( data,w_pi,w_x,T)
vol_pi = (mean(data.v_solution(2,:).^2));
vol_x = (mean(data.v_solution(1,:).^2));
LossVal = w_pi * vol_pi …
+ w_x * vol_x;
end

I increased the number of T and my loss is 3.7500e+11 instead of 13,…
The model which iam trying to replicate is the peformance of monetary policy rules in case of stochastic cost-push shocks
I think in my code iam only doing it for one single shock in one period, so I need to programm ias in every new period there is a new cost push shock.
Is this a correct approach and explanation for the differences in loss?

I still don’t understand what you are doing. Why are you feeding in a trend?

still don’t understand what you are doing

Which line do you mean?

I try again to explain what Iam doing here and I want as a result.
At first this is my matlab code for the hybrid NKM model.
COMMITMENT.m (3.1 KB)
I did the same model in dynare, where I got the finally loss.
I want to do exaclty the same as I did in dynare.
My simulation length for the model is T=100000. This T is defined in the main script as in the input argument for the lossfunction. I want to find the loss of this model, when the economy is faced by cost shocks. These shocks are happening in every new period.

Input Arguments:

data = struct(‘v_solution’,[1,T;2,T]);

here we have the v_solution as a 2 by 1 vector with the predetermined variables x and pi.
I want Matlab for x to take the first row till T (1,T) and for pi the second row till T (2,T).

T=1000000;

This is my T

w_pi = 0.5 ;
w_x = 0.25;

These are my weights for the loss funtion

[ LossVal,vol_pi, vol_x ] = Loss_fun( data,w_pi, w_x )

This is how I run the loss function in the main script from the m.file

Lossfun.m

function [ LossVal,vol_pi, vol_x ] = Loss_fun( data,w_pi,w_x,T)

this is the loss function with 4 inputs.

vol_pi = (mean(data.v_solution(2,:).^2));
vol_x = (mean(data.v_solution(1,:).^2));

in this line matlab calculates the mean of x and pi.
and plugs it in the lossfunction.

LossVal = w_pi * vol_pi …

  • w_x * vol_x;
    end

Why a function. Why not simply

w_pi = 0.5 ;   
w_x = 0.25; 
LossVal = w_pi * var(v_solution(2,:)) + w_x * var(v_solution(1,:));

Thanks this is much easier!!
I run your code and my loss is:

LossVal =
1.1611e-05

I still have the difference between the loss in dynare and matlab.
The result of dynare is :
L= 13.2610

The Matlab main script is correct too.
As I understood the model there are cost push shocks in every period. My result in matlab should be the loss for a single impulse now.
Is this correct?
I want the loss for a random shock in every new period.
How can I integrate this in my simulation?

Instead of computing an IRF where a one-time shock hits, you need to generate a simulation where shocks hit each period.

Thank you! I will have a look on it.

What I do not understand is:
I computed this model in dynare too without considering that the shock hits every period.
which part of my dynare commitment code/ or of your sample from the forum is considering this detail?
Or is my dynare Code also not complete?
I suggest that I also ignored this information in my dynare Code?

oo_.var stores the theoretical or simulated variance. It takes into account shocks in every period.

I tried to find some examples to see how others simulated the model when a shock hits each period.
But unfortunately I havent find anything.
Do you know where I can find a example or a hint how to handle it?

But you have all the necessary pieces in place. Instead of simulation an IRF with a shock at time zero, you need to have a shock in every period. That can be easily added in your loop.

Thank you!!

The code for Matlab:

w_pi = 0.5 ;
w_x = 0.25;
LossVal = w_pi * var(v_solution(2,:)) + w_x * var(v_solution(1,:));

are these varinances not the theoretical ones but the empirical ones?
Is it also possible to calculate the loss with the square deviation?