Generate random numbers with a given (numerical) distribution


In [1]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [2]:
from scipy.stats import norm

Theoretical distribution

With mean 50 and standard deviation 10


In [5]:
# Create a normal distribution
mu = 50
sigma = 10 # standard deviation
rv = norm(loc = mu, scale = sigma)

start = rv.ppf(0.00001)

stop  = rv.ppf(0.99999)
x = np.linspace(start, stop, 100)

print(start, stop)
plt.plot(x, rv.pdf(x))
plt.xlabel('Cells IDs'), plt.ylabel('Conductance');
plt.xlim(0,100);


(7.3510920607717551, 92.648907939238399)

Empirical distribution


In [8]:
# generate empirical distribution
cell_idx = np.arange(int(start), int(stop))
mycond = [rv.pdf(i) for i in cell_idx]
print(mycond[50])


0.0312253933367

In [9]:
plt.bar( cell_idx, mycond, width=.85, facecolor='k', label='empirical');
plt.xlabel('Cells IDs'), plt.ylabel('Conductance');


note that cells with ID <12 or > 87 receive almost zero conductance (check sigma)


In [10]:
rv.pdf(87)


Out[10]:
4.2478027055075142e-05

Plot both together


In [12]:
plt.plot(x, rv.pdf(x), c='r')
plt.bar( cell_idx, mycond, width=.85, facecolor='k', label='empirical');
plt.xlabel('Cells IDs'), plt.ylabel('Conductance');


let's generate another distribution with mean = 0 and standar deviation of 10


In [13]:
rv2 = norm(loc = 0, scale = 10)
start = rv2.ppf(0.0001)
stop  = rv2.ppf(0.9999)
x = np.linspace(start, stop, 100)

mycond = [rv2.pdf(i) for i in x]
print(start, stop)


(-37.190164854556805, 37.190164854557089)

In [15]:
cell_idx = np.arange(int(start), int(stop))
mycond = [rv2.pdf(i) for i in cell_idx]

plt.plot(x, rv2.pdf(x), color = 'red')
plt.bar( cell_idx, mycond, width=.85, facecolor='k', label='empirical');
plt.xlabel('Cells IDs'), plt.ylabel('Conductance');



In [22]:
rv3 = norm(loc = 0, scale = 20)
start = rv3.ppf(0.0001)
stop  = rv3.ppf(0.9999)

x = np.arange(start, stop)
plt.plot(x, rv3.pdf(x)/10, 'red')

print(start, stop)


(-74.38032970911361, 74.380329709114179)

In [ ]: