forum.alglib.net

ALGLIB forum
It is currently Sun Dec 22, 2024 10:49 am

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  [ 5 posts ] 
Author Message
 Post subject: alglib, C#, VS2010 and that Excel curve....
PostPosted: Wed Sep 08, 2010 9:49 am 
Offline

Joined: Wed Sep 08, 2010 9:05 am
Posts: 3
Hi all -

I'm quite obviously a n00b (1 post! ;) ) so please be gentle! I'm also don't resemble anything close to a mathematician, and the users want bloody trend lines!!!! :roll:

Using the above tools, would it be possible to generate a best-fit (NOT necessarily dissecting all points), smooth trend curve on a chart?? I've looked at lsfitlinear which I understand should be the answer, but I can't for the life of me figure out what the usage is in c#?!?

Your input would be GREATLY appreciated, and free beer doled out if you ever find yourself in South Africa! :mrgreen:

Cheers,
Reinhardt


Top
 Profile  
 
 Post subject: Re: alglib, C#, VS2010 and that Excel curve....
PostPosted: Wed Sep 08, 2010 10:55 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
Yes, you can use lsfit unit, but it requires you to specify some function which will be fitted.

If you want to have flexible fit, I recommend you to use spline fitting (see http://www.alglib.net/interpolation/spline3.php). You can use Spline1DFitCubic function which accepts only one "tweak" parameter - number of degrees of freedom. You can also use Spline1DFitHermite, it is more flexible, but it have one additional requirement - number of degrees of freedom must be even.

See http://www.alglib.net/translator/man/ma ... line1d_fit for examples on this subject.

If you want to use linear/polynomial fitting, take a look at the BarycentricFit... functions.


Top
 Profile  
 
 Post subject: Re: alglib, C#, VS2010 and that Excel curve....
PostPosted: Wed Sep 08, 2010 12:23 pm 
Offline

Joined: Wed Sep 08, 2010 9:05 am
Posts: 3
Fantastic - thanks for the quick reply!

I have my code as follows:
Code:

            double[] y = dataPoints.Select(p => (double)p.Result).ToArray();
            double[] x = new double[dataPoints.Count];

            for (int i = 0; i < x.Length; i++) // it is always a counter...right?
                x[i] = i;

            int resultIndicator = -1;
            alglib.spline1d.spline1dinterpolant spline = new alglib.spline1d.spline1dinterpolant();
            alglib.spline1d.spline1dfitreport report = new alglib.spline1d.spline1dfitreport();

            alglib.spline1d.spline1dfitcubic(ref x, ref y, dataPoints.Count, 4, ref resultIndicator, ref spline, ref report);

            if (resultIndicator > 0) // success
                for (int i = 0; i < dataPoints.Count; i++ )
                    dataPoints[i].TrendPosition = (decimal)alglib.spline1d.spline1dcalc(ref spline, (double)i);



does this look right?


Top
 Profile  
 
 Post subject: Re: alglib, C#, VS2010 and that Excel curve....
PostPosted: Wed Sep 08, 2010 2:10 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
Almost right. I recommend you to:
* pay attention to number of degrees of freedom. 4 may be too small for highly nonlinear trends.
* calculate spline values at even intervals spanning [min x, max x], not at the points themselves.

However, these are mostly cosmetic changes. Your code looks right.


Top
 Profile  
 
 Post subject: Re: alglib, C#, VS2010 and that Excel curve....
PostPosted: Sun Sep 12, 2010 7:30 am 
Offline

Joined: Wed Sep 08, 2010 9:05 am
Posts: 3
Thanks Sergey - it's working!

I can not set the degrees of freedom any lower than 4 (I suppose that's by design), but higher than 4 makes a significant difference to the curve! I'll have to add that in as a parameter.

Thank you so much - it works spectacularly well!

I hope Google finds this thread, I struggled my ass of to find an answer to this!

Thanks again!


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 6 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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group