Continuous Distribution Functions- Normal distribution

  • Exponential distribution
  • T-distribution
  • F-distribution
  • Logistic distribution
  • Lognormal distribution
  • Uniform distribution

Author: Thomas Haslwanter, Feb-2017

In [1]:
# Note: here I use the iPython approach, which is best suited for interactive work
%pylab inline
from scipy import stats
matplotlib.rcParams.update({'font.size': 18})
Populating the interactive namespace from numpy and matplotlib

Function Definition

The following function will be used to show the different distributions functions

In [2]:
x = linspace(-10,10,201)
def showDistribution(d1, d2, tTxt, xTxt, yTxt, legendTxt, xmin=-10, xmax=10):
    '''Utility function to show the distributions, and add labels and title.'''
    plot(x, d1.pdf(x))
    if d2 != '':
        plot(x, d2.pdf(x), 'r')
    xlim(xmin, xmax)

Normal distribution

In [3]:
showDistribution(stats.norm, stats.norm(loc=2, scale=4),
                 'Normal Distribution', 'Z', 'P(Z)','')
In [4]:
# Exponential distribution
showDistribution(stats.expon, stats.expon(loc=-2, scale=4),
                 'Exponential Distribution', 'X', 'P(X)','')

Students' T-distribution

In [5]:
# ... with 4, and with 10 degrees of freedom (DOF)
plot(x, stats.norm.pdf(x), 'g')
showDistribution(stats.t(4), stats.t(10),
                 'T-Distribution', 'X', 'P(X)',['normal', 't=4', 't=10'])


In [6]:
# ... with (3,4) and (10,15) DOF
showDistribution(stats.f(3,4), stats.f(10,15),
                 'F-Distribution', 'F', 'P(F)',['(3,4) DOF', '(10,15) DOF'])
C:\Programs\WinPython-64bit-\python-3.6.0.amd64\lib\site-packages\scipy\stats\ RuntimeWarning: divide by zero encountered in log
  lPx = m/2 * log(m) + n/2 * log(n) + (n/2 - 1) * log(x)

Uniform distribution

In [7]:
showDistribution(stats.uniform,'' ,
                 'Uniform Distribution', 'X', 'P(X)','')

Logistic distribution

In [8]:
showDistribution(stats.norm, stats.logistic,
                 'Logistic Distribution', 'X', 'P(X)',['Normal', 'Logistic'])

Lognormal distribution

In [9]:
x = logspace(-9,1,1001)+1e-9
showDistribution(stats.lognorm(2), '',
                 'Lognormal Distribution', 'X', 'lognorm(X)','', xmin=-0.1)
In [10]:
# The log-lin plot has to be done by hand:
plot(log(x), stats.lognorm.pdf(x,2))
xlim(-10, 4)
title('Lognormal Distribution')
<matplotlib.text.Text at 0x1f2d47ca518>