Exponential based Curve-Fit using Math.Net
Asked Answered
B

3

7

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?

Burge answered 20/6, 2014 at 21:48 Comment(0)
U
1

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
Urson answered 28/2, 2017 at 18:24 Comment(0)
U
2

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.

Unlimber answered 23/4, 2015 at 21:16 Comment(0)
P
1

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

Parson answered 23/4, 2015 at 21:31 Comment(0)
U
1

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
Urson answered 28/2, 2017 at 18:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.