How we can use NonlinearLeastSquares ?

150 views
Skip to first unread message

anism...@gmail.com

unread,
Jun 17, 2015, 10:12:55 AM6/17/15
to accor...@googlegroups.com
Hi Cezar,

Can you please provide us an example to know how we can use NonlinearLeastSquares class ?

Thank you
Best Regards.

César

unread,
Jun 17, 2015, 12:43:48 PM6/17/15
to accor...@googlegroups.com
Hi there,

Sure, here it is. Hope it helps!

Best regards,
Cesar

// Suppose we would like to map the continuous values in the
// second column to the integer values in the first column.
double[,] data =
{
   
{ -40,    -21142.1111111111 },
   
{ -30,    -21330.1111111111 },
   
{ -20,    -12036.1111111111 },
   
{ -10,      7255.3888888889 },
   
{   0,     32474.8888888889 },
   
{  10,     32474.8888888889 },
   
{  20,      9060.8888888889 },
   
{  30,    -11628.1111111111 },
   
{  40,    -15129.6111111111 },
};


// Extract inputs and outputs
double[][] inputs = data.GetColumn(0).ToArray();
double[] outputs = data.GetColumn(1);


// Create a Nonlinear regression using
var regression = new NonlinearRegression(3,


   
// Let's assume a quadratic model function: ax² + bx + c
   
function: (w, x) => w[0] * x[0] * x[0] + w[1] * x[0] + w[2],


   
// Derivative in respect to the weights:
    gradient
: (w, x, r) =>
   
{
        r
[0] = 2 * w[0]; // w.r.t a: 2a  
        r
[1] = w[1];     // w.r.t b: b
        r
[2] = w[2];     // w.r.t c: 0
   
}
);


// Create a non-linear least squares teacher
var nls = new NonlinearLeastSquares(regression);


// Initialize to some random values
regression
.Coefficients[0] = 4.2;
regression
.Coefficients[1] = 0.3;
regression
.Coefficients[2] = 1;


// Run the function estimation algorithm
double error;
for (int i = 0; i < 100; i++)
    error
= nls.Run(inputs, outputs);


// Use the function to compute the input values
double[] predict = inputs.Apply(regression.Compute);

anism...@gmail.com

unread,
Jun 18, 2015, 4:07:43 AM6/18/15
to accor...@googlegroups.com
Thank You Cesar it of course help me.
Thank you again.
Reply all
Reply to author
Forward
0 new messages