【问题标题】:Changing the recovery rate of SIR model改变 SIR 模型的回收率
【发布时间】:2019-07-29 03:12:23
【问题描述】:
import networkx as nx
from collections import defaultdict
from collections import Counter

def test_transmission(u, v, p):

    return random.random()<p


def discrete_SIR(G,w,initial_infecteds,beta,Vl,duration):

    if G.has_node(initial_infecteds):
        initial_infecteds=[initial_infecteds]           

    N=G.order()
    #t = [tmin]
    S = [N-len(initial_infecteds)]
    #I = [len(initial_infecteds)]
    R = [0]
    V = [0]

    susceptible = defaultdict(lambda: True)  
    #above line is equivalent to u.susceptible=True for all nodes.

    for u in initial_infecteds:
        susceptible[u] = False

    infecteds = [{}]*duration  #bunch of empty sets  
    infecteds[0] = set(initial_infecteds)

    I = [sum(map(len, infecteds))]  #set I[0] to be the total number of infections

    while I[-1]>0 :
        new_infecteds = set()
        vaccinated= set()

        for u in infecteds:
            for v in G.neighbors(u):
                if len(vaccinated)+V[-1]< (Vl*N)  : #check if vaccination over or not


                    if susceptible[v] and test_transmission(u, v, w): 
                        vaccinated.add(v)
                        susceptible[v] = False
         #               print('transmitting vaccination')

                    elif susceptible[v] and test_transmission(u,v,beta):
                        new_infecteds.add(v)
                        susceptible[v]=False
         #               print('transmitting infection')
                else:

        #            print("BYE")
                    if susceptible[v] and test_transmission(u, v,beta): 
                        new_infecteds.add(v)
                        susceptible[v] = False

               #infector[v] = [u]
        recovering_nodes = infecteds.pop()

        infecteds.insert(0,new_infecteds)      


        infecteds = new_infecteds

        I.append(sum(map(len, infecteds)))

        R.append(R[-1]+I[-1])
        V.append(len(vaccinated)+V[-1])
        S.append(N-V[-1]-I[-1]-R[-1])




    return scipy.array(S),scipy.array(V), scipy.array(I),scipy.array(R)



m=100
w=0.2
#ran=nx.gnp_random_graph(100,0.003)
G=nx.grid_2d_graph(m,m,periodic=True)

initial_infections = [(u,v) for (u,v) in G if u==int(m/2) and v==int(m/2)]



S, V, I, R = discrete_SIR(G,w,initial_infecteds=initial_infections,beta=0.5,Vl=1,duration=8)            

这是 SIR 模型的代码,但这是用于恢复率 1。我想更改此代码以包含可变参数恢复率,而不是默认值,在这种情况下为 1。我试图更改代码以包含它。基本代码是 SIR 模型。

我在修改后的 SIR 模型中添加了 Joels 帖子中所做的更改。

用于记账-

    next_time = t[-1]+1
    if next_time <= tmax:
        for i in infecteds:
            for u in i:
                node_history[u][0].append(next_time+duration)
                node_history[u][1].append('R')
        for j in new_infecteds:
            for v in j:
                node_history[v][0].append(next_time)
                node_history[v][1].append('I')     

【问题讨论】:

  • 您是否希望它们在每个时间步以概率 q 恢复并以概率 p 传输?或者你在想什么不同的东西?在第一种情况下,此代码的一些小修改将起作用。在第二种情况下,您可能最终需要查看EoN.fast_nonMarkov_SIR
  • @Joel 第一个时间步,初始受感染节点将感染概率 p。假设其中有 5 人被感染。这五个节点将在每个时间步继续感染概率 p,直到它们在 Tr 时间步后仍未恢复。
  • 我需要再澄清一点。如果其中 5 个被感染,我们如何决定这 5 个何时恢复(所有节点的持续时间都相同吗?一些更复杂的分布?)。在他们感染期间,他们是否在每个时间步都以p 的概率传播,还是在感染期间发生变化?
  • @Joel 如果恢复率为 10,则受感染的节点在被感染后的 10 个时间步后恢复。在感染期间,他们将在每个时间步以相同的概率 p 感染,直到他们恢复

标签: python numpy networkx eon


【解决方案1】:

infecteds 是一个集合列表,这样infecteds[T] 是那些刚刚被感染的,infecteds[T-1] 是那些已经被感染了1个时间步的人,等等。然后弹出infecteds[0] [例如,@987654325 @] 并将新感染的节点追加到列表中。

然后对于每个时间步,只需遍历感染者中的所有集合。

这是一些相关的伪代码:

duration = 8
infecteds = [{}]*duration  #bunch of empty sets  
infecteds[0] = {1,2,3}
I = [sum(map(len, infecteds))]  #set I[0] to be the total number of infections

while I[-1] >0:
    new_infecteds = {}
    for infected_set in infecteds:
        for infected_node in infected_set:
            Do some stuff with the node and its neighbors.
            new_infecteds gets some things added to it.
    recovering_nodes = infecteds.pop()

    infecteds.insert(0,new_infecteds)

    for node in recovering_nodes:
        update status and do any bookkeeping.

    I.append(sum(map(len, infecteds)))

请谨慎使用“利率”一词。更高的速率应该意味着更快的恢复,因此更短的持续时间(持续时间就像 1/速率)。您的评论似乎使用“速率”一词来表示“持续时间”,因此对您而言,更高的“速率”实际上是更长的“持续时间”。这与大多数人理解你的意思相反。

【讨论】:

  • 你能用一些例子详细说明你的答案吗?..我听不懂
  • 我不明白你为什么在感染一个时间步后弹出感染者?..他们应该在整个 8 的持续时间内都在感染吗?
  • infecteds 由一堆不同的集合组成。我只弹出在duration 单位时间内被感染的那些。其他列表仍保留在集合中。
  • 那么recovering_nodes有什么用?只是记账吗?
  • 我也在我的 SIRV 模型中进行了更改,但它不起作用。我在问题中添加了修改后的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-22
  • 2021-08-18
  • 2020-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-18
相关资源
最近更新 更多