This Python tutorial will teach us how to generate a lognormal distribution using “** Python Scipy Lognormal**” and how to customize the distribution using the parameters mean and standard deviation. And also go through the following topics.

- What is a Lognormal Distribution?
- Python Scipy Lognormal
- Python Scipy Lognormal Mean
- Python Scipy Lognormal Random
- Python Scipy Lognormal Sample
- Python Scipy Lognormal Shape
- Python Scipy Lognormal Standard Deviation
- Python Scipy Lognormal Cdf
- Python Scipy Lognormal Fit
- Python Scipy Lognormal Pdf
- Python Scipy Lognormal Log Pdf

**Table of Contents**show

## What is a Lognormal Distribution?

A related normal distribution’s logarithmic values are statistically distributed as a log-normal distribution. With the use of related logarithmic calculations, a log-normal distribution can be converted into a normal distribution and vice versa.

Symmetrical or bell-shaped probability distribution of outcomes is referred to as a normal distribution. 95% of the data fall within two standard deviations and 68% of the results fall within one standard deviation in a normal distribution.

- A normal distribution is something that most people are familiar with, but a log-normal distribution might not be. Logarithmic mathematics can be used to transform a normal distribution into a log-normal distribution. This is the main foundation since log-normal distributions can only result from a set of random variables that are normally distributed.
- In general, a normal distribution curve is used to plot the log of random variables using the log-normal distribution. The log is typically thought of as the exponent that must be raised from a base number to obtain the random variable (x) that is observed along a normally distributed curve.
- There may be a few issues with normal distributions that log-normal distributions can address. Generally speaking, while log-normal distributions only include positive variables, normal distributions can also include negative random variables.

The study of stock prices is one of the most popular financial applications of log-normal distributions.

A normal distribution can be used to graph a stock’s anticipated returns. But a log-normal distribution can be used to graph the stock’s price movements.

Therefore, it is possible to utilize the log-normal distribution curve to better determine the compound return that the stock is likely to experience over time.

In this tutorial, we will use the lognormal method of Python Scipy to explore how lognormal works and implement it.

Read: PyTorch Conv1d

## Python Scipy Lognormal

The Python Scipy has a method

in the module *lognorm()*

which is a continuous random variable that is lognormal.*scipy.stats*

The syntax is given below.

`scipy.stats.lognorm.method_name(data,loc,size,moments,scale)`

Where parameters are:

**data:**It is a collection of values or points that reflect uniformly sampled data as an array of values.**loc:**It’s used to provide the mean and has a default value of 0.**moments:**It is used to compute statistics like the mean, kurtosis, and standard deviation.**scale:**It is used to indicate the standard deviation, which is set to 1 by default.

The parameters listed above serve as the common parameter for all methods in the

object. The methods are listed below.*scipy.stats.lognorm()*

**scipy.stats.lognorm.cdf():**It is used for the cumulative distribution function.**scipy.stats.lognorm.pdf():**It is used for the probability density function.**scipy.stats.lognorm.rvs():**To get the random variates.**scipy.stats.lognorm.stats():**It is used to get the standard deviation, mean, kurtosis, and skew.**scipy.stats.lognorm.logpdf():**It is used to get the log related to the probability density function.**scipy.stats.lognorm.logcdf():**It is used to find the log related to the cumulative distribution function.**scipy.stats.lognorm.sf():**It is used to get the values of the survival function.**scipy.stats.lognorm.isf():**It is used to get the values of the inverse survival function.**scipy.stats.lognorm.logsf():**It is used to find the log related to the survival function.**scipy.stats.lognorm.mean():**It is used to find the mean of the distribution.**scipy.stats.lognorm.median():**It is used to find the median of the distribution.**scipy.stats.lognorm.var():**It is used to find the variance related to the distribution.**scipy.stats.lognorm.std():**It is used to find the standard deviation related to the distribution

Let’s understand with an example by following the below steps:

Import the required libraries using the below python code.

```
from scipy import stats
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
```

Create a shape parameter for lognormal distribution using the below code.

`s_parameter = 0.999`

Create an array of data using the below code.

```
fig, ax = plt.subplots(1, 1)
array_data = np.linspace(stats.lognorm.ppf(0.02,s_parameter),
stats.lognorm.ppf(0.98,s_parameter), 99)
```

Generate lognormal distribution by passing the array of data with shape parameters to a method

using the below code*lognorm()*

```
ax.plot(array_data, stats.lognorm.pdf(array_data,s_parameter),
'r-', lw=5, alpha=0.6, label='truncnorm pdf')
```

This is how to create lognormal distribution using the method `lognorm()`

of Python Scipy.

Read: PyTorch Reshape Tensor

## Python Scipy Lognormal Mean

The Python Scipy method

accepts a parameter *lognormal()*

which is the mean for the lognormal distribution. Using this parameter we can shift the distribution to different positions.*loc*

Let’s take an example and shift lognormal distribution to a different position.

Import the required libraries or methods using the below python code.

```
import numpy as np
from scipy.stats import lognorm
import matplotlib.pyplot as plt
%matplotlib inline
```

Create an array of data using the below code.

```
x_array = np.arange(-5, 5, 0.3)
x_array
```

Now compute the lognormal distribution using the below code.

```
y_pdf = lognorm.pdf(x_array,0.1,1)
plt.plot(x_array, y_pdf)
plt.show()
```

In the above code, we have provided a `loc`

value equal to 1. Below is the output of the above code, notice the position of the distribution.

Now again use the same code `loc`

equal to 0.9 using the below code.

```
y_pdf = lognorm.pdf(x_array,0.1,0.9)
plt.plot(x_array, y_pdf)
plt.show()
```

The output shows the different positions of the distribution, so using the parameter

or **loc**

we can control the position of the lognormal distribution.**mean**

Read: PyTorch Linear Regression

## Python Scipy Lognormal Random

The SciPy library’s

function in Python can be used to create a random variable that has a log-normal distribution.*lognorm()*

To create a random variable log-normal distribution with mean = 1 and standard-deviation = 1, use the following python codes:

Import the required libraries or methods using the below code

```
import numpy as np
from math import exp
from scipy.stats import lognorm
```

Make a 2000-value log-normal distributed random variable.

```
np.random.seed(1)
lognorm_val = lognorm.rvs(s=1.0, scale=exp(1), size=2000)
```

Check the first ten values using the below code.

`lognorm_val[:10]`

This is how to generate random variable values that have lognormal distribution using the method

of Python Scipy.*lognorm.rvs()*

Read: PyTorch Numpy to Tensor

## Python Scipy Lognormal Sample

To generate random numbers as a sample of the lognormal distribution, In this section, we will use the method `lognormal.rvs()`

of Python Scipy that exists in a module

.*scipy.stats*

The syntax is given below.

`lognorm.rvs(s, loc=0, scale=1, size=1)`

Where parameters are:

**s:**This is the shape parameter of the lognormal distribution.**loc:**It’s used to provide the mean and has a default value of 0.**scale:**It is used to indicate the standard deviation.**size:**It is the size of the sample.

Let’s generate a sample with help of an example by following the below steps:

Import the necessary libraries or methods using the below python code.

`from scipy.stats import lognorm`

Now generate sample data using the below code.

```
s=0.5
lognorm.rvs(s,size=2000)
```

In the above code, `s`

is the shape parameter for the sample data which is equal to 0.5, where `size`

is the sample size.

The output shows the array containing sample data for lognormal distribution, this is how to generate random numbers for sample data using the method

of Python Scipy.*lognorm.rvs()*

Read: PyTorch Activation Function

## Python Scipy Lognormal Shape

Probability distribution with a normally distributed logarithm is referred to as a lognormal (Galton, or log-normal) distribution. If the logarithm of a random variable is normally distributed, the variable has a lognormal distribution.

Skewed distributions with all-positive values, low mean values, and high variation frequently fit this sort of distribution. Values must be positive because log(x) can only exist for positive x values.

The shape parameter(σ): The basic form of the distribution is also impacted by the lognormal’s standard deviation. Usually, historical data is used to determine these factors.

On occasion, we might be able to predict it using the most recent data. The graph’s position or height is unaffected by the shape parameter; only the overall shape is changed.

In the above subsections of this tutorial, we have already learned about how to tune this parameter to get the desired lognormal distribution.

## Python Scipy Lognormal Standard Deviation

The Python Scipy method

accepts the parameter *lognormal()*

which is the standard deviation for the lognormal distribution. Using this parameter we can change the shape of the distribution.*scale*

Let’s take an example and change the shape of the lognormal distribution.

Import the required libraries or methods using the below python code.

```
import numpy as np
from scipy.stats import lognorm
import matplotlib.pyplot as plt
%matplotlib inline
```

Create an array of data using the below code.

```
x_array = np.arange(-5, 5, 0.3)
x_array
```

Now compute the lognormal distribution using the below code.

```
y_pdf = lognorm.pdf(x_array,0.5,1)
plt.plot(x_array, y_pdf)
plt.show()
```

In the above code, we have provided a `scale`

value equal to 0.5. Below is the output of the above code, notice the shape of the distribution.

Now again use the same code `scale`

equal to 1 using the below code.

```
y_pdf = lognorm.pdf(x_array,1,1)
plt.plot(x_array, y_pdf)
plt.show()
```

The output shows the different shapes of the distribution, so using the parameter

or *scale*

we can control the shape of the lognormal distribution.*standard deviation*

Read: PyTorch MNIST Tutorial

## Python Scipy Lognormal Cdf

The Python Scipy has a method `lognorm.cdf()`

that computes the cumulative distribuiton of lognormal.

Let’s understand with an example by following the below steps:

Import the required libraries using the below python code.

```
from scipy import stats
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
```

Create an array of data using the below code.

```
x_array = np.arange(-10, 10, 0.3)
x_array
```

Generate lognormal cumulative distribution by passing the array of data to a method

using the below code*lognorm()*

```
y_pdf = lognorm.cdf(x_array,1,1)
plt.plot(x_array, y_pdf)
plt.show()
```

This is how to compute the CDF of lognormal using the method

of Python Scipy.*lognorm.cdf()*

Raed: PyTorch Binary Cross Entropy

## Python Scipy Lognormal Fit

The Python Scipy contains a method

to fit the data to the Lognormal distribution.*lognormal.fit()*

Here in this section, we will fit the data to a lognormal distribution by following the below steps:

Import the required libraries or methods using the below python code.

`from scipy import stats`

Generate some data that fits using the lognormal distribution, and create random variables.

```
s=0.5
x_data = stats.lognorm.rvs(s size=700, random_state=120)
```

Now fit for the three parameters using the below code.

```
shape_loc, loc_param, scale_param = stats.lognorm.fit(x_data)
print(shape_loc)
print(loc_param)
print(scale_param)
```

This is how to fit the data to lognormal distribution using the method

of Python Scipy.*lognorm.fit()*

Read: PyTorch Dataloader + Examples

## Python Scipy Lognormal Pdf

The Python Scipy has a method** ** that computes the probability distribuiton of lognormal.

`lognorm.pdf()`

Let’s understand with an example by following the below steps:

Import the required libraries using the below python code.

```
from scipy import stats
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
```

Create an array of data using the below code.

```
x_array = np.arange(-5, 5, 0.5)
x_array
```

Generate lognormal pdf by passing the array of data to a method

using the below code.*lognorm.pdf()*

```
y_pdf = stats.lognorm.pdf(x_array,0.8,0.8)
plt.plot(x_array, y_pdf)
plt.show()
```

This is how to compute the PDF of lognormal using the method

of Python Scipy.*lognorm.pdf()*

Read: PyTorch Early Stopping + Examples

## Python Scipy Lognormal log pdf

The Python Scipy has a method** ** that computes the log probability distribution of lognormal.

`lognorm.logpdf()`

Let’s understand with an example by following the below steps:

Import the required libraries using the below python code.

```
from scipy import stats
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
```

Create an array of data using the below code.

```
x_array = np.arange(-2, 2, 0.2)
x_array
```

Generate lognormal log pdf by passing the array of data to a method

using the below code*lognorm.logpdf()*

```
y_pdf = stats.lognorm.logpdf(x_array,0.8,0.8)
plt.plot(x_array, y_pdf)
plt.show()
```

This is how to compute the log PDF of lognormal using the method

of Python Scipy.*lognorm.logpdf()*

Read: PyTorch Pretrained Model

## Python Scipy Lognormal Log Cdf

The Python Scipy has a method** ** that computes the log cumulative distribuiton of lognormal.

`lognorm.logcdf()`

Let’s understand with an example by following the below steps:

Import the required libraries using the below python code.

```
from scipy import stats
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
```

Create an array of data using the below code.

`x_array = np.arange(-3, 3, 0.2)`

Generate lognormal log cdf by passing the array of data to a method

using the below code*lognorm.logcdf()*

```
s=1.5
y_pdf = stats.lognorm.logcdf(s,x_array,0.8,0.8)
plt.plot(x_array, y_pdf)
plt.show()
```

This is how to compute the log CDF of lognormal using the method

of Python Scipy.*lognorm.logcdf()*

You may also like to read the following Python Scipy tutorials.

- PyTorch Load Model + Examples
- Python Scipy Sparse Csr_matrix
- PyTorch nn linear + Examples
- Cross Entropy Loss PyTorch

So, in this tutorial, we have generated the lognormal distribution and also controlled the distribution using the parameter shape and position (standard deviation and mean), and covered the following topics.

- What is a Lognormal Distribution?
- Python Scipy Lognormal
- Python Scipy Lognormal Mean
- Python Scipy Lognormal Random
- Python Scipy Lognormal Sample
- Python Scipy Lognormal Shape
- Python Scipy Lognormal Standard Deviation
- Python Scipy Lognormal Cdf
- Python Scipy Lognormal Fit
- Python Scipy Lognormal Pdf
- Python Scipy Lognormal Log Pdf

Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.