forum.alglib.net http://forum.alglib.net/ |
|
alglib, C#, VS2010 and that Excel curve.... http://forum.alglib.net/viewtopic.php?f=2&t=57 |
Page 1 of 1 |
Author: | ReinhardtB [ Wed Sep 08, 2010 9:49 am ] |
Post subject: | alglib, C#, VS2010 and that Excel curve.... |
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 |
Author: | Sergey.Bochkanov [ Wed Sep 08, 2010 10:55 am ] |
Post subject: | Re: alglib, C#, VS2010 and that Excel curve.... |
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. |
Author: | ReinhardtB [ Wed Sep 08, 2010 12:23 pm ] |
Post subject: | Re: alglib, C#, VS2010 and that Excel curve.... |
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? |
Author: | Sergey.Bochkanov [ Wed Sep 08, 2010 2:10 pm ] |
Post subject: | Re: alglib, C#, VS2010 and that Excel curve.... |
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. |
Author: | ReinhardtB [ Sun Sep 12, 2010 7:30 am ] |
Post subject: | Re: alglib, C#, VS2010 and that Excel curve.... |
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! |
Page 1 of 1 | All times are UTC |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |