用于年龄分布的 Numpy 梯形分布

Numpy Trapezoidal Distribution for Age Distribution

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

我觉得这可以通过梯形分布最简单地建模,梯形分布保持均匀,直到在 50 岁左右下降。但是,numpy 似乎没有提供利用此分布函数的能力。因此,我想知道是否可以 "combine" 两个分布函数(在这种情况下,最大值为 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()