forum.alglib.net
http://forum.alglib.net/

how to formulate least squares problem
http://forum.alglib.net/viewtopic.php?f=2&t=638
Page 1 of 1

Author:  slap [ Sat Nov 03, 2012 10:16 pm ]
Post subject:  how to formulate least squares problem

I am trying to understand how to formualte a problem using Alglib that I hope I may be able to solve with the lsfit routines. so I am trying to start with a very simple example to help me understand how to formulate it.

In my simple example I wish to fit/model a curve of 3 points, thier values are 1,7,4
I am trying to fit them as a combination of two curves of three points 0,3.5,1 and 1,0,2

I have tried to modify the example for lsfit_d_nlf example.

Maybe my function is incorrect, maybe I have mixed up what should be the y values and what should be the X, I have tried several different varaitions of where the inputs should go but without success.

So I would be very greatful if someone can help me, and show me teh correct way to formulate this?

thanks in advance

Code:
Module Module1

    'changed EXAMPLE FROM ALGLIB PACKAGE

    Public Sub function_cx_1_func(c As Double(), x As Double(,), ByRef func As Double, obj As Object)
        '
        '
        func = (c(0) * x(0, 0)) + (c(0) * x(0, 1)) + (c(0) * x(0, 2)) + (c(1) * x(0, 1)) + (c(1) * x(1, 1)) + (c(1) * x(2, 1))

    End Sub

    Public Sub Main()
        '
        ' 
        '
        '  Gradient is estimated using combination of numerical differences
        '  and secant updates. diffstep variable stores differentiation step
        '  (we have to tell algorithm what step to use).
        '
        Dim x(,) As Double = New Double(,) {{0.0, 1.0}, {3.5, 0.0}, {1.0, 2.0}}
        Dim y() As Double = New Double() {1, 4, 7}
        Dim c() As Double = New Double() {0.6, 0.3}
        Dim epsf As Double = 0
        Dim epsx As Double = 0.000001
        Dim maxits As Integer = 0
        Dim info As Integer
        Dim state As lsfitstate = New XAlglib.lsfitstate() ' initializer can be dropped, but compiler will issue warning
        Dim rep As lsfitreport = New XAlglib.lsfitreport() ' initializer can be dropped, but compiler will issue warning
        Dim diffstep As Double = 0.0001

        '
        '  Fitting without weights
        '
        XAlglib.lsfitcreatef(x, y, c, diffstep, state)


        xalglib.lsfitsetcond(state, epsf, epsx, maxits)
        XAlglib.lsfitfit(state, AddressOf function_cx_1_func, Nothing, Nothing)
        xalglib.lsfitresults(state, info, c, rep)
        System.Console.WriteLine("{0}", info) ' EXPECTED: 2
        System.Console.WriteLine("{0}", alglib.ap.format(c, 1)) ' EXPECTED: 2,1


       
        System.Console.ReadLine()
        Environment.Exit(0)
    End Sub
End Module

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/