You are on page 1of 3

Density=true;histogram is normalized and area under the curve is 1.

The values represent pdf

```pytho
n
#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(

You might also like