Non Negative ODE Solutions with functools in R?

南楼画角 提交于 2020-05-28 10:19:20

问题


I am trying to implement an R algortihm dealing with non-negative ODE Systems. I need something like ode45 in MATLAB to define states which have to be none-negative.

I discussed about that already 3 years ago but with no real solution. deSolve is still not the way to go. I found some python code which looks very promising. Maybe this is possible in R as well. In the end I have to define a function wraper, as functools in python. What it does is pretty simple. Here is the code the of the python wraper:

def wrap(f):

        @wraps(f)

        def wrapper(t, y, *args, **kwargs):

            low = y < 0

            y = np.maximum(y, np.ones(np.shape(y))*0)

            result = f(t, y, *args, **kwargs)

            result[too_low]  = np.maximum(result[low], np.ones(low.sum())*0)


            return result

        return wrapper

    return wrap

I mean in python this is straight forward. The wraper will be used in each step of the integration called by

solver = scipy.integrate.odeint(f, y0)
solution = solver.solve()

Is the same possible in R? I know there is a functools package and functools function, as well. But I have no clue if this really works. Can I use events in deSolve for that?

I am working now on this project for 5 years and I am out of ideas. I used an MATLAB, C++ and Python interface but all this is to slow, I need it in R. Thank you very much for your help!


回答1:


deSolve does not support automatic non-negativity constraints for good reasons. We had such questions several times in the past, but it turned out in all these cases, that the reason of the negative value was an incomplete model specification. The typical case is that something is exported from an empty pool. Because unwanted negative values are usually an indicator of an inadequate model specification, we do (currently) not consider to add a "non-negative" constraint in the future.

Example: in the following equation, X can become negative by model design:

dX/dt = -k

whereas the following cannot:

dX/dt = -k * X

If you need a linear decrease "most of the time" that reduces to zero shortly before X becomes zero, you can use a Monod-type safeguard (or something similar):

dX/dt = -k*X / (k2 + X)

The selection of k2 is relatively uncritical. It should be small enough not to influence the overall behavior and not too small, compared to the numerical accuracy of the solver.

Another method to avoid negative values is to work in log-transformed space. Here are some related threads:

https://stat.ethz.ch/pipermail/r-sig-dynamic-models/2010q2/000028.html

https://stat.ethz.ch/pipermail/r-sig-dynamic-models/2013q3/000222.html

https://stat.ethz.ch/pipermail/r-sig-dynamic-models/2016/000437.html

In addition, it is of course also possible to write an own wrapper in R.

Hope it helps



来源:https://stackoverflow.com/questions/56614347/non-negative-ode-solutions-with-functools-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!