Biogeme: Python Library  2.5
distributions.py
Go to the documentation of this file.
1 ## \file
2 # Implementation of the pdf and CDF of common distributions
3 # \author Michel Bierlaire
4 # \date Thu Apr 23 12:01:49 2015
5 from biogeme import *
6 
7 ## \brief Normal pdf
8 #
9 # Probability density function of a normal distribution \f[ f(x;\mu,\sigma) = \frac{1}{\sigma \sqrt{2\pi}} \exp{-\frac{(x-\mu)^2}{2\sigma^2}} \f]
10 # \ingroup pdfcdf
11 # \param x argument of the pdf
12 # \param mu location parameter \f$\mu\f$ (Default: 0)
13 # \param s scale parameter \f$\sigma\f$ (Default: 1)
14 # \note It is assumed that \f$\sigma > 0\f$, but it is not verified by the code.
15 def normalpdf(x,mu=0.0,s=1.0):
16  d = -(x-mu)*(x-mu)
17  n = 2.0*s*s
18  a = d/n
19  num = exp(a)
20  den = s*2.506628275
21  p = num / den
22  return p
23 
24 ## \brief Log normal pdf
25 #
26 # Probability density function of a log normal distribution \f[ f(x;\mu,\sigma) = \frac{1}{x\sigma \sqrt{2\pi}} \exp{-\frac{(\ln x-\mu)^2}{2\sigma^2}} \f]
27 # \ingroup pdfcdf
28 # \param x argument of the pdf
29 # \param mu location parameter \f$\mu\f$ (Default: 0)
30 # \param s scale parameter \f$\sigma\f$ (Default: 1)
31 # \note It is assumed that \f$\sigma > 0\f$, but it is not verified by the code.
32 def lognormalpdf(x,mu,s):
33  d = -(log(x)-mu)*(log(x)-mu)
34  n = 2.0*s*s
35  a = d/n
36  num = exp(a)
37  den = x*s*2.506628275
38  p = (x>0)* num / den
39  return p
40 
41 ## \brief Uniform pdf
42 #
43 # Probability density function of a uniform distribution \f[ f(x;a,b) = \left\{ \begin{array}{ll} \frac{1}{b-a} & \text{for } x \in [a,b] \\ 0 & \text{otherwise}\end{array} \right.\f]
44 # \ingroup pdfcdf
45 # \param x argument of the pdf
46 # \param a lower bound \f$a\f$ of the distribution (Default: -1)
47 # \param b upper bound \f$b\f$ of the distribution (Default: 1)
48 # \note It is assumed that \f$a < b \f$, but it is not verified by the code.
49 def uniformpdf(x,a=-1,b=1.0):
50  result = (x < a) * 0.0 + (x >= b) * 0.0 + (x >= a) * (x < b) / (b-a)
51  return result
52 
53 ## \brief Triangular pdf
54 #
55 # Probability density function of a triangular distribution \f[ f(x;a,b,c) = \left\{ \begin{array}{ll} 0 & \text{if } x < a \\\frac{2(x-a)}{(b-a)(c-a)} & \text{if } a \leq x < c \\\frac{2(b-x)}{(b-a)(b-c)} & \text{if } c \leq x < b \\0 & \text{if } x \geq b.\end{array} \right.\f]
56 # \ingroup pdfcdf
57 # \param x argument of the pdf
58 # \param a lower bound \f$a\f$ of the distribution (Default: -1)
59 # \param b upper bound \f$b\f$ of the distribution (Default: 1)
60 # \param c mode \f$c\f$ of the distribution (Default: 0)
61 # \note It is assumed that \f$a < b \f$, and \f$a \leq c \leq b\f$, but it is not verified by the code.
62 def triangularpdf(x,a=-1.0,b=1.0,c=0.0):
63  result = (x < a) * 0.0 + (x >= b) * 0.0 + (x >= a) * (x < c) * 2.0 * ((x-a)/((b-a)*(c-a))) * (x >= c) * (x < b) * 2.0 * (b-x) / ((b-a)*(b-c))
64  return result
65 
66 ## \brief Logistic CDF
67 #
68 # Cumulative distribution function of a logistic distribution \f[ f(x;\mu,\sigma) = \frac{1}{1+\exp\left(-\frac{x-\mu}{\sigma} \right)} \f]
69 # \ingroup pdfcdf
70 # \param x argument of the pdf
71 # \param mu location parameter \f$\mu\f$ (Default: 0)
72 # \param s scale parameter \f$\sigma\f$ (Default: 1)
73 # \note It is assumed that \f$\sigma > 0\f$, but it is not verified by the code.
74 def logisticcdf(x,mu=0.0,s=1.0):
75  result = 1.0 /( 1.0 + exp(-(x-mu)/s))
76  return result
def normalpdf(x, mu=0.0, s=1.0)
Normal pdf.
def triangularpdf(x, a=-1.0, b=1.0, c=0.0)
Triangular pdf.
def lognormalpdf(x, mu, s)
Log normal pdf.
def logisticcdf(x, mu=0.0, s=1.0)
Logistic CDF.
def uniformpdf(x, a=-1, b=1.0)
Uniform pdf.
Copyright 2016 Michel Bierlaire