gpt4 book ai didi

python - 在 python 2.7 中有效的循环

转载 作者:太空宇宙 更新时间:2023-11-04 10:23:27 25 4
gpt4 key购买 nike

我正在进行一项关于蚊帐对蚊子数量的影响以及它如何影响疟疾传播的研究。 EIR 告诉我们一个人一年内被传染性叮咬的次数。为了计算这个,我必须在 python 中求和,它使用递归函数 S(t)。该系列在大约 50 次迭代后收敛,但如果我将求和限制增加到 20 以上,则需要很长时间(大约一个小时)。我可能做错了什么,非常感谢你的帮助。用python编写的代码如下所示。

##IMPORTING PACKAGES
import sympy as sy
import matplotlib.pyplot as plt
import numpy as np
import profile

##Declaring the constants.
f = 1/3 ## frequency of feeding under zero net coverage
tau_1 = 0.69 ## time spent seeking blood under zero net coverage
tau_2 = 2.31 ## time spent resting during feeding cycle
Q_0 = 0.38 ## proportion of blood meals on humans under zero net coverage
mu_M0 = 0.096 ## daily mosquito mortality under zero net coverage
p_1 =0.91 ##probability of a mosquito survivng a blood meal at zero net coverage
p_2 = 0.74 ##probability of surviving resting phase at zero net coverage
phi_LLIN = 0.89 ##proportion of bites taken on humans when in bed
d_LLIN = 0.41 ##probability that a mosquito is killed by LLIN
s_LLIN = 0.03 ##probability that a mosquito feeds successfully with LLIN
r_LLIN = 0.56 ##probability that a mosquito is repelled by LLIN
phi_IRS = 0.97 ##proportion of bites taken on humans while indoors
psi = 0.86

#chi_LLIN = sy.symbols('chi_LLIN')
chi_LLIN=np.linspace(0.0,1.0,101)
w_LLIN =1. - Q_0*chi_LLIN*phi_LLIN*(1.-s_LLIN);w_LLIN

z_LLIN = Q_0 *chi_LLIN*phi_LLIN*r_LLIN; z_LLIN
tau_1chiLLIN = tau_1 /(1.-z_LLIN);tau_1chiLLIN
f_chiLLIN =1./ (tau_1chiLLIN + tau_2);f_chiLLIN
p1_chiLLIN = (p_1*w_LLIN)/(1.-(z_LLIN*p_1));p1_chiLLIN
p_chiLLIN = (p1_chiLLIN*p_2)**f_chiLLIN;p_chiLLIN
mu_MchiLLIN = -np.log(p_chiLLIN);mu_MchiLLIN

##Variable human blood index
Q_LLIN = Q_0 * ((1.+phi_LLIN*chi_LLIN*(s_LLIN-1.))/(1.+Q_0*phi_LLIN*chi_LLIN*(s_LLIN-1.) ))


##DEFINING E
totalx =39 ##limit of the summation
def E1(x):
E =0.
for t in xrange(1,x+1):
E = E+((p_chiLLIN)**t)/f_chiLLIN
return E


##DEFINING THE SPOROZOITE INFECTION PREVALENCE
n = 11 #the threshold
kappa=0.0297 ##human infectivity to mosquitoes

def S(x):
if x<=n:
return 0
elif x==n+1:
return 0.05
else:
return S(x-1) + ((kappa*Q_0*(1-S(x-1)))/f_chiLLIN)

B=0
for x in xrange(1,totalx+1):
B = B + ((p_chiLLIN)**x)/f_chiLLIN*S(x)


beta_h = (Q_0/f_chiLLIN)*B

EIR = beta_h * E1(totalx)

EIR

最佳答案

def S(x):
if x<=n:
return 0
elif x==n+1:
return 0.05
else:
return S(x-1) + ((kappa*Q_0*(1-S(x-1)))/f_chiLLIN)
# ^^^^^^ ^^^^^^

S(x) 中,您进行了 2 次递归调用来计算 S(x-1),这进行了 4 次递归调用来计算 S(x -2),这会进行 8 次递归调用以计算 S(x-3),依此类推,直到达到递归的基本情况。您不需要重新计算太多东西;只需计算一次 S(x-1) 并使用该值两次:

def S(x):
if x<=n:
return 0
elif x==n+1:
return 0.05
else:
s_lower = S(x-1)
return s_lower + ((kappa*Q_0*(1-s_lower))/f_chiLLIN)

不过,这仍然会进行比必要更多的重新计算,因为每次调用 S 时:

for x in xrange(1,totalx+1):
B = B + ((p_chiLLIN)**x)/f_chiLLIN*S(x)

它必须再次将 S 的所有值重新计算为基本情况。考虑进一步更改,例如 memoizing S.

关于python - 在 python 2.7 中有效的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30859867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com