8000 Merge pull request #16 from bashtage/sync-numpy · mattip/numpy@f3ef206 · GitHub
[go: up one dir, main page]

Skip to content

Commit f3ef206

Browse files
authored
Merge pull request #16 from bashtage/sync-numpy
SYNC/CLN: Sync with upstream changes
2 parents 7c53827 + 657aab0 commit f3ef206

File tree

10 files changed

+114
-113
lines changed

10 files changed

+114
-113
lines changed

_randomgen/randomgen/examples/cython/setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
include_dirs=[np.get_include()])
1111
distributions = Extension("extending_distributions",
1212
sources=['extending_distributions.pyx',
13-
join('..', '..', 'randomgen', 'src', 'distributions', 'distributions.c')],
13+
join('..', '..', 'randomgen', 'src',
14+
'distributions', 'distributions.c')],
1415
include_dirs=[np.get_include()])
1516

1617
extensions = [extending, distributions]

_randomgen/randomgen/examples/numba/extending.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import datetime as dt
2+
13
import numpy as np
24
import numba as nb
35

@@ -31,7 +33,7 @@ def bounded_uint(lb, ub, state):
3133
def bounded_uints(lb, ub, n, state):
3234
out = np.empty(n, dtype=np.uint32)
3335
for i in range(n):
34-
bounded_uint(lb, ub, state)
36+
out[i] = bounded_uint(lb, ub, state)
3537

3638

3739
bounded_uints(323, 2394691, 10000000, s.value)
@@ -57,20 +59,18 @@ def normals(n, state):
5759
out[2 * i + 1] = f * x2
5860
return out
5961

62+
6063
print(normals(10, cffi_state).var())
6164
# Warm up
6265
normalsj = nb.jit(normals, nopython=True)
6366
normalsj(1, state_addr)
64-
import datetime as dt
6567

6668
start = dt.datetime.now()
6769
normalsj(1000000, state_addr)
6870
ms = 1000 * (dt.datetime.now() - start).total_seconds()
6971
print('1,000,000 Box-Muller (numba/Xoroshiro128) randoms in '
7072
'{ms:0.1f}ms'.format(ms=ms))
7173

72-
import numpy as np
73-
7474
start = dt.datetime.now()
7575
np.random.standard_normal(1000000)
7676
ms = 1000 * (dt.datetime.now() - start).total_seconds()

_randomgen/randomgen/examples/numba/extending_distributions.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
r"""
22
On *nix, execute in randomgen/src/distributions
33
4-
export PYTHON_INCLUDE=#path to Python's include folder, usually ${PYTHON_HOME}/include/python${PYTHON_VERSION}m
5-
export NUMPY_INCLUDE=#path to numpy's include folder, usually ${PYTHON_HOME}/lib/python${PYTHON_VERSION}/site-packages/numpy/core/include
6-
gcc -shared -o libdistributions.so -fPIC distributions.c -I${NUMPY_INCLUDE} -I${PYTHON_INCLUDE}
4+
export PYTHON_INCLUDE=#path to Python's include folder, usually \
5+
${PYTHON_HOME}/include/python${PYTHON_VERSION}m
6+
export NUMPY_INCLUDE=#path to numpy's include folder, usually \
7+
${PYTHON_HOME}/lib/python${PYTHON_VERSION}/site-packages/numpy/core/include
8+
gcc -shared -o libdistributions.so -fPIC distributions.c -I${NUMPY_INCLUDE} \
9+
-I${PYTHON_INCLUDE}
710
mv libdistributions.so ../../examples/numba/
811
912
On Windows
1013
1114
rem PYTHON_HOME is setup dependent, this is an example
1215
set PYTHON_HOME=c:\Anaconda
13-
cl.exe /LD .\distributions.c -DDLL_EXPORT -I%PYTHON_HOME%\lib\site-packages\numpy\core\include -I%PYTHON_HOME%\include %PYTHON_HOME%\libs\python36.lib
16+
cl.exe /LD .\distributions.c -DDLL_EXPORT \
17+
-I%PYTHON_HOME%\lib\site-packages\numpy\core\include \
18+
-I%PYTHON_HOME%\include %PYTHON_HOME%\libs\python36.lib
1419
move distributions.dll ../../examples/numba/
1520
"""
1621
import os

_randomgen/randomgen/generator.pyx

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ cdef class RandomGenerator:
552552
array([[[ True, True],
553553
[ True, True]],
554554
[[ True, True],
555-
[ True, True]]], dtype=bool)
555+
[ True, True]]])
556556
557557
"""
558558
cdef np.npy_intp n
@@ -959,7 +959,7 @@ cdef class RandomGenerator:
959959
probability density function:
960960
961961
>>> import matplotlib.pyplot as plt
962-
>>> count, bins, ignored = plt.hist(s, 15, normed=True)
962+
>>> count, bins, ignored = plt.hist(s, 15, density=True)
963963
>>> plt.plot(bins, np.ones_like(bins), linewidth=2, color='r')
964964
>>> plt.show()
965965
"""
@@ -1059,7 +1059,7 @@ cdef class RandomGenerator:
10591059
argument is provided.
10601060
10611061
This is a convenience function. If you want an interface that takes a
1062-
tuple as the first argument, use `numpy.random.standard_normal` instead.
1062+
tuple as the first argument, use `standard_normal` instead.
10631063
10641064
Parameters
10651065
----------
@@ -1080,7 +1080,7 @@ cdef class RandomGenerator:
10801080
10811081
See Also
10821082
--------
1083-
random.standard_normal : Similar, but takes a tuple as its argument.
1083+
standard_normal : Similar, but takes a tuple as its argument.
10841084
10851085
Notes
10861086
-----
@@ -1141,7 +1141,7 @@ cdef class RandomGenerator:
11411141
11421142
See Also
11431143
--------
1144-
random.randint : Similar to `random_integers`, only for the half-open
1144+
randint : Similar to `random_integers`, only for the half-open
11451145
interval [`low`, `high`), and 0 is the lowest value if `high` is
11461146
omitted.
11471147
@@ -1179,7 +1179,7 @@ cdef class RandomGenerator:
11791179
Display results as a histogram:
11801180
11811181
>>> import matplotlib.pyplot as plt
1182-
>>> count, bins, ignored = plt.hist(dsums, 11, normed=True)
1182+
>>> count, bins, ignored = plt.hist(dsums, 11, density=True)
11831183
>>> plt.show()
11841184
11851185
"""
@@ -1329,7 +1329,7 @@ cdef class RandomGenerator:
13291329
the probability density function:
13301330
13311331
>>> import matplotlib.pyplot as plt
1332-
>>> count, bins, ignored = plt.hist(s, 30, normed=True)
1332+
>>> count, bins, ignored = plt.hist(s, 30, density=True)
13331333
>>> plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) *
13341334
... np.exp( - (bins - mu)**2 / (2 * sigma**2) ),
13351335
... linewidth=2, color='r')
@@ -1573,7 +1573,7 @@ cdef class RandomGenerator:
15731573
15741574
>>> import matplotlib.pyplot as plt
15751575
>>> import scipy.special as sps
1576-
>>> count, bins, ignored = plt.hist(s, 50, normed=True)
1576+
>>> count, bins, ignored = plt.hist(s, 50, density=True)
15771577
>>> y = bins**(shape-1) * ((np.exp(-bins/scale))/ \\
15781578
... (sps.gamma(shape) * scale**shape))
15791579
>>> plt.plot(bins, y, linewidth=2, color='r')
@@ -1660,7 +1660,7 @@ cdef class RandomGenerator:
16601660
16611661
>>> import matplotlib.pyplot as plt
16621662
>>> import scipy.special as sps
1663-
>>> count, bins, ignored = plt.hist(s, 50, normed=True)
1663+
>>> count, bins, ignored = plt.hist(s, 50, density=True)
16641664
>>> y = bins**(shape-1)*(np.exp(-bins/scale) /
16651665
... (sps.gamma(shape)*scale**shape))
16661666
>>> plt.plot(bins, y, linewidth=2, color='r')
@@ -1819,9 +1819,9 @@ cdef class RandomGenerator:
18191819
>>> dfden = 20 # within groups degrees of freedom
18201820
>>> nonc = 3.0
18211821
>>> nc_vals = randomgen.noncentral_f(dfnum, dfden, nonc, 1000000)
1822-
>>> NF = np.histogram(nc_vals, bins=50, normed=True)
1822+
>>> NF = np.histogram(nc_vals, bins=50, density=True)
18231823
>>> c_vals = randomgen.f(dfnum, dfden, 1000000)
1824-
>>> F = np.histogram(c_vals, bins=50, normed=True)
1824+
>>> F = np.histogram(c_vals, bins=50, density=True)
18251825
>>> plt.plot(F[1][1:], F[0])
18261826
>>> plt.plot(NF[1][1:], NF[0])
18271827
>>> plt.show()
@@ -1957,17 +1957,17 @@ cdef class RandomGenerator:
19571957
19581958
>>> import matplotlib.pyplot as plt
19591959
>>> values = plt.hist(randomgen.noncentral_chisquare(3, 20, 100000),
1960-
... bins=200, normed=True)
1960+
... bins=200, density=True)
19611961
>>> plt.show()
19621962
19631963
Draw values from a noncentral chisquare with very small noncentrality,
19641964
and compare to a chisquare.
19651965
19661966
>>> plt.figure()
19671967
>>> values = plt.hist(randomgen.noncentral_chisquare(3, .0000001, 100000),
1968-
... bins=np.arange(0., 25, .1), normed=True)
1968+
... bins=np.arange(0., 25, .1), density=True)
19691969
>>> values2 = plt.hist(randomgen.chisquare(3, 100000),
1970-
... bins=np.arange(0., 25, .1), normed=True)
1970+
... bins=np.arange(0., 25, .1), density=True)
19711971
>>> plt.plot(values[1][0:-1], values[0]-values2[0], 'ob')
19721972
>>> plt.show()
19731973
@@ -1976,7 +1976,7 @@ cdef class RandomGenerator:
19761976
19771977
>>> plt.figure()
19781978
>>> values = plt.hist(randomgen.noncentral_chisquare(3, 20, 100000),
1979-
... bins=200, normed=True)
1979+
... bins=200, density=True)
19801980
>>> plt.show()
19811981
19821982
"""
@@ -2125,7 +2125,7 @@ cdef class RandomGenerator:
21252125
21262126
>>> t = (np.mean(intake)-7725)/(intake.std(ddof=1)/np.sqrt(len(intake)))
21272127
>>> import matplotlib.pyplot as plt
2128-
>>> h = plt.hist(s, bins=100, normed=True)
2128+
>>> h = plt.hist(s, bins=100, density=True)
21292129
21302130
For a one-sided t-test, how far out in the distribution does the t
21312131
statistic appear?
@@ -2214,7 +2214,7 @@ cdef class RandomGenerator:
22142214
22152215
>>> import matplotlib.pyplot as plt
22162216
>>> from scipy.special import i0
2217-
>>> plt.hist(s, 50, normed=True)
2217+
>>> plt.hist(s, 50, density=True)
22182218
>>> x = np.linspace(-np.pi, np.pi, num=51)
22192219
>>> y = np.exp(kappa*np.cos(x-mu))/(2*np.pi*i0(kappa))
22202220
>>> plt.plot(x, y, linewidth=2, color='r')
@@ -2313,7 +2313,7 @@ cdef class RandomGenerator:
23132313
density function:
23142314
23152315
>>> import matplotlib.pyplot as plt
2316-
>>> count, bins, _ = plt.hist(s, 100, normed=True)
2316+
>>> count, bins, _ = plt.hist(s, 100, density=True)
23172317
>>> fit = a*m**a / bins**(a+1)
23182318
>>> plt.plot(bins, max(count)*fit/max(fit), linewidth=2, color='r')
23192319
>>> plt.show()
@@ -2502,17 +2502,17 @@ cdef class RandomGenerator:
25022502
>>> powpdf = stats.powerlaw.pdf(xx,5)
25032503
25042504
>>> plt.figure()
2505-
>>> plt.hist(rvs, bins=50, normed=True)
2505+
>>> plt.hist(rvs, bins=50, density=True)
25062506
>>> plt.plot(xx,powpdf,'r-')
25072507
>>> plt.title('randomgen.power(5)')
25082508
25092509
>>> plt.figure()
2510-
>>> plt.hist(1./(1.+rvsp), bins=50, normed=True)
2510+
>>> plt.hist(1./(1.+rvsp), bins=50, density=True)
25112511
>>> plt.plot(xx,powpdf,'r-')
25122512
>>> plt.title('inverse of 1 + randomgen.pareto(5)')
25132513
25142514
>>> plt.figure()
2515-
>>> plt.hist(1./(1.+rvsp), bins=50, normed=True)
2515+
>>> plt.hist(1./(1.+rvsp), bins=50, density=True)
25162516
>>> plt.plot(xx,powpdf,'r-')
25172517
>>> plt.title('inverse of stats.pareto(5)')
25182518
@@ -2589,7 +2589,7 @@ cdef class RandomGenerator:
25892589
the probability density function:
25902590
25912591
>>> import matplotlib.pyplot as plt
2592-
>>> count, bins, ignored = plt.hist(s, 30, normed=True)
2592+
>>> count, bins, ignored = plt.hist(s, 30, density=True)
25932593
>>> x = np.arange(-8., 8., .01)
25942594
>>> pdf = np.exp(-abs(x-loc)/scale)/(2.*scale)
25952595
>>> plt.plot(x, pdf)
@@ -2691,7 +2691,7 @@ cdef class RandomGenerator:
26912691
the probability density function:
26922692
26932693
>>> import matplotlib.pyplot as plt
2694-
>>> count, bins, ignored = plt.hist(s, 30, normed=True)
2694+
>>> count, bins, ignored = plt.hist(s, 30, density=True)
26952695
>>> plt.plot(bins, (1/beta)*np.exp(-(bins - mu)/beta)
26962696
... * np.exp( -np.exp( -(bins - mu) /beta) ),
26972697
... linewidth=2, color='r')
@@ -2706,7 +2706,7 @@ cdef class RandomGenerator:
27062706
... a = randomgen.normal(mu, beta, 1000)
27072707
... means.append(a.mean())
27082708
... maxima.append(a.max())
2709-
>>> count, bins, ignored = plt.hist(maxima, 30, normed=True)
2709+
>>> count, bins, ignored = plt.hist(maxima, 30, density=True)
27102710
>>> beta = np.std(maxima) * np.sqrt(6) / np.pi
27112711
>>> mu = np.mean(maxima) - 0.57721*beta
27122712
>>> plt.plot(bins, (1/beta)*np.exp(-(bins - mu)/beta)
@@ -2873,7 +2873,7 @@ cdef class RandomGenerator:
28732873
the probability density function:
28742874
28752875
>>> import matplotlib.pyplot as plt
2876-
>>> count, bins, ignored = plt.hist(s, 100, normed=True, align='mid')
2876+
>>> count, bins, ignored = plt.hist(s, 100, density=True, align='mid')
28772877
28782878
>>> x = np.linspace(min(bins), max(bins), 10000)
28792879
>>> pdf = (np.exp(-(np.log(x) - mu)**2 / (2 * sigma**2))
@@ -2895,7 +2895,7 @@ cdef class RandomGenerator:
28952895
... b.append(np.product(a))
28962896
28972897
>>> b = np.array(b) / np.min(b) # scale values to be positive
2898-
>>> count, bins, ignored = plt.hist(b, 100, normed=True, align='mid')
2898+
>>> count, bins, ignored = plt.hist(b, 100, density=True, align='mid')
28992899
>>> sigma = np.std(np.log(b))
29002900
>>> mu = np.mean(np.log(b))
29012901
@@ -2958,7 +2958,7 @@ cdef class RandomGenerator:
29582958
--------
29592959
Draw values from the distribution and plot the histogram
29602960
2961-
>>> values = hist(randomgen.rayleigh(3, 100000), bins=200, normed=True)
2961+
>>> values = hist(randomgen.rayleigh(3, 100000), bins=200, density=True)
29622962
29632963
Wave heights tend to follow a Rayleigh distribution. If the mean wave
29642964
height is 1 meter, what fraction of waves are likely to be larger than 3
@@ -3038,7 +3038,7 @@ cdef class RandomGenerator:
30383038
Draw values from the distribution and plot the histogram:
30393039
30403040
>>> import matplotlib.pyplot as plt
3041-
>>> h = plt.hist(randomgen.wald(3, 2, 100000), bins=200, normed=True)
3041+
>>> h = plt.hist(randomgen.wald(3, 2, 100000), bins=200, density=True)
30423042
>>> plt.show()
30433043
30443044
"""
@@ -3106,7 +3106,7 @@ cdef class RandomGenerator:
31063106
31073107
>>> import matplotlib.pyplot as plt
31083108
>>> h = plt.hist(randomgen.triangular(-3, 0, 8, 100000), bins=200,
3109-
... normed=True)
3109+
... density=True)
31103110
>>> plt.show()
31113111
31123112
"""
@@ -3297,7 +3297,7 @@ cdef class RandomGenerator:
32973297
Draw samples from a negative binomial distribution.
32983298
32993299
Samples are drawn from a negative binomial distribution with specified
3300-
parameters, `n` trials and `p` probability of success where `n` is an
3300+
parameters, `n` trials and `p` probability of failure where `n` is an
33013301
integer > 0 and `p` is in the interval [0, 1].
33023302
33033303
Parameters
@@ -3317,20 +3317,19 @@ cdef class RandomGenerator:
33173317
-------
33183318
out : ndarray or scalar
33193319
Drawn samples from the parameterized negative binomial distribution,
3320-
where each sample is equal to N, the number of trials it took to
3321-
achieve n - 1 successes, N - (n - 1) failures, and a success on the,
3322-
(N + n)th trial.
3320+
where each sample is equal to N, the number of successes that
3321+
occurred before n failures, and a failure on the (N + n)th trial.
33233322
33243323
Notes
33253324
-----
33263325
The probability density for the negative binomial distribution is
33273326
3328-
.. math:: P(N;n,p) = \\binom{N+n-1}{n-1}p^{n}(1-p)^{N},
3327+
.. math:: P(N;n,p) = \\binom{N+n-1}{N}p^{n}(1-p)^{N},
33293328
3330-
where :math:`n-1` is the number of successes, :math:`p` is the
3331-
probability of success, and :math:`N+n-1` is the number of trials.
3332-
The negative binomial distribution gives the probability of n-1
3333-
successes and N failures in N+n-1 trials, and success on the (N+n)th
3329+
where :math:`n` is the number of successes, :math:`p` is the
3330+
probability of failure, and :math:`N+n` is the number of trials.
3331+
The negative binomial distribution gives the probability of n
3332+
successes and N failures in N+n trials, and a success on the (N+n)th
33343333
trial.
33353334
33363335
If one throws a die repeatedly until the third time a "1" appears,
@@ -3355,7 +3354,7 @@ cdef class RandomGenerator:
33553354
for each successive well, that is what is the probability of a
33563355
single success after drilling 5 wells, after 6 wells, etc.?
33573356
3358-
>>> s = randomgen.negative_binomial(1, 0.1, 100000)
3357+
>>> s = np.random.negative_binomial(1, 0.9, 100000)
33593358
>>> for i in range(1, 11):
33603359
... probability = sum(s<i) / 100000.
33613360
... print i, "wells drilled, probability of one success =", probability
@@ -3424,7 +3423,7 @@ cdef class RandomGenerator:
34243423
Display histogram of the sample:
34253424
34263425
>>> import matplotlib.pyplot as plt
3427-
>>> count, bins, ignored = plt.hist(s, 14, normed=True)
3426+
>>> count, bins, ignored = plt.hist(s, 14, density=True)
34283427
>>> plt.show()
34293428
34303429
Draw each 100 values for lambda 100 and 500:
@@ -3504,7 +3503,7 @@ cdef class RandomGenerator:
35043503
35053504
Truncate s values at 50 so plot is interesting:
35063505
3507-
>>> count, bins, ignored = plt.hist(s[s<50], 50, normed=True)
3506+
>>> count, bins, ignored = plt.hist(s[s<50], 50, density=True)
35083507
>>> x = np.arange(1., 50.)
35093508
>>> y = x**(-a) / special.zetac(a)
35103509
>>> plt.plot(x, y/max(y), linewidth=2, color='r')
@@ -3906,7 +3905,7 @@ cdef class RandomGenerator:
39063905
#
39073906
# Also check that cov is positive-semidefinite. If so, the u.T and v
39083907
# matrices should be equal up to roundoff error if cov is
3909-
# symmetrical and the singular value of the corresponding row is
3908+
# symmetric and the singular value of the corresponding row is
39103909
# not zero. We continue to use the SVD rather than Cholesky in
39113910
# order to preserve current outputs. Note that symmetry has not
39123911
# been checked.

0 commit comments

Comments
 (0)
0