Skip to content

Commit

Permalink
Merge pull request numpy#6613 from ev-br/noncentral_chisq
Browse files Browse the repository at this point in the history
MAINT: random: allow nonc==0 in noncentral_chisquare.
  • Loading branch information
charris committed Nov 2, 2015
2 parents 80abf6e + 04e40fc commit f2f61b8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
3 changes: 3 additions & 0 deletions numpy/random/mtrand/distributions.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ double rk_chisquare(rk_state *state, double df)

double rk_noncentral_chisquare(rk_state *state, double df, double nonc)
{
if (nonc == 0){
return rk_chisquare(state, df);
}
if(1 < df)
{
const double Chi2 = rk_chisquare(state, df - 1);
Expand Down
8 changes: 4 additions & 4 deletions numpy/random/mtrand/mtrand.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2219,7 +2219,7 @@ cdef class RandomState:
Degrees of freedom, should be > 0 as of Numpy 1.10,
should be > 1 for earlier versions.
nonc : float
Non-centrality, should be > 0.
Non-centrality, should be non-negative.
size : int or tuple of ints, optional
Output shape. If the given shape is, e.g., ``(m, n, k)``, then
``m * n * k`` samples are drawn. Default is None, in which case a
Expand Down Expand Up @@ -2285,8 +2285,8 @@ cdef class RandomState:
if not PyErr_Occurred():
if fdf <= 0:
raise ValueError("df <= 0")
if fnonc <= 0:
raise ValueError("nonc <= 0")
if fnonc < 0:
raise ValueError("nonc < 0")
return cont2_array_sc(self.internal_state, rk_noncentral_chisquare,
size, fdf, fnonc, self.lock)

Expand All @@ -2296,7 +2296,7 @@ cdef class RandomState:
ononc = <ndarray>PyArray_FROM_OTF(nonc, NPY_DOUBLE, NPY_ARRAY_ALIGNED)
if np.any(np.less_equal(odf, 0.0)):
raise ValueError("df <= 0")
if np.any(np.less_equal(ononc, 0.0)):
if np.any(np.less(ononc, 0.0)):
raise ValueError("nonc < 0")
return cont2_array(self.internal_state, rk_noncentral_chisquare, size,
odf, ononc, self.lock)
Expand Down
7 changes: 7 additions & 0 deletions numpy/random/tests/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,13 @@ def test_noncentral_chisquare(self):
[ 0.332334982684171 , 0.15451287602753125]])
np.testing.assert_array_almost_equal(actual, desired, decimal=14)

np.random.seed(self.seed)
actual = np.random.noncentral_chisquare(df=5, nonc=0, size=(3, 2))
desired = np.array([[9.597154162763948, 11.725484450296079],
[10.413711048138335, 3.694475922923986],
[13.484222138963087, 14.377255424602957]])
np.testing.assert_array_almost_equal(actual, desired, decimal=14)

def test_noncentral_f(self):
np.random.seed(self.seed)
actual = np.random.noncentral_f(dfnum=5, dfden=2, nonc=1,
Expand Down

0 comments on commit f2f61b8

Please sign in to comment.