Created
October 14, 2012 07:02
-
-
Save sandeepraju/3887682 to your computer and use it in GitHub Desktop.
Probability Distribution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import pylab | |
import random | |
import math | |
def uniform(): | |
result = {} | |
hist = [] | |
a = 100 | |
b = 200 | |
for i in xrange(100000): | |
u =random.random() | |
rv = round(a + (b - a) * u, 0) | |
hist.append(rv) | |
if result.has_key(rv): | |
result[rv] += 1 | |
else: | |
result[rv] = 1 | |
pylab.plot(result.keys(), result.values(), '.') | |
pylab.xlabel("Random Variables") | |
pylab.ylabel("Number of occurences") | |
pylab.title("Uniform Probability distribution") | |
pylab.grid(True) | |
pylab.savefig("uniform_gra.png") | |
# pylab.show() | |
pylab.cla() | |
pylab.hist(hist, bins=100) | |
pylab.xlabel("Random Vars") | |
pylab.ylabel("freq") | |
pylab.title("uniform dist") | |
pylab.grid(True) | |
pylab.savefig("uniform_hist.png") | |
# pylab.show() | |
pylab.cla() | |
print "uniform distribution mean: ", sum(hist)/len(hist) | |
def exponential(): | |
result = {} | |
hist = [] | |
m = 150 | |
for i in xrange(100000): | |
u =random.random() | |
# rv = round(a + (b - a) * u, 0) | |
rv = round(-m * math.log(u), 0) | |
hist.append(rv) | |
if result.has_key(rv): | |
result[rv] += 1 | |
else: | |
result[rv] = 1 | |
pylab.plot(result.keys(), result.values(), '.') | |
pylab.xlabel("Random Variables") | |
pylab.ylabel("Number of occurences") | |
pylab.title("Exponential Probability distribution") | |
pylab.grid(True) | |
pylab.savefig("exp_gra.png") | |
# pylab.show() | |
pylab.cla() | |
pylab.hist(hist, bins=100) | |
pylab.xlabel("Random Vars") | |
pylab.ylabel("freq") | |
pylab.title("Exponential dist") | |
pylab.grid(True) | |
pylab.savefig("exp_hist.png") | |
# pylab.show() | |
pylab.cla() | |
def main(): | |
uniform() | |
exponential() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment