I am trying to fit exponential data using a sigmoidal function (4PL) with the following formula:
y = a + (k-a) /(1 + exp((v-x)/c)
While with R I have good results, using C# and the framework Accord.Net I get very poor fit.
Here is my code:
var nls = new NonlinearLeastSquares()
{
NumberOfParameters = 4,
StartValues = new[] {0d, 40000d, 35d,1d }
Function = (parameters, input) =>
{
return parameters[0] + ((parameters[1] - parameters[0]) / ( 1 +
Math.Exp((parameters[2] - input[0] )/parameters[3]) ) );
},
Gradient = (parameters, input, result) =>
{
result[0] = 1 - ( 1 / (1 + Math.Exp((parameters[2] - input[0]) /
parameters[3]))); // d/da
result[1] = 1 / (1 + Math.Exp((parameters[2] - input[0]) /
parameters[3])); // d/dk
result[2] = -((parameters[1] - parameters[0])*
Math.Exp((parameters[2] - input[0]) / parameters[3])) /
parameters[3]*Math.Pow(1 + Math.Exp((parameters[2] -
input[0]) / parameters[3]),2); // d/dv
result[3] = ((parameters[1] - parameters[0]) * (parameters[2] -
input[0]) * Math.Exp((parameters[2] - input[0]) /
parameters[3])) / Math.Pow(parameters[3], 2) *
Math.Pow(1 + Math.Exp((parameters[2] - input[0]) /
parameters[3]), 2); // d/dc
},
Algorithm = new LevenbergMarquardt()
{
MaxIterations = 100,
Tolerance = 0
}
};
var regression = nls.Learn(inputs, outputs);
// The solution will be at:
double a = regression.Coefficients[0];
double k = regression.Coefficients[1];
double v = regression.Coefficients[2];
double c = regression.Coefficients[3];
I am stuck with this problem, any help would be really appreciate,
For whom interested I found out the solution.
The problem were the gradients of result[2] (d/dv) and result[3] (d/dc).
Parentheses were missing in the denominator.