Manipulating the distribution of numpy.random.exponential in Python

I am trying to create an array of random numbers using the Numpy random exponential distribution. This works fine for me, however, I have one additional requirement for my project, and this is the ability to specify exactly how many elements of the array have a certain value.

Let me explain (the code below, but I will talk about it here): I generate my random exponential distribution and build a histogram of the data, creating a good exponential curve. What I really want to do is use a variable to indicate the y-intercept of this curve (the point where the curve corresponds to the y axis). I can achieve this in the main way by changing the number of bins in my histogram, but this only changes the graph, not the original data.

I pasted the bones of my code here. To give some context, I'm trying to create an exponential disk from the galaxy, so the random array I want to generate is an array of radii, and the variable I want to specify is the number density at the center of the galaxy

import numpy as N import matplotlib.pyplot as P n = 1000 scale_radius = 2 central_surface_density = 100 #I would like this to be the controlling variable, even if it specification had knock on effects on n. radius_array = N.random.exponential(scale_radius,(n,1)) P.figure() nbins = 100 number_density, radii = N.histogram(radius_array, bins=nbins,normed=False) P.plot(radii[0:-1], number_density) P.xlabel('$R$') P.ylabel(r'$\Sigma$') P.ylim(0, central_surface_density) P.legend() P.show() 

This code creates the following histogram:

enter image description here

So, to summarize, I would like to indicate where this graph captures the y axis, controlling how I generated the data, and not changing how the histogram was built.

Any help or requests for further clarification would be greatly appreciated.

+4
source share
1 answer

According to the docs for numpy.random.exponential input parameter beta is 1 / lambda for the definition exponentially described in wikipedia .

You want this function to evaluate to f(x=0)=lambda=1/beta . Therefore, in a normalized distribution, your y-interception should be simply inverted by the numpy function:

 import numpy as np import pylab as plt target = 250 beta = 1.0/target Y = np.random.exponential(beta, 5000) plt.hist(Y, normed=True, bins=200,lw=0,alpha=.8) plt.plot([0,max(Y)],[target,target],'r--') plt.ylim(0,target*1.1) plt.show() 

enter image description here

Yes, the y-interception of the histogram will vary with different sizes of boxes, but this does not mean anything. The only thing you can talk about here is the basic probability distribution (hence, normed=true )

+7
source

All Articles