Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exponential based Curve-Fit using Math.Net

I'm very new to the Math.Net Library and I'm having problems trying to do curve-fitting based on an exponential function. More specifically I intend to use this function:

f(x) = a*exp(b*x) + c*exp(d*x)

Using MATLAB I get pretty good results, as shown in the following image:

blah

MATLAB calculates the following parameters:

f(x) = a*exp(b*x) + c*exp(d*x)
Coefficients (with 95% confidence bounds):
a =   29.6       ( 29.49     , 29.71)
b =    0.000408  (  0.0003838,  0.0004322)
c =   -6.634     ( -6.747    , -6.521)
d =   -0.03818   ( -0.03968  , -0.03667)

Is it possible to achieve these results using Math.Net?

like image 995
eol Avatar asked Jun 20 '14 21:06

eol


3 Answers

Looking at Math.net, it seems that Math.net does various types of regression, whereas your function require some type of iterative method. For instance Gauss-Newton's method where you would use linear regression in each iteration to solve a (overdetermined) system of linear equations, but this would still require some "manual" work with writing the method.

like image 127
NLindbom Avatar answered Oct 07 '22 00:10

NLindbom


No it appears there is not exponential support at this time. However there's a discussion on Math.NET forums where a maintainer proposes a workaround:

https://discuss.mathdotnet.com/t/exponential-fit/131

Contents duplicated in case link gets broken:

You can, by transforming it, similar to Linearizing non-linear models by transformation. Something along the lines of the following should work:

double[] Exponential(double[] x, double[] y,
    DirectRegressionMethod method = DirectRegressionMethod.QR)
{
    double[] y_hat = Generate.Map(y, Math.Log);
    double[] p_hat = Fit.LinearCombination(x, y_hat, method, t => 1.0, t => t);
    return new[] {Math.Exp(p_hat[0]), p_hat[1]}; 
}

Example usage:

double[] x = new[] { 1.0, 2.0, 3.0 };
double[] y = new[] { 2.0, 4.1, 7.9 };
double[] p = Exponential(x,y); // a=1.017, r=0.687
double[] yh = Generate.Map(x,k => p[0]*Math.Exp(p[1]*k)) // 2.02, 4.02, 7.98
like image 40
Steve Cadwallader Avatar answered Oct 07 '22 00:10

Steve Cadwallader


Answer is: not yet, I believe. Basically, there is contribution of whole csmpfit package, but it yet to be integrated into Math.Net. You could use it as separate library and then after full integration move to Math.Net. Link http://csmpfit.codeplex.com

like image 1
Severin Pappadeux Avatar answered Oct 07 '22 00:10

Severin Pappadeux