# Finding Second Moments in DYNARE as in Schmitt-Grohe, Uribe 2004

**URL:** <https://forum.dynare.org/t/finding-second-moments-in-dynare-as-in-schmitt-grohe-uribe-2004/12099>\
**Category:** Stochastic simulations\
**Created:** [11 July 2018 13:36 UTC](https://forum.dynare.org/t/finding-second-moments-in-dynare-as-in-schmitt-grohe-uribe-2004/12099 "2018-07-11T13:36:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanbasten](https://forum.dynare.org/letter_avatar_proxy/v4/letter/v/35a633/32.png) [@vanbasten](https://forum.dynare.org/u/vanbasten)\
**Post date:** [11 July 2018 13:36 UTC](https://forum.dynare.org/t/finding-second-moments-in-dynare-as-in-schmitt-grohe-uribe-2004/12099/1 "2018-07-11T13:36:38Z")

</div>

Hello,

I need to get the second order (variance and covariances of state variables(SchmitT Grohe notation)). Schmitt-Grohe package for 2nd order approximation has an .m file called mom.m which calculates the moments for both control and state variables.  
The notation dynare and SGU uses are slightly different. I was wondering if DYNARE actually stores somewhere the 2nd moments of variables other than the exogenous shocks somewhere.

Thanks a lot

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [11 July 2018 14:39 UTC](https://forum.dynare.org/t/finding-second-moments-in-dynare-as-in-schmitt-grohe-uribe-2004/12099/2 "2018-07-11T14:39:08Z")

</div>

After calling `stoch_simul` without the `periods`-option, theoretical moments will be stored in e.g. `oo_.var`. See the manual for details

---

<div class="post-metadata">

**Author:** ![vanbasten](https://forum.dynare.org/letter_avatar_proxy/v4/letter/v/35a633/32.png) [@vanbasten](https://forum.dynare.org/u/vanbasten)\
**Post date:** [11 July 2018 17:11 UTC](https://forum.dynare.org/t/finding-second-moments-in-dynare-as-in-schmitt-grohe-uribe-2004/12099/3 "2018-07-11T17:11:02Z")

</div>

Thanks a lot for the quick response. I appreciate it

I am working on risky steady state and need second order moments to iterate until they converge. I am, using the simplest example, trying see the mapping between the dynare codes you have posted on your website SGU\_2004.mod and the NEOCLASSICAL\_MODEL\_RUN.M that uses the SGU package. When the autocorrelation coefficient is set to 0.

- PHO=0 ar1 coef on exogenous shocks

FOR SGU:\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\__FOR DYNARE SGU\_2004.mod  
 **Steady States:**  
log(kss)=-1.7932\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_log(kss)=-1.7932  
log(css)=-0.8734\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_log(css)=-0.8734  
 **Policy Functions**  
c\_hat=0.2525k{t}+0.8417\*a_{t}\_\_\_\_\_\_\_\_\_\_\_\_\__c\_hat=0.2525k{t}+0.8417\*eps{t}  
k{t+1}=0.4191k{t}+1.3970\*a_{t}\_\_\_\_\_\_\_\_\_\_\_\_\_\__k{t+1}=0.4191k{t}+1.3970\*eps_{t}  
**Covariance Matrix**  
var(c\_hat)=0.8595\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_var(c\_hat)=0.8595  
var(k\_hat)=2.3676\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_var(k\_hat)=2.3676  
var(a\_hat)=1\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_var(a\_hat)=1  
cov(k\_hat,a\_hat)=0\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_cov(k\_hat,a\_hat)=0

- pho 0.95

**Steady States:**  
log(kss)=-1.7932\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_log(kss)=-1.7932  
log(css)=-0.8734\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_log(css)=-0.8734

**Policy Functions**  
c\_hat=0.2525k\_{t}+1.0496_a\_{t} \_\_\_\_\_ \_\_\_\_c\_hat=0.2525_k\_{t}+0.997161_at\_{t}+1.049643_eps\_{t}  
here RHO\*1.0496=0.997161 DIFFERENT REPRESENTATION SO IT IS ok

k\_{t+1}=0.4191k\_{t}+0.8755_a\_{t}\_\_\_\_\_\_\_k{t+1}=0.4191_k\_{t}+0.831683_at\_{t-1}+0.875456_eps\_{t}  
HERE ALSO SAME LOGIC

**Covariance Matrix**

var(c\_hat)=20.2261\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_var(c\_hat)=20.2261

var(k\_hat)=22.1525\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_var(k\_hat)=22.1525  
var(a\_hat)=14.1732\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_var(a\_hat)=10.2564  
cov(k\_hat,a\_hat)=10.2564\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_cov(k\_hat,a\_hat)=???

Sorry for writing this long. But I have this huge model with bunch of other variance and covariances and i was wondering if the SGU mom.m function and dynare one is actually different

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [12 July 2018 09:14 UTC](https://forum.dynare.org/t/finding-second-moments-in-dynare-as-in-schmitt-grohe-uribe-2004/12099/4 "2018-07-12T09:14:11Z")

</div>

The covariance matrix is stored in  
`oo_.gamma_y{1}`  
And moments should be the same in both packages after accounting for notational differences

---

<div class="post-metadata">

**Author:** ![vanbasten](https://forum.dynare.org/letter_avatar_proxy/v4/letter/v/35a633/32.png) [@vanbasten](https://forum.dynare.org/u/vanbasten)\
**Post date:** [13 July 2018 20:56 UTC](https://forum.dynare.org/t/finding-second-moments-in-dynare-as-in-schmitt-grohe-uribe-2004/12099/5 "2018-07-13T20:56:23Z")

</div>

Thanks Johannes,

This is the third time I am editing this post. 🙂

For the neclassical growth model the moments generated by SGU mom.m file and SGU\_2004.mod oo\_.gamma\_y{1} seems to match.

But when I wanted to test whether the moments of a bigger model (Closing Small Open Economies-External Debt Elastic Interest Rate Model) match with files I get different results. I ran 1- the original SGU package, your code SGU\_2003 model 2 and third dynare file from this website soe2.mod

Here are the results

 ![image](https://forum.dynare.org/uploads/default/original/2X/4/4677a3943513edf76b4b3562525c048c44d62f99.png)

The steady states, parameters are identical in 3 cases. However due to different structure the policy functions seem to differ.

- I can match the policy functions of **yt** and **ht** identically with your mod. file but with 3rd dynare file the error coefficient is different

I assume this is because in that file the shocks are set:

shocks;var e; stderr sigmae;end; which scales shocks to whole variables

while in your set up (and i think that is the correct one to compare with SGU) you add the scaling parameter directly in model part of shock equation

- ct poicy function is different. it might be because of the different structurre

AND THE MAIN ISSUE OF COVARIANCE MATRIX

 ![image](https://forum.dynare.org/uploads/default/original/2X/c/cb49e0b9f8f83cd2db94601107d617d566ce7f70.png)

The results

- List item SUMMARY OF THE RESULTS (YOU CAN SKIP TO SEE THE SHORT SUMMARY OF COMPARISON)

 ![image](https://forum.dynare.org/uploads/default/original/2X/d/d7e38b535dee6c8d19fb16e5966128cb3e5befc0.png)

- List item Although i am able to match the policy functions of yt and ht and the covariance/variance of these although similar are not totally identical.

1. Could it be that SGU using analytical derrivatives offers a more robust estimation of moments  
or

2. the discrepancies are mainly due to the fact that rt-1 is state variable in dynare specification, which differs with the SGU specification

- List item I could not match the error term coefficent ht and yt with second dynare file but the covariance matrix seems similar but not identical

That could be maybe why i cant converge to a solution when i iterate on moments in different states ☹

Thanks a lot for your help and wish you a wonderful weekend

The mod files I used: SGU\_2003.MOD model 2 risk premium on interest rate  
And soe.2mod file from this link  
[http://www.dynare.org/phpBB3/download/file.php?id=1450&sid=41a33f96c18d27f61fc0776353e2aab8](http://www.dynare.org/phpBB3/download/file.php?id=1450&sid=41a33f96c18d27f61fc0776353e2aab8)

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [19 July 2018 06:20 UTC](https://forum.dynare.org/t/finding-second-moments-in-dynare-as-in-schmitt-grohe-uribe-2004/12099/6 "2018-07-19T06:20:18Z")

</div>

Dynare also uses analytical derivatives, so that should not be the difference. My hunch is that for the SOE-paper, the specifications are not identical.
