bugGNU Octave - Bugs: bug #57964, polyfit() results for polynomial...

 
 

bug #57964: polyfit() results for polynomial degree greater or equal than number of data

Submitted by:  José Luis García Pallero <jgpallero>
Submitted on:  Fri 06 Mar 2020 04:23:45 PM UTC  
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Missed Error or Warning
Status:  Confirmed Assigned to:  None
Originator Name:  Open/Closed:  Open
Release:  dev Operating System:  Any

Add a New Comment (Rich Markup)
   

You are not logged in

Please log in, so followups can be emailed to you.

 

Tue 10 Mar 2020 03:39:40 PM UTC, comment #3: 

Changing the item group to "Missed Error or Warning".  Bumping the Release to "dev".  Marking as confirmed.

At the very least, it would be good to issue a warning.  The return value for the degrees of freedom output "df" is -1 which is nonsensical.

I tested a bit with Matlab and it does return consistent outputs for the second and third outputs S and MU.

[P, S, MU] = polyfit (...)

However, the values are definitely not unique.  If I call polyfit with a single output I get

[p] = polyfit ([1 2],[3 4],2)
p = [0.3333  0   2.6667]

But when I call it with three outputs I get

[p, s, mu] = polyfit ([1 2],[3 4],2)
p = [0   0.7071  3.5]

Given this pseudo-randomness, I'm not sure we have to exactly match Matlab behavior so much as issue a warning and be consistent.

Rik <rik5>
Project Administrator
Sat 07 Mar 2020 02:20:41 PM UTC, comment #2: 

I just looked into the matlab documentation, and it seems to be undocumented (searching for "unique" on the polyfit page does not find anything). But yes, it would not hurt if in polyfit.m a small additional check would be introduced to test whether n is larger or equal to the number of data points, in which case n would temporarily be decreased to the number of data points minus one for the computation, and then the output be zero-padded to a length of n, and a warning emitted. Such usages will always indicate either an error or a situation in a user's program that was not foreseen.  However, if the other output arguments are required, it would perhaps become messy, because of course also all these matrices have to be expanded, and offhand I do not know whether this is trivial.

Michael Leitner <mleitner>
Fri 06 Mar 2020 04:49:50 PM UTC, comment #1: 

Well, I was playing with other values and I noted that in such cases Matlab does not perform, as I wrote in my firs comment, "the problem is solved using the correct maximum degree according to the data". But anyway in this cases the solutions provided by Matlab and Octave are not the same. For example, in Matlab:

p=polyfit([1 2],[3 4],2)
Warning: Polynomial is not unique; degree >= number of data points.
> In polyfit (line 70)

p =

   0.333333333333333                   0   2.666666666666667

and in Octave

p=polyfit([1 2],[3 4],2)
p =

  -0.071429   1.214286   1.857143

If I call the function with a second output argument I can see that the R matrix of the QR decomposition is the same in both cases, but with different column ordering (in this case the QR decomposition must be performed with pivoting). In effect, in Octave we have

>> [p,s]=polyfit([1 2],[3 4],2)
p =

  -0.071429   1.214286   1.857143

s =

  scalar structure containing the fields:

    yf =

       3.0000   4.0000

    X =

       1   1   1
       4   2   1

    R =

      -4.12311  -1.21268  -2.18282
       0.00000  -0.72761  -0.48507

    C =

       NaN   NaN   NaN
       NaN   NaN   NaN
       NaN   NaN   NaN

    df = -1
    normr =    1.2561e-15

so the QR decomposition was performed with pivoting as

>> [q,r,p]=qr(s.X)
q =

  -0.24254  -0.97014
  -0.97014   0.24254

r =

  -4.12311  -1.21268  -2.18282
   0.00000  -0.72761  -0.48507

p =

Permutation Matrix

   1   0   0
   0   0   1
   0   1   0

Apparently Matlab does not use pivoting in the QR, but I can't check it as the polyfit() output struct in Matlab does not store the Vandermonde matrix of the problem, but if I use the Octave's one I obtain

>> [q,r]=qr(s.X)
q =

  -0.24254  -0.97014
  -0.97014   0.24254

r =

  -4.12311  -2.18282  -1.21268
   0.00000  -0.48507  -0.72761

which is the Matlab's result.

So my original submission must not be considered a bug nor incorrect result, but an incompatibility with Matlab.

José Luis García Pallero <jgpallero>
Fri 06 Mar 2020 04:23:45 PM UTC, original submission:  

Hello:

If I call polyfit as

polyfit(1,2,0)

I obtain the expected result of

ans =  2

But if I call the function with a polynomial degree greater than 0 I obtain a vector of strange results

polyfit(1,2,3)
ans =

   0.50000   0.50000   0.50000   0.50000

while Matlab gives the correct solution (the problem is solved using the correct maximum degree according to the data) plus a warning

>> polyfit(1,2,3)
Warning: Polynomial is not unique; degree >= number of data points.
> In polyfit (line 70)

ans =

     2     0     0     0

Is this Octave's behavior a bug or am I misunderstanding something?

José Luis García Pallero <jgpallero>

 

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

Attach Files:
   
   
Comment:
   

No files currently attached

 

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 mleitner (Posted a comment)
  • -email is unavailable- added by jgpallero (Submitted the item)
  •  

    Do you think this task is very important?
    If so, you can add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.

     

     

     

    Follow 3 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2020-03-10 rik5 Item GroupIncorrect Result => Missed Error or Warning
        StatusNone => Confirmed
        Release5.2.0 => dev

    Back to the top


    Powered by Savane 3.5