0% found this document useful (0 votes)
18 views5 pages

DS4 1

The document outlines a lab practical focused on implementing various probability distributions using the NumPy random library in Python. It covers the types of probability distributions, their properties, and provides code examples for generating and visualizing normal and Poisson distributions. The conclusion emphasizes the importance of understanding these distributions for modeling real-world phenomena and making informed decisions in various fields.

Uploaded by

Armankhan Pathan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

DS4 1

The document outlines a lab practical focused on implementing various probability distributions using the NumPy random library in Python. It covers the types of probability distributions, their properties, and provides code examples for generating and visualizing normal and Poisson distributions. The conclusion emphasizes the importance of understanding these distributions for modeling real-world phenomena and making informed decisions in various fields.

Uploaded by

Armankhan Pathan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Enrolment No.

: 210430116083

Experiment No: 4

Date:

AIM: Implementation of Various Probability Distributions with NumPy Random Library


Functions

Relevant CO: - CO3


Objective:
The objective of this lab practical is to gain an understanding of various probability
distributions and implement those using NumPy random library functions.

Materials Used:
- Python environment (Anaconda, Jupyter Notebook, etc.)
- NumPy library

Procedure:
1. Introduction to Probability Distributions:
2. Probability theory is the branch of mathematics that deals with the study of random
events or phenomena. In probability theory, a probability distribution is a function that
describes the likelihood of different outcomes in a random process. Probability
distributions can be categorized into two types: discrete and continuous.
3. Discrete probability distributions are used when the possible outcomes of a random
process are countable and can be listed. The most commonly used discrete probability
distributions are Bernoulli, Binomial, and Poisson distributions.
4. Continuous probability distributions are used when the possible outcomes of a random
process are not countable and can take any value within a certain range. The most
commonly used continuous probability distributions are Normal and Exponential
distributions.
5. Each probability distribution has its own set of properties, such as mean, variance,
skewness, and kurtosis. Mean represents the average value of the random variable,
variance represents how much the values vary around the mean, skewness represents
the degree of asymmetry of the distribution, and kurtosis represents the degree of
peakedness or flatness of the distribution.
6. Probability distributions are widely used in fields such as finance, engineering, physics,
and social sciences to model real-world phenomena and make predictions about future
events. Understanding different probability distributions and their properties is an
important tool for analyzing data and making informed decisions.

7. Implementation of Probability Distributions using NumPy random library functions:

#python
import numpy as np
import matplotlib.pyplot as plt

# Generate 1000 random numbers following a normal distribution with mean 0 and standard
deviation 1
normal_dist = np.random.normal(0, 1, 1000)

# Calculate the mean and standard deviation of the distribution


mean = np.mean(normal_dist)
std_dev = np.std(normal_dist)

Information Technology 23
Enrolment No.: 210430116083

# Generate 1000 random numbers following a Poisson distribution with lambda 5


poisson_dist = np.random.poisson(5, 1000)

# Calculate the mean and variance of the Poisson distribution


poisson_mean = np.mean(poisson_dist)
poisson_var = np.var(poisson_dist)

# Plot the PDF and CDF of the normal distribution


plt.hist(normal_dist, bins=30, density=True, alpha=0.5)
plt.plot(np.sort(normal_dist), 1/(std_dev*np.sqrt(2*np.pi))*np.exp(-(np.sort(normal_dist)-
mean)**2/(2*std_dev**2)), linewidth=2)
plt.plot(np.sort(normal_dist), 0.5*(1+np.tanh((np.sort(normal_dist)-
mean)/std_dev*np.sqrt(2))), linewidth=2)
plt.show()

# Plot the PDF and CDF of the Poisson distribution


plt.hist(poisson_dist, bins=15, density=True, alpha=0.5)
plt.plot(np.arange(0, 15, 0.1), np.exp(-poisson_mean)*poisson_mean**np.arange(0, 15,
0.1)/np.math.factorial(np.arange(0, 15, 0.1)), linewidth=2)
plt.plot(np.arange(0, 15, 0.1),
np.exp(oisson_mean)*np.array([np.sum(poisson_var**np.arange(0,
i+1))/np.math.factorial(np.arange(0, i+1)) for i in np.arange(0, 15, 0.1)]), linewidth=2)
plt.show()

In this example, we generate 1000 random numbers following a normal distribution with mean
0 and standard deviation 1 using the `np.random.normal()` function. We then calculate the
mean and standard deviation of the distribution using the `np.mean()` and `np.std()` functions.

We also generate 1000 random numbers following a Poisson distribution with lambda 5 using
the `np.random.poisson()` function. We calculate the mean and variance of the Poisson
distribution using the `np.mean()` and `np.var()` functions.

We then plot the probability density function (PDF) and cumulative distribution function
(CDF) of both distributions using the `plt.hist()` and `plt.plot()` functions from the Matplotlib
library.

3. Exercise:
- Generate a dataset of your choice or given by faculty with a given probability distribution
using NumPy random library functions
- Plot the probability density function and cumulative distribution function for the generated
data
- Calculate the descriptive statistics of the generated data

Interpretation/Program/code:
import numpy as np
import matplotlib.pyplot as plt

normal_dist = np.random.normal(0, 1, 1000)

Information Technology 24
Enrolment No.: 210430116083

mean = np.mean(normal_dist)
std_dev = np.std(normal_dist)

plt.hist(normal_dist, bins=30, density=True, alpha=0.5)


plt.plot(np.sort(normal_dist), 1/(std_dev*np.sqrt(2*np.pi))*np.exp(-(np.sort(normal_dist)-
mean)**2/(2*std_dev**2)), linewidth=2)
plt.plot(np.sort(normal_dist), 0.5*(1+np.tanh((np.sort(normal_dist)-
mean)/std_dev*np.sqrt(2))), linewidth=2)
plt.show()

sorted_dist = np.sort(normal_dist)
cdf = np.arange(1, len(sorted_dist) + 1) / len(sorted_dist)

plt.plot(sorted_dist, cdf, linewidth=2)


plt.show()

descriptive_stats = {
'Mean': np.mean(normal_dist),
'Standard Deviation': np.std(normal_dist),
'Minimum': np.min(normal_dist),
'Maximum': np.max(normal_dist),
'Median': np.median(normal_dist),
'25th Percentile': np.percentile(normal_dist, 25),
'75th Percentile': np.percentile(normal_dist, 75)
}

print(descriptive_stats)

Output:

Information Technology 25
Enrolment No.: 210430116083

Conclusion:
This lab practical provided an opportunity to explore and implement various probability
distributions using NumPy random library functions. By understanding and applying different
probability distributions, one can model real-world phenomena and make predictions about
future events. With the knowledge gained in this lab practical, student will be equipped to work
with probability distributions and analyze data in a wide range of fields, including finance,
engineering, and social sciences.
Quiz:
1. Which NumPy function can be used to generate random numbers from a normal
distribution?
a) numpy.random.uniform
b) numpy.random.poisson
c) numpy.random.normal
d) numpy.random.exponential

Information Technology 26
Enrolment No.: 210430116083

2. What is the purpose of the probability density function (PDF) in probability


distributions?
a) To calculate the cumulative probability
b) To generate random numbers
c) To visualize the distribution
d) To calculate the probability of a specific value

Suggested References:-
1. Dinesh Kumar, Business Analytics, Wiley India Business alytics: The Science
2. V.K. Jain, Data Science & Analytics, Khanna Book Publishing, New Delhi of Dat
3. Data Science For Dummies by Lillian Pierson , Jake Porway
Rubrics wise marks obtained

Understanding of Analysis of Capability of Documentation


Problem the Problem writing program Total

02 02 05 01 10

Information Technology 27

You might also like