bugGNU Octave - Bugs: bug #42671, [PATCH] corr() does not have...


bug #42671: [PATCH] corr() does not have p-values output, returns 1.0 with one observation.

Submitter:  Philipp Kutin <pkutin>
Submitted:  Thu 03 Jul 2014 12:36:46 PM UTC
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Fixed Assigned to:  None
Originator Name:  Open/Closed:  * Closed
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment (Rich Markup)

Comment Type & Canned Response:


( Jump to the original submission )

Fri 14 Dec 2018 08:57:54 PM UTC, comment #6: 

This has been resolved through the addition of the corrcoef function in March 2017.  Marking as fixed and closing report.

Rik <rik5>
Project Administrator
Thu 15 Dec 2016 06:00:00 PM UTC, comment #5: 

This issue is still present in Octave 4.2.0.

The results of the code examples from comment #0 still throw errors in Octave, where as they are reported to work in Matlab.

There is a PATCH submitted in comment #4 that claims to fix the original issue. It just leaves a small corner case open (see comment #2).

Hartmut <hardy>
Thu 15 Jan 2015 06:37:59 AM UTC, comment #4: 

Thanks for the patch.

I agree that it would be better to stick with the stricter (Matlab-compatible) interpretation of vectors.


Fri 04 Jul 2014 12:03:24 PM UTC, comment #3: 

One more update. The patch attached with this comment is from 'hg export' instead of my local MQ, er, stack and as such includes author info. I find MQ very convenient to cleanly separate my patches as I can't know whether/when they're going to be committed to Octave HG. In case anyone's interested, I keep my .hg/patches directory here: https://github.com/helixhorned/pk-octave-patches .

Anyhow, I think I've gotten the hang of the occasional contributor workflow:
- work locally on some source code
- save a new MQ patch
- hg qpop -a
- commit the topmost MQ patch to the real HG repo
- hg export, this one will be uploaded
- hg rollback
- hg qpush -a

This is hopefully my last noisy comment. Sorry!

(file #31672)

Philipp Kutin <pkutin>
Thu 03 Jul 2014 01:09:48 PM UTC, comment #2: 

Alright... an updated patch is attached that supersedes the first one. Currently, Octave is incompatible to MATLAB in another way, not handled with this patch. In MATLAB, the input arguments are interpreted strictly as stated in the doc: columns are variables, rows are observations:

>> corr(1:4)
ans =
   NaN   NaN   NaN   NaN
   NaN   NaN   NaN   NaN
   NaN   NaN   NaN   NaN
   NaN   NaN   NaN   NaN

Octave, in contrast, makes an expection for vector input(s), always handling each one as one variable with as many observations as it has elements. This special case is not documented and may cause confusion. Personally, I prefer stricter semantics whenever possible.

(file #31671)

Philipp Kutin <pkutin>
Thu 03 Jul 2014 12:52:18 PM UTC, comment #1: 

Oops, there are unhandled corner cases related to the ambiguity when passing row vector inputs and one '==' that should read '<='. Don't use yet.

Philipp Kutin <pkutin>
Thu 03 Jul 2014 12:36:46 PM UTC, original submission:  

The current corr.m is behind MATLAB's in various ways. First, there's no p-values output with the second 'PVAL' outarg. Because of this, there's also no option as to which kind of alternative hypothesis to consider ('both', 'left' or 'right').

The attached patch adds the PVAL output for the both-sided case. As stated in the MATLAB docs, a transformation from r to values that are t-distributed (assuming the input variables are uncorrelated bivariate Gaussian) is used there.

Additionally, when correlating data sets with one observation, return NaN instead of 1 -- the Pearson correlation coefficient is not defined in this case since the variance of either variable isn't.

Patch message:

corr.m: obtain p-values from r-to-t transformation; return NaN for 1 observation.

* when correlating data sets with one observation, return NaN instead of 1.
* use a transformation into a t-distributed variable (assuming the input
  variables are uncorrelated bivariate Gaussian) to obtain both-sided p-values

Future directions:
For corr() to accept key/value pairs like 'KIND' it would be nice to have a factored system to extract these from a varargin passed to a function. Searching for the K/V pattern in the Octave code, it seems like these are done by hand each time now.
The remaining measures of assiciation -- spearman() and kendall() -- are there, so dispatching to those could then be done from corr(), too. Estimating p-values for them is a different story.

Tests on MATLAB R2013a:

>> corr(1,2)
ans =
>> [c,p]=corr([1 2]',[2 3]')
c =
p =
>> [c,p]=corr([1 2 3]',[2 3 4]')
c =
p =

>> [c,p]=corr([1 2 3 4]',[2 3 4 7]', 'tail','right')
c =
p =
>> [c,p]=corr([1 2 3 4]',[2 3 4 7]', 'tail','left')
c =
p =
>> [c,p]=corr([1 2 3 4]',[2 3 4 7]', 'tail','both')
c =
p =

Philipp Kutin <pkutin>


(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:

Attached Files
file #31672:  corr-pval-3.patch added by pkutin (2KiB - text/x-patch)
file #31671:  corr-pval-2.patch added by pkutin (2KiB - text/x-patch)
file #31670:  corr-pval-1.patch added by pkutin (2KiB - text/x-patch)


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by hardy (Posted a comment)
  • -email is unavailable- added by mtmiller (Updated the item)
  • -email is unavailable- added by pkutin (Submitted the item)

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only project members can vote.





    Follow 6 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2018-12-14 rik5 StatusPatch Submitted Fixed
        Open/ClosedOpen Closed
    2014-07-04 pkutin Attached File- Added corr-pval-3.patch, #31672
    2014-07-03 mtmiller StatusNone Patch Submitted
    2014-07-03 pkutin Attached File- Added corr-pval-2.patch, #31671
    2014-07-03 pkutin Attached File- Added corr-pval-1.patch, #31670

    Back to the top

    Powered by Savane 3.9