从 csv 文件应用年龄分布时出错
error applying an age distribution from csv file
我刚刚在我的模型中发现了一个错误,我在该模型中创建了一个具有家庭规模分布和受抚养年龄分布的人口。我的问题是,在确定年龄时,我的程序似乎没有正确分配每个结果的概率。我应该有 16% 的老人,但我的模型给了我 3%(总人口)。
我认为这与程序的内部顺序有关,因为老年人是最后分配的。请参阅下面的代码。
分配家庭人数的程序:
to hatch-family ; hatching the rest of the household using percentual distribution of household size in berlin population
ifelse random-float 1 <= 0.01 [set household-size 6 set dwelling patch-here hatch 5]
[ifelse random-float 1 <= 0.02 [set household-size 5 set dwelling patch-here hatch 4]
[ifelse random-float 1 <= 0.06 [set household-size 4 set dwelling patch-here hatch 3]
[ifelse random-float 1 <= 0.11 [set household-size 3 set dwelling patch-here hatch 2]
[ifelse random-float 1 <= 0.31 [set household-size 2 set dwelling patch-here hatch 1]
[ifelse random-float 1 <= 0.49 [set household-size 1 set dwelling patch-here]
[hatch-family]
]]]]]
end
分配年龄的程序:
to assign-age; assign age depending on household size according to distribution on csv file,
;i = the different rows representing different household sizes
get-age-data
if household-size = 1 [get-age-breed 1]
if household-size = 2 [get-age-breed 2]
if household-size = 3 [get-age-breed 3]
if household-size = 4 [get-age-breed 4]
if household-size = 5 [get-age-breed 5]
if household-size = 6 [get-age-breed 6]
end
to get-age-data ; reading csv file and creating dataset with age distribution of the population
file-close-all ; close all open files
if not file-exists? "Data/age-dist-breeds.csv" [
user-message "No file 'age-dist-breeds' exists!"
stop
]
file-open "Data/age-dist-breeds.csv" ; open the file with the turtle data
set age-data csv:from-file "Data/age-dist-breeds.csv"
file-close ; making sure to close the file
end
to get-age-breed [i]; process for assigning age after distribution for household size i, will repeat itself if no age is assigned in previous run
let child-var item 1 item i age-data
ifelse (random-float 1 <= child-var)
[set age random 14 set breed children set age-susceptibility 0.0025]
[let teen-var item 2 (item i age-data)
ifelse (random-float 1 <= teen-var)
[set age 15 + random 4 set breed teens set age-susceptibility 0.005]
[let adult-var item 3 (item i age-data)
ifelse ( random-float 1 <= adult-var)
[let age-susc-var random 49
set age 20 + age-susc-var set breed adults set age-susceptibility (age-susc-var / 49) * 0.075 + 0.015]
[let elder-var item 4 (item i age-data)
ifelse ( random-float 1 <= elder-var )
[ set age 70 + random 30 set breed elderly set age-susceptibility 0.08]
[get-age-breed i]
]]
]
end
我试图在开始时制作一个随机变量,而不是为每一步重新计算它,但后来我最终没有任何老人。
有没有比随机浮动过程更好的方法来准确分配非正态概率分布?
你的问题是你一直在抽取一个新的随机数。这是您的代码结构,缩进以便您可以看到您的逻辑:
to hatch-family
ifelse random-float 1 <= 0.01
[]
[ ifelse random-float 1 <= 0.02
[]
[ ifelse random-float 1 <= 0.06
[]
[ ifelse random-float 1 <= 0.11
[]
[ ifelse random-float 1 <= 0.31
[]
[ ifelse random-float 1 <= 0.49
[]
[ hatch-family ]
]
]
]
]
]
end
所以它抽一个数字,有1%的机会满足第一个条件,如果不满足就抽另一个数字,依此类推,直到结束。更重要的是,它会在大约一半的时间内再次执行整个操作,因为该过程将自己称为最终绘制的假分支。
因此,第一步是只抽取一次随机数并简单地比较该值,它看起来像这样:
to hatch-family
let my-draw random-float 1
ifelse my-draw <= 0.01 []
[ ifelse my-draw <= 0.02 []
[ ifelse my-draw <= 0.06 []
但您也可以使用多项选择 ifelse
来清理代码(假设您使用的是当前版本的 NetLogo),它看起来像:
to hatch-family
let my-draw random-float 1
( ifelse
my-draw <= 0.01 []
my-draw <= 0.02 []
my-draw <= 0.06 []
注意开头 ( 让 NetLogo 知道需要两个以上的子句。
或者您可以使用史蒂夫已经提到的 rnd
扩展。
我终于通过@JenB 的一些输入解决了它。
代码现在如下所示:
根据-累计-户数分布孵化其余户的程序:
; hatching the rest of the household using percentual distribution of household size of Berlin population (Zensus Datenbank)
; (nr of households with specific size / total number of households in Berlin,
; hence the percentage of the total population will be accurate as the population increases with each hatching)
to hatch-family
ask turtles
[
let my-draw random-float 1
(ifelse
my-draw <= 0.01 [set household-size 6 set dwelling patch-here hatch 5]
my-draw <= 0.03 [set household-size 5 set dwelling patch-here hatch 4]
my-draw <= 0.09 [set household-size 4 set dwelling patch-here hatch 3]
my-draw <= 0.20 [set household-size 3 set dwelling patch-here hatch 2]
my-draw <= 0.51 [set household-size 2 set dwelling patch-here hatch 1]
my-draw <= 1 [set household-size 1 set dwelling patch-here]
)
]
end
根据每个家庭规模的年龄分布分配品种和年龄的程序:
(我用 up-to-n-of
命令而不是 random-float 解决了概率并不总是按最低->最高顺序排列的问题)
to assign-age; assign age depending on household size according to distribution on csv file,
;second item [i] = the different rows representing different household sizes
ask turtles [set age 0]
get-age-data ; reading csv file and naming it age-data
get-age-breed 1
get-age-breed 2
get-age-breed 3
get-age-breed 4
get-age-breed 5
get-age-breed 6
end
to get-age-breed [i]; process for assigning age after distribution for household size i
let child-var up-to-n-of ((count turtles with [household-size = i]) * item 1 (item i age-data)) (turtles with [household-size = i and age = 0])
ask child-var [set age random 14 set breed children set age-susceptibility 0.0025]
let teen-var up-to-n-of (count turtles with [household-size = i] * item 2 item i age-data) (turtles with [household-size = i and age = 0])
ask teen-var [set age 15 + random 4 set breed teens set age-susceptibility 0.005]
let elder-var up-to-n-of (count turtles with [household-size = i] * item 4 item i age-data) (turtles with [household-size = i and age = 0])
ask elder-var [set age 70 + random 30 set breed elderly set age-susceptibility 0.08]
let adult-var up-to-n-of (count turtles with [household-size = i] * item 3 item i age-data) (turtles with [household-size = i and age = 0])
ask adult-var [let age-susc-var random 49 set age 20 + age-susc-var set breed adults set age-susceptibility (age-susc-var / 49) * 0.075 + 0.015]
end
我刚刚在我的模型中发现了一个错误,我在该模型中创建了一个具有家庭规模分布和受抚养年龄分布的人口。我的问题是,在确定年龄时,我的程序似乎没有正确分配每个结果的概率。我应该有 16% 的老人,但我的模型给了我 3%(总人口)。
我认为这与程序的内部顺序有关,因为老年人是最后分配的。请参阅下面的代码。
分配家庭人数的程序:
to hatch-family ; hatching the rest of the household using percentual distribution of household size in berlin population
ifelse random-float 1 <= 0.01 [set household-size 6 set dwelling patch-here hatch 5]
[ifelse random-float 1 <= 0.02 [set household-size 5 set dwelling patch-here hatch 4]
[ifelse random-float 1 <= 0.06 [set household-size 4 set dwelling patch-here hatch 3]
[ifelse random-float 1 <= 0.11 [set household-size 3 set dwelling patch-here hatch 2]
[ifelse random-float 1 <= 0.31 [set household-size 2 set dwelling patch-here hatch 1]
[ifelse random-float 1 <= 0.49 [set household-size 1 set dwelling patch-here]
[hatch-family]
]]]]]
end
分配年龄的程序:
to assign-age; assign age depending on household size according to distribution on csv file,
;i = the different rows representing different household sizes
get-age-data
if household-size = 1 [get-age-breed 1]
if household-size = 2 [get-age-breed 2]
if household-size = 3 [get-age-breed 3]
if household-size = 4 [get-age-breed 4]
if household-size = 5 [get-age-breed 5]
if household-size = 6 [get-age-breed 6]
end
to get-age-data ; reading csv file and creating dataset with age distribution of the population
file-close-all ; close all open files
if not file-exists? "Data/age-dist-breeds.csv" [
user-message "No file 'age-dist-breeds' exists!"
stop
]
file-open "Data/age-dist-breeds.csv" ; open the file with the turtle data
set age-data csv:from-file "Data/age-dist-breeds.csv"
file-close ; making sure to close the file
end
to get-age-breed [i]; process for assigning age after distribution for household size i, will repeat itself if no age is assigned in previous run
let child-var item 1 item i age-data
ifelse (random-float 1 <= child-var)
[set age random 14 set breed children set age-susceptibility 0.0025]
[let teen-var item 2 (item i age-data)
ifelse (random-float 1 <= teen-var)
[set age 15 + random 4 set breed teens set age-susceptibility 0.005]
[let adult-var item 3 (item i age-data)
ifelse ( random-float 1 <= adult-var)
[let age-susc-var random 49
set age 20 + age-susc-var set breed adults set age-susceptibility (age-susc-var / 49) * 0.075 + 0.015]
[let elder-var item 4 (item i age-data)
ifelse ( random-float 1 <= elder-var )
[ set age 70 + random 30 set breed elderly set age-susceptibility 0.08]
[get-age-breed i]
]]
]
end
我试图在开始时制作一个随机变量,而不是为每一步重新计算它,但后来我最终没有任何老人。
有没有比随机浮动过程更好的方法来准确分配非正态概率分布?
你的问题是你一直在抽取一个新的随机数。这是您的代码结构,缩进以便您可以看到您的逻辑:
to hatch-family
ifelse random-float 1 <= 0.01
[]
[ ifelse random-float 1 <= 0.02
[]
[ ifelse random-float 1 <= 0.06
[]
[ ifelse random-float 1 <= 0.11
[]
[ ifelse random-float 1 <= 0.31
[]
[ ifelse random-float 1 <= 0.49
[]
[ hatch-family ]
]
]
]
]
]
end
所以它抽一个数字,有1%的机会满足第一个条件,如果不满足就抽另一个数字,依此类推,直到结束。更重要的是,它会在大约一半的时间内再次执行整个操作,因为该过程将自己称为最终绘制的假分支。
因此,第一步是只抽取一次随机数并简单地比较该值,它看起来像这样:
to hatch-family
let my-draw random-float 1
ifelse my-draw <= 0.01 []
[ ifelse my-draw <= 0.02 []
[ ifelse my-draw <= 0.06 []
但您也可以使用多项选择 ifelse
来清理代码(假设您使用的是当前版本的 NetLogo),它看起来像:
to hatch-family
let my-draw random-float 1
( ifelse
my-draw <= 0.01 []
my-draw <= 0.02 []
my-draw <= 0.06 []
注意开头 ( 让 NetLogo 知道需要两个以上的子句。
或者您可以使用史蒂夫已经提到的 rnd
扩展。
我终于通过@JenB 的一些输入解决了它。
代码现在如下所示:
根据-累计-户数分布孵化其余户的程序:
; hatching the rest of the household using percentual distribution of household size of Berlin population (Zensus Datenbank)
; (nr of households with specific size / total number of households in Berlin,
; hence the percentage of the total population will be accurate as the population increases with each hatching)
to hatch-family
ask turtles
[
let my-draw random-float 1
(ifelse
my-draw <= 0.01 [set household-size 6 set dwelling patch-here hatch 5]
my-draw <= 0.03 [set household-size 5 set dwelling patch-here hatch 4]
my-draw <= 0.09 [set household-size 4 set dwelling patch-here hatch 3]
my-draw <= 0.20 [set household-size 3 set dwelling patch-here hatch 2]
my-draw <= 0.51 [set household-size 2 set dwelling patch-here hatch 1]
my-draw <= 1 [set household-size 1 set dwelling patch-here]
)
]
end
根据每个家庭规模的年龄分布分配品种和年龄的程序:
(我用 up-to-n-of
命令而不是 random-float 解决了概率并不总是按最低->最高顺序排列的问题)
to assign-age; assign age depending on household size according to distribution on csv file,
;second item [i] = the different rows representing different household sizes
ask turtles [set age 0]
get-age-data ; reading csv file and naming it age-data
get-age-breed 1
get-age-breed 2
get-age-breed 3
get-age-breed 4
get-age-breed 5
get-age-breed 6
end
to get-age-breed [i]; process for assigning age after distribution for household size i
let child-var up-to-n-of ((count turtles with [household-size = i]) * item 1 (item i age-data)) (turtles with [household-size = i and age = 0])
ask child-var [set age random 14 set breed children set age-susceptibility 0.0025]
let teen-var up-to-n-of (count turtles with [household-size = i] * item 2 item i age-data) (turtles with [household-size = i and age = 0])
ask teen-var [set age 15 + random 4 set breed teens set age-susceptibility 0.005]
let elder-var up-to-n-of (count turtles with [household-size = i] * item 4 item i age-data) (turtles with [household-size = i and age = 0])
ask elder-var [set age 70 + random 30 set breed elderly set age-susceptibility 0.08]
let adult-var up-to-n-of (count turtles with [household-size = i] * item 3 item i age-data) (turtles with [household-size = i and age = 0])
ask adult-var [let age-susc-var random 49 set age 20 + age-susc-var set breed adults set age-susceptibility (age-susc-var / 49) * 0.075 + 0.015]
end