Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manipulating the numpy.random.exponential distribution in Python

I am trying to create an array of random numbers using Numpy's random exponential distribution. I've got this working fine, however I have one extra requirement for my project and that is the ability to specify precisely how many array elements have a certain value.

Let me explain (code is below, but I'll have a go at explaining it here): I generate my random exponential distribution and plot a histogram of the data, producing a nice exponential curve. What I really want to be able to do is use a variable to specify the y-intercept of this curve (point where curve meets the y-axis). I can achieve this in a basic way by changing the number of bins in my histogram, but this only changes the plot and not the original data.

I have inserted the bones of my code here. To give some context, I am trying to create the exponential disc of a galaxy, hence the random array I want to generate is an array of radii and the variable I want to be able to specify is the number density in the centre 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's 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 summarise, I would like to be able to specify where this plot intercepts the y-axis by controlling how I've generated the data, not by changing how the histogram has been plotted.

Any help or requests for further clarification would be very much appreciated.

like image 462
user2111574 Avatar asked Feb 26 '13 14:02

user2111574


People also ask

How do you generate a random number from exponential distribution in Python?

exponential() in Python. With the help of numpy. random. exponential() method, we can get the random samples from exponential distribution and returns the numpy array of random samples by using this method.

How do you fit an exponential distribution in Python?

The solution is to fit using an exponential function where `b` is constrained to 0 (or whatever value you know it to be). ```python def monoExpZeroB(x, m, t): return m * np. exp(-t * x) # perform the fit using the function where B is 0 p0 = (2000, . 1) # start with values near those we expect paramsB, cv = scipy.

What is scale in Numpy random exponential?

numpy.random.exponential(scale=1.0, size=None) Exponential distribution. Its probability density function is. for x > 0 and 0 elsewhere. is the scale parameter, which is the inverse of the rate parameter.


1 Answers

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

What you want is this function evaluated at f(x=0)=lambda=1/beta. Therefore in a normed distribution, your y-intercept should just be the inverse of 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-intercept of the histogram will change with different bin sizes, but this doesn't mean anything. The only thing that you can reasonably talk about here is the underlying probability distribution (hence the normed=true)

like image 183
Hooked Avatar answered Nov 07 '22 19:11

Hooked