forum.alglib.net

ALGLIB forum
It is currently Thu Mar 28, 2024 2:04 pm

All times are UTC


Forum rules


1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.



Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: alglib not fitting correctly
PostPosted: Tue Oct 18, 2016 2:33 pm 
Offline

Joined: Thu Mar 31, 2016 8:18 am
Posts: 2
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 437 times
Top
 Profile  
 
 Post subject: Re: alglib not fitting correctly
PostPosted: Wed Oct 19, 2016 6:54 am 
Offline

Joined: Thu Mar 31, 2016 8:18 am
Posts: 2
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 57 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group