;ò
˜+ÛAc@s~dZdkZeideidd„Z[dklZdefd„ƒYZd„Zd efd
„ƒYZ [dZ
dS(sôThe gaussian distribution.
The primitive distribution is given by function gauss(). For (one-dimensional)
gaussian distributions, use the class Normal(mean, stddev), derived from
variate.Variate, q.v., so as to provide probabilities as integrals of gauss(),
with suitable scalings.
Some day I may add support for multi-dimensional gaussian distributions. I'll
use Gaussian(mean, variance) as its interface; mean will be a vector quantity,
variance will be a tensor quantity of the same rank as mean*mean. For the
present, a function Gaussian(mean, variance) is defined, working only for
one-dimensional data, implemented using Normal.
See also gamma.py for Gamma distributions.
Note that a Gaussian distribution always has non-zero probability of delivering
negative values. Many variates are incapable of delivering negative values (for
example, the heights of human beings) so clearly cannot be normally distributed
(though they may be Gamma distributed). However, provided the variate is also
incapable of being zero, we can take the logarithm of the variate (divided, if
necessary, by some suitable unit) to obtain a variate which may be negative or
positive, so may be well modelled by a normal distribution. From this we can
then infer a distribution for the original variate, the `lognormal'
distribution, which does guarantee positive values, yet has roughly the form of
the normal distribution.
From HAKMEM, http://www.inwap.com/pdp10/hbaker/hakmem/random.html:
ITEM 26 (? via Salamin):
A mathematically exact method of generating a Gaussian distribution
from a uniform distribution: let x be uniform on [0,1] and y uniform
on [0, 2 pi], x and y independent. Calculate r = sqrt(-log x). Then
r cos y and r sin y are two independent Gaussian distributed random
numbers.
ITEM 27 (Salamin):
PROBLEM: Generate random unit vectors in N-space uniform on the unit
sphere. SOLUTION: Generate N Gaussian random numbers and normalize
to unit length.
$Id: gauss.py,v 1.2 2005/01/04 23:49:44 eddy Exp $
Nif0.5cCs|||dƒ|SdS(Ni(sesxsn(sxsesn((s'/home/eddy/.sys/py/study/maths/gauss.pysgauss/s(sVariatesNormalcBs5tZeiZd„ZdkZeid„Z[RS(NcCséd}y.|i|ift|iƒo
t}nWntj
onXy.|i|ift|iƒo
t}nWntj
onX|o||t d„}n||t d„}|i||ƒ||f\|_|_
dS(sšInitialises a (one-dimensional) Normal distribution.
Takes two arguments; mean and stddev, the mean and standard deviation
of the normal distribution. icCs||||ƒ|SdS(N(sgsvalsmss(svalsmsssg((s'/home/eddy/.sys/py/study/maths/gauss.pysfuncGscCs|||i|ƒ|SdS(N(svalsmsssevaluatesg(svalsmsssg((s'/home/eddy/.sys/py/study/maths/gauss.pysfuncKsN(sscalarsmeanswidthsbestscallablesevaluatesNonesAttributeErrorsstddevsgausssfuncsselfs_Normal__upinitssigma(sselfsmeansstddevsfuncsscalar((s'/home/eddy/.sys/py/study/maths/gauss.pys__init__5s&cCs||i|iƒSdS(N(sgsselfsmeanssigma(sselfsg((s'/home/eddy/.sys/py/study/maths/gauss.pyssampleRs(s__name__s
__module__sVariates__init__s_Normal__upinitsrandomsgaussssample(((s'/home/eddy/.sys/py/study/maths/gauss.pysNormal3s
cCst||dƒSdS(Nf0.5(sNormalsmeansvariance(smeansvariance((s'/home/eddy/.sys/py/study/maths/gauss.pysGaussianVss logNormalcBs tZdZeiZd„ZRS(s§Distribution of a variate whose logarithm is normally distributed.
If the variate is X, then there is some constant m for which log(X/m) is
normally distributed with expected value 0. [To find such an m; chose any
value X could take, say x, and find the mean, say q, of log(X/x); then
log(X/x) -q is normally distributed with mean 0; and log(X/x) -q is just
log(X/x.exp(q)) so use m = x.exp(q).]
cCsdS(N((sselfsmeansvary((s'/home/eddy/.sys/py/study/maths/gauss.pys__init__es(s__name__s
__module__s__doc__sVariates__init__s_logNormal__upinit(((s'/home/eddy/.sys/py/study/maths/gauss.pys logNormalYs s•
$Log: gauss.py,v $
Revision 1.2 2005/01/04 23:49:44 eddy
Added notes from HAKMEM.
Revision 1.1 2002/10/08 21:35:02 eddy
Initial revision
(s__doc__smathsexpspisgausssvariatesVariatesNormalsGaussians logNormals_rcs_log(s logNormalsNormalsGaussiansgausssVariates_rcs_logsmath((s'/home/eddy/.sys/py/study/maths/gauss.pys?,s
#