Multivariable Module

Multivariable handles multi dimensional data where a vector of independent x values should be used to predict a single y value.

Functions and values

Function or value Description

fit xData yData

Full Usage: fit xData yData

Parameters:
    xData : Matrix<float> - matrix of x vectors
    yData : Vector<float> - vector of y values

Returns: Coefficients vector of linear coefficients

Calculates the coefficients for a straight line fitting the data. Linear regression minimizes the sum of squared residuals.

xData : Matrix<float>

matrix of x vectors

yData : Vector<float>

vector of y values

Returns: Coefficients

vector of linear coefficients

Example

 
   let xVectorMulti =
       [
       [1.; 1. ;2.  ]
       [2.; 0.5;6.  ]
       [3.; 0.8;10. ]
       [4.; 2. ;14. ]
       [5.; 4. ;18. ]
       [6.; 3. ;22. ]
       ]
       |> Matrix.ofJaggedSeq
   
   // Here the x values are transformed. In most cases the y values are just provided.
   let yVectorMulti = 
       let transformX (x) =
           x
           |> Matrix.mapiRows (fun _ v -> 100. + (v.[0] * 2.5) + (v.[1] * 4.) + (v.[2] * 0.5))
       xVectorMulti
       |> transformX
       |> vector
   
   let coefficientsMV = 
       Multivariable.fit xVectorMulti yVectorMulti

fitCholesky xData yData

Full Usage: fitCholesky xData yData

Parameters:
    xData : Matrix<float> - matrix of x vectors
    yData : Vector<float> - vector of y values

Returns: Coefficients vector of linear coefficients

Calculates the coefficients for a straight line fitting the data using Cholesky Decomposition. Linear regression minimizes the sum of squared residuals.

xData : Matrix<float>

matrix of x vectors

yData : Vector<float>

vector of y values

Returns: Coefficients

vector of linear coefficients

Example

 
   let xVectorMulti =
       [
       [1.; 1. ;2.  ]
       [2.; 0.5;6.  ]
       [3.; 0.8;10. ]
       [4.; 2. ;14. ]
       [5.; 4. ;18. ]
       [6.; 3. ;22. ]
       ]
       |> Matrix.ofJaggedSeq
   
   // Here the x values are transformed. In most cases the y values are just provided.
   let yVectorMulti = 
       let transformX (x) =
           x
           |> Matrix.mapiRows (fun _ v -> 100. + (v.[0] * 2.5) + (v.[1] * 4.) + (v.[2] * 0.5))
       xVectorMulti
       |> transformX
       |> vector
   
   let coefficientsMV = 
       Multivariable.fitCholesky xVectorMulti yVectorMulti

predict coef x

Full Usage: predict coef x

Parameters:
Returns: float predicted y value with given coefficients at X=x

Takes linear coefficients and x vector to predict the corresponding y value.

coef : Coefficients

Coefficients from linear regression.

x : Vector<float>
Returns: float

predicted y value with given coefficients at X=x

Example

 
   let xVectorMulti =
       [
       [1.; 1. ;2.  ]
       [2.; 0.5;6.  ]
       [3.; 0.8;10. ]
       [4.; 2. ;14. ]
       [5.; 4. ;18. ]
       [6.; 3. ;22. ]
       ]
       |> Matrix.ofJaggedSeq
   
   // Here the x values are transformed. In most cases the y values are just provided.
   let yVectorMulti = 
       let transformX (x) =
           x
           |> Matrix.mapiRows (fun _ v -> 100. + (v.[0] * 2.5) + (v.[1] * 4.) + (v.[2] * 0.5))
       xVectorMulti
       |> transformX
       |> vector
   
   let coefficientsMV = 
       Multivariable.fit xVectorMulti yVectorMulti

   let fittingFunctionMV x = 
       Multivariable.predict coefficientsMV x