年龄分布的Numpy梯形分布

2024-09-24 10:29:56 发布

您现在位置:Python中文网/ 问答频道 /正文

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

US Population Distribution

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


Tags: 函数模型numpy利用来源图片能力样本
1条回答
网友
1楼 · 发布于 2024-09-24 10:29:56

是的,你可以任意组合样品。只需使用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

相关问题 更多 >