<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="scipy.stats.kendalltau([], [])"><pre class="notranslate"><code class="notranslate">scipy.stats.kendalltau([], [])
</code></pre></div>
<p dir="auto">fails with an <code class="notranslate">maximum recursion depth exceeded</code> <code class="notranslate">RuntimeError</code>. It should check for invalid input.</p>
<p dir="auto">(via <a href="http://stackoverflow.com/questions/30841262/" rel="nofollow">http://stackoverflow.com/questions/30841262/</a>)</p>
<p dir="auto">I have encountered a RuntimeError when using the kendall tau correlation function on empty arrays. Obviously a correlation of empty data is meaningless, but I have code that uses randomization so therefore I may find in certain instances that "a" and "b" are sometimes empty (see below). I think it would be more elegant if <code class="notranslate">stats.kendalltau</code> would check for empty arrays (so I don't have to).</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="In [14]: import numpy as np
In [15]: from scipy import stats
In [16]: a = np.array([])  # in actual code 'a' is not statically defined like this
In [17]: b = np.array([])  # in actual code 'b' is not statically defined like this
In [18]: stats.kendalltau(a, b)
...
/mnt/hn03_disk001/HN03_HOMES/ctokheim/envs/python2.7.5/lib/python2.7/site-packages/scipy/stats/stats.pyc in mergesort(offs, length)
   2858     def mergesort(offs, length):
   2859         exchcnt = 0
-&gt; 2860         if length == 1:
   2861             return 0
   2862         if length == 2:
RuntimeError: maximum recursion depth exceeded in cmp"><pre class="notranslate"><span class="pl-v">In</span> [<span class="pl-c1">14</span>]: <span class="pl-s1">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-v">In</span> [<span class="pl-c1">15</span>]: <span class="pl-s1">from</span> <span class="pl-s1">scipy</span> <span class="pl-k">import</span> <span class="pl-s1">stats</span>
<span class="pl-v">In</span> [<span class="pl-c1">16</span>]: <span class="pl-s1">a</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">array</span>([])  <span class="pl-c"># in actual code 'a' is not statically defined like this</span>
<span class="pl-v">In</span> [<span class="pl-c1">17</span>]: <span class="pl-s1">b</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">array</span>([])  <span class="pl-c"># in actual code 'b' is not statically defined like this</span>
<span class="pl-v">In</span> [<span class="pl-c1">18</span>]: <span class="pl-s1">stats</span>.<span class="pl-en">kendalltau</span>(<span class="pl-s1">a</span>, <span class="pl-s1">b</span>)
...
<span class="pl-c1">/</span><span class="pl-s1">mnt</span><span class="pl-c1">/</span><span class="pl-s1">hn03_disk001</span><span class="pl-c1">/</span><span class="pl-v">HN03_HOMES</span><span class="pl-c1">/</span><span class="pl-s1">ctokheim</span><span class="pl-c1">/</span><span class="pl-s1">envs</span><span class="pl-c1">/</span><span class="pl-s1">python2</span>.<span class="pl-c1">7.5</span><span class="pl-c1">/</span><span class="pl-s1">lib</span><span class="pl-c1">/</span><span class="pl-s1">python2</span>.<span class="pl-c1">7</span><span class="pl-c1">/</span><span class="pl-s1">site</span><span class="pl-c1">-</span><span class="pl-s1">packages</span><span class="pl-c1">/</span><span class="pl-s1">scipy</span><span class="pl-c1">/</span><span class="pl-s1">stats</span><span class="pl-c1">/</span><span class="pl-s1">stats</span>.<span class="pl-s1">pyc</span> <span class="pl-c1">in</span> <span class="pl-en">mergesort</span>(<span class="pl-s1">offs</span>, <span class="pl-s1">length</span>)
   <span class="pl-c1">2858</span>     <span class="pl-k">def</span> <span class="pl-en">mergesort</span>(<span class="pl-s1">offs</span>, <span class="pl-s1">length</span>):
   <span class="pl-c1">2859</span>         <span class="pl-s1">exchcnt</span> <span class="pl-c1">=</span> <span class="pl-c1">0</span>
<span class="pl-c1">-</span><span class="pl-c1">&gt;</span> <span class="pl-c1">2860</span>         <span class="pl-k">if</span> <span class="pl-s1">length</span> <span class="pl-c1">==</span> <span class="pl-c1">1</span>:
   <span class="pl-c1">2861</span>             <span class="pl-s1">return</span> <span class="pl-c1">0</span>
   <span class="pl-c1">2862</span>         <span class="pl-k">if</span> <span class="pl-s1">length</span> <span class="pl-c1">==</span> <span class="pl-c1">2</span>:
<span class="pl-v">RuntimeError</span>: <span class="pl-s1">maximum</span> <span class="pl-s1">recursion</span> <span class="pl-s1">depth</span> <span class="pl-s1">exceeded</span> <span class="pl-c1">in</span> <span class="pl-s1">cmp</span></pre></div>
<p dir="auto">I noticed when the lengths of the arrays are 1 then stats.kendalltau returns NaN's.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="In [11]: a = np.array([1])
In [12]: b = np.array([1])
In [13]: stats.kendalltau(a, b)
Out[13]: (nan, nan)"><pre class="notranslate"><span class="pl-v">In</span> [<span class="pl-c1">11</span>]: <span class="pl-s1">a</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">array</span>([<span class="pl-c1">1</span>])
<span class="pl-v">In</span> [<span class="pl-c1">12</span>]: <span class="pl-s1">b</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">array</span>([<span class="pl-c1">1</span>])
<span class="pl-v">In</span> [<span class="pl-c1">13</span>]: <span class="pl-s1">stats</span>.<span class="pl-en">kendalltau</span>(<span class="pl-s1">a</span>, <span class="pl-s1">b</span>)
<span class="pl-v">Out</span>[<span class="pl-c1">13</span>]: (<span class="pl-s1">nan</span>, <span class="pl-s1">nan</span>)</pre></div>
<p dir="auto">Perhaps stats.kendalltau could return <code class="notranslate">(nan, nan)</code> in the case of zero-length? I'm using version 0.14.0 but I had the same problem on 0.12.0.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="In [19]: import scipy as sp
In [20]: sp.__version__
Out[20]: '0.14.0'"><pre class="notranslate"><span class="pl-v">In</span> [<span class="pl-c1">19</span>]: <span class="pl-s1">import</span> <span class="pl-s1">scipy</span> <span class="pl-k">as</span> <span class="pl-s1">sp</span>
<span class="pl-v">In</span> [<span class="pl-c1">20</span>]: <span class="pl-s1">sp</span>.<span class="pl-s1">__version__</span>
<span class="pl-v">Out</span>[<span class="pl-c1">20</span>]: <span class="pl-s">'0.14.0'</span></pre></div>