@@ -253,6 +253,8 @@ Functions for sequences
253
253
order so that the sample is reproducible.
254
254
255
255
256
+ .. _real-valued-distributions :
257
+
256
258
Real-valued distributions
257
259
-------------------------
258
260
@@ -516,26 +518,42 @@ Simulation of arrival times and service deliveries for a multiserver queue::
516
518
print(f'Mean wait: {mean(waits):.1f} Max wait: {max(waits):.1f}')
517
519
print('Quartiles:', [round(q, 1) for q in quantiles(waits)])
518
520
521
+ .. seealso ::
522
+
523
+ `Statistics for Hackers <https://www.youtube.com/watch?v=Iq9DzN6mvYA >`_
524
+ a video tutorial by
525
+ `Jake Vanderplas <https://us.pycon.org/2016/speaker/profile/295/ >`_
526
+ on statistical analysis using just a few fundamental concepts
527
+ including simulation, sampling, shuffling, and cross-validation.
528
+
529
+ `Economics Simulation
530
+ <http://nbviewer.jupyter.org/url/norvig.com/ipython/Economics.ipynb> `_
531
+ a simulation of a marketplace by
532
+ `Peter Norvig <http://norvig.com/bio.html >`_ that shows effective
533
+ use of many of the tools and distributions provided by this module
534
+ (gauss, uniform, sample, betavariate, choice, triangular, and randrange).
535
+
536
+ `A Concrete Introduction to Probability (using Python)
537
+ <http://nbviewer.jupyter.org/url/norvig.com/ipython/Probability.ipynb> `_
538
+ a tutorial by `Peter Norvig <http://norvig.com/bio.html >`_ covering
539
+ the basics of probability theory, how to write simulations, and
540
+ how to perform data analysis using Python.
541
+
542
+
519
543
Recipes
520
544
-------
521
545
522
546
The default :func: `.random ` returns multiples of 2⁻⁵³ in the range
523
- *0.0 ≤ x < 1.0 *. All such numbers are evenly spaced and exactly
547
+ *0.0 ≤ x < 1.0 *. All such numbers are evenly spaced and are exactly
524
548
representable as Python floats. However, many floats in that interval
525
549
are not possible selections. For example, ``0.05954861408025609 ``
526
550
isn't an integer multiple of 2⁻⁵³.
527
551
528
552
The following recipe takes a different approach. All floats in the
529
- interval are possible selections. Conceptually it works by choosing
530
- from evenly spaced multiples of 2⁻¹⁰⁷⁴ and then rounding down to the
531
- nearest representable float.
532
-
533
- For efficiency, the actual mechanics involve calling
534
- :func: `~math.ldexp ` to construct a representable float. The mantissa
535
- comes from a uniform distribution of integers in the range *2⁵² ≤
536
- mantissa < 2⁵³ *. The exponent comes from a geometric distribution
537
- where exponents smaller than *-53 * occur half as often as the next
538
- larger exponent.
553
+ interval are possible selections. The mantissa comes from a uniform
554
+ distribution of integers in the range *2⁵² ≤ mantissa < 2⁵³ *. The
555
+ exponent comes from a geometric distribution where exponents smaller
556
+ than *-53 * occur half as often as the next larger exponent.
539
557
540
558
::
541
559
@@ -553,35 +571,23 @@ larger exponent.
553
571
exponent += x.bit_length() - 32
554
572
return ldexp(mantissa, exponent)
555
573
556
- All of the real valued distributions will use the new method::
574
+ All :ref: `real valued distributions <real-valued-distributions >`
575
+ in the class will use the new method::
557
576
558
577
>>> fr = FullRandom()
559
578
>>> fr.random()
560
579
0.05954861408025609
561
580
>>> fr.expovariate(0.25)
562
581
8.87925541791544
563
582
583
+ The recipe is conceptually equivalent to an algorithm that chooses from
584
+ all the multiples of 2⁻¹⁰⁷⁴ in the range *0.0 ≤ x < 1.0 *. All such
585
+ numbers are evenly spaced, but most have to be rounded down to the
586
+ nearest representable Python float. (The value 2⁻¹⁰⁷⁴ is the smallest
587
+ positive unnormalized float and is equal to ``math.ulp(0.0) ``.)
564
588
565
- .. seealso ::
566
-
567
- `Statistics for Hackers <https://www.youtube.com/watch?v=Iq9DzN6mvYA >`_
568
- a video tutorial by
569
- `Jake Vanderplas <https://us.pycon.org/2016/speaker/profile/295/ >`_
570
- on statistical analysis using just a few fundamental concepts
571
- including simulation, sampling, shuffling, and cross-validation.
572
-
573
- `Economics Simulation
574
- <http://nbviewer.jupyter.org/url/norvig.com/ipython/Economics.ipynb> `_
575
- a simulation of a marketplace by
576
- `Peter Norvig <http://norvig.com/bio.html >`_ that shows effective
577
- use of many of the tools and distributions provided by this module
578
- (gauss, uniform, sample, betavariate, choice, triangular, and randrange).
579
589
580
- `A Concrete Introduction to Probability (using Python)
581
- <http://nbviewer.jupyter.org/url/norvig.com/ipython/Probability.ipynb> `_
582
- a tutorial by `Peter Norvig <http://norvig.com/bio.html >`_ covering
583
- the basics of probability theory, how to write simulations, and
584
- how to perform data analysis using Python.
590
+ .. seealso ::
585
591
586
592
`Generating Pseudo-random Floating-Point Values
587
593
<https://allendowney.com/research/rand/downey07randfloat.pdf> `_ a
0 commit comments