Show Non-Central F-Distribution in Statistics using Python


In the given problem we have to show the non-central F-distribution with the help of Python and its libraries. So we will explore what non-central F-distribution is and how to show it using Python.

Understanding the Non-Central F-Distribution

The Non-Central F-Distribution is a probability distribution in statistics which is basically used for analyzing variance in the given data. It uses the central F-distribution by using the non-centrality parameters which are used to make deviation.

The non-central F-distribution used to determine the probability of observing a particular statistics. The figure of this distribution is generated using the degrees of freedom with the numerator and denominator. And it also used the non-centrality parameter to show the distribution. So the distribution will be changed according to the values of these parameters.

The distribution is looks like below −

Logic for The Above Problem

To plot this distribution we will be using the Python library 'scipy.stats' which needs to be installed in the system before using it in the program. And for generating the probability density function (PDF) and cumulative density functions (CDF) we will use stats.ncf() function. And to show the distribution we have to generate the random numbers that follow this distribution. So we will get this by using the noncentral_f() function from the scipy.stats library. This function will accept the degrees of freedom and the non-centrality parameters as its input. And by plotting the PDF and CDF we can show the non-central F-distribution.

Algorithm

  • Step 1 − First we will have to import the necessary libraries of Python to show the Non-central F-distribution. In our program we will use Numpy, Matplotlib and Scipy.stats libraries. If these libraries are not installed on the system then we are required to install first using pip install 'library_name' and then we can use these libraries in our program.

import numpy as nmp
import scipy.stats as stats
import matplotlib.pyplot as mt_plt
  • Step 2 − After importing the required libraries we will define the parameters of Non-central F-distribution like numerator degrees of freedom as num_df, denominator degrees of freedom as deno_df and non-centrality parameter as nc.

# Initialize the required parameters
num_df = 5  # numerator df
deno_df = 10  # denominator df
nc = 1  
  • Step 3 − Now we will be generating the values for x-coordinate for plotting the F-distribution.

# initialize the random numbers for x-values
x = nmp.linspace(0.1, 10, 1000)
  • Step 4 − Next, we will calculate the values of the probability density function and give it a name as y_pdf and cumulative density function as y_cdf using stats.ncf() function.

# compute PDF and CDF
y_pdf = stats.ncf.pdf(x, num_df, deno_df, nc)  
y_cdf = stats.ncf.cdf(x, num_df, deno_df, nc)  
  • Step 5 − After calculating all the required values we will be required to plot the probability density function using matplotlib. In our plot we will show x-label, y-label and title of the plot.

# plotting the Probability density function
mt_plt.figure(figsize=(8, 6))
mt_plt.plot(x, y_pdf, label='PDF')
mt_plt.xlabel('x')
mt_plt.ylabel('Probability Density')
mt_plt.title('Non-Central F-Distribution (PDF)')
mt_plt.legend()
mt_plt.grid(True)
  • Step 6 − Now we will also plot the cumulative density function (CDF) using matplotlib library.

# plotting the cumulative density function
mt_plt.figure(figsize=(8, 6))
mt_plt.plot(x, y_cdf, label='CDF')
mt_plt.xlabel('x')
mt_plt.ylabel('Cumulative Probability')
mt_plt.title('Non-Central F-Distribution (CDF)')
mt_plt.legend()
mt_plt.grid(True)
  • Step 7 − At the end of the program, we will show the plot using the show() function.

# Show the plots
mt_plt.show()

Example

import numpy as nmp
import scipy.stats as stats
import matplotlib.pyplot as mt_plt

# Initialize the required parameters
num_df = 5  # numerator df
deno_df = 10  # denominator df
nc = 1  

# initialize the random numbers for x-values
x = nmp.linspace(0.1, 10, 1000)

# compute PDF and CDF
y_pdf = stats.ncf.pdf(x, num_df, deno_df, nc)  
y_cdf = stats.ncf.cdf(x, num_df, deno_df, nc)  

# plotting the Probability density function
mt_plt.figure(figsize=(8, 6))
mt_plt.plot(x, y_pdf, label='PDF')
mt_plt.xlabel('x')
mt_plt.ylabel('Probability Density')
mt_plt.title('Non-Central F-Distribution (PDF)')
mt_plt.legend()
mt_plt.grid(True)

# plotting the cumulative density function
mt_plt.figure(figsize=(8, 6))
mt_plt.plot(x, y_cdf, label='CDF')
mt_plt.xlabel('x')
mt_plt.ylabel('Cumulative Probability')
mt_plt.title('Non-Central F-Distribution (CDF)')
mt_plt.legend()
mt_plt.grid(True)

# Show the plots
mt_plt.show()

Output

Complexity

The complexity for generating the random x-values and plotting the non-central F-distribution is linear as O(n), here n is the number of points in the x-axis. In our code we have generated 1000 points to plot the PDF and CDF. And these both density functions have same complexity.

Conclusion

In the article we have demonstrated the non-central F-distribution with the help of Python libraries. As we have use numerator degrees of freedom, denominator degrees of freedom and non-centrality parameters to generate the x-values which causes the linear complexity which is efficient to generate this distribution.

Updated on: 18-Oct-2023

47 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements