vendredi 9 janvier 2015

Small random values in range [0,1], non-uniform, on log scale for GA? in Python

I recreated the initialisation and mutation of a GA and found that if I have a min and max of 0.001 and 1 for the parameters of a chromosome, and do random.uniform(min, max), the probability that a random value lies between [0.1, 1] is ~0.9, and between [0.01, 0.1] is ~0.09, and between [0.001, 0.01] is ~0.009 and so on.. Am I correct on this one?


The same happens for random.randint(1, 1000)/1000.0. This is because both are uniformly distributed right?


This makes sense, but in my opinion it is not fair that a new initialised value is more likely to have a greater value, in case of the GA. The probability should be equal between [0.1, 1.0] and [0.01, 0.1].


I found random number generators with different distribution functions. But it looks that none of them do what I think it should do. (numpy.random.lognormal, random.lognormvariate). Maybe some of them will fit, but I have no idea how to set the mu and sigma to scale between my min and max. Can someone help?


Some code that plots the distribution as a histogram:



import random
import numpy as np
min = 0.001
max = 1.0
s = []
for i in range(100000):
s.append(random.uniform(min, max))

#for i in range(100000):
# s.append(random.lognormvariate(0.00, 0.5))

print(s)
import matplotlib.pyplot as plt
count, bins, ignored = plt.hist(s, 500, normed=True)
plt.plot(bins, np.ones_like(bins), linewidth=2, color='r')
plt.show()




Aucun commentaire:

Enregistrer un commentaire