# Perform moment matching with Matlab

**URL:** <https://forum.dynare.org/t/perform-moment-matching-with-matlab/19031>\
**Category:** SMM/GMM/IRF-Matching\
**Created:** [6 October 2021 02:15 UTC](https://forum.dynare.org/t/perform-moment-matching-with-matlab/19031 "2021-10-06T02:15:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dsge\_modeling](https://forum.dynare.org/letter_avatar_proxy/v4/letter/d/9de053/32.png) [@dsge\_modeling](https://forum.dynare.org/u/dsge_modeling)\
**Post date:** [6 October 2021 02:15 UTC](https://forum.dynare.org/t/perform-moment-matching-with-matlab/19031/1 "2021-10-06T02:15:36Z")

</div>

I’m trying to numerically calibrate my model by setting to zero (or minimizing) the distance between data moments and model moments. My Matlab code is something similar to:

```auto
dynare('model.mod')
param_0 = 0.1;
x0(1) = param_0;
target_moments(1) = 1;

options = optimset(...);

[calibrated_params FFinal] = fsolve(@(x) moment_distance(x, target_moments, M, oo, var_list), param_0, options);

function V = moment_distance(x,target_moments, M, oo, var_list)
    param = x(1);
    [info, oo_, options_] = stoch_simul(M, options, oo, var_list);
    % Say model target moment is located at oo_.var(1,1)
    V(1) = target_moments(1) - oo_.var(1,1);
end

```

The problem with this is that checking the values `V` takes, I note that they never change when the solver tries different values, resulting in not finding a solution as the function is regular. This is wrong since I’ve already done parameter identification and moments do change when I change parameter values.

I think this comes from the fact that running `stoch_simul()` locally in a function without having run `dynare('model.mod')` alone first is not the same as running this first, since it sets-up the environment with `M_`, `oo_`, `options_`, `var_list_` variables.

Nonetheless, if I change my function to:

```auto
function V = moment_distance(x,target_moments)
    dynare('model.mod')
    param = x(1);
    [info, oo_, options_] = stoch_simul(M_, options_, oo_, var_list_);
    % Say model target moment is located at oo_.var(1,1)
    V(1) = target_moments(1) - oo_.var(1,1);
end

```

I now obtain an error inside the function which prompts that `M_` ( and I guess the other related variables) does not exist. I’m not sure what should I do in order to get Dynare changing effectively the parameter values for iteration and trying to find a solution, I’d be very grateful with some help.

Thanks!

---

<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:** [6 October 2021 13:31 UTC](https://forum.dynare.org/t/perform-moment-matching-with-matlab/19031/2 "2021-10-06T13:31:31Z")

</div>

Inside of your function the call to

> [@dsge\_modeling](#):
>
> ` param = x(1);`

is not a proper way to set parameters. Use `set_param_value` for that. See e.g. [DSGE\_mod/RBC\_IRF\_matching/IRF\_matching\_objective.m at master · JohannesPfeifer/DSGE\_mod · GitHub](https://github.com/JohannesPfeifer/DSGE_mod/blob/master/RBC_IRF_matching/IRF_matching_objective.m)

---

<div class="post-metadata">

**Author:** ![dsge\_modeling](https://forum.dynare.org/letter_avatar_proxy/v4/letter/d/9de053/32.png) [@dsge\_modeling](https://forum.dynare.org/u/dsge_modeling)\
**Post date:** [8 October 2021 00:34 UTC](https://forum.dynare.org/t/perform-moment-matching-with-matlab/19031/3 "2021-10-08T00:34:14Z")

</div>

Indeed. Sorry, I forgot to put that part in the sample code. Checking the link you shared I used the `global oo_ M_ options_ var_list_` at the beginning of the function which put my code working. Thanks!

---

<div class="post-metadata">

**Author:** ![hosey\_29](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/8797f3/32.png) [@hosey\_29](https://forum.dynare.org/u/hosey_29)\
**Post date:** [3 July 2025 20:21 UTC](https://forum.dynare.org/t/perform-moment-matching-with-matlab/19031/4 "2025-07-03T20:21:19Z")

</div>

Hello,  
I have a similar issue.  
I have tried to set up a .m file for my mod file so that I can run the simulations, but when changing the stderr for productivity shock eps\_a, I want it to automatically simulate and change the initial value. .

I want it to compute the data moments and simulated moments, and calculate the residuals.

And then run simulations until convergence. But I struggled to understand this example. Is there any other examples, Johannes? I have not done this before, so I struggle a bit

---

<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:** [4 July 2025 10:27 UTC](https://forum.dynare.org/t/perform-moment-matching-with-matlab/19031/5 "2025-07-04T10:27:24Z")

</div>

Can you provide your trial codes?

---

<div class="post-metadata">

**Author:** ![hosey\_29](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/8797f3/32.png) [@hosey\_29](https://forum.dynare.org/u/hosey_29)\
**Post date:** [4 July 2025 14:43 UTC](https://forum.dynare.org/t/perform-moment-matching-with-matlab/19031/7 "2025-07-04T14:43:18Z")

</div>

[calib.m](https://forum.dynare.org/uploads/short-url/fZhF6Z7bnlOWnJZzZh5Bu7dH4QK.m) (254 Bytes)  
I also added the sigma\_a to parameter and shock in mod file but fail to make.m file run

---

<div class="post-metadata">

**Author:** ![hosey\_29](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/8797f3/32.png) [@hosey\_29](https://forum.dynare.org/u/hosey_29)\
**Post date:** [4 July 2025 14:55 UTC](https://forum.dynare.org/t/perform-moment-matching-with-matlab/19031/8 "2025-07-04T14:55:42Z")

</div>

[calib.m](https://forum.dynare.org/uploads/short-url/fwPns9kaxBKs8fWkQVgeN7ASuSz.m) (458 Bytes)  
I also tried this but, It says I must declaree endogenous variables. But I feel it is still wrong

---

<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:** [5 July 2025 13:51 UTC](https://forum.dynare.org/t/perform-moment-matching-with-matlab/19031/9 "2025-07-05T13:51:56Z")

</div>

But the mod-file is missing.

---

<div class="post-metadata">

**Author:** ![hosey\_29](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/8797f3/32.png) [@hosey\_29](https://forum.dynare.org/u/hosey_29)\
**Post date:** [7 July 2025 15:43 UTC](https://forum.dynare.org/t/perform-moment-matching-with-matlab/19031/10 "2025-07-07T15:43:55Z")

</div>

All I am trying to do is change stderr for technology shock 0.01\*0; (for now set to zero) but I want to in my m file add a new value, and have it run simulation replacing the old value through the m file. And calculate the residuals between data and simulations.
