[New-bugs-announce] [issue36018] Add a Normal Distribution class to the statistics module

Raymond Hettinger report at bugs.python.org
Sun Feb 17 19:00:59 EST 2019


New submission from Raymond Hettinger <raymond.hettinger at gmail.com>:

Attached is a class that I've found useful for doing practical statistics work with normal distributions.  It provides a nice, high-level API that makes short-work of everyday statistical problems.

------ Examples --------

# Simple scaling and translation
temperature_february = NormalDist(5, 2.5)            # Celsius
print(temperature_february * (9/5) + 32)             # Fahrenheit


# Classic probability problems
# https://blog.prepscholar.com/sat-standard-deviation
# The mean score on a SAT exam is 1060 with a standard deviation of 195
# What percentage of students score between 1100 and 1200?
sat = NormalDist(1060, 195)
fraction = sat.cdf(1200) - sat.cdf(1100)
print(f'{fraction * 100 :.1f}% score between 1100 and 1200')


# Combination of normal distributions by summing variances
birth_weights = NormalDist.from_samples([2.5, 3.1, 2.1, 2.4, 2.7, 3.5])
drug_effects = NormalDist(0.4, 0.15)
print(birth_weights + drug_effects)


# Statistical calculation estimates using simulations
# Estimate the distribution of X * Y / Z
n = 100_000
X = NormalDist(350, 15).examples(n)
Y = NormalDist(47, 17).examples(n)
Z = NormalDist(62, 6).examples(n)
print(NormalDist.from_samples(x * y / z for x, y, z in zip(X, Y, Z)))


# Naive Bayesian Classifier
# https://en.wikipedia.org/wiki/Naive_Bayes_classifier#Sex_classification

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])

prior_male = 0.5
prior_female = 0.5
posterior_male = prior_male * height_male.pdf(6) * weight_male.pdf(130) * foot_size_male.pdf(8)
posterior_female = prior_female * height_female.pdf(6) * weight_female.pdf(130) * foot_size_female.pdf(8)
print('Predict', 'male' if posterior_male > posterior_female else 'female')

----------
assignee: steven.daprano
components: Library (Lib)
files: gauss.py
messages: 335792
nosy: rhettinger, steven.daprano
priority: normal
severity: normal
status: open
title: Add a Normal Distribution class to the statistics module
type: enhancement
versions: Python 3.8
Added file: https://bugs.python.org/file48147/gauss.py

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36018>
_______________________________________


More information about the New-bugs-announce mailing list