Show Power - Function Distribution in Statistics using Python


In this article we will learn about Power-Function distribution in statistics. We will checkout various methods using which we can analyze the power function distribution. In this post we will also know about how to fit the power function distributions if required. Before that let’s get to know about what Power-Function Distribution is.

Power-Function Distribution

This is continuous probability distribution used to model the data. Using the power function, we can analyze the small event which create some impact. Using the power function, we can get detail about rare events, identify outliers and can also make predictions about extreme values.

Method 1: Using NumPy

In this method we would generate the random numbers from power function distribution using random.number.power function from NumPy library. We can also pass parameters as shape and size into the function according to our need.

Example

import numpy as np

alpha = 2.0

random_numbers = np.random.power(alpha, size=50)
print(random_numbers)

Output

[0.66577177 0.1679034  0.95589386 0.52785199 0.74796992 0.98980016
 0.56024262 0.93486863 0.74621463 0.34786775 0.68608517 0.75162506
 0.99800817 0.77750768 0.2619546  0.71702682 0.98451869 0.9473717
 0.6938956  0.62181687 0.87593638 0.97820392 0.61942659 0.3998725
 0.60715901 0.41922618 0.69290475 0.22384672 0.94772692 0.42051637
 0.37011727 0.46666447 0.80132086 0.51449008 0.70068973 0.48051619
 0.97054498 0.47957822 0.58914979 0.68553321 0.27976004 0.46894656
 0.82430862 0.66817431 0.54340906 0.61010354 0.61893109 0.0923075
 0.08385714 0.94302131]

Explanation

Here in the above function, we define the shape parameter and then we generate 50 random numbers using the power function distribution.

random_numbers = np.random.power(alpha, size=50)

The resultant array will have all of these generated numbers.

Method 2: Using SciPy

In this method we will use the scipy.Stats.Expon module from the SciPy library. This module affords numerous techniques to work with the expon function distribution. We can use the rvs function for generating the random numbers and for calculation of the pdf and cdf.

Example

from scipy.stats import expon

alpha = 2.0
random_numbers = expon.rvs(scale=alpha, size=100)

x = 0.5
pdf_value = expon.pdf(x, scale=alpha)

cdf_value = expon.cdf(x, scale=alpha)
print(cdf_value)

Output

0.22119921692859515

Explanation

In this program we generate 50 random numbers from the expon function having shape parameters as 2.0. After generation of numbers we computed the pdf and cdf of some point x using the rvs function.

Method 3: Using Searborn

Here in this method, we will use np.random.power which will generate random number from the power function distributions. Using hist function we will display histogram of generated random numbers.

Example

import seaborn as sns

random_num = np.random.power(2.0, size=1000)

sns.distplot(random_num, hist=True, kde=True, bins=30)

Output

Explanation

In this function we first generate the random numbers using power function distributions. Here is the line to generate random numbers.

random_num = np.random.power(2.0, size=1000)

After generating the random numbers, we will use the distplot function from matplotlib library to plot the histogram for visualizing the data.

Method 4. Using Matplotlib

It is famous python library used to plot graph and draw visual representation of the data. We will use plot function from the matplotlib for visualizing power function distribution.

Example

import matplotlib.pyplot as plt

random_num = np.random.power(2.0, size=1000)

plt.hist(random_numbers, bins=30, density=True, alpha=0.5)
plt.xlabel('Values')
plt.ylabel('Probab')
plt.title('Power-fn Distribution')
plt.show()

Output

Explanation

Here in this method, we use np.random.power to generate random number from the power function distributions. We used hist function from the matplotlib library for displaying histogram of generated random numbers.

Method 5. Fitting the Distribution to Data

Sometimes while working with python, you have faced situation when you want to fit power function distributions. So, to perform fitting we use the fit method from the scipy.stats.exponpow module which can estimate the shape parameter based on the data.

Example

from scipy.stats import exponpow
import numpy as np

data = np.array([0.5, 0.6, 0.7, 0.8, 0.9])

params = exponpow.fit(data)

random_num = exponpow.rvs(*params, size=50)
print(random_num)

Output

[0.50115245 0.60597359 0.94355261 1.11336666 0.55644219 1.14871851
 0.83830517 0.84752829 0.52997894 0.79574141 0.59136949 0.67407663
 0.70188119 0.64854262 0.62878517 0.75472879 0.60225659 0.67842203
 0.96134614 0.72039852 0.56041962 0.50058046 0.84463519 0.74643548
 0.60791998 0.52271536 0.79289889 0.65340353 0.92378584 0.51754508
 0.51642979 0.71560587 0.69216641 0.59897395 0.80894948 0.50057274
 0.77434037 0.51578354 0.63737268 0.55335583 0.82872006 0.66339485
 0.50978196 0.68614946 0.73608196 0.58178696 0.85285616 1.03381135
 0.76055906 1.18414065]

So, we get to know about various methods to show the power function distribution.

We learned how to generate random number from the power function and calculate PDF (Probability density function) and CDF (cumulative distribution function). Using Seaborn and Matplotlib we plot visual representation of any statistical data.

Updated on: 13-Oct-2023

55 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements