0

I have N agents to place in a grid n x n 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    
emax
  • 6,965
  • 19
  • 74
  • 141

1 Answers1

0

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

zimmerrol
  • 4,872
  • 3
  • 22
  • 41
  • If I have `minValue = 0` and `maxValue = 48` `x = np.linspace(minValue, minValue, (minValue-minValue)*2)` and then `sum(my_cv.cdf(x))=2.134`. I thought that this value should be `1`. – emax Sep 05 '17 at 12:44
  • You have to normalize your `pdf` by yourself. I think, that the equation you gave in your question is not normalized at all. – zimmerrol Sep 05 '17 at 13:19