Someone
Someone

Reputation: 133

Numpy Trapezoidal Distribution for Age Distribution

I'm trying to create a rough model of US population distribution to generate random ages for a sample population, with the following image as a source, of sorts.

US Population Distribution

I feel that this could be most simply modeled by a trapezoidal distribution that remains uniform until dropping off at around the age of 50. However it seems that numpy does not offer the ability to utilize this distribution function. Because of this, I was wondering if it is possible to "combine" two distribution functions (in this case, a uniform distribution function with a maximum value of 50, and a triangular distribution function with a minimum of 51 and a maximum of 100). Is this possible, and is there a way to directly express a trapezoidal distribution function in python?

Upvotes: 1

Views: 1302

Answers (1)

roadrunner66
roadrunner66

Reputation: 7941

Yes, you can combine the samples arbitrarily. Just use 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

Upvotes: 1

Related Questions