gpt4 book ai didi

python - 年龄分布的 Numpy 梯形分布

转载 作者:太空宇宙 更新时间:2023-11-03 16:43:41 25 4
gpt4 key购买 nike

我正在尝试创建一个美国人口分布的粗略模型,以生成样本人口的随机年龄,以下图像作为来源。

US Population Distribution

我觉得这可以通过梯形分布来最简单地建模,梯形分布一直保持均匀,直到 50 岁左右下降。然而,numpy 似乎不提供利用这种分布函数的能力。因此,我想知道是否可以“组合”两个分布函数(在本例中,最大值为 50 的均匀分布函数,以及最小值为 51、最大值为 100 的三角分布函数) 。这可能吗?有没有办法直接用python表达梯形分布函数?

最佳答案

是的,您可以任意组合样本。只需使用np.concatenate

import numpy as np
import matplotlib.pyplot as p
%matplotlib inline

def agedistro(turn,end,size):
pass
totarea = turn + (end-turn)/2 # e.g. 50 + (90-50)/2
areauptoturn = turn # say 50
areasloped = (end-turn)/2 # (90-50)/2
size1= int(size*areauptoturn/totarea)
size2= size- size1
s1 = np.random.uniform(low=0,high=turn,size= size1) # (low=0.0, high=1.0, size=None)
s2 = np.random.triangular(left=turn,mode=turn,right=end,size=size2) #(left, mode, right, size=None)
# mode : scalar- the value where the peak of the distribution occurs.
#The value should fulfill the condition left <= mode <= right.
s3= np.concatenate((s1,s2)) # don't use add , it will add the numbers piecewise
return s3

s3=agedistro(turn=50,end=90,size=1000000)
p.hist(s3,bins=50)
p.show()

enter image description here

关于python - 年龄分布的 Numpy 梯形分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36537811/

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