Last active
April 5, 2024 15:08
-
-
Save idrissrasheed/312d444693991cb6e341a36fe49ad6a8 to your computer and use it in GitHub Desktop.
Exponential and uniform distributions in Python
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
```python | |
#Import libraries | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import matplotlib.mlab as mlab | |
from __future__ import division | |
%matplotlib inline | |
``` | |
Question 1 | |
Part A | |
```python | |
#CDF of the uniform distribution | |
def CDFUniform( a,b,x ): | |
if x>=a and x<=b: | |
cdf=(x-a)/(b-a) | |
elif x>=b: | |
cdf=1 | |
else: | |
cdf=0 | |
return cdf | |
``` | |
```python | |
#CDF for x=3/4, a=0 and b=1 | |
print CDFUniform(0,1,3/4) | |
``` | |
Part B | |
```python | |
#Mean of the uniform distribution | |
def MeanUniform( a,b ): | |
mean=(b+a)/2 | |
return mean | |
#Variance of the uniform distribution | |
def VarianceUniform( a,b ): | |
Var=((b-a)**2)/12 | |
return Var | |
``` | |
```python | |
print MeanUniform( 0,1 ) | |
print VarianceUniform( 0,1 ) | |
``` | |
Question 2 | |
Part A | |
```python | |
#Generating a random sample of size 1000 from a standard uniform distribution | |
U=np.random.uniform(0,1,1000) | |
print(np.mean(U))#Display the sample mean | |
print(np.var(U))#Display the sample variance | |
``` | |
Part B | |
```python | |
#Plot uniform histogram | |
plt.hist(U,facecolor='blue') | |
plt.title("Uniform Histogram") | |
plt.xlabel("Value") | |
plt.ylabel("Frequency") | |
plt.show() | |
``` | |
Part C | |
```python | |
#Plot of the PDF | |
# the histogram of the data | |
n, bins, patches = plt.hist(U, 50, normed=1, | |
facecolor='green', alpha=0.75) | |
#Add best fit line | |
from scipy.stats import uniform | |
rv = uniform() | |
l = plt.plot(bins, rv.pdf(bins), 'r--', linewidth=1) | |
plt.xlabel('Value') | |
plt.ylabel('Probability') | |
plt.title("Uniform Histogram and its PDF") | |
plt.axis([0, 1, 0, 1.5]) | |
plt.grid(True) | |
plt.show() | |
``` | |
Question 3 | |
Part A | |
#Function for the CDF of the exponential distribution | |
def CDFExponential(lamb,x): #lamb = lambda | |
if x<=0: | |
cdf=0 | |
else: | |
cdf=1-np.exp(-lamb*x) | |
return cdf | |
#Function to compute the mean of the exponential distribution | |
def MeanExponential(lamb): | |
return 1/lamb; | |
def VarianceExponential(lamb): | |
return (1/lamb)**2; | |
Part B | |
```python | |
#CDF of the exponential distribution | |
#when lambda=1/5 and x=3 | |
print CDFExponential(1/5,3) | |
print MeanExponential(1/5) | |
print VarianceExponential(1/5) | |
``` | |
Question 4 | |
Part A | |
```python | |
#Generate 1000 random random samples | |
#from a standard uniform distribution | |
U=np.random.uniform(0,1,1000) | |
lamb=1/5 | |
X=-np.log(1-U)/lamb | |
``` | |
Part B | |
```python | |
print(np.mean(X))#Display the sample mean | |
print(np.var(X))#Display the sample variance | |
``` | |
Question 5 | |
Part A | |
```python | |
#Exponential CDF plot | |
plt.hist(X) | |
plt.title("Exponential Histogram") | |
plt.xlabel("Value") | |
plt.ylabel("Frequency") | |
plt.show() | |
``` | |
Part B | |
```python | |
#Exponential pdf histogram | |
n, bins, patches = plt.hist(X, 50, normed=1, | |
facecolor='green', alpha=0.75) | |
#Add best fit line | |
from scipy.stats import expon | |
rv = expon() | |
lamb = plt.plot(bins, lamb*rv.pdf(bins*lamb), 'r--', linewidth=1) | |
plt.xlabel('Value') | |
plt.ylabel('Probability') | |
plt.title("Uniform Histogram and its PDF") | |
plt.axis([0, 50, 0, 0.3]) | |
plt.grid(True) | |
plt.show() | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment