Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cc353a0

Browse files
rhettingermiss-islington
authored andcommitted
Various refinements to the NormalDist examples and recipes (GH-12272)
1 parent 491ef53 commit cc353a0

1 file changed

Lines changed: 26 additions & 23 deletions

File tree

Doc/library/statistics.rst

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -510,10 +510,9 @@ of applications in statistics.
510510

511511
.. classmethod:: NormalDist.from_samples(data)
512512

513-
Class method that makes a normal distribution instance
514-
from sample data. The *data* can be any :term:`iterable`
515-
and should consist of values that can be converted to type
516-
:class:`float`.
513+
Makes a normal distribution instance computed from sample data. The
514+
*data* can be any :term:`iterable` and should consist of values that
515+
can be converted to type :class:`float`.
517516

518517
If *data* does not contain at least two elements, raises
519518
:exc:`StatisticsError` because it takes at least one point to estimate
@@ -536,11 +535,10 @@ of applications in statistics.
536535
the given value *x*. Mathematically, it is the ratio ``P(x <= X <
537536
x+dx) / dx``.
538537

539-
Note the relative likelihood of *x* can be greater than `1.0`. The
540-
probability for a specific point on a continuous distribution is `0.0`,
541-
so the :func:`pdf` is used instead. It gives the probability of a
542-
sample occurring in a narrow range around *x* and then dividing that
543-
probability by the width of the range (hence the word "density").
538+
The relative likelihood is computed as the probability of a sample
539+
occurring in a narrow range divided by the width of the range (hence
540+
the word "density"). Since the likelihood is relative to other points,
541+
its value can be greater than `1.0`.
544542

545543
.. method:: NormalDist.cdf(x)
546544

@@ -568,7 +566,8 @@ of applications in statistics.
568566
>>> temperature_february * (9/5) + 32 # Fahrenheit
569567
NormalDist(mu=41.0, sigma=4.5)
570568

571-
Dividing a constant by an instance of :class:`NormalDist` is not supported.
569+
Dividing a constant by an instance of :class:`NormalDist` is not supported
570+
because the result wouldn't be normally distributed.
572571

573572
Since normal distributions arise from additive effects of independent
574573
variables, it is possible to `add and subtract two independent normally
@@ -581,8 +580,10 @@ of applications in statistics.
581580
>>> birth_weights = NormalDist.from_samples([2.5, 3.1, 2.1, 2.4, 2.7, 3.5])
582581
>>> drug_effects = NormalDist(0.4, 0.15)
583582
>>> combined = birth_weights + drug_effects
584-
>>> f'mean: {combined.mean :.1f} standard deviation: {combined.stdev :.1f}'
585-
'mean: 3.1 standard deviation: 0.5'
583+
>>> round(combined.mean, 1)
584+
3.1
585+
>>> round(combined.stdev, 1)
586+
0.5
586587

587588
.. versionadded:: 3.8
588589

@@ -595,14 +596,15 @@ of applications in statistics.
595596
For example, given `historical data for SAT exams
596597
<https://blog.prepscholar.com/sat-standard-deviation>`_ showing that scores
597598
are normally distributed with a mean of 1060 and a standard deviation of 192,
598-
determine the percentage of students with scores between 1100 and 1200:
599+
determine the percentage of students with scores between 1100 and 1200, after
600+
rounding to the nearest whole number:
599601

600602
.. doctest::
601603

602604
>>> sat = NormalDist(1060, 195)
603605
>>> fraction = sat.cdf(1200 + 0.5) - sat.cdf(1100 - 0.5)
604-
>>> f'{fraction * 100 :.1f}% score between 1100 and 1200'
605-
'18.4% score between 1100 and 1200'
606+
>>> round(fraction * 100.0, 1)
607+
18.4
606608

607609
What percentage of men and women will have the same height in `two normally
608610
distributed populations with known means and standard deviations
@@ -616,18 +618,19 @@ distributed populations with known means and standard deviations
616618

617619
To estimate the distribution for a model than isn't easy to solve
618620
analytically, :class:`NormalDist` can generate input samples for a `Monte
619-
Carlo simulation <https://en.wikipedia.org/wiki/Monte_Carlo_method>`_ of the
620-
model:
621+
Carlo simulation <https://en.wikipedia.org/wiki/Monte_Carlo_method>`_:
621622

622623
.. doctest::
623624

625+
>>> def model(x, y, z):
626+
... return (3*x + 7*x*y - 5*y) / (11 * z)
627+
...
624628
>>> n = 100_000
625-
>>> X = NormalDist(350, 15).samples(n)
626-
>>> Y = NormalDist(47, 17).samples(n)
627-
>>> Z = NormalDist(62, 6).samples(n)
628-
>>> model_simulation = [x * y / z for x, y, z in zip(X, Y, Z)]
629-
>>> NormalDist.from_samples(model_simulation) # doctest: +SKIP
630-
NormalDist(mu=267.6516398754636, sigma=101.357284306067)
629+
>>> X = NormalDist(10, 2.5).samples(n)
630+
>>> Y = NormalDist(15, 1.75).samples(n)
631+
>>> Z = NormalDist(5, 1.25).samples(n)
632+
>>> NormalDist.from_samples(map(model, X, Y, Z)) # doctest: +SKIP
633+
NormalDist(mu=19.640137307085507, sigma=47.03273142191088)
631634

632635
Normal distributions commonly arise in machine learning problems.
633636

0 commit comments

Comments
 (0)