You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Garrett Kane <gk...@us.ibm.com> on 2012/08/08 18:42:55 UTC
[math] QR decomposition in HarmonicFitter
I am trying to set up a harmonic fitter to determine periodicity of a data
set. My code:
import org.apache.commons.math3.optimization.fitting.HarmonicFitter;
import org.apache.commons.math3.optimization.fitting.WeightedObservedPoint;
import
org.apache.commons.math3.optimization.general.LevenbergMarquardtOptimizer;
public class FFT {
public static void main(String[] args){
double[] real =
{0,1,2,3,2,1,0,-1,-2,-3,-2,-1,0,1,2,3,2,1,0,-1,-2,-3,-2,-1,0,1,2,3,2,1,0};
WeightedObservedPoint[] points = new WeightedObservedPoint
[real.length];
HarmonicFitter fitter = new HarmonicFitter(new
LevenbergMarquardtOptimizer());
for(int i = 0; i < real.length; i++){
points[i] = new WeightedObservedPoint(1,i,real[i]);
fitter.addObservedPoint(new WeightedObservedPoint
(1,i,real[i]));
}
HarmonicFitter.ParameterGuesser guesser = new
HarmonicFitter.ParameterGuesser(points);
double[] guess = fitter.fit(guesser.guess());
for(double x:guess){
System.out.println(x);
}
}
}
and the error:
Exception in thread "main"
org.apache.commons.math3.exception.ConvergenceException: illegal state:
unable to perform Q.R decomposition on the 31x3 jacobian matrix
at
org.apache.commons.math3.optimization.general.LevenbergMarquardtOptimizer.qrDecomposition
(
LevenbergMarquardtOptimizer.java:848)
at
org.apache.commons.math3.optimization.general.LevenbergMarquardtOptimizer.doOptimize
(
LevenbergMarquardtOptimizer.java:312)
at
org.apache.commons.math3.optimization.direct.BaseAbstractMultivariateVectorOptimizer.optimize
(
BaseAbstractMultivariateVectorOptimizer.java:134)
at
org.apache.commons.math3.optimization.general.AbstractLeastSquaresOptimizer.optimize
(
AbstractLeastSquaresOptimizer.java:273)
at
org.apache.commons.math3.optimization.general.AbstractLeastSquaresOptimizer.optimize
(
AbstractLeastSquaresOptimizer.java:51)
at org.apache.commons.math3.optimization.fitting.CurveFitter.fit(
CurveFitter.java:157)
at org.apache.commons.math3.optimization.fitting.CurveFitter.fit(
CurveFitter.java:124)
at org.apache.commons.math3.optimization.fitting.HarmonicFitter.fit(
HarmonicFitter.java:61)
at FFT.main(FFT.java:16)
Can anyone help me? Should I be using a different
optimizer/fitter/something out of a separate library? The double[] real is
just a test set, actual data will be much more noisy as it comes from
real-world sensors
Thanks
-Garrett
Re: [math] QR decomposition in HarmonicFitter
Posted by Gilles Sadowski <gi...@harfang.homelinux.org>.
Hello.
> >
> > I am trying to set up a harmonic fitter to determine periodicity of a data
> > set. My code:
> > import org.apache.commons.math3.optimization.fitting.HarmonicFitter;
> > import org.apache.commons.math3.optimization.fitting.WeightedObservedPoint;
> > import
> > org.apache.commons.math3.optimization.general.LevenbergMarquardtOptimizer;
> >
> >
> > public class FFT {
> > public static void main(String[] args){
> > double[] real =
> > {0,1,2,3,2,1,0,-1,-2,-3,-2,-1,0,1,2,3,2,1,0,-1,-2,-3,-2,-1,0,1,2,3,2,1,0};
> > WeightedObservedPoint[] points = new WeightedObservedPoint
> > [real.length];
> > HarmonicFitter fitter = new HarmonicFitter(new
> > LevenbergMarquardtOptimizer());
> > for(int i = 0; i < real.length; i++){
> > points[i] = new WeightedObservedPoint(1,i,real[i]);
> > fitter.addObservedPoint(new WeightedObservedPoint
> > (1,i,real[i]));
> > }
> > HarmonicFitter.ParameterGuesser guesser = new
> > HarmonicFitter.ParameterGuesser(points);
> > double[] guess = fitter.fit(guesser.guess());
> > for(double x:guess){
> > System.out.println(x);
> > }
> > }
> > }
> >
> > and the error:
> > Exception in thread "main"
> > org.apache.commons.math3.exception.ConvergenceException: illegal state:
> > unable to perform Q.R decomposition on the 31x3 jacobian matrix
> > [...]
>
> This seems to be caused by the "degenerate" input (a slight change in the
> values and the fit proceeds fine). In order to investigate where the problem
> occurs, it might be worth opening a report on the Commons Math bug tracking
> system. [Unfortunately the web site is currently down...]
The "guess()" method in "HarmonicFitter" returns a wrong value for
"amplitude".
The problem is in the value of "c2", computed at line 295 in
"HarmonicFitter.java": For this set of points, the value is zero, which
produces infinity (at line 320).
[I don't know whether the "guesser" code could be improved to avoid such a
situation, or if we can just check for "c2" being zero and return something
more appropriate if this is the case...]
Best,
Gilles
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: [math] QR decomposition in HarmonicFitter
Posted by Gilles Sadowski <gi...@harfang.homelinux.org>.
Hi.
>
> I am trying to set up a harmonic fitter to determine periodicity of a data
> set. My code:
> import org.apache.commons.math3.optimization.fitting.HarmonicFitter;
> import org.apache.commons.math3.optimization.fitting.WeightedObservedPoint;
> import
> org.apache.commons.math3.optimization.general.LevenbergMarquardtOptimizer;
>
>
> public class FFT {
> public static void main(String[] args){
> double[] real =
> {0,1,2,3,2,1,0,-1,-2,-3,-2,-1,0,1,2,3,2,1,0,-1,-2,-3,-2,-1,0,1,2,3,2,1,0};
> WeightedObservedPoint[] points = new WeightedObservedPoint
> [real.length];
> HarmonicFitter fitter = new HarmonicFitter(new
> LevenbergMarquardtOptimizer());
> for(int i = 0; i < real.length; i++){
> points[i] = new WeightedObservedPoint(1,i,real[i]);
> fitter.addObservedPoint(new WeightedObservedPoint
> (1,i,real[i]));
> }
> HarmonicFitter.ParameterGuesser guesser = new
> HarmonicFitter.ParameterGuesser(points);
> double[] guess = fitter.fit(guesser.guess());
> for(double x:guess){
> System.out.println(x);
> }
> }
> }
>
> and the error:
> Exception in thread "main"
> org.apache.commons.math3.exception.ConvergenceException: illegal state:
> unable to perform Q.R decomposition on the 31x3 jacobian matrix
> [...]
This seems to be caused by the "degenerate" input (a slight change in the
values and the fit proceeds fine). In order to investigate where the problem
occurs, it might be worth opening a report on the Commons Math bug tracking
system. [Unfortunately the web site is currently down...]
Regards,
Gilles
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org