/
gaussian.py
103 lines (90 loc) · 3.27 KB
/
gaussian.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""The Gaussian / Normal distribution."""
from equadratures.distributions.template import Distribution
import numpy as np
from scipy.stats import norm
from scipy.special import erf, erfinv, gamma, beta, betainc, gammainc
RECURRENCE_PDF_SAMPLES = 8000
class Gaussian(Distribution):
"""
The class defines a Gaussian object. It is the child of Distribution.
:param double mean:
Mean of the Gaussian distribution.
:param double variance:
Variance of the Gaussian distribution.
"""
def __init__(self, mean, variance):
if mean is None:
self.mean = 0.0
else:
self.mean = mean
if variance is None:
self.variance = 1.0
else:
self.variance = variance
super
if self.variance <= 0:
raise ValueError('Invalid Gaussian distribution parameters. Variance should be positive.')
self.sigma = np.sqrt(self.variance)
self.x_range_for_pdf = np.linspace(-15.0 * self.sigma, 15.0*self.sigma, RECURRENCE_PDF_SAMPLES) + self.mean
self.parent = norm(loc=self.mean, scale=self.sigma)
self.skewness = 0.0
self.kurtosis = 0.0
self.bounds = np.array([-np.inf, np.inf])
def get_description(self):
"""
A description of the Gaussian.
:param Gaussian self:
An instance of the Gaussian class.
:return:
A string describing the Gaussian.
"""
text = "is a Gaussian distribution with a mean of "+str(self.mean)+" and a variance of "+str(self.variance)+"."
return text
def get_samples(self, m=None):
"""
Generates samples from the Gaussian distribution.
:param Gaussian self:
An instance of the Gaussian class.
:param integer m:
Number of random samples. If no value is provided, a default of 5e5 is assumed.
:return:
A N-by-1 vector that contains the samples.
"""
if m is not None:
number = m
else:
number = 500000
return self.parent.rvs(size=number)
def get_pdf(self, points=None):
"""
A Gaussian probability distribution.
:param Gaussian self:
An instance of the Gaussian class.
:return:
An array of N equidistant values over the support of the distribution.
:return:
Probability density values along the support of the Gaussian distribution.
"""
return self.parent.pdf(points)
def get_cdf(self, points=None):
"""
A Gaussian cumulative density function.
:param Gaussian self:
An instance of the Gaussian class.
:param array points
Points for which the cumulative density function is required.
:return:
Gaussian cumulative density values.
"""
return self.parent.cdf(points)
def get_icdf(self, xx):
"""
An inverse Gaussian cumulative density function.
:param Gaussian self:
An instance of the Gaussian class.
:param array xx:
A numpy array of uniformly distributed samples between [0,1].
:return:
Inverse CDF samples associated with the Gaussian distribution.
"""
return self.parent.ppf(xx)