<p dir="auto">The issue is a duplicate of <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="68024870" data-permission-text="Title is private" data-url="https://github.com/scipy/scipy/issues/4720" data-hovercard-type="issue" data-hovercard-url="/scipy/scipy/issues/4720/hovercard" href="https://github.com/scipy/scipy/issues/4720">scipy/scipy#4720</a></p>
<p dir="auto">The <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.noncentral_chisquare.html#numpy.random.noncentral_chisquare" rel="nofollow">document</a>  suggests that the parameter <em>degree of freedom</em> should be <code class="notranslate">df &gt;= 1</code> to use <code class="notranslate">numpy.random.noncentral_chisquare</code>. However, taking <code class="notranslate">df = 1</code> raises error.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
df = 1
nc = 2
np.random.noncentral_chisquare(df, nc)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
&lt;ipython-input-2-7c9f3f6a3a16&gt; in &lt;module&gt;()
----&gt; 1 np.random.noncentral_chisquare(df, nc)

mtrand.pyx in mtrand.RandomState.noncentral_chisquare (numpy/random/mtrand/mtrand.c:15618)()

ValueError: df &lt;= 0"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-s1">df</span> <span class="pl-c1">=</span> <span class="pl-c1">1</span>
<span class="pl-s1">nc</span> <span class="pl-c1">=</span> <span class="pl-c1">2</span>
<span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">noncentral_chisquare</span>(<span class="pl-s1">df</span>, <span class="pl-s1">nc</span>)

<span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span>
<span class="pl-v">ValueError</span>                                <span class="pl-v">Traceback</span> (<span class="pl-s1">most</span> <span class="pl-s1">recent</span> <span class="pl-s1">call</span> <span class="pl-s1">last</span>)
<span class="pl-c1">&lt;</span><span class="pl-s1">ipython</span><span class="pl-c1">-</span><span class="pl-s1">input</span><span class="pl-c1">-</span><span class="pl-c1">2</span><span class="pl-c1">-</span><span class="pl-c1">7</span><span class="pl-s1">c9f3f6a3a16</span><span class="pl-c1">&gt;</span> <span class="pl-s1">in</span> <span class="pl-c1">&lt;</span><span class="pl-s1">module</span><span class="pl-c1">&gt;</span>()
<span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">&gt;</span> <span class="pl-c1">1</span> <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">noncentral_chisquare</span>(<span class="pl-s1">df</span>, <span class="pl-s1">nc</span>)

<span class="pl-s1">mtrand</span>.<span class="pl-s1">pyx</span> <span class="pl-c1">in</span> <span class="pl-s1">mtrand</span>.<span class="pl-v">RandomState</span>.<span class="pl-en">noncentral_chisquare</span> (<span class="pl-s1">numpy</span><span class="pl-c1">/</span><span class="pl-s1">random</span><span class="pl-c1">/</span><span class="pl-s1">mtrand</span><span class="pl-c1">/</span><span class="pl-s1">mtrand</span>.<span class="pl-s1">c</span>:<span class="pl-c1">15618</span>)()

<span class="pl-v">ValueError</span>: <span class="pl-s1">df</span> <span class="pl-c1">&lt;=</span> <span class="pl-c1">0</span></pre></div>
<p dir="auto">It makes me suspect that the random variate generation of the noncentral chi-square distribution with degree of freedom <code class="notranslate">df</code> and noncentrality parameter <code class="notranslate">nc</code> is implemented in numpy using a method similar to the following.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="np.random.chisquare(df-1) + (np.random.standard_normal()+np.sqrt(nc))**2"><pre class="notranslate"><span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">chisquare</span>(<span class="pl-s1">df</span><span class="pl-c1">-</span><span class="pl-c1">1</span>) <span class="pl-c1">+</span> (<span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">standard_normal</span>()<span class="pl-c1">+</span><span class="pl-s1">np</span>.<span class="pl-en">sqrt</span>(<span class="pl-s1">nc</span>))<span class="pl-c1">**</span><span class="pl-c1">2</span></pre></div>
<p dir="auto">However, this method is valid only for the case where <code class="notranslate">df &gt; 1</code> because with <code class="notranslate">df = 1</code>, it needs to generate a chi-square random variate with degree of freedom 0, which is not mathematically well-defined.</p>
<p dir="auto">On the other hand, noncentral chi-square distribution is indeed mathematically well-defined for all <code class="notranslate">df &gt; 0</code>. It simply requires a different algorithm for random variate generation if <code class="notranslate">0 &lt; df &lt;= 1</code>, such as the following one:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="np.random.chisquare(df + 2*np.random.poisson(nc / 2.0))"><pre class="notranslate"><span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">chisquare</span>(<span class="pl-s1">df</span> <span class="pl-c1">+</span> <span class="pl-c1">2</span><span class="pl-c1">*</span><span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">poisson</span>(<span class="pl-s1">nc</span> <span class="pl-c1">/</span> <span class="pl-c1">2.0</span>))</pre></div>
<p dir="auto">Numpy's bool_ type seems to fail an isinstance check with the Python numbers.Integral ABC:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy
import numbers
x = numpy.array([True])
print( isinstance(x[0],numbers.Integral) ) # -&gt; False"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span>
<span class="pl-k">import</span> <span class="pl-s1">numbers</span>
<span class="pl-s1">x</span> <span class="pl-c1">=</span> <span class="pl-s1">numpy</span>.<span class="pl-en">array</span>([<span class="pl-c1">True</span>])
<span class="pl-en">print</span>( <span class="pl-en">isinstance</span>(<span class="pl-s1">x</span>[<span class="pl-c1">0</span>],<span class="pl-s1">numbers</span>.<span class="pl-v">Integral</span>) ) <span class="pl-c"># -&gt; False</span></pre></div>
<p dir="auto">I find this odd since Python's built-in bool passes:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="print( isinstance(bool(x[0]),numbers.Integral) ) # -&gt;True"><pre class="notranslate"><span class="pl-en">print</span>( <span class="pl-en">isinstance</span>(<span class="pl-en">bool</span>(<span class="pl-s1">x</span>[<span class="pl-c1">0</span>]),<span class="pl-s1">numbers</span>.<span class="pl-v">Integral</span>) ) <span class="pl-c"># -&gt;True</span></pre></div>
<p dir="auto">Is this a bug?</p>
<p dir="auto">My system information is:</p>
<p dir="auto">PYTHON:<br>
Python 2.7.5 (default, May 19 2013, 02:56:42)<br>
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin</p>
<p dir="auto">NUMPY INSTALLED WITH PIP:<br>
Downloading/unpacking numpy from <a href="https://pypi.python.org/packages/source/n/numpy/numpy-1.7.1.tar.gz#md5=0ab72b3b83528a7ae79c6df9042d61c6" rel="nofollow">https://pypi.python.org/packages/source/n/numpy/numpy-1.7.1.tar.gz#md5=0ab72b3b83528a7ae79c6df9042d61c6</a><br>
Downloading numpy-1.7.1.tar.gz (2.8MB): 2.8MB downloaded<br>
Running setup.py egg_info for package numpy<br>
Running from numpy source directory.<br>
non-existing path in 'numpy/distutils': 'site.cfg'<br>
F2PY Version 2<br>
blas_opt_info:<br>
FOUND:<br>
extra_link_args = ['-Wl,-framework', '-Wl,Accelerate']<br>
define_macros = [('NO_ATLAS_INFO', 3)]<br>
extra_compile_args = ['-msse3', '-I/System/Library/Frameworks/vecLib.framework/Headers']<br>
lapack_opt_info:<br>
FOUND:<br>
extra_link_args = ['-Wl,-framework', '-Wl,Accelerate']<br>
define_macros = [('NO_ATLAS_INFO', 3)]<br>
extra_compile_args = ['-msse3']</p>