R中带有functools的非负ODE解决方案? [英] Non Negative ODE Solutions with functools in R?

查看:93
本文介绍了R中带有functools的非负ODE解决方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个处理非负ODE系统的算法.我需要在MATLAB中使用ode45之类的东西来定义必须为非负的状态.

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.

3年前,我已经讨论过这个问题,但没有真正的解决方案. deSolve仍然不是要走的路.我发现了一些看起来很有前途的python代码.也许这在R中也是可能的.最后,我必须定义一个函数包装器,如python中的functools.它的作用非常简单.这是python包装器的代码:

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

我的意思是在python中这很简单.包装程序将在

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()

R中是否可能相同?我知道也有一个functools包和functools函数.但是我不知道这是否真的有效.我可以在deSolve中使用事件吗?

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?

我现在已经在这个项目上工作了5年,但我没有想法.我使用了MATLAB,C ++和Python接口,但是所有这些都很慢,我在R中需要它.非常感谢您的帮助!

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!

推荐答案

deSolve出于充分的理由不支持自动非负约束.过去,我们曾多次提出这样的问题,但在所有这些情况下,结果都是负值的原因是模型规范不完整.典型的情况是某些内容是从空池中导出的.由于不必要的负值通常表示模型规格不足,因此我们(目前)不考虑在将来添加非负"约束.

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.

示例:在以下等式中,通过模型设计,X可以变为负数:

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

dX/dt = -k

dX/dt = -k

以下内容不能:

dX/dt = -k * X

dX/dt = -k * X

如果您需要在大多数情况下"在X变为零之前不久立即减小到零的线性下降,则可以使用Monod类型的防护措施(或类似方法):

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)

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

k2的选择相对不严格.与求解器的数值精度相比,它应该足够小而不影响整体性能,并且也不要太小.

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

此外,当然也可以在R中编写自己的包装器.

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

希望有帮助

这篇关于R中带有functools的非负ODE解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆