# test case for R<-1 in scipy.stats.linregress and fix in the code #433

Closed
wants to merge 4 commits into
from

## Conversation

Projects
None yet
3 participants
Contributor

### kif commented Feb 12, 2013

 Code for preventing R to be lower than -1.0 due to accumulation of numerical error scipy.stats.linregress and a test-case (tested under debian7, python2.7, amd64 and probably dependent on the compiler as well)

### kif added some commits Feb 12, 2013

``` testing bad regression coefficient (leading to NaN stderr) ```
``` e410d5c ```
``` fix a bug making stderr NaN when R<-1 due to rounding ```
`errors in scipy.stats.linregress`
``` 499b89c ```

### josef-pkt reviewed Feb 12, 2013

 + y = np.linspace(2 * a, a, n) + stats.linregress(x, y) + res = stats.linregress(x, y) + assert_(res[2] >= -1, "R factor is in [-1..1]")

#### josef-pkt Feb 12, 2013

Member

the assertion is too loose, shouldn't this be
`assert_equal(res[2], -1, ...)`
there is a small chance that equality doesn't hold for all platforms
the we could use both (just add an almost equal)

``````assert_(res[2] >= -1, "R factor is in [-1..1]")
assert_almost_equal(res[2]), -1)
``````

### josef-pkt reviewed Feb 12, 2013

 @@ -2962,6 +2962,7 @@ def linregress(x, y=None): else: r = r_num / r_den if (r > 1.0): r = 1.0 # from numerical error + elif (r < -1.0): r = -1.0 # from numerical error

#### josef-pkt Feb 12, 2013

Member

cosmetic: move r = ... into new lines (also the old)

Member

### josef-pkt commented Feb 12, 2013

 @rgommers should go into 0.12
``` Cosmetic clean up for stats.linregress ```
``` 2b1e789 ```
Contributor

### kif commented Feb 12, 2013

 I think all your comment were taken into account. Josef, is it OK ?

### josef-pkt commented on `scipy/stats/tests/test_stats.py` in `2b1e789`Feb 12, 2013

 error message could be improved, it's purpose is mainly to get the information about the test in case of failure maybe just something like "perfect negative correlation case"

### josef-pkt replied Feb 12, 2013

 just additional info: The numbers, actual and expected, are shown automatically in case of a test failure, so it doesn't need to be in the error message.
``` better log messages in non-regression tests (stats.linregression) ```
``` 8f7ca90 ```
Member

### rgommers commented Feb 14, 2013

 Squashed and merged as c415f8d. Make a minor change to the messages - they're comments now. Failure will then show the actual numerical values. Thanks Jerome, Josef.