Python 年龄分布的Numpy梯形分布

Python 年龄分布的Numpy梯形分布,python,numpy,scipy,statistics,distribution,Python,Numpy,Scipy,Statistics,Distribution,我试图创建一个美国人口分布的粗略模型,以生成样本人口的随机年龄,并以下图作为来源 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

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

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()

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()

我觉得这可以最简单地用梯形分布来建模,梯形分布在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()
将numpy导入为np
将matplotlib.pyplot导入为p
%matplotlib内联
def AGEDRISTRO(转弯、结束、大小):
通过
总面积=转弯+(末端转弯)/2#例如50+(90-50)/2
areauptoturn=转身,比如说50
区域坡度=(末端转弯)/2#(90-50)/2
size1=int(大小*面积向上翻转/总面积)
size2=大小-大小1
s1=np.random.uniform(低=0,高=turn,大小=size1)#(低=0.0,高=1.0,大小=None)
s2=np.random.Triangal(左=转弯,模式=转弯,右=结束,大小=大小2)#(左,模式,右,大小=无)
#模式:标量-分布峰值出现的值。

#该值应满足以下条件,仅供参考:很快就会出现在您身边:@Warren在下一个版本之前,我能做些什么吗?哇,这真的很有用。谢谢