Skip Menu |

This queue is for tickets about the Algorithm-CurveFit CPAN distribution.

Report information
The Basics
Id: 29954
Status: open
Priority: 0/
Queue: Algorithm-CurveFit

People
Owner: Nobody in particular
Requestors: bitcard [...] adammorton.com
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: (no value)
Fixed in: (no value)



Subject: Math::MatrixReal->new_from_cols fails when $deriv->value is undef
It seems that $deriv->value will return an undef when the partial derivative is infinite. When this gets passed into Math::MatrixReal->new_from_cols, death occurs. The following parabola fit should demonstrate: perl -MAlgorithm::CurveFit -e "print Algorithm::CurveFit->curve_fit(params=>[['A',-5,'0.05'],['x_0',2,'0.02'],['y_0',20,'0.2']], variable=>'x', formula=>'A*(x-x_0)^2+y_0', xdata=>[0,' 0.66','1.33',2,'2.66','3.33',4,'4.66','5.33',6], ydata=>[0,'6.66','13.33',20,'13.33','6.66',0,'-20','-40',-60])" Math::MatrixReal::new_from_string(): syntax error in input stringString is [ ] [ 6.6 ] [ 13.3 ] [ 20 ] [ 26.6 ] [ 33.3 ] [ 40 ] --- at /usr/lib/perl5/site_perl/5.8.8/Algorithm/CurveFit.pm line 158 I "fixed" this locally by substituting 1e100 for undef when $deriv->value returns undef. This is clearly not correct in edge cases...
From: pub [...] johnrefior.com
When I call curve_fit on a parabola formula A*(x - x_0)^2 + y_0 where the initial guess for x-value of the vertex of the parabola (x_0) equals one of the x-data values submitted to the subroutine (x), it causes the following error: Math::MatrixReal::new_from_string(): syntax error in input stringString is [ ] [ 6.6666666666667 ] [ 13.3333333333333 ] [ 20 ] [ 26.6666666666667 ] [ 33.3333333333333 ] [ 40 ] --- at /usr/lib/perl5/site_perl/5.8.8/Algorithm/CurveFit.pm line 149 Please see attached test script which reproduces the error. The following parameter values were used: formula: A*(x-x_0)^2+y_0 params: (['A',-5,'0.05'], ['x_0',2,'0.02'], ['y_0',20,'0.2']) variable: x xdata: 0, 0.666666666666667, 1.33333333333333, 2, 2.66666666666667, 3.33333333333333, 4, 4.66666666666667, 5.33333333333333, 6 ydata: 0, 10, 16.6666666666667, 20, 16.6666666666667, 10, 0, -16.6666666666667, -36.6666666666667, -60 maximum iterations: 100 The cause of this error is an unexpected value of undef returned from $deriv->value: When I ask Math::Symbolic::Derivative to take the partial derivative of A*(x - x_0)^2 + y_0 with respect to x_0, and simplify, it returns the expression (-2A*(x - x_0)^2)/(x - x_0) When I do the same by hand, I get -2A*(x - x_0) The two are equivalent except when x == x_0. Then the Symbolic expression returns undef, while mine by hand returns 0. Algorithm::CurveFit has no handling for a returned value of undef at this point, so it proceeds and the error is generated at Math::MatrixReal::new_from_string(). Running perl v5.8.8 built for i686-linux, using Math::Symbolic version 0.603, and Algorithm::CurveFit version 1.04. I have submitted bug #55842 at CPAN ( https://rt.cpan.org/Ticket/Display.html?id=55842 ) for Math-Symbolic, please check there for updates and more information. - John Refior
Subject: alg_curve_fit_test.pl
#!/usr/bin/perl use strict; use warnings; use Algorithm::CurveFit; my $formula = 'A*(x-x_0)^2+y_0'; my @parameters = ( [ 'A', -5, '0.05' ], [ 'x_0', 2, '0.02' ], [ 'y_0', 20, '0.2' ] ); my @xDataPts = (0, 0.666666666666667, 1.33333333333333, 2, 2.66666666666667, 3.33333333333333, 4, 4.66666666666667, 5.33333333333333, 6); my @yDataPts = (0, 10, 16.6666666666667, 20, 16.6666666666667, 10, 0, -16.6666666666667, -36.6666666666667, -60); my $squareResidual = Algorithm::CurveFit->curve_fit( formula => $formula, params => \@parameters, # values in @parameters will be modified in-place variable => 'x', xdata => \@xDataPts, # data corresponsing to variable 'x' ydata => \@yDataPts, # data corresponsing to 'y' maximum_iterations => 100, # fit ends if an iteration introduces smaller changes than the accuracy ) or warn "Error-parabola"; # "Parabola function could not be fitted to data"; print "Done!\n"; print "Square Residual is $squareResidual.\n";
Unfortunately, I'm swamped with work and all I can do realistically is accept patches. Algorithm::CurveFit is in revision control (SVN) at http://svn.ali.as/cpan/trunk/Algorithm-CurveFit/. Patches against the CPAN release are also acceptable. Solving this issue requires at least two things: a) Check for undefs (efficiently...) before passing values to Math::MatrixReal. b) Provide a facility to work around Math::Symbolic limitations. (Cf. my reply to the M::S ticket referenced in John's message.) Sorry, I wish I could do more. --Steffen