Logistic Distribution in Python

In this tutorial you will learn:

  • What is Logistic Distribution?
  • Logistic Distribution Implementation in python
  • Visualization of Logistic Distribution

Logistic Distribution

Logistic distribution is a continuous distribution used for modelling growth and logistic regression. Its graphical representation replicates the normal distribution, belongs to symmetrical family of distribution and always have one peak. It has really simple cumulative distribution formula due to which it replicates normal distribution extremely well. It is used in different areas such as physical sciences, sports modeling, financing, machine learning and neural networks.

Logistic Distribution in Python

Logistic distribution in python is implemented using an inbuilt function logistic() which is included in the random module of NumPy library. The logistic() function takes in one mandatory parameter and two optional parameters. First parameter “size” is the size of the output array which could be 1D, 2D, 3D or n-dimensional (depending on the programmer’s requirement). The second parameter is the standard deviation defined by “scale”, and if not explicitly defined, its value is taken as 1. Third parameter is the mean, it determines the location of peak and is defined by “loc”. If mean is not explicitly defined, it is taken as 0 by the logistic() function. Lets take an example, we will generate a 1D array(1,4) of logistic distribution having mean at 3 with standard deviation of 1.5. Here you can observe that if we re run the program again and again, the values of the distribution will keep on changing.
  1. #importing the random module
  2. from numpy import random
  3. #applying the logistic function with mean value 3 and standard deviation 1.5
  4. res_arr= random.logistic(loc=3, scale=1.5, size=4)
  5. #printing the results
  6. print('1D array having Logistic Distribution with mean 3 and standard deviation 1.5:/n')
  7. print(res_arr)
In this example we will generate a 2D array of logistic distribution having size (2,4), in the first line of code we are importing random module from numpy library and in third line we are calling the logistic function with mean and standard deviation having values 6 and 3.
  1. #importing the random module
  2. from numpy import random
  3. #here we are using logistic function to generate rectangular distribution of size 2 x 4 with mean at 6 and standard deviation 3
  4. res_arr = random.logistic(scale = 3, loc=6, size=(2,4))
  5. print('2D Logistic Distribution as output from logistic() function:\n')
  6. #printing the result
  7. print(res_arr)
Lets take another example, in this example we will generate a 3D array of Logistic Distribution of the size(2,2,6) with mean at 2 and standard deviation 4
  1. #importing the random module
  2. from numpy import random
  3. #here we are using logistic function to generate logistic distribution of size 2 x 2 x 6
  4. res = random.logistic(size=(2,2,6), scale = 4, loc=2)
  5. print('3D Logistic Distribution as output from logistic() function:\n')
  6. #printing the result
  7. print(res)

Visualization of Rectangular Distribution

In this example we will visualize the Logistic Distribution and Normal Distribution using the distplot() function, here we will be using the default values for both functions. Here we can observe that the plotted graphs appear almost identical. In order to clarify the differences, both graphs are plotted simultaneously.
  1. #importing all the required modules and packages
  2. from numpy import random
  3. import matplotlib.pyplot as mpl
  4. import seaborn as sb
  5. #here we are using normal and logistic function to generate distributions of size 1000 with default parameters
  6. sb.distplot(random.normal(size=1000), hist=False, label='normal')
  7. sb.distplot(random.logistic(size=1000), hist=False, label='logistic')
  8. #plotting the graph
  9. mpl.show()

Add new comment