# Non-negative variable

**URL:** <https://forum.dynare.org/t/non-negative-variable/21475>\
**Category:** Stochastic simulations\
**Created:** [19 November 2022 10:54 UTC](https://forum.dynare.org/t/non-negative-variable/21475 "2022-11-19T10:54:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mara](https://forum.dynare.org/letter_avatar_proxy/v4/letter/m/71e660/32.png) [@mara](https://forum.dynare.org/u/mara)\
**Post date:** [19 November 2022 10:54 UTC](https://forum.dynare.org/t/non-negative-variable/21475/1 "2022-11-19T10:54:58Z")

</div>

Dear all,  
I am quite new to Dynare. I would like to code up the following:

v\_t = s\_t-sbar-Omega if s\_t \> sbar+Omega. Otherwise (i.e. if s\_t \<= sbar+Omega) v\_t should be zero.

What I tried in the Dynare model block is

1. v = max(0, (s-sbar-Omega));  
and
2. v= (s\>(sbar+Omega))\*(s-sbar-Omega);

However both do not seem to do what I need. While the first suggestion yields also negative values for v\_t, the second suggestion is always zero.

Could anybody help me on this issue? Many thanks in advance.

---

<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:** [21 November 2022 08:49 UTC](https://forum.dynare.org/t/non-negative-variable/21475/2 "2022-11-21T08:49:19Z")

</div>

Are you doing stochastic simulations or perfect foresight?

---

<div class="post-metadata">

**Author:** ![mara](https://forum.dynare.org/letter_avatar_proxy/v4/letter/m/71e660/32.png) [@mara](https://forum.dynare.org/u/mara)\
**Post date:** [21 November 2022 16:20 UTC](https://forum.dynare.org/t/non-negative-variable/21475/3 "2022-11-21T16:20:02Z")

</div>

Currently I am trying stochastic simulation.

---

<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:** [21 November 2022 16:28 UTC](https://forum.dynare.org/t/non-negative-variable/21475/4 "2022-11-21T16:28:01Z")

</div>

That’s the problem with linearizations:

> [@Non-negativity constraints](https://forum.dynare.org/t/non-negativity-constraints/2606/2):
>
> First, Dynare does not impose non-negativity constraints. How should it do that and know for which variables such a constraint applies? Second, in a linearly approximated model, there are no bounds on the endogeneous variables as every policy rule is linear. Hence the curvature implied by the Inada conditions is always lost. Using higher order approximation may make the problem better but will not get rid of it (unless you go to order infinity)

---

<div class="post-metadata">

**Author:** ![mara](https://forum.dynare.org/letter_avatar_proxy/v4/letter/m/71e660/32.png) [@mara](https://forum.dynare.org/u/mara)\
**Post date:** [22 November 2022 08:00 UTC](https://forum.dynare.org/t/non-negative-variable/21475/5 "2022-11-22T08:00:17Z")

</div>

So, did I get it correctly that what I would like to do does not work, at least no when doing stochastic simulation (what is with perfect foresight?)?

Would it then be possible to have something like  
if x\> some threshold, y=f(x) and otherwise if x\< some threshold, y=g(x), with f(x) and g(x) being 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:** [22 November 2022 14:29 UTC](https://forum.dynare.org/t/non-negative-variable/21475/6 "2022-11-22T14:29:44Z")

</div>

It depends on the context. You could use a piecewise linear solution (OccBin) or perfect foresight simulation with mcp-tags.

---

<div class="post-metadata">

**Author:** ![mara](https://forum.dynare.org/letter_avatar_proxy/v4/letter/m/71e660/32.png) [@mara](https://forum.dynare.org/u/mara)\
**Post date:** [23 November 2022 07:40 UTC](https://forum.dynare.org/t/non-negative-variable/21475/7 "2022-11-23T07:40:32Z")

</div>

Thank you so much for your help
