简体   繁体   中英

Python: how to create a distance network with a given distribution?

I have N agents to place in a grid nxn following the (truncated Levy) distribution

px = (r + r0)**(-beta)*exp(-r/k)

Each agent has two favorite cells: home and work and px is the probability for each agent to move from home and work with a distance r .

def returnLevy(r, beta):
    r0 = 100
    k = 1500
    px = (r + r0)**(-beta)*exp(-r/k)
    return px

I have compute the distance among all the cells in my grid, so

allDistances.head(5):

    distances   cell_a  cell_b  
0   1.322959      0       1 
1   0.717737      0       2 
2   0.454170      0       3 
3   0.321495      0       4 
4   0.454248      0       5 

I would like to know if there is a way to randomly assign to each agent a distance r from home and work following the aforementioned distribution. At the end I would like to have a dataframe:

agentsCells 

    distance    home    work    
0   1.322959     320    1089    
1   0.717737      4      765    
2   0.454170     2100    388    

You can define your own custom pdf using scipy and use this to calculate useful metrics of your probability density

import scipy.stats as st

class LevyPDF(st.rv_continuous):
    def _pdf(self,r):
        r0 = 100
        k = 1500
        return (r + r0)**(-beta)*exp(-r/k) #Normalized over its range [minValue,maxValue]

my_cv = LevyPDF(a=minValue, b=maxValue, name='LevyPDF')

To randomly draw values you have to calculate at first the cumulative distribution function ( cdf ) and then invert it to get the icdf . This can now be used the draw the random values following your pdf (you can find more details here ). If you have calculated the cdf , you can check your result using the numerical result of my_cv.cdf() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM