# Search optimal loss function

**URL:** <https://forum.dynare.org/t/search-optimal-loss-function/13072>\
**Category:** Optimal Policy\
**Created:** [10 January 2019 06:18 UTC](https://forum.dynare.org/t/search-optimal-loss-function/13072 "2019-01-10T06:18:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiang](https://forum.dynare.org/letter_avatar_proxy/v4/letter/j/3e96dc/32.png) [@jiang](https://forum.dynare.org/u/jiang)\
**Post date:** [10 January 2019 06:18 UTC](https://forum.dynare.org/t/search-optimal-loss-function/13072/1 "2019-01-10T06:18:45Z")

</div>

Dear dynare team, now I have a question. I write the loss function:  
Loss = var(inflation)+alpha\*var(output)  
let the parameters alpha belongs to [0, 0.2, 0.4, …,2]. I use the standard Taylor Rule, policy response parameters take grid values, that is  
psi\_pi = 0:0.5:5 psi\_y = 0:0.25:2.5  
How I search policy parameters (psi\_pi, psi\_y) to minimum loss function value for each given alpha ? thank you so much.

My best,  
ZL

---

<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:** [10 January 2019 19:43 UTC](https://forum.dynare.org/t/search-optimal-loss-function/13072/2 "2019-01-10T19:43:37Z")

</div>

Please search the forum, for example [Loop over parameters to find maximized welfare](https://forum.dynare.org/t/loop-over-parameters-to-find-maximized-welfare/12358/11)

---

<div class="post-metadata">

**Author:** ![jiang](https://forum.dynare.org/letter_avatar_proxy/v4/letter/j/3e96dc/32.png) [@jiang](https://forum.dynare.org/u/jiang)\
**Post date:** [11 January 2019 12:05 UTC](https://forum.dynare.org/t/search-optimal-loss-function/13072/3 "2019-01-11T12:05:06Z")

</div>

Thanks so much, but in the sample code by you provided:  
weight\_grid=0:0.01:1;  
n\_grid\_points=length(weight\_grid);  
var\_y=NaN(n\_grid\_points,1);  
var\_pi=NaN(n\_grid\_points,1);  
for grid\_iter=1:n\_grid\_points  
M\_.osr.variable\_weights(pi\_pos,pi\_pos) = weight\_grid(grid\_iter);  
M\_.osr.variable\_weights(y\_pos,y\_pos) = (1-weight\_grid(grid\_iter));  
oo\_.osr = osr(var\_list\_,M\_.osr.param\_names,M\_.osr.variable\_indices,M\_.osr.variable\_weights);  
if oo\_.osr.error\_indicator==0  
var\_y(grid\_iter)=oo\_.var(y\_pos\_var\_list\_,y\_pos\_var\_list\_);  
var\_pi(grid\_iter)=oo\_.var(pi\_pos\_var\_list\_,pi\_pos\_var\_list\_);  
end  
end  
I want to ask this line:  
oo\_.osr = osr(var\_list\_,M\_.osr.param\_names,M\_.osr.variable\_indices,M\_.osr.variable\_weights);  
what dose it means? What I confused is in reference I saw that M\_.osr.param\_names, M\_.osr.variable\_indices, M\_.osr.variable\_weights are output by an execution of the command osr, so it cannot write in options which specified in command osr. Please help me, 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 January 2019 12:21 UTC](https://forum.dynare.org/t/search-optimal-loss-function/13072/4 "2019-01-11T12:21:39Z")

</div>

See

> [@Rerun OSR to check for optimality or local extremum](https://forum.dynare.org/t/rerun-osr-to-check-for-optimality-or-local-extremum/12988/7):
>
> Your loop is improper because i) you are looping over the Dynare syntax of the command, which is extremely error prone;(you should use the functional command syntax that I outline above) and ii) your should be using the set\_param\_value command to update parameters (what you did above by assigning a value to the parameter name directly in the loop does not assure correct updating) opt\_algo=4 is a Newton-based optimizer and therefore local by construction. opt\_algo=9 in contrast is a global opti…

---

<div class="post-metadata">

**Author:** ![jiang](https://forum.dynare.org/letter_avatar_proxy/v4/letter/j/3e96dc/32.png) [@jiang](https://forum.dynare.org/u/jiang)\
**Post date:** [11 January 2019 12:23 UTC](https://forum.dynare.org/t/search-optimal-loss-function/13072/5 "2019-01-11T12:23:57Z")

</div>

Thank you for professor jpfeifer, I read this right now. Thanks!
