ParaMonte Fortran 2.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
pm_distExp Module Reference

This module contains classes and procedures for computing various statistical quantities related to the Exponential distribution. More...

Data Types

type  distExp_type
 This is the derived type for signifying distributions that are of type Exponential as defined in the description of pm_distExp. More...
 
interface  getExpCDF
 Generate and return the Cumulative Distribution Function (CDF) of the Exponential distribution for an input x within the support of the distribution \([\mu, +\infty)\). More...
 
interface  getExpLogPDF
 Generate and return the natural logarithm of the Probability Density Function (PDF) of the Exponential distribution for an input x within the support of the distribution \([\mu, +\infty)\). More...
 
interface  getExpRand
 Return a scalar (or array of arbitrary rank of) random value(s) from the Exponential distribution, optionally with the specified input location and scale parameters mu, sigma.
More...
 
interface  setExpCDF
 Return the Cumulative Distribution Function (CDF) of the Exponential distribution for an input x within the support of the distribution \([\mu, +\infty)\). More...
 
interface  setExpLogPDF
 Return the natural logarithm of the Probability Density Function (PDF) of the Exponential distribution for an input x within the support of the distribution \([\mu, +\infty)\). More...
 
interface  setExpRand
 Return a scalar (or array of arbitrary rank of) random value(s) from the Exponential distribution, optionally with the specified input location and scale parameters mu, sigma.
More...
 

Variables

character(*, SK), parameter MODULE_NAME = "@pm_distExp"
 

Detailed Description

This module contains classes and procedures for computing various statistical quantities related to the Exponential distribution.

Specifically, this module contains routines for computing the following quantities of the Exponential distribution:

  1. the Probability Density Function (PDF)
  2. the Cumulative Distribution Function (CDF)
  3. the Random Number Generation from the distribution (RNG)
  4. the Inverse Cumulative Distribution Function (ICDF) or the Quantile Function

The PDF of the Exponential distribution with the two location and scale parameters \((\mu, \sigma)\) is defined as,

\begin{equation} \large F(x | \mu, \sigma) = \begin{cases} \frac{1}{\sigma} \exp(-\frac{x - \mu}{\sigma}) &,~ \mu \leq x < +\infty \\ 0 &,~ x < \mu \end{cases} \end{equation}

where \(-\infty < \mu < +\infty\) is the location parameter and \(0 < \sigma < +\infty\) is the scale parameter of the distribution.

The corresponding CDF with the two location and scale parameters \((\mu, \sigma)\) is defined as,

\begin{equation} \large \mathrm{CDF}(x | \mu, \sigma) = \begin{cases} 1 - \exp(-\frac{x - \mu}{\sigma}) &,~ \mu \leq x < +\infty \\ 0 &,~ x < \mu \end{cases} \end{equation}

where \(-\infty < \mu < +\infty\) is the location parameter and \(0 < \sigma < +\infty\) is the scale parameter of the distribution.

Random Number Generation

Assuming \(U \in (0, 1]\) is a uniformly-distributed random variate,

\begin{equation} \large T = -\sigma\log(U) + \mu ~, \end{equation}

is a random variate from the Exponential distribution with the location parameter \(-\infty < \mu < +\infty\) and the scale parameter \(0 < \sigma < +\infty\) such that \(\mu \leq T < +\infty\).

Note
Note that the mean of the Exponential distribution is the scale parameter: \(\mathrm{mean} = \sigma\)).
See also
pm_distUnif
pm_distNegExp
Benchmarks:


Benchmark :: The runtime performance of getExpLogPDF vs. setExpLogPDF

1! Test the performance of `getExpLogPDF()` vs. `setExpLogPDF()`.
2program benchmark
3
4 use iso_fortran_env, only: error_unit
5 use pm_bench, only: bench_type
6 use pm_kind, only: IK, RK, SK
7
8 implicit none
9
10 integer(IK) :: i
11 integer(IK) :: isize
12 integer(IK) :: fileUnit
13 integer(IK) , parameter :: NSIZE = 18_IK
14 integer(IK) , parameter :: NBENCH = 2_IK
15 integer(IK) :: arraySize(NSIZE)
16 real(RK) , allocatable :: Array(:), Point(:)
17 real(RK) :: dummy = 0._RK
18 type(bench_type) :: bench(NBENCH)
19
20 bench(1) = bench_type(name = SK_"getExpLogPDF", exec = getExpLogPDF , overhead = setOverhead)
21 bench(2) = bench_type(name = SK_"setExpLogPDF", exec = setExpLogPDF , overhead = setOverhead)
22
23 arraySize = [( 2_IK**isize, isize = 1_IK, NSIZE )]
24
25 write(*,"(*(g0,:,' '))")
26 write(*,"(*(g0,:,' vs. '))") (bench(i)%name, i = 1, NBENCH)
27 write(*,"(*(g0,:,' '))")
28
29 open(newunit = fileUnit, file = "main.out", status = "replace")
30
31 write(fileUnit, "(*(g0,:,','))") "arraySize", (bench(i)%name, i = 1, NBENCH)
32
33 loopOverArraySize: do isize = 1, NSIZE
34
35 write(*,"(*(g0,:,' '))") "Benchmarking with size", arraySize(isize)
36
37 allocate(Array(arraySize(isize)), Point(arraySize(isize)))
38 do i = 1, NBENCH
39 bench(i)%timing = bench(i)%getTiming(minsec = 0.05_RK)
40 end do
41 deallocate(Array, Point)
42
43 write(fileUnit,"(*(g0,:,','))") arraySize(isize), (bench(i)%timing%mean, i = 1, NBENCH)
44
45 end do loopOverArraySize
46
47 write(*,"(*(g0,:,' '))") dummy
48 write(*,"(*(g0,:,' '))")
49
50 close(fileUnit)
51
52contains
53
54 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55 ! procedure wrappers.
56 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57
58 subroutine setOverhead()
59 call initialize()
60 call finalize()
61 end subroutine
62
63 subroutine initialize()
64 call random_number(Point)
65 end subroutine
66
67 subroutine finalize()
68 dummy = dummy + Array(1)
69 end subroutine
70
71 subroutine setExpLogPDF()
72 block
73 use pm_distExp, only: setExpLogPDF
74 call initialize()
75 call setExpLogPDF(Array, Point)
76 call finalize()
77 end block
78 end subroutine
79
80 subroutine getExpLogPDF()
81 block
82 use pm_distExp, only: getExpLogPDF
83 call initialize()
84 Array = getExpLogPDF(Point)
85 call finalize()
86 end block
87 end subroutine
88
89end program benchmark
Generate and return an object of type timing_type containing the benchmark timing information and sta...
Definition: pm_bench.F90:574
Generate and return the natural logarithm of the Probability Density Function (PDF) of the Exponentia...
Definition: pm_distExp.F90:221
Return the natural logarithm of the Probability Density Function (PDF) of the Exponential distributio...
Definition: pm_distExp.F90:358
This module contains abstract interfaces and types that facilitate benchmarking of different procedur...
Definition: pm_bench.F90:41
This module contains classes and procedures for computing various statistical quantities related to t...
Definition: pm_distExp.F90:112
This module defines the relevant Fortran kind type-parameters frequently used in the ParaMonte librar...
Definition: pm_kind.F90:268
integer, parameter RK
The default real kind in the ParaMonte library: real64 in Fortran, c_double in C-Fortran Interoperati...
Definition: pm_kind.F90:543
integer, parameter IK
The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
Definition: pm_kind.F90:540
integer, parameter SK
The default character kind in the ParaMonte library: kind("a") in Fortran, c_char in C-Fortran Intero...
Definition: pm_kind.F90:539
This is the class for creating benchmark and performance-profiling objects.
Definition: pm_bench.F90:386
subroutine bench(sort, arraySize)

Example Unix compile command via Intel ifort compiler
1#!/usr/bin/env sh
2rm main.exe
3ifort -fpp -standard-semantics -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example Windows Batch compile command via Intel ifort compiler
1del main.exe
2set PATH=..\..\..\lib;%PATH%
3ifort /fpp /standard-semantics /O3 /I:..\..\..\include main.F90 ..\..\..\lib\libparamonte*.lib /exe:main.exe
4main.exe

Example Unix / MinGW compile command via GNU gfortran compiler
1#!/usr/bin/env sh
2rm main.exe
3gfortran -cpp -ffree-line-length-none -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Postprocessing of the benchmark output
1#!/usr/bin/env python
2
3import matplotlib.pyplot as plt
4import pandas as pd
5import numpy as np
6
7fontsize = 14
8
9methods = ["setExpLogPDF", "getExpLogPDF"]
10
11df = pd.read_csv("main.out")
12
13
16
17ax = plt.figure(figsize = 1.25 * np.array([6.4,4.6]), dpi = 200)
18ax = plt.subplot()
19
20for method in methods:
21 plt.plot( df["arraySize"].values
22 , df[method].values
23 , linewidth = 2
24 )
25
26plt.xticks(fontsize = fontsize)
27plt.yticks(fontsize = fontsize)
28ax.set_xlabel("Array Size", fontsize = fontsize)
29ax.set_ylabel("Runtime [ seconds ]", fontsize = fontsize)
30ax.set_title("setExpLogPDF() vs. getExpLogPDF()\nLower is better.", fontsize = fontsize)
31ax.set_xscale("log")
32ax.set_yscale("log")
33plt.minorticks_on()
34plt.grid(visible = True, which = "both", axis = "both", color = "0.85", linestyle = "-")
35ax.tick_params(axis = "y", which = "minor")
36ax.tick_params(axis = "x", which = "minor")
37ax.legend ( methods
38 #, loc='center left'
39 #, bbox_to_anchor=(1, 0.5)
40 , fontsize = fontsize
41 )
42
43plt.tight_layout()
44plt.savefig("benchmark.getExpLogPDF_vs_setExpLogPDF.runtime.png")
45
46
49
50ax = plt.figure(figsize = 1.25 * np.array([6.4,4.6]), dpi = 200)
51ax = plt.subplot()
52
53plt.plot( df["arraySize"].values
54 , np.ones(len(df["arraySize"].values))
55 #, linestyle = "--"
56 #, color = "black"
57 , linewidth = 2
58 )
59plt.plot( df["arraySize"].values
60 , df["getExpLogPDF"].values / df["setExpLogPDF"].values
61 , linewidth = 2
62 )
63
64plt.xticks(fontsize = fontsize)
65plt.yticks(fontsize = fontsize)
66ax.set_xlabel("Array Size", fontsize = fontsize)
67ax.set_ylabel("Runtime compared to setExpLogPDF()", fontsize = fontsize)
68ax.set_title("getExpLogPDF() / setExpLogPDF()\nLower means faster. Lower than 1 means faster than setExpLogPDF().", fontsize = fontsize)
69ax.set_xscale("log")
70#ax.set_yscale("log")
71plt.minorticks_on()
72plt.grid(visible = True, which = "both", axis = "both", color = "0.85", linestyle = "-")
73ax.tick_params(axis = "y", which = "minor")
74ax.tick_params(axis = "x", which = "minor")
75ax.legend ( ["setExpLogPDF", "getExpLogPDF"]
76 #, bbox_to_anchor = (1, 0.5)
77 #, loc = "center left"
78 , fontsize = fontsize
79 )
80
81plt.tight_layout()
82plt.savefig("benchmark.getExpLogPDF_vs_setExpLogPDF.runtime.ratio.png")

Visualization of the benchmark output

Benchmark moral
  1. The procedures under the generic interface getExpLogPDF are functions while the procedures under the generic interface setExpLogPDF are subroutines.
    From the benchmark results, it appears that the functional interface performs slightly less efficiently than the subroutine interface when the input array size is small.
    Otherwise, the difference appears to be marginal and insignificant in most practical situations.


Benchmark :: The runtime performance of setExpLogPDF with and without logInvSigma

1program benchmark
2
3 use iso_fortran_env, only: error_unit
4 use pm_bench, only: bench_type
5 use pm_kind, only: IK, RK, SK
6
7 implicit none
8
9 integer(IK) :: i
10 integer(IK) :: isize
11 integer(IK) :: fileUnit
12 integer(IK) , parameter :: NSIZE = 18_IK
13 integer(IK) , parameter :: NBENCH = 2_IK
14 integer(IK) :: arraySize(0:NSIZE)
15 real(RK) , allocatable :: logPDF(:), point(:)
16 real(RK) , allocatable :: logInvSigma(:), invSigma(:)
17 real(RK) :: dummy = 0._RK
18 type(bench_type) :: bench(NBENCH)
19
20 bench(1) = bench_type(name = SK_"logInvSigmaMissing", exec = logInvSigmaMissing , overhead = setOverhead)
21 bench(2) = bench_type(name = SK_"logInvSigmaPresent", exec = logInvSigmaPresent , overhead = setOverhead)
22
23 arraySize = [( 2_IK**isize, isize = 0_IK, NSIZE )]
24
25 write(*,"(*(g0,:,' '))")
26 write(*,"(*(g0,:,' vs. '))") (bench(i)%name, i = 1, NBENCH)
27 write(*,"(*(g0,:,' '))")
28
29 open(newunit = fileUnit, file = "main.out", status = "replace")
30
31 write(fileUnit, "(*(g0,:,','))") "arraySize", (bench(i)%name, i = 1, NBENCH)
32
33 loopOverArraySize: do isize = 0, NSIZE
34
35 write(*,"(*(g0,:,' '))") "Benchmarking with size", arraySize(isize)
36
37 allocate(logPDF(arraySize(isize)), point(arraySize(isize)), invSigma(arraySize(isize)))
38 call random_number(point)
39 call random_number(invSigma)
40 logInvSigma = log(invSigma)
41 do i = 1, NBENCH
42 bench(i)%timing = bench(i)%getTiming(minsec = 0.05_RK)
43 end do
44 deallocate(logPDF, point, invSigma)
45
46 write(fileUnit,"(*(g0,:,','))") arraySize(isize), (bench(i)%timing%mean, i = 1, NBENCH)
47
48 end do loopOverArraySize
49
50 write(*,"(*(g0,:,' '))") dummy
51 write(*,"(*(g0,:,' '))")
52
53 close(fileUnit)
54
55contains
56
57 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58 ! procedure wrappers.
59 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60
61 subroutine setOverhead()
62 call initialize()
63 call finalize()
64 end subroutine
65
66 subroutine initialize()
67 !call random_number(invSigma)
68 !logInvSigma = log(invSigma)
69 end subroutine
70
71 subroutine finalize()
72 dummy = dummy + logPDF(1)
73 end subroutine
74
75 subroutine logInvSigmaPresent()
76 use pm_distExp, only: setExpLogPDF
77 call initialize()
78 if (arraySize(isize) > 1_IK) then
79 call setExpLogPDF(logPDF, point, invSigma, logInvSigma)
80 else
81 call setExpLogPDF(logPDF(1), point(1), invSigma(1), logInvSigma(1))
82 end if
83 call finalize()
84 end subroutine
85
86 subroutine logInvSigmaMissing()
87 use pm_distExp, only: getExpLogPDF
88 call initialize()
89 if (arraySize(isize) > 1_IK) then
90 logPDF = getExpLogPDF(point, invSigma = invSigma)
91 else
92 logPDF(1) = getExpLogPDF(point(1), invSigma = invSigma(1))
93 end if
94 call finalize()
95 end subroutine
96
97end program benchmark

Example Unix compile command via Intel ifort compiler
1#!/usr/bin/env sh
2rm main.exe
3ifort -fpp -standard-semantics -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example Windows Batch compile command via Intel ifort compiler
1del main.exe
2set PATH=..\..\..\lib;%PATH%
3ifort /fpp /standard-semantics /O3 /I:..\..\..\include main.F90 ..\..\..\lib\libparamonte*.lib /exe:main.exe
4main.exe

Example Unix / MinGW compile command via GNU gfortran compiler
1#!/usr/bin/env sh
2rm main.exe
3gfortran -cpp -ffree-line-length-none -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Postprocessing of the benchmark output
1#!/usr/bin/env python
2
3import matplotlib.pyplot as plt
4import pandas as pd
5import numpy as np
6
7fontsize = 14
8
9methods = ["logInvSigmaPresent", "logInvSigmaMissing"]
10
11df = pd.read_csv("main.out")
12
13
16
17ax = plt.figure(figsize = 1.25 * np.array([6.4,4.6]), dpi = 200)
18ax = plt.subplot()
19
20for method in methods:
21 plt.plot( df["arraySize"].values
22 , df[method].values
23 , linewidth = 2
24 )
25
26plt.xticks(fontsize = fontsize)
27plt.yticks(fontsize = fontsize)
28ax.set_xlabel("Array Size", fontsize = fontsize)
29ax.set_ylabel("Runtime [ seconds ]", fontsize = fontsize)
30ax.set_title("logInvSigmaPresent() vs. logInvSigmaMissing()\nLower is better.", fontsize = fontsize)
31ax.set_xscale("log")
32ax.set_yscale("log")
33plt.minorticks_on()
34plt.grid(visible = True, which = "both", axis = "both", color = "0.85", linestyle = "-")
35ax.tick_params(axis = "y", which = "minor")
36ax.tick_params(axis = "x", which = "minor")
37ax.legend ( methods
38 #, loc='center left'
39 #, bbox_to_anchor=(1, 0.5)
40 , fontsize = fontsize
41 )
42
43plt.tight_layout()
44plt.savefig("benchmark.setExpLogPDF-logInvSigma-missing_vs_present.runtime.png")
45
46
49
50ax = plt.figure(figsize = 1.25 * np.array([6.4,4.6]), dpi = 200)
51ax = plt.subplot()
52
53plt.plot( df["arraySize"].values
54 , np.ones(len(df["arraySize"].values))
55 #, linestyle = "--"
56 #, color = "black"
57 , linewidth = 2
58 )
59plt.plot( df["arraySize"].values
60 , df["logInvSigmaMissing"].values / df["logInvSigmaPresent"].values
61 , linewidth = 2
62 )
63
64plt.xticks(fontsize = fontsize)
65plt.yticks(fontsize = fontsize)
66ax.set_xlabel("Array Size", fontsize = fontsize)
67ax.set_ylabel("Runtime compared to logInvSigmaPresent()", fontsize = fontsize)
68ax.set_title("logInvSigmaMissing / logInvSigmaPresent\nLower means faster. Lower than 1 means faster than logInvSigmaPresent.", fontsize = fontsize)
69ax.set_xscale("log")
70#ax.set_yscale("log")
71plt.minorticks_on()
72plt.grid(visible = True, which = "both", axis = "both", color = "0.85", linestyle = "-")
73ax.tick_params(axis = "y", which = "minor")
74ax.tick_params(axis = "x", which = "minor")
75ax.legend ( ["logInvSigmaPresent", "logInvSigmaMissing"]
76 #, bbox_to_anchor = (1, 0.5)
77 #, loc = "center left"
78 , fontsize = fontsize
79 )
80
81plt.tight_layout()
82plt.savefig("benchmark.setExpLogPDF-logInvSigma-missing_vs_present.runtime.ratio.png")

Visualization of the benchmark output

Benchmark moral
  1. The procedures under the generic interface setExpLogPDF accept an extra argument logInvSigma = log(invSigma) while the procedures under the generic interface getExpLogPDF compute this term internally with every procedure call.
    In the presence of this argument, the logarithmic computation log(invSigma) will be avoided.
    As such, the presence of logInvSigma is expected to lead to faster computations.
Test:
test_pm_distExp


Final Remarks


If you believe this algorithm or its documentation can be improved, we appreciate your contribution and help to edit this page's documentation and source file on GitHub.
For details on the naming abbreviations, see this page.
For details on the naming conventions, see this page.
This software is distributed under the MIT license with additional terms outlined below.

  1. If you use any parts or concepts from this library to any extent, please acknowledge the usage by citing the relevant publications of the ParaMonte library.
  2. If you regenerate any parts/ideas from this library in a programming environment other than those currently supported by this ParaMonte library (i.e., other than C, C++, Fortran, MATLAB, Python, R), please also ask the end users to cite this original ParaMonte library.

This software is available to the public under a highly permissive license.
Help us justify its continued development and maintenance by acknowledging its benefit to society, distributing it, and contributing to it.

Author:
Amir Shahmoradi, Oct 16, 2009, 11:14 AM, Michigan

Variable Documentation

◆ MODULE_NAME

character(*, SK), parameter pm_distExp::MODULE_NAME = "@pm_distExp"

Definition at line 118 of file pm_distExp.F90.