statistics
— Mathematical statistics functions¶
Added in version 3.4.
Source code:Lib/statistics.py
This module provides functions for calculating mathematical statistics ofnumeric (Real
-valued) data.
The module is not intended to be a competitor to third-party libraries suchasNumPy,SciPy, orproprietary full-featured statistics packages aimed at professionalstatisticians such as Minitab, SAS and Matlab. It is aimed at the level ofgraphing and scientific calculators.
Unless explicitly noted, these functions supportint
,float
,Decimal
andFraction
.Behaviour with other types (whether in the numeric tower or not) iscurrently unsupported. Collections with a mix of types are also undefinedand implementation-dependent. If your input data consists of mixed types,you may be able to usemap()
to ensure a consistent result, forexample:map(float,input_data)
.
Some datasets useNaN
(not a number) values to represent missing data.Since NaNs have unusual comparison semantics, they cause surprising orundefined behaviors in the statistics functions that sort data or that countoccurrences. The functions affected aremedian()
,median_low()
,median_high()
,median_grouped()
,mode()
,multimode()
, andquantiles()
. TheNaN
values should be stripped before calling thesefunctions:
>>>fromstatisticsimportmedian>>>frommathimportisnan>>>fromitertoolsimportfilterfalse>>>data=[20.7,float('NaN'),19.2,18.3,float('NaN'),14.4]>>>sorted(data)# This has surprising behavior[20.7, nan, 14.4, 18.3, 19.2, nan]>>>median(data)# This result is unexpected16.35>>>sum(map(isnan,data))# Number of missing values2>>>clean=list(filterfalse(isnan,data))# Strip NaN values>>>clean[20.7, 19.2, 18.3, 14.4]>>>sorted(clean)# Sorting now works as expected[14.4, 18.3, 19.2, 20.7]>>>median(clean)# This result is now well defined18.75
Averages and measures of central location¶
These functions calculate an average or typical value from a populationor sample.
Arithmetic mean («average») of data. | |
Fast, floating-point arithmetic mean, with optional weighting. | |
Geometric mean of data. | |
Harmonic mean of data. | |
Median (middle value) of data. | |
Low median of data. | |
High median of data. | |
Median (50th percentile) of grouped data. | |
Single mode (most common value) of discrete or nominal data. | |
List of modes (most common values) of discrete or nominal data. | |
Divide data into intervals with equal probability. |
Measures of spread¶
These functions calculate a measure of how much the population or sampletends to deviate from the typical or average values.
Population standard deviation of data. | |
Population variance of data. | |
Sample standard deviation of data. | |
Sample variance of data. |
Statistics for relations between two inputs¶
These functions calculate statistics regarding relations between two inputs.
Sample covariance for two variables. | |
Pearson and Spearman’s correlation coefficients. | |
Slope and intercept for simple linear regression. |
Function details¶
Note: The functions do not require the data given to them to be sorted.However, for reading convenience, most of the examples show sorted sequences.
- statistics.mean(data)¶
Return the sample arithmetic mean ofdata which can be a sequence or iterable.
The arithmetic mean is the sum of the data divided by the number of datapoints. It is commonly called «the average», although it is only one of manydifferent mathematical averages. It is a measure of the central location ofthe data.
Ifdata is empty,
StatisticsError
will be raised.Some examples of use:
>>>mean([1,2,3,4,4])2.8>>>mean([-1.0,2.5,3.25,5.75])2.625>>>fromfractionsimportFractionasF>>>mean([F(3,7),F(1,21),F(5,3),F(1,3)])Fraction(13, 21)>>>fromdecimalimportDecimalasD>>>mean([D("0.5"),D("0.75"),D("0.625"),D("0.375")])Decimal('0.5625')
Σημείωση
The mean is strongly affected byoutliers and is not necessarily atypical example of the data points. For a more robust, although lessefficient, measure ofcentral tendency, see
median()
.The sample mean gives an unbiased estimate of the true population mean,so that when taken on average over all the possible samples,
mean(sample)
converges on the true mean of the entire population. Ifdata represents the entire population rather than a sample, thenmean(data)
is equivalent to calculating the true population mean μ.
- statistics.fmean(data,weights=None)¶
Convertdata to floats and compute the arithmetic mean.
This runs faster than the
mean()
function and it always returns afloat
. Thedata may be a sequence or iterable. If the inputdataset is empty, raises aStatisticsError
.>>>fmean([3.5,4.0,5.25])4.25
Optional weighting is supported. For example, a professor assigns agrade for a course by weighting quizzes at 20%, homework at 20%, amidterm exam at 30%, and a final exam at 30%:
>>>grades=[85,92,83,91]>>>weights=[0.20,0.20,0.30,0.30]>>>fmean(grades,weights)87.6
Ifweights is supplied, it must be the same length as thedata ora
ValueError
will be raised.Added in version 3.8.
Άλλαξε στην έκδοση 3.11:Added support forweights.
- statistics.geometric_mean(data)¶
Convertdata to floats and compute the geometric mean.
The geometric mean indicates the central tendency or typical value of thedata using the product of the values (as opposed to the arithmetic meanwhich uses their sum).
Raises a
StatisticsError
if the input dataset is empty,if it contains a zero, or if it contains a negative value.Thedata may be a sequence or iterable.No special efforts are made to achieve exact results.(However, this may change in the future.)
>>>round(geometric_mean([54,24,36]),1)36.0
Added in version 3.8.
- statistics.harmonic_mean(data,weights=None)¶
Return the harmonic mean ofdata, a sequence or iterable ofreal-valued numbers. Ifweights is omitted or
None
, thenequal weighting is assumed.The harmonic mean is the reciprocal of the arithmetic
mean()
of thereciprocals of the data. For example, the harmonic mean of three valuesa,b andc will be equivalent to3/(1/a+1/b+1/c)
. If one of thevalues is zero, the result will be zero.The harmonic mean is a type of average, a measure of the centrallocation of the data. It is often appropriate when averagingratios or rates, for example speeds.
Suppose a car travels 10 km at 40 km/hr, then another 10 km at 60 km/hr.What is the average speed?
>>>harmonic_mean([40,60])48.0
Suppose a car travels 40 km/hr for 5 km, and when traffic clears,speeds-up to 60 km/hr for the remaining 30 km of the journey. Whatis the average speed?
>>>harmonic_mean([40,60],weights=[5,30])56.0
StatisticsError
is raised ifdata is empty, any elementis less than zero, or if the weighted sum isn’t positive.The current algorithm has an early-out when it encounters a zeroin the input. This means that the subsequent inputs are not testedfor validity. (This behavior may change in the future.)
Added in version 3.6.
Άλλαξε στην έκδοση 3.10:Added support forweights.
- statistics.median(data)¶
Return the median (middle value) of numeric data, using the common «mean ofmiddle two» method. Ifdata is empty,
StatisticsError
is raised.data can be a sequence or iterable.The median is a robust measure of central location and is less affected bythe presence of outliers. When the number of data points is odd, themiddle data point is returned:
>>>median([1,3,5])3
When the number of data points is even, the median is interpolated by takingthe average of the two middle values:
>>>median([1,3,5,7])4.0
This is suited for when your data is discrete, and you don’t mind that themedian may not be an actual data point.
If the data is ordinal (supports order operations) but not numeric (doesn’tsupport addition), consider using
median_low()
ormedian_high()
instead.
- statistics.median_low(data)¶
Return the low median of numeric data. Ifdata is empty,
StatisticsError
is raised.data can be a sequence or iterable.The low median is always a member of the data set. When the number of datapoints is odd, the middle value is returned. When it is even, the smaller ofthe two middle values is returned.
>>>median_low([1,3,5])3>>>median_low([1,3,5,7])3
Use the low median when your data are discrete and you prefer the median tobe an actual data point rather than interpolated.
- statistics.median_high(data)¶
Return the high median of data. Ifdata is empty,
StatisticsError
is raised.data can be a sequence or iterable.The high median is always a member of the data set. When the number of datapoints is odd, the middle value is returned. When it is even, the larger ofthe two middle values is returned.
>>>median_high([1,3,5])3>>>median_high([1,3,5,7])5
Use the high median when your data are discrete and you prefer the median tobe an actual data point rather than interpolated.
- statistics.median_grouped(data,interval=1.0)¶
Estimates the median for numeric data that has beengrouped or binned around the midpointsof consecutive, fixed-width intervals.
Thedata can be any iterable of numeric data with each value beingexactly the midpoint of a bin. At least one value must be present.
Theinterval is the width of each bin.
For example, demographic information may have been summarized intoconsecutive ten-year age groups with each group being representedby the 5-year midpoints of the intervals:
>>>fromcollectionsimportCounter>>>demographics=Counter({...25:172,# 20 to 30 years old...35:484,# 30 to 40 years old...45:387,# 40 to 50 years old...55:22,# 50 to 60 years old...65:6,# 60 to 70 years old...})...
The 50th percentile (median) is the 536th person out of the 1071member cohort. That person is in the 30 to 40 year old age group.
The regular
median()
function would assume that everyone in thetricenarian age group was exactly 35 years old. A more tenableassumption is that the 484 members of that age group are evenlydistributed between 30 and 40. For that, we usemedian_grouped()
:>>>data=list(demographics.elements())>>>median(data)35>>>round(median_grouped(data,interval=10),1)37.5
The caller is responsible for making sure the data points are separatedby exact multiples ofinterval. This is essential for getting acorrect result. The function does not check this precondition.
Inputs may be any numeric type that can be coerced to a float duringthe interpolation step.
- statistics.mode(data)¶
Return the single most common data point from discrete or nominaldata.The mode (when it exists) is the most typical value and serves as ameasure of central location.
If there are multiple modes with the same frequency, returns the first oneencountered in thedata. If the smallest or largest of those isdesired instead, use
min(multimode(data))
ormax(multimode(data))
.If the inputdata is empty,StatisticsError
is raised.mode
assumes discrete data and returns a single value. This is thestandard treatment of the mode as commonly taught in schools:>>>mode([1,1,2,3,3,3,3,4])3
The mode is unique in that it is the only statistic in this package thatalso applies to nominal (non-numeric) data:
>>>mode(["red","blue","blue","red","green","red","red"])'red'
Only hashable inputs are supported. To handle type
set
,consider casting tofrozenset
. To handle typelist
,consider casting totuple
. For mixed or nested inputs, considerusing this slower quadratic algorithm that only depends on equality tests:max(data,key=data.count)
.Άλλαξε στην έκδοση 3.8:Now handles multimodal datasets by returning the first mode encountered.Formerly, it raised
StatisticsError
when more than one mode wasfound.
- statistics.multimode(data)¶
Return a list of the most frequently occurring values in the order theywere first encountered in thedata. Will return more than one result ifthere are multiple modes or an empty list if thedata is empty:
>>>multimode('aabbbbccddddeeffffgg')['b', 'd', 'f']>>>multimode('')[]
Added in version 3.8.
- statistics.pstdev(data,mu=None)¶
Return the population standard deviation (the square root of the populationvariance). See
pvariance()
for arguments and other details.>>>pstdev([1.5,2.5,2.5,2.75,3.25,4.75])0.986893273527251
- statistics.pvariance(data,mu=None)¶
Return the population variance ofdata, a non-empty sequence or iterableof real-valued numbers. Variance, or second moment about the mean, is ameasure of the variability (spread or dispersion) of data. A largevariance indicates that the data is spread out; a small variance indicatesit is clustered closely around the mean.
If the optional second argumentmu is given, it should be thepopulationmean of thedata. It can also be used to compute the second moment arounda point that is not the mean. If it is missing or
None
(the default),the arithmetic mean is automatically calculated.Use this function to calculate the variance from the entire population. Toestimate the variance from a sample, the
variance()
function is usuallya better choice.Raises
StatisticsError
ifdata is empty.Examples:
>>>data=[0.0,0.25,0.25,1.25,1.5,1.75,2.75,3.25]>>>pvariance(data)1.25
If you have already calculated the mean of your data, you can pass it as theoptional second argumentmu to avoid recalculation:
>>>mu=mean(data)>>>pvariance(data,mu)1.25
Decimals and Fractions are supported:
>>>fromdecimalimportDecimalasD>>>pvariance([D("27.5"),D("30.25"),D("30.25"),D("34.5"),D("41.75")])Decimal('24.815')>>>fromfractionsimportFractionasF>>>pvariance([F(1,4),F(5,4),F(1,2)])Fraction(13, 72)
Σημείωση
When called with the entire population, this gives the population varianceσ². When called on a sample instead, this is the biased sample variances², also known as variance with N degrees of freedom.
If you somehow know the true population mean μ, you may use thisfunction to calculate the variance of a sample, giving the knownpopulation mean as the second argument. Provided the data points are arandom sample of the population, the result will be an unbiased estimateof the population variance.
- statistics.stdev(data,xbar=None)¶
Return the sample standard deviation (the square root of the samplevariance). See
variance()
for arguments and other details.>>>stdev([1.5,2.5,2.5,2.75,3.25,4.75])1.0810874155219827
- statistics.variance(data,xbar=None)¶
Return the sample variance ofdata, an iterable of at least two real-valuednumbers. Variance, or second moment about the mean, is a measure of thevariability (spread or dispersion) of data. A large variance indicates thatthe data is spread out; a small variance indicates it is clustered closelyaround the mean.
If the optional second argumentxbar is given, it should be thesamplemean ofdata. If it is missing or
None
(the default), the mean isautomatically calculated.Use this function when your data is a sample from a population. To calculatethe variance from the entire population, see
pvariance()
.Raises
StatisticsError
ifdata has fewer than two values.Examples:
>>>data=[2.75,1.75,1.25,0.25,0.5,1.25,3.5]>>>variance(data)1.3720238095238095
If you have already calculated the sample mean of your data, you can pass itas the optional second argumentxbar to avoid recalculation:
>>>m=mean(data)>>>variance(data,m)1.3720238095238095
This function does not attempt to verify that you have passed the actual meanasxbar. Using arbitrary values forxbar can lead to invalid orimpossible results.
Decimal and Fraction values are supported:
>>>fromdecimalimportDecimalasD>>>variance([D("27.5"),D("30.25"),D("30.25"),D("34.5"),D("41.75")])Decimal('31.01875')>>>fromfractionsimportFractionasF>>>variance([F(1,6),F(1,2),F(5,3)])Fraction(67, 108)
Σημείωση
This is the sample variance s² with Bessel’s correction, also known asvariance with N-1 degrees of freedom. Provided that the data points arerepresentative (e.g. independent and identically distributed), the resultshould be an unbiased estimate of the true population variance.
If you somehow know the actual population mean μ you should pass it to the
pvariance()
function as themu parameter to get the variance of asample.
- statistics.quantiles(data,*,n=4,method='exclusive')¶
Dividedata inton continuous intervals with equal probability.Returns a list of
n-1
cut points separating the intervals.Setn to 4 for quartiles (the default). Setn to 10 for deciles. Setn to 100 for percentiles which gives the 99 cuts points that separatedata into 100 equal sized groups. Raises
StatisticsError
ifnis not least 1.Thedata can be any iterable containing sample data. For meaningfulresults, the number of data points indata should be larger thann.Raises
StatisticsError
if there are not at least two data points.The cut points are linearly interpolated from thetwo nearest data points. For example, if a cut point falls one-thirdof the distance between two sample values,
100
and112
, thecut-point will evaluate to104
.Themethod for computing quantiles can be varied depending onwhether thedata includes or excludes the lowest andhighest possible values from the population.
The defaultmethod is «exclusive» and is used for data sampled froma population that can have more extreme values than found in thesamples. The portion of the population falling below thei-th ofm sorted data points is computed as
i/(m+1)
. Given ninesample values, the method sorts them and assigns the followingpercentiles: 10%, 20%, 30%, 40%, 50%, 60%, 70%, 80%, 90%.Setting themethod to «inclusive» is used for describing populationdata or for samples that are known to include the most extreme valuesfrom the population. The minimum value indata is treated as the 0thpercentile and the maximum value is treated as the 100th percentile.The portion of the population falling below thei-th ofm sorteddata points is computed as
(i-1)/(m-1)
. Given 11 samplevalues, the method sorts them and assigns the following percentiles:0%, 10%, 20%, 30%, 40%, 50%, 60%, 70%, 80%, 90%, 100%.# Decile cut points for empirically sampled data>>>data=[105,129,87,86,111,111,89,81,108,92,110,...100,75,105,103,109,76,119,99,91,103,129,...106,101,84,111,74,87,86,103,103,106,86,...111,75,87,102,121,111,88,89,101,106,95,...103,107,101,81,109,104]>>>[round(q,1)forqinquantiles(data,n=10)][81.0, 86.2, 89.0, 99.4, 102.5, 103.6, 106.0, 109.8, 111.0]
Added in version 3.8.
- statistics.covariance(x,y,/)¶
Return the sample covariance of two inputsx andy. Covarianceis a measure of the joint variability of two inputs.
Both inputs must be of the same length (no less than two), otherwise
StatisticsError
is raised.Examples:
>>>x=[1,2,3,4,5,6,7,8,9]>>>y=[1,2,3,1,2,3,1,2,3]>>>covariance(x,y)0.75>>>z=[9,8,7,6,5,4,3,2,1]>>>covariance(x,z)-7.5>>>covariance(z,x)-7.5
Added in version 3.10.
- statistics.correlation(x,y,/,*,method='linear')¶
Return thePearson’s correlation coefficientfor two inputs. Pearson’s correlation coefficientr takes valuesbetween -1 and +1. It measures the strength and direction of a linearrelationship.
Ifmethod is «ranked», computesSpearman’s rank correlation coefficientfor two inputs. The data is replaced by ranks. Ties are averaged so thatequal values receive the same rank. The resulting coefficient measures thestrength of a monotonic relationship.
Spearman’s correlation coefficient is appropriate for ordinal data or forcontinuous data that doesn’t meet the linear proportion requirement forPearson’s correlation coefficient.
Both inputs must be of the same length (no less than two), and neednot to be constant, otherwise
StatisticsError
is raised.Example withKepler’s laws of planetary motion:
>>># Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, and Neptune>>>orbital_period=[88,225,365,687,4331,10_756,30_687,60_190]# days>>>dist_from_sun=[58,108,150,228,778,1_400,2_900,4_500]# million km>>># Show that a perfect monotonic relationship exists>>>correlation(orbital_period,dist_from_sun,method='ranked')1.0>>># Observe that a linear relationship is imperfect>>>round(correlation(orbital_period,dist_from_sun),4)0.9882>>># Demonstrate Kepler's third law: There is a linear correlation>>># between the square of the orbital period and the cube of the>>># distance from the sun.>>>period_squared=[p*pforpinorbital_period]>>>dist_cubed=[d*d*dfordindist_from_sun]>>>round(correlation(period_squared,dist_cubed),4)1.0
Added in version 3.10.
Άλλαξε στην έκδοση 3.12:Added support for Spearman’s rank correlation coefficient.
- statistics.linear_regression(x,y,/,*,proportional=False)¶
Return the slope and intercept ofsimple linear regressionparameters estimated using ordinary least squares. Simple linearregression describes the relationship between an independent variablex anda dependent variabley in terms of this linear function:
y = slope * x + intercept + noise
where
slope
andintercept
are the regression parameters that areestimated, andnoise
represents thevariability of the data that was not explained by the linear regression(it is equal to the difference between predicted and actual valuesof the dependent variable).Both inputs must be of the same length (no less than two), andthe independent variablex cannot be constant;otherwise a
StatisticsError
is raised.For example, we can use therelease dates of the MontyPython filmsto predict the cumulative number of Monty Python filmsthat would have been produced by 2019assuming that they had kept the pace.
>>>year=[1971,1975,1979,1982,1983]>>>films_total=[1,2,3,4,5]>>>slope,intercept=linear_regression(year,films_total)>>>round(slope*2019+intercept)16
Ifproportional is true, the independent variablex and thedependent variabley are assumed to be directly proportional.The data is fit to a line passing through the origin.Since theintercept will always be 0.0, the underlying linearfunction simplifies to:
y = slope * x + noise
Continuing the example from
correlation()
, we look to seehow well a model based on major planets can predict the orbitaldistances for dwarf planets:>>>model=linear_regression(period_squared,dist_cubed,proportional=True)>>>slope=model.slope>>># Dwarf planets: Pluto, Eris, Makemake, Haumea, Ceres>>>orbital_periods=[90_560,204_199,111_845,103_410,1_680]# days>>>predicted_dist=[math.cbrt(slope*(p*p))forpinorbital_periods]>>>list(map(round,predicted_dist))[5912, 10166, 6806, 6459, 414]>>>[5_906,10_152,6_796,6_450,414]# actual distance in million km[5906, 10152, 6796, 6450, 414]
Added in version 3.10.
Άλλαξε στην έκδοση 3.11:Added support forproportional.
Exceptions¶
A single exception is defined:
- exceptionstatistics.StatisticsError¶
Subclass of
ValueError
for statistics-related exceptions.
NormalDist
objects¶
NormalDist
is a tool for creating and manipulating normaldistributions of arandom variable. It is aclass that treats the mean and standard deviation of datameasurements as a single entity.
Normal distributions arise from theCentral Limit Theorem and have a wide rangeof applications in statistics.
- classstatistics.NormalDist(mu=0.0,sigma=1.0)¶
Returns a newNormalDist object wheremu represents thearithmeticmean andsigmarepresents thestandard deviation.
Ifsigma is negative, raises
StatisticsError
.- mean¶
A read-only property for thearithmetic mean of a normaldistribution.
- stdev¶
A read-only property for thestandard deviation of a normaldistribution.
- variance¶
A read-only property for thevariance of a normaldistribution. Equal to the square of the standard deviation.
- classmethodfrom_samples(data)¶
Makes a normal distribution instance withmu andsigma parametersestimated from thedata using
fmean()
andstdev()
.Thedata can be anyiterable and should consist of valuesthat can be converted to type
float
. Ifdata does notcontain at least two elements, raisesStatisticsError
because ittakes at least one point to estimate a central value and at least twopoints to estimate dispersion.
- samples(n,*,seed=None)¶
Generatesn random samples for a given mean and standard deviation.Returns a
list
offloat
values.Ifseed is given, creates a new instance of the underlying randomnumber generator. This is useful for creating reproducible results,even in a multi-threading context.
- pdf(x)¶
Using aprobability density function (pdf), computethe relative likelihood that a random variableX will be near thegiven valuex. Mathematically, it is the limit of the ratio
P(x<=X<x+dx)/dx
asdx approaches zero.The relative likelihood is computed as the probability of a sampleoccurring in a narrow range divided by the width of the range (hencethe word «density»). Since the likelihood is relative to other points,its value can be greater than
1.0
.
- cdf(x)¶
Using acumulative distribution function (cdf),compute the probability that a random variableX will be less than orequal tox. Mathematically, it is written
P(X<=x)
.
- inv_cdf(p)¶
Compute the inverse cumulative distribution function, also known as thequantile functionor thepercent-pointfunction. Mathematically, it is written
x:P(X<=x)=p
.Finds the valuex of the random variableX such that theprobability of the variable being less than or equal to that valueequals the given probabilityp.
- overlap(other)¶
Measures the agreement between two normal probability distributions.Returns a value between 0.0 and 1.0 givingthe overlapping area forthe two probability density functions.
- quantiles(n=4)¶
Divide the normal distribution inton continuous intervals withequal probability. Returns a list of (n - 1) cut points separatingthe intervals.
Setn to 4 for quartiles (the default). Setn to 10 for deciles.Setn to 100 for percentiles which gives the 99 cuts points thatseparate the normal distribution into 100 equal sized groups.
- zscore(x)¶
Compute theStandard Scoredescribingx in terms of the number of standard deviationsabove or below the mean of the normal distribution:
(x-mean)/stdev
.Added in version 3.9.
Instances of
NormalDist
support addition, subtraction,multiplication and division by a constant. These operationsare used for translation and scaling. For example:>>>temperature_february=NormalDist(5,2.5)# Celsius>>>temperature_february*(9/5)+32# FahrenheitNormalDist(mu=41.0, sigma=4.5)
Dividing a constant by an instance of
NormalDist
is not supportedbecause the result wouldn’t be normally distributed.Since normal distributions arise from additive effects of independentvariables, it is possible toadd and subtract two independent normallydistributed random variablesrepresented as instances of
NormalDist
. For example:>>>birth_weights=NormalDist.from_samples([2.5,3.1,2.1,2.4,2.7,3.5])>>>drug_effects=NormalDist(0.4,0.15)>>>combined=birth_weights+drug_effects>>>round(combined.mean,1)3.1>>>round(combined.stdev,1)0.5
Added in version 3.8.
Examples and Recipes¶
Classic probability problems¶
NormalDist
readily solves classic probability problems.
For example, givenhistorical data for SAT exams showingthat scores are normally distributed with a mean of 1060 and a standarddeviation of 195, determine the percentage of students with test scoresbetween 1100 and 1200, after rounding to the nearest whole number:
>>>sat=NormalDist(1060,195)>>>fraction=sat.cdf(1200+0.5)-sat.cdf(1100-0.5)>>>round(fraction*100.0,1)18.4
Find thequartiles anddeciles for the SAT scores:
>>>list(map(round,sat.quantiles()))[928, 1060, 1192]>>>list(map(round,sat.quantiles(n=10)))[810, 896, 958, 1011, 1060, 1109, 1162, 1224, 1310]
Monte Carlo inputs for simulations¶
To estimate the distribution for a model that isn’t easy to solveanalytically,NormalDist
can generate input samples for aMonteCarlo simulation:
>>>defmodel(x,y,z):...return(3*x+7*x*y-5*y)/(11*z)...>>>n=100_000>>>X=NormalDist(10,2.5).samples(n,seed=3652260728)>>>Y=NormalDist(15,1.75).samples(n,seed=4582495471)>>>Z=NormalDist(50,1.25).samples(n,seed=6582483453)>>>quantiles(map(model,X,Y,Z))[1.4591308524824727, 1.8035946855390597, 2.175091447274739]
Approximating binomial distributions¶
Normal distributions can be used to approximateBinomialdistributionswhen the sample size is large and when the probability of a successfultrial is near 50%.
For example, an open source conference has 750 attendees and two rooms with a500 person capacity. There is a talk about Python and another about Ruby.In previous conferences, 65% of the attendees preferred to listen to Pythontalks. Assuming the population preferences haven’t changed, what is theprobability that the Python room will stay within its capacity limits?
>>>n=750# Sample size>>>p=0.65# Preference for Python>>>q=1.0-p# Preference for Ruby>>>k=500# Room capacity>>># Approximation using the cumulative normal distribution>>>frommathimportsqrt>>>round(NormalDist(mu=n*p,sigma=sqrt(n*p*q)).cdf(k+0.5),4)0.8402>>># Exact solution using the cumulative binomial distribution>>>frommathimportcomb,fsum>>>round(fsum(comb(n,r)*p**r*q**(n-r)forrinrange(k+1)),4)0.8402>>># Approximation using a simulation>>>fromrandomimportseed,binomialvariate>>>seed(8675309)>>>mean(binomialvariate(n,p)<=kforiinrange(10_000))0.8406
Naive bayesian classifier¶
Normal distributions commonly arise in machine learning problems.
Wikipedia has anice example of a Naive Bayesian Classifier.The challenge is to predict a person’s gender from measurements of normallydistributed features including height, weight, and foot size.
We’re given a training dataset with measurements for eight people. Themeasurements are assumed to be normally distributed, so we summarize the datawithNormalDist
:
>>>height_male=NormalDist.from_samples([6,5.92,5.58,5.92])>>>height_female=NormalDist.from_samples([5,5.5,5.42,5.75])>>>weight_male=NormalDist.from_samples([180,190,170,165])>>>weight_female=NormalDist.from_samples([100,150,130,150])>>>foot_size_male=NormalDist.from_samples([12,11,12,10])>>>foot_size_female=NormalDist.from_samples([6,8,7,9])
Next, we encounter a new person whose feature measurements are known but whosegender is unknown:
>>>ht=6.0# height>>>wt=130# weight>>>fs=8# foot size
Starting with a 50%prior probability of being male or female,we compute the posterior as the prior times the product of likelihoods for thefeature measurements given the gender:
>>>prior_male=0.5>>>prior_female=0.5>>>posterior_male=(prior_male*height_male.pdf(ht)*...weight_male.pdf(wt)*foot_size_male.pdf(fs))>>>posterior_female=(prior_female*height_female.pdf(ht)*...weight_female.pdf(wt)*foot_size_female.pdf(fs))
The final prediction goes to the largest posterior. This is known as themaximum a posteriori or MAP:
>>>'male'ifposterior_male>posterior_femaleelse'female''female'
Kernel density estimation¶
It is possible to estimate a continuous probability distributionfrom a fixed number of discrete samples.
The basic idea is to smooth the data usinga kernel function such as anormal distribution, triangular distribution, or uniform distribution.The degree of smoothing is controlled by a scaling parameter,h
,which is called thebandwidth.
fromrandomimportchoice,randomdefkde_normal(data,h):"Create a continuous probability distribution from discrete samples."# Smooth the data with a normal distribution kernel scaled by h.K_h=NormalDist(0.0,h)defpdf(x):'Probability density function. P(x <= X < x+dx) / dx'returnsum(K_h.pdf(x-x_i)forx_iindata)/len(data)defcdf(x):'Cumulative distribution function. P(X <= x)'returnsum(K_h.cdf(x-x_i)forx_iindata)/len(data)defrand():'Random selection from the probability distribution.'returnchoice(data)+K_h.inv_cdf(random())returnpdf,cdf,rand
Wikipedia has an examplewhere we can use thekde_normal()
recipe to generate and plota probability density function estimated from a small sample:
>>>sample=[-2.1,-1.3,-0.4,1.9,5.1,6.2]>>>pdf,cdf,rand=kde_normal(sample,h=1.5)>>>xarr=[i/100foriinrange(-750,1100)]>>>yarr=[pdf(x)forxinxarr]
The points inxarr
andyarr
can be used to make a PDF plot:

Resamplethe data to produce 100 new selections:
>>>new_selections=[rand()foriinrange(100)]
Determine the probability of a new selection being below2.0
:
>>>round(cdf(2.0),4)0.5794
Add a new sample data point and find the new CDF at2.0
:
>>>sample.append(4.9)>>>round(cdf(2.0),4)0.5005