Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Julia中随机SIR模型的建立_Julia - Fatal编程技术网

Julia中随机SIR模型的建立

Julia中随机SIR模型的建立,julia,Julia,我是julia的新手,希望通过以下方式创建随机SIR模型: 我写了自己的解释,几乎是一样的: # Following the Gillespie algorthim: # 1. Initialization of states & parameters # 2. Monte-carlo step. Random process/step selection. # 3. Update all states. e.g., I = I + 1 (increase of infected by

我是julia的新手,希望通过以下方式创建随机SIR模型:

我写了自己的解释,几乎是一样的:

# Following the Gillespie algorthim:
# 1. Initialization of states & parameters
# 2. Monte-carlo step. Random process/step selection.
# 3. Update all states. e.g., I = I + 1 (increase of infected by 1 person). Note: only +/- by 1.
# 4. Repeat until stopTime.

# p - Parameter array: β, ɣ for infected rate and recovered rate, resp. 
# initialState - initial states of S, I, R information.
# stopTime - Total run time. 
using Plots, Distributions

function stochasticSIR(p, initialState, stopTime)

    # Hold the states of S,I,R separately w/ a NamedTuple. See '? NamedTuple' in the REML for details
    # Populate the data storage arrays with the initial data and initialize the run time

    sirData = (dataₛ = [initialState[1]], dataᵢ = [initialState[2]], dataᵣ = [initialState[3]], time = [0]);


    while sirData.time[end] < stopTime

        if sirData.dataᵢ[end] == 0 # If somehow # of infected = 0, break the loop.
            break
        end   

        # Probabilities of each process (infection, recovery). p[1] = β and p[2] = ɣ
        probᵢ = p[1] * sirData.dataₛ[end] * sirData.dataᵢ[end];
        probᵣ = p[2] * sirData.dataᵣ[end];
        probₜ  = probᵢ + probᵣ; # Total reaction rate

        # When the next process happens
        k    = rand(Exponential(1/probₜ));
        push!(sirData.time, sirData.time[end] + k) # time step by k

        # Probability that the reaction is:
        #    probᵢ, probᵣ resp. is: probᵢ / probₜ, probᵣ / probₜ
        randNum = rand();

        # Update the states by randomly picking process (Gillespie algo.)
        if randNum < (probᵢ / probₜ)
            push!(sirData.dataₛ, sirData.dataₛ[end] - 1);
            push!(sirData.dataᵢ, sirData.dataᵢ[end] + 1);
        else
            push!(sirData.dataᵢ, sirData.dataᵢ[end] - 1);
            push!(sirData.dataᵣ, sirData.dataᵣ[end] +1)
        end

    end

end



sirOutput = stochasticSIR([0.0001, 0.05], [999,1,0], 200)
#plot(hcat(sirData.dataₛ, sirData.dataᵢ, sirData.dataᵣ), sirData.time)
遵循Gillespie算法: # 1. 状态和参数的初始化 # 2. 蒙特卡罗步骤。随机过程/步骤选择。 # 3. 更新所有状态。e、 g,I=I+1(增加1人感染的人数)。注:仅+/-乘以1。 # 4. 重复此操作直到停止。 #p-参数数组:β,ɣ表示感染率和恢复率,分别为。 #initialState—S、I、R信息的初始状态。 #stopTime—总运行时间。 使用绘图、分布 函数随机SIR(p,初始状态,停止时间) #分别保存S、I、R的状态,并命名为双。看到了吗?详细信息请参见REML中的NamedTuple #使用初始数据填充数据存储阵列并初始化运行时 sirData=(数据ₛ = [初始状态[1]],数据ᵢ = [初始状态[2]],数据ᵣ = [initialState[3]],时间=[0]); 而sirData.time[结束] 错误:

不精确错误:Int64(2.508057234147307)

Stacktrace:[1]Int64 at.\float.jl:709[内联][2]转换at .\number.jl:7[内联][3]推!at.\array.jl:868[内联][4] 随机sir(::数组{Float64,1},::数组{Int64,1},::Int64)位于 .[9]:33[5]在[9]:51中的顶级范围

有人能解释一下我为什么会收到这个错误吗?它没有告诉我是哪一行(我使用的是Jupyter笔记本),我也不理解它。

第一个错误 您必须将对
time
的引用限定为
sirData.time

错误消息有点混乱,因为
time
也是
Base
中的一个函数,因此它会自动进入范围

第二个错误 您需要将数据表示为
Float64
,因此必须明确键入输入数组:

sirOutput = stochasticSIR([0.0001, 0.05], Float64[999,1,0], 200)

或者,您可以使用浮动文字创建数组:
[999.0,1,0]
。如果您创建的数组仅使用文字整数,Julia将创建一个整数数组。

我不确定StackOverflow是否是进行此操作的最佳场所,因为您似乎会在处理新错误时编辑原始帖子

编写时出现的当前错误(
increacterror:Int(2.50805)
)告诉您正在尝试从
Float64
浮点数创建整数,如果不显式四舍五入,则无法创建整数


我强烈建议您阅读Julia文档,了解基本用法,或者使用Julia Discussion论坛与社区进行更多的交互调试。

David,谢谢。您的回答修复了我最初的错误。我已经用一个新错误更新了我的OP,我有一个相同的问题:这意味着什么?好的,我有帖子我的问题在那里引起了争论。