1

好的,我正在撰写一篇关于在 Python 中生成增长函数的论文 ( Paine et al 2012 )。增长函数以多种不同的方式表示,我需要使用其中的一种以上(用于不同的应用程序)。

等式 1:Gompertz 函数表示为随时间变化的质量变化等式 2: Gompertz 函数表示为时间的函数

等式 1 是一个 Gompertz 函数,表示为每时间变化的质量变化,而等式 2 是相同的 Gompertz 函数,表示为经过的总时间的函数。一个根据当前质量计算质量变化,另一个根据它生长的时间计算质量。

我已经将它们翻译成python并编写了一些代码来检查它们是否匹配(因为它们是同一个方程的不同表达式,所以这应该不是问题......)

import numpy as np
import matplotlib.pyplot as plt

def gompertz_t(m_0, K, r, t):
    return K*(m_0/K)**np.exp(-r*t)

def gompertz_m(m, K, r):
    eq1 = r*m
    eq2 = np.log(K/m)
    return m+eq1*eq2

x = list(range(0,50,1))

m_0 = 0.1 #(must not be equal to zero)

#Set up values for year 0
y_1 = [m_0]
y_2 = [m_0]

for i in x[1:]:
    #And run for x years
    y_1.append(gompertz_m(y_1[-1],50,0.5))
    y_2.append(gompertz_t(y_2[0],50,0.5,i))

# df = pd.DataFrame(index = x)
# df["gompertz_m"] = y_1
# df["gompertz_t"] = y_2
# df.to_clipboard()

fig = plt.figure()
chart = fig.add_subplot(1, 1, 1)
chart.plot(x,y_1, label="Gompertz curve (time)")
chart.plot(x,y_2, label="Gompertz curve (mass)")

plt.legend()
fig.tight_layout()
plt.show()

然而......他们不匹配。结果曲线不一致。

一个对比

谁能告诉我这里出了什么问题?我在其他增长曲线上也遇到了同样的问题,所以我认为这是我的实现问题,而不是原始方程的问题。

4

1 回答 1

1

我相信差异来自等式1是等式2的导数这一事实。这意味着您的循环增量为1个时间单位(年)将用等式1“离散地”近似变化,而等式2产生连续值(即等式1的积分)。为了更好地拟合,您需要大大增加等式 1 的“采样率”。

为此,您需要使函数返回其真正的 delta 值(即不将其添加到质量参数中):

def gompertz_m(m, K, r):
    eq1 = r*m
    eq2 = np.log(K/m)
    return eq1*eq2           # removed the m + 

然后通过应用多个较小的时间间隔来提高采样率。

例如(每天采样):

for i in x[1:]:
    #And run for x years
    cumMass = y_1[-1]
    for _ in range(365):
        cumMass += gompertz_m(cumMass,50,0.5)/365
    y_1.append(cumMass)
    y_2.append(gompertz_t(y_2[0],50,0.5,i))
于 2020-03-06T16:19:11.613 回答