How to resolve the algorithm Multiple regression step by step in the FreeBASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Multiple regression step by step in the FreeBASIC programming language

Table of Contents

Problem Statement

Given a set of data vectors in the following format: Compute the vector

β

{

β

1

,

β

2

, . . . ,

β

k

}

{\displaystyle \beta ={\beta _{1},\beta _{2},...,\beta _{k}}}

using ordinary least squares regression using the following equation: You can assume y is given to you as a vector (a one-dimensional array), and X is given to you as a two-dimensional array (i.e. matrix).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Multiple regression step by step in the FreeBASIC programming language

Source code in the freebasic programming language

Const N = 14, M = 2, Q = 3    ' number of points and M.R. polynom degree

Dim As Double X(0 to N) = {1.47,1.50,1.52,1.55,1.57, _
1.60,1.63,1.65,1.68,1.70,1.73,1.75,1.78,1.80,1.83}              ' data points
Dim As Double Y(0 to N) = {52.21,53.12,54.48,55.84,57.20, _
58.57,59.93,61.29,63.11,64.47,66.28,68.10,69.92,72.19,74.46}    ' data points
Dim As Double S(N), T(N)        ' linear system coefficient
Dim As Double A(M, Q)           ' sistem to be solved
Dim As Integer i, k, j, fila, columna
Dim as Double z

For k = 0 To 2*M
    S(k) = 0 : T(k) = 0
    For i = 0 To N
        S(k) += X(i) ^ k
        If k <= M Then T(k) += Y(i) * X(i) ^ k
    Next i
Next k

' build linear system
For fila = 0 To M
    For columna = 0 To M
        A(fila, columna) = S(fila+columna)
    Next columna
    A(fila, columna) = T(fila)
Next fila

Print "Linear system coefficents:"
For i = 0 To M
    For j = 0 To M+1
        Print Using "######.#"; A(i,j);
    Next j
    Print
Next i

For j = 0 To M
    For i = j To M
        If A(i,j) <> 0 Then Exit For
    Next i
    If i = M+1 Then
        Print !"\nSINGULAR MATRIX '"
        Sleep: End
    End If
    For k = 0 To M+1
        Swap A(j,k), A(i,k)
    Next k
    z = 1 / A(j,j)
    For k = 0 To M+1
        A(j,k) = z * A(j,k)
    Next k
    For i = 0 To M
        If i <> j Then
            z = -A(i,j)
            For k = 0 To M+1
                A(i,k) += z * A(j,k)
            Next k
        End If
    Next i
Next j

Print !"\nSolutions:"
For i = 0 To M
    Print Using "  #####.#######"; A(i,M+1);
Next i

Sleep

  

You may also check:How to resolve the algorithm Currency step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Ordered words step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Riordan numbers step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Doubly-linked list/Traversal step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Read a configuration file step by step in the FreeBASIC programming language