2

Given the following set of xs and ys:

xs = [8294400, 2073600, 921600, 409920]

ys = [124, 433, 853, 1449]

Fitting this with a power law in Excel yields a good approximation:

enter image description here

Excel found a function of the form a(x^b). How can one determine a and b in C#? I tried using Math.Net numerics but I don't see any method that would work for a function of this form. All the functions in the Linear Regression module just find linear coefficients to functions of various forms but none seem to be able to determine an exponent.

Asik
  • 21,506
  • 6
  • 72
  • 131
  • That is because the kind of regression you want is nonlinear. – André Chalella May 26 '15 at 19:35
  • You may need http://metanumerics.codeplex.com/ and the documentation http://metanumerics.codeplex.com/wikipage?title=Tests&referringTitle=Documentation – gtzinos May 26 '15 at 19:37
  • This case is actually described on the linked page: http://numerics.mathdotnet.com/Regression.html#Linearizing-non-linear-models-by-transformation – Christoph Rüegg May 27 '15 at 20:20

2 Answers2

5

The equation you want looks like this:

y = a*x^b

Take the natural log of both sides:

ln(y) = ln(a*x^b) = ln(a) + b*ln(x)

Now you can use linear regression on the new transformed variables (ln(x), ln(y)) and calculate the two parameters you want: ln(a) and b.

duffymo
  • 305,152
  • 44
  • 369
  • 561
1

In an exponential system, the best way to do a regression is probably by doing a linear regression on a log scale. To clarify, even though your function isn't linear, taking the natural log of both sides of the equation will result in a more linear system

Non-linear function: y = a x^b

This then becomes ln(y) = ln(a x^b) = ln(a) b ln(x)

In Math.NET Numerics, a good way to code it could be:

var y = y.Select(r => Math.Log(r)).ToArray(); // transform y = ln(z)
double[] w = Fit.LinearCombination(xy, z_hat,
    d => 1.0,
    d => Math.Log(d[0]),
    d => Math.Log(d[1]))

Or if you wanted to have it return a function you could use LinearCombinationFunc()

Sources: http://numerics.mathdotnet.com/Regression.html and http://numerics.mathdotnet.com/api/MathNet.Numerics/Fit.htm

Andrew Schade
  • 808
  • 1
  • 7
  • 14