Task

Given a set of data vectors in the following format:

   
  
    
      
        y
        =
        {
        
          y
          
            1
          
        
        ,
        
          y
          
            2
          
        
        ,
        .
        .
        .
        ,
        
          y
          
            n
          
        
        }
        
      
    
    {\displaystyle y=\{y_{1},y_{2},...,y_{n}\}\,}
  


   
  
    
      
        
          X
          
            i
          
        
        =
        {
        
          x
          
            i
            1
          
        
        ,
        
          x
          
            i
            2
          
        
        ,
        .
        .
        .
        ,
        
          x
          
            i
            n
          
        
        }
        ,
        i
        ∈
        1..
        k
        
      
    
    {\displaystyle X_{i}=\{x_{i1},x_{i2},...,x_{in}\},i\in 1..k\,}
  


Compute the vector 



β
=
{

β

1


,

β

2


,
.
.
.
,

β

k


}


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

 using ordinary least squares regression using the following equation:

   
  
    
      
        
          y
          
            j
          
        
        =
        
          Σ
          
            i
          
        
        
          β
          
            i
          
        
        ⋅
        
          x
          
            i
            j
          
        
        ,
        j
        ∈
        1..
        n
      
    
    {\displaystyle y_{j}=\Sigma _{i}\beta _{i}\cdot x_{ij},j\in 1..n}
  


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).




with javascript_semantics
constant N = 15, M=3
sequence x = {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},
         y = {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},
         s = repeat(0,N),
         t = repeat(0,N),
         a = repeat(repeat(0,M+1),M)
 
for k=1 to 2*M do
    for i=1 to N do
        s[k] += power(x[i],k-1)
        if k<=M then t[k] += y[i]*power(x[i],k-1) end if
    end for
end for

-- build linear system

for row=1 to M do
    for col=1 to M do
        a[row,col] = s[row+col-1]
    end for
    a[row,M+1] = t[row]
end for

puts(1,"Linear system coefficents:\n")
pp(a,{pp_Nest,1,pp_IntFmt,"%7.1f",pp_FltFmt,"%7.1f"})

for j=1 to M do
    integer i = j
    while a[i,j]=0 do i += 1 end while
    if i=M+1 then
        ?"SINGULAR MATRIX !"
        ?9/0
    end if
    for k=1 to M+1 do
        {a[j,k],a[i,k]} = {a[i,k],a[j,k]}
    end for
    atom Y = 1/a[j,j]
    a[j] = sq_mul(a[j],Y)
    for k=1 to M do
        if k<>j then
            Y=-a[k,j]
            for m=1 to M+1 do
                a[k,m] += Y*a[j,m]
            end for
        end if
    end for
end for

puts(1,"Solutions:\n")
?columnize(a,M+1)[1]
