To be continue … …
这是创新训练传染病的
微分方程
模型Richards
曲线笔记
Logistic曲线拟合方法及初值的选取
因为COVID-19
累计病人数具有慢-快-慢
的变化特征,且初值较小,随着时间的延长逐渐增大,最终稳定
在一个饱和值上。Logistics回归分析中的Richards
模型能够很好地描述其初始生长阶段、快速增长阶段和稳定生长阶段的累计病人数变化情况。
Richards
方程可用下列微分方程描述
{ d V d t = η V m − γ V V ( t 0 ) = V 0 \begin{aligned} \begin{cases} \frac{\mathrm{d}V}{\mathrm{d}t}=\eta V^{m}-\gamma V\\\\ V(t_{0})=V_{0} \end{cases} \end{aligned} ⎩⎪⎨⎪⎧dtdV=ηVm−γVV(t0)=V0
这是 m m m次的 B e r n o u l l i Bernoulli Bernoulli微分方程,令 z = V 1 − m z=V^{1-m} z=V1−m,解的其通解
为
V ( t ) = { ( η γ ) [ 1 + ( V 0 1 − m − η γ e ( m − 1 ) γ t ) ] } 1 / ( 1 − m ) V(t)=\left\{(\frac{\eta}{\gamma})\big[1+(V_{0}^{1-m}-\frac{\eta}{\gamma}\mathrm{e}^{(m-1)\gamma t}) \big]\right\}^{1/(1-m)} V(t)={ (γη)[1+(V01−m−γηe(m−1)γt)]}1/(1−m)
求解过程如下:
d z d t = d z d V ⋅ d V d t = ( 1 − m ) V − m ⋅ d V d t \frac{\mathrm{d}z}{\mathrm{d}t}=\frac{\mathrm{d}z}{\mathrm{d}V}\cdot\frac{\mathrm{d}V}{\mathrm{d}t}=(1-m)V^{-m}\cdot\frac{\mathrm{d}V}{\mathrm{d}t} dtdz=dVdz⋅dtdV=(1−m)V−m⋅dtdV
以 V − m V^{-m} V−m同乘原微分方程两边,得
V 1 − m d V d t = η − γ V 1 − m V^{1-m}\frac{\mathrm{d}V}{\mathrm{d}t}=\eta-\gamma V^{1-m} V1−mdtdV=η−γV1−m
将 1 1 − m d z d t = V − m d V d t \frac{1}{1-m}\frac{\mathrm{d}z}{\mathrm{d}t}=V^{-m}\frac{\mathrm{d}V}{\mathrm{d}t} 1−m1dtdz=V−mdtdV和 z = V 1 − m z=V^{1-m} z=V1−m代入上式得
d z d t = γ ( m − 1 ) z + ( 1 − m ) η \frac{\mathrm{d}z}{\mathrm{d}t}=\gamma(m-1)z+(1-m)\eta dtdz=γ(m−1)z+(1−m)η
这是非齐次线性微分方程,即 P ( t ) = γ ( m − 1 ) , Q ( t ) = ( 1 − m ) η P(t)=\gamma(m-1),Q(t)=(1-m)\eta P(t)=γ(m−1),Q(t)=(1−m)η,根据通解公式可得
z = e ∫ P ( t ) d t ( ∫ Q ( t ) ⋅ e − ∫ P ( t ) d t d t + c ) = e ∫ γ ( m − 1 ) d t ( ∫ ( 1 − m ) η ⋅ e − ∫ ( γ ( m − 1 ) ) d t d t + c ) = e γ ( m − 1 ) t ( ∫ ( 1 − m ) η ⋅ e γ ( 1 − m ) t d t + c ) = e γ ( m − 1 ) t ( η γ e γ ( 1 − m ) t + c ) = V 1 − m \begin{aligned} z&=\mathrm{e}^{\int P(t)\mathrm{d}t}(\int Q(t)\cdot\mathrm{e}^{-\int P(t)\mathrm{d}t}\mathrm{d}t+c)\\ &=\mathrm{e}^{\int \gamma(m-1)\mathrm{d}t}(\int (1-m)\eta\cdot\mathrm{e}^{-\int (\gamma(m-1))\mathrm{d}t}\mathrm{d}t+c)\\ &=\mathrm{e}^{ \gamma(m-1)t}(\int (1-m)\eta\cdot\mathrm{e}^{ \gamma(1-m)t}\mathrm{d}t+c)\\ &=\mathrm{e}^{ \gamma(m-1)t}(\frac{\eta}{\gamma}\mathrm{e}^{\gamma(1-m)t}+c)=V^{1-m} \end{aligned} z=e∫P(t)dt(∫Q(t)⋅e−∫P(t)dtdt+c)=e∫γ(m−1)dt(∫(1−m)η⋅e−∫(γ(m−1))dtdt+c)=eγ(m−1)t(∫(1−m)η⋅eγ(1−m)tdt+c)=eγ(m−1)t(γηeγ(1−m)t+c)=V1−m
所以
V ( t ) = ( η γ ) 1 / ( 1 − m ) ( 1 + γ c η e γ ( m − 1 ) t ) 1 / ( 1 − m ) V(t)=(\frac{\eta}{\gamma})^{1/(1-m)}(1+\frac{\gamma c}{\eta}\mathrm{e}^{\gamma(m-1)t})^{1/(1-m)} V(t)=(γη)1/(1−m)(1+ηγceγ(m−1)t)1/(1−m)
其中 c c c为任意常数
其中, A = ( η γ ) 1 / ( 1 − m ) A=(\frac{\eta}{\gamma})^{1/(1-m)} A=(γη)1/(1−m), B = − γ η c B=-\frac{\gamma}{\eta}c B=−ηγc, k = γ ( 1 − m ) k=\gamma(1-m) k=γ(1−m),且满足初值 V ( t 0 ) = V 0 V(t_{0})=V_{0} V(t0)=V0,即
V ( t ) = A ( 1 − B e − k t ) 1 / ( 1 − m ) V(t)=A(1-Be^{-kt})^{1/(1-m)} V(t)=A(1−Be−kt)1/(1−m)
式中各参数的生物学意义是: A A A是累计生长的饱和值
, B B B是生长初始值参数
, k k k是生长速率参数
, m m m是异速增长参数
。
由上式可看出: lim n → ∞ V ( t ) = A \lim_{n\to\infty}V(t)=A limn→∞V(t)=A,此即累计生长的饱和值
对于具有“S”型
增长的Richards
方程 V ( t ) = A ( 1 − B e − k t ) 1 / ( 1 − m ) V(t)=A(1-Be^{-kt})^{1/(1-m)} V(t)=A(1−Be−kt)1/(1−m),根据
d V d t = η V m − γ V = V ( η V 1 − m − γ ) \frac{\mathrm{d}V}{\mathrm{d}t}=\eta V^{m}-\gamma V=V(\eta V^{1-m}-\gamma) dtdV=ηVm−γV=V(ηV1−m−γ)
由上式 V > 0 V>0 V>0时,推得当 0 < V < ( η γ ) 1 / ( 1 − m ) 0
0<V<(γη)1/(1−m) 时, d V d t > 0 \frac{\mathrm{d}V}{\mathrm{d}t}>0 dtdV>0恒成立,即 V ( t ) V(t) V(t)在 0 < V < ( η γ ) 1 / ( 1 − m ) 00<V<(γη)1/(1−m) 是递增的
递增
函数;由上式对 t t t求导,得
d 2 V d t 2 = η m V m − 1 − γ \frac{\mathrm{d}^{2}V}{\mathrm{d}t^{2}}=\eta mV^{m-1}-\gamma dt2d2V=ηmVm−1−γ
令 d 2 V d t 2 = 0 \frac{\mathrm{d}^{2}V}{\mathrm{d}t^{2}}=0 dt2d2V=0,即得 V = ( η m γ ) 1 / ( 1 − m ) = A m 1 / ( 1 − m ) V=(\frac{\eta m}{\gamma})^{1/(1-m)}=Am^{1/(1-m)} V=(γηm)1/(1−m)=Am1/(1−m),此时
V = A m 1 / ( 1 − m ) = A ( 1 − B e − k t ) 1 / ( 1 − m ) ⇒ t = 1 k ⋅ ln B 1 − m V=Am^{1/(1-m)}=A(1-Be^{-kt})^{1/(1-m)}\Rightarrow t=\frac{1}{k}\cdot\ln\frac{B}{1-m} V=Am1/(1−m)=A(1−Be−kt)1/(1−m)⇒t=k1⋅ln1−mB在该点上曲线的瞬时生长速率达到最大值,由 k = γ ( 1 − m ) k=\gamma(1-m) k=γ(1−m)得 − γ = − k 1 − m -\gamma=-\frac{k}{1-m} −γ=−1−mk,代入 A = ( η γ ) 1 / ( 1 − m ) A=(\frac{\eta}{\gamma})^{1/(1-m)} A=(γη)1/(1−m)得 η = k 1 − m A 1 − m \eta=\frac{k}{1-m}A^{1-m} η=1−mkA1−m,由 d V d t = η V m − γ V \frac{\mathrm{d}V}{\mathrm{d}t}=\eta V^{m}-\gamma V dtdV=ηVm−γV可得
d V d t = η V m − γ V = k 1 − m A 1 − m ⋅ A m m m 1 − m − γ A m 1 / ( 1 − m ) = k A 1 − m m m 1 − m ( m − 1 − 1 ) = k A m m / ( 1 − m ) \begin{aligned} \frac{\mathrm{d}V}{\mathrm{d}t}&=\eta V^{m}-\gamma V\\ &=\frac{k}{1-m}A^{1-m}\cdot A^{m}m^{\frac{m}{1-m}}-\gamma Am^{1/(1-m)}\\ &=\frac{kA}{1-m}m^{\frac{m}{1-m}}(m^{-1}-1)\\ &=kAm^{m/(1-m)} \end{aligned} dtdV=ηVm−γV=1−mkA1−m⋅Amm1−mm−γAm1/(1−m)=1−mkAm1−mm(m−1−1)=kAmm/(1−m)所以此时 d V d t = k A m m / ( 1 − m ) \frac{\mathrm{d}V}{\mathrm{d}t}=kAm^{m/(1-m)} dtdV=kAmm/(1−m)
拐点
t = 1 k ⋅ ln B 1 − m , V ( t ) = A m 1 / ( 1 − m ) t=\frac{1}{k}\cdot\ln\frac{B}{1-m},V(t)=Am^{1/(1-m)} t=k1⋅ln1−mB,V(t)=Am1/(1−m),且 d V d t = k A m m / ( 1 − m ) \frac{\mathrm{d}V}{\mathrm{d}t}=kAm^{m/(1-m)} dtdV=kAmm/(1−m);当 t → ∞ t\to\infty t→∞时, V ( t ) → A V(t)\to A V(t)→A
渐进线
,达到生物生长的饱和值;还可分析当 m > 1 m>1 m>1或 m < 1 m<1 m<1时曲线的性质
令 m − 1 = δ m-1=\delta m−1=δ,即得 V ( t ) = A ( 1 − B e − K t ) − 1 / δ V(t)=A(1-B\mathrm{e}^{-Kt})^{-1/\delta} V(t)=A(1−Be−Kt)−1/δ,对其用对数求导法得
ln y = − 1 δ [ ln A + ln ( 1 − B e − K t ) ] \ln y=-\frac{1}{\delta}[\ln A+\ln(1-B\mathrm{e}^{-Kt})] lny=−δ1[lnA+ln(1−Be−Kt)]
左右两边对 t t t求导得
y ′ y = − 1 δ ⋅ K B e − K t 1 − B e − K t \frac{y'}{y}=-\frac{1}{\delta}\cdot\frac{KB\mathrm{e}^{-Kt}}{1-B\mathrm{e}^{-Kt}} yy′=−δ1⋅1−Be−KtKBe−Kt
整理得,即
y ′ = − 1 δ ⋅ K B e − K t 1 − B e − K t ⋅ A ( 1 − B e − K t ) − 1 / δ = − 1 δ ⋅ K B e − K t ⋅ y ⋅ ( y A ) δ = − K B δ A − δ ⋅ e − K t ⋅ y δ + 1 \begin{aligned} y'&=-\frac{1}{\delta}\cdot\frac{KB\mathrm{e}^{-Kt}}{1-B\mathrm{e}^{-Kt}}\cdot A(1-B\mathrm{e}^{-Kt})^{-1/\delta}\\ &=-\frac{1}{\delta}\cdot KB\mathrm{e}^{-Kt}\cdot y\cdot(\frac{y}{A})^{\delta}\\ &=-\frac{KB}{\delta}A^{-\delta}\cdot \mathrm{e}^{-Kt}\cdot y^{\delta+1} \end{aligned} y′=−δ1⋅1−Be−KtKBe−Kt⋅A(1−Be−Kt)−1/δ=−δ1⋅KBe−Kt⋅y⋅(Ay)δ=−δKBA−δ⋅e−Kt⋅yδ+1
再令 − B A − δ = C -BA^{-\delta}=C −BA−δ=C,对上式取对数,得
ln y ′ = ln ( K δ C ) + ( δ + 1 ) ln y ( t ) = K t \ln y'=\ln(\frac{K}{\delta}C)+(\delta+1)\ln y(t)=Kt lny′=ln(δKC)+(δ+1)lny(t)=Kt
用向后差分 Δ h y ( t ) = y ( t ) − y ( t − 1 ) \Delta_{h}y(t)=y(t)-y(t-1) Δhy(t)=y(t)−y(t−1)估计 y ′ y' y′,并令
{ ln K + ln C − ln δ = β 0 δ + 1 = β 1 − K = β 3 \begin{aligned} \begin{cases} \ln K+\ln C-\ln\delta=\beta_{0}\\ \delta+1=\beta_{1}\\ -K=\beta_{3} \end{cases} \end{aligned} ⎩⎪⎨⎪⎧lnK+lnC−lnδ=β0δ+1=β1−K=β3设误差为 e ( t ) e(t) e(t)
即得
ln Δ h y ( t ) = β 0 + β 1 ln y ( t ) + β 2 t + e ( t ) \ln\Delta_{h}y(t)=\beta_{0}+\beta_{1}\ln y(t)+\beta_{2}t+e(t) lnΔhy(t)=β0+β1lny(t)+β2t+e(t)
将其写成矩阵形式为
[ ln Δ h y ( 2 ) ln Δ h y ( 3 ) ⋮ ln Δ h y ( n ) ] = [ 1 ln y ( 2 ) 2 1 ln y ( 3 ) 3 ⋮ 1 ln y ( n ) n ] [ β 0 β 1 β 2 ] + [ e ( 2 ) e ( 3 ) ⋮ e ( n ) ] \begin{bmatrix} \ln\Delta_{h}y(2)\\ \ln\Delta_{h}y(3)\\ \vdots\\ \ln\Delta_{h}y(n) \end{bmatrix}= \begin{bmatrix} 1&\ln y(2)&2\\ 1&\ln y(3)&3\\ \vdots\\ 1&\ln y(n)&n \end{bmatrix} \begin{bmatrix} \beta_{0}\\ \beta_{1}\\ \beta_{2} \end{bmatrix}+ \begin{bmatrix} e(2)\\ e(3)\\ \vdots\\ e(n) \end{bmatrix} ⎣⎢⎢⎢⎡lnΔhy(2)lnΔhy(3)⋮lnΔhy(n)⎦⎥⎥⎥⎤=⎣⎢⎢⎢⎡11⋮1lny(2)lny(3)lny(n)23n⎦⎥⎥⎥⎤⎣⎡β0β1β2⎦⎤+⎣⎢⎢⎢⎡e(2)e(3)⋮e(n)⎦⎥⎥⎥⎤
即
Y = X β + e Y=X\beta+e Y=Xβ+e
其中
X = [ 1 ln y ( 2 ) 2 1 ln y ( 3 ) 3 ⋮ 1 ln y ( n ) n ] X=\begin{bmatrix} 1&\ln y(2)&2\\ 1&\ln y(3)&3\\ \vdots\\ 1&\ln y(n)&n \end{bmatrix} X=⎣⎢⎢⎢⎡11⋮1lny(2)lny(3)lny(n)23n⎦⎥⎥⎥⎤
并且 Y = ( ln Δ h y ( 2 ) ln Δ h y ( 2 ) ⋯ ln Δ h y ( n ) ) T Y=(\ln\Delta_{h}y(2)\ \ln\Delta_{h}y(2)\ \cdots\ \ln\Delta_{h}y(n))^{T} Y=(lnΔhy(2) lnΔhy(2) ⋯ lnΔhy(n))T, β = ( β 0 β 1 β 1 ) T \beta=(\beta_{0}\ \beta_{1}\ \beta_{1})^{T} β=(β0 β1 β1)T, e = ( e ( 2 ) e ( 3 ) ⋯ e ( n ) T e=(e(2)\ e(3)\ \cdots\ e(n)^{T} e=(e(2) e(3) ⋯ e(n)T
并假设有 E ( e ) = 0 , c o v ( e ) = σ 2 I n × n E(e)=0,cov(e)=\sigma^{2}I_{n\times n} E(e)=0,cov(e)=σ2In×n
此时变为线性模型,则根据最小二乘法估计原理可得
β ^ = L − 1 X T Y \hat{\beta}=L^{-1}X^{T}Y β^=L−1XTY
其中, L = X T X L=X^{T}X L=XTX
到此即可估计出 β 0 , β 1 , β 2 \beta_{0},\beta_{1},\beta_{2} β0,β1,β2的值,进而可估计出 δ , K , C \delta,K,C δ,K,C的值
由 − B A − δ = C -BA^{-\delta}=C −BA−δ=C及 V ( t ) = A ( 1 − B e − K t ) − 1 / δ V(t)=A(1-B\mathrm{e}^{-Kt})^{-1/ \delta} V(t)=A(1−Be−Kt)−1/δ可得
( A V ( t ) ) δ = 1 + C A δ e − K t (\frac{A}{V(t)})^{\delta}=1+CA^{\delta}\mathrm{e}^{-Kt} (V(t)A)δ=1+CAδe−Kt
即
A δ = V δ ( t ) ⋅ ( 1 + C A δ e − K t ) A^{\delta}=V^{\delta}(t)\cdot(1+CA^{\delta}\mathrm{e}^{-Kt}) Aδ=Vδ(t)⋅(1+CAδe−Kt)
整理得
A = ( V δ ( t ) 1 − C V δ ( t ) e − K t ) 1 / δ ⇒ A = ( V − δ ( t ) − C e − K t ) − 1 / δ \begin{aligned} A&=\big(\frac{V^{\delta}(t)}{1-CV^{\delta}(t)\mathrm{e}^{-Kt}}\big)^{1/\delta}\\ &\Rightarrow A=(V^{-\delta}(t)-C\mathrm{e}^{-Kt})^{-1/\delta} \end{aligned} A=(1−CVδ(t)e−KtVδ(t))1/δ⇒A=(V−δ(t)−Ce−Kt)−1/δ
代入一个点 ( t , V ( t ) ) (t,V(t)) (t,V(t)),即可得 A A A的估计式,进而得到 B B B的估计式为 B ^ = − C ^ A ^ δ \hat{B}=-\hat{C}\hat{A}^{\delta} B^=−C^A^δ。
综上所述,Richards增长曲线方程的四个待估参数的初始值得以确定。
而后利用L-M算法求得最优解。
对于如上的数据,首先求出一组初值
clear
clc
%确定四个待估参数的初始值
y=[41,45,62,291,440,571,830,1287,1975,2744,4515,5974,7711,9692,11791];
ydelta=y(2:end)-y(1:end-1);
lny=log(ydelta);n=length(y);
x1=ones(n-1,1);x2=log(y(2:end))';x3=2:n;x3=x3';
X=[x1,x2,x3];
Y=lny';
belta=(inv(X'*X))*X'*Y;
syms delta K C m A B
eq1=log(K)+log(C)-log(delta)==belta(1);
eq2=delta+1==belta(2);
eq3=-K==belta(3);
eq4=(y(4)^(-delta)-C*exp(-4*K))^(-1/delta)==A;
eq5=delta+1==m;
eq6=-C*A^delta==B;
[delta,K,C,m,A,B]=solve(eq1,eq2,eq3,eq4,eq5,eq6,delta,K,C,m,A,B);
disp(vpa(A,4));disp(vpa(B,4));
disp(vpa(K,4));disp(vpa(m,4));
初值利用L-M算法求得最优解
clear
clc
x=1:15;
%用所求初始值利用梯度降速算法求得最优解
y=[41,45,62,291,440,571,830,1287,1975,2744,4515,5974,7711,9692,11791];
c0=[14355,-172.2,0.3966,1.921];
fun=inline('c(1)*(1-c(2)*exp(-c(3)*x)).^(1/(1-c(4)))','c','x');
b=lsqcurvefit(fun,c0,x,y);disp(b);
t=0:0.1:30;T=fun(b,t);
plot(x,y,'rs',t,fun(b,t),'b');set(gca,'ygrid','on');
legend("真实值","预测曲线");xlabel("日数");ylabel("累计病人数");
hold on;
err=abs(sum(T(1:15)-y));disp(err);
同理可以用python做非线性最小二乘拟合
''' 给定一组数据,利用非线性曲线拟合方法
拟合Richards方程
'''
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
plt.style.use('ggplot') #使用ggplot绘图风格
#plt.rcdefaults() #恢复默认画图方式
#给出一组数据
x=np.array(range(1,16))
y=np.array([41,45,62,291,440,571,830,1287,1975,2744,4515,5974,7711,9692,11791])
plot1=plt.plot(x,y,'x',label="confirm")
c0=np.array([14355,-172.2,0.3966,1.921])#根据线性模型计算得到的一组初值
def func(x,a,b,c,d):
result=a*(1-b*np.exp(-c*x))**(1/(1-d))
return result
p_est,err_est=curve_fit(func,x,y,c0)
#print(p_est)
plot2=plt.plot(x,func(x, *p_est), "-",label="curve-fitting")
plt.legend(loc=0) #指定legend的位置右上角