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

标签 python numpy scipy statistics distribution

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

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/

相关文章:

python - 如何通过引用从另一个变量中删除字典中的对象

python - 让 scipy 的 rv_discrete 处理浮点值?

python - jupyter中没有名为tensorflow的模块

python - 如何从 PyTorch 的 ResNet 模型中删除最后一个 FC 层?

python - 解析电子邮件标题的文本抄送字段的方法?

python - Eigen 矩阵 vs Numpy 数组乘法性能

python - 您可以通过 numpy 数组广播字典定义吗?

python - 如何将多维列变成单值向量以用于sklearn pandas中的训练数据

python - 用一个变量求大量函数的根

python - 在有和没有SciPy的情况下计算k组合的数量