Variable transformation inside mod file

Hello all,

I have a model and where I would like to transform a variable in the following way:
x_{t} = \frac{x^{\ast}_{t}+\sqrt{\left(x^{\ast}_{t}\right)^{2}}}{2}
so that x_{t} = x^{\ast}_{t} for x^{\ast}_{t}\geq0, otherwise x_{t} =0.
This is fine in Matlab but it doesn’t seem to work in Dynare (for any order). I just end up obtaining x_{t} = x^{\ast}_{t} and I suspect it’s because the square root is cancelling with the squared variable.
Is there a way of forcing Dynare to square the variable and then to take the square root or am I doing something wrong?
Thank you.

The function you describe is not differentiable at 0. Performing a polynomial approximation with stoch_simul will obviously not capture the kink at 0. That seems to be what you are experiencing. If you want that function, you need a solution technique being able to handle such things. You could use OccBin.

Dear Johannes,

Thank you for the prompt reply. What if the steady state for both x^{\ast} = x = \bar x>0?
The function would then be differentiable but having tried this in Dynare still doesn’t give the results one would expect.

That above description of the amended function does not make sense. What is \bar x in this case?

Dear Johannes,

You’re right. I should have clarified that in this case the equation should be
x_{t}-x_{0} = \frac{\left(x^{\ast}_{t}-x_{0}\right)+\sqrt{\left(x^{\ast}-x_{0}\right)^{2}}}{2}

Unless I’m making a silly mistake, the steady states of x_{t}^{\ast} and x_{t} are the same and need not be equal to x_{0} (the lower bound).
Then, the above is differentiable at the steady state (coming from some other equation) that is larger than x_{0}.
I hope that the above makes sense and thank you once again for your help.

Yes, that function is differentiable at the steady state, but again, taking a linear approximation at the steady state will not be representative for the function left of the kink.

Dear Johannes,

You’re right, I’d forgotten that even with a second order expansion the shape is not preserved.
That was very useful, thank you.