forum.alglib.net
http://forum.alglib.net/

alglib not fitting correctly
http://forum.alglib.net/viewtopic.php?f=2&t=3774
Page 1 of 1

Author:  bochelie [ Tue Oct 18, 2016 2:33 pm ]
Post subject:  alglib not fitting correctly

I have been trying to fit some data. I tried alglib and extremeoptimization libraries. Alglib does not fit the data correctly while the extremeoptimization dll always does a good job. Am I doing something wrong? I attached the data which Im trying to fit in a csv attachment. And here is my code:

alglib results:
c 0= 1.0000013700962485
c 1= 0.10000154265966896
c 2= 0.000018706632069718112
extremeoptimization results:
c 0= 0.997214300534702
c 1= 1.07503847563227
c 2= 3.50620429325812E-05

Code:
   class LeastSquaresFit
    {
        double[] x2;
        internal void LeastSquaresFitting(Fited fitted, List<double> lx, List<double> ly, double Flux_1, double Flux_2, double Flux_3)
        {
            //load data file
            double[] X = new double[0];
            double[] y = new double[0];
            using (StreamReader sr = File.OpenText(@"C:\temp\data1.csv"))
            {
                int t = 0;
                string line = "";
                while ((line = sr.ReadLine()) != null)
                {
                    char[] delimiters = new char[] { ';' };
                    string[] parts = line.Split(delimiters);

                    Array.Resize(ref X, t + 1);
                    Array.Resize(ref y, t + 1);
                    X[t] = double.Parse(parts[0], NumberStyles.Any, CultureInfo.CurrentCulture);
                    y[t] = double.Parse(parts[0], NumberStyles.Any, CultureInfo.CurrentCulture);
                    t++;
                }
            }
            //

            //Set to double array
            double[,] x = new double[X.Length, 1];
            x2 = new double[lx.Count];
            for (int t = 0; t < X.Length; t++)
            {
                x[t, 0] = X[t];
            }


            //////////////////////////////////////////////////////////////////////////////////

            alglib.lsfitreport fited1 = GetFit(x, y, HelpClasses.HelpClasses.FitTypes.func, 1);


        }

        private alglib.lsfitreport GetFit(double[,] x, double[] y, HelpClasses.HelpClasses.FitTypes Mode, int fitindx)
        {
            //set start variables
            double[] c = new double[] { 1, 0, 0 };
            double[] bndl = new double[] { 0, 0, 0 }; //lowerBounds
            double[] bndu = new double[] { double.PositiveInfinity, double.PositiveInfinity, double.PositiveInfinity }; //uperBounds

            double epsf = 0;
            double epsx = 0.000001; //0.000001;
            int maxits = 1000;
            int info;
            alglib.lsfitstate state;

            double diffstep = 0.0001;
            // Mode > 1 = function values, 2 = gradient, 3 = hessian

            alglib.lsfitcreatef(x, y, c, diffstep, out state);
            alglib.lsfitsetcond(state, epsf, epsx, maxits);
            alglib.lsfitsetbc(state, bndl, bndu);
            alglib.lsfitfit(state, function_cx_1_func, null, null);

            alglib.lsfitreport rep;
            alglib.lsfitresults(state, out info, out c, out rep);

           
            return rep;
        }
       
        private static void function_cx_1_func(double[] c, double[] x, ref double func, object obj)
        {
            //func = a*EXP(-EXP(b-c*MW))*1
            func = c[0] * Math.Exp(-Math.Exp(c[1] - c[2] * x[0])) * 1;
        }
               
    }


Attachments:
data1.csv [20.94 KiB]
Downloaded 708 times

Author:  bochelie [ Wed Oct 19, 2016 6:54 am ]
Post subject:  Re: alglib not fitting correctly

I fixed the problem. It seems I had to use lsfitsetscale as mentioned in this thread: http://forum.alglib.net/viewtopic.php?f=2&t=3762&sid=415639538a88a051149f0b2565ff440c

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/