Skip to content

Commit

Permalink
add constrained simple linear regression
Browse files Browse the repository at this point in the history
  • Loading branch information
bvenn committed Apr 25, 2020
1 parent 7865755 commit 16b9628
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/FSharp.Stats/Fitting/LinearRegression.fs
Expand Up @@ -63,7 +63,7 @@ module LinearRegression =
let fit (coef: float) (x:float) =
coef * x

module Univariable =
module Univariable =
/// Calculates the coefficients for linear regression
/// in the form of [|intercept; slope;|]
let coefficient (x_data : Vector<float>) (y_data : Vector<float>) =
Expand All @@ -72,7 +72,14 @@ module LinearRegression =
let N = x_data.Length
let X = Matrix.init N 2 (fun m x -> if x = 0 then 1. else x_data.[m] )
Algebra.LinearAlgebra.LeastSquares X y_data


/// Calculates the coefficients for linear regression through a specified point (xC,yC)
let coefficientConstrained (x_data : Vector<float>) (y_data : Vector<float>) ((xC,yC): float*float) =
let x_transformed = x_data |> Vector.map (fun x -> x - xC)
let y_transformed = y_data |> Vector.map (fun y -> y - yC)
let slope = RTO.coefficientOfVector x_transformed y_transformed
[|- xC * slope - yC;slope|]

/// Fit to x
let fit (coef : Vector<float>) (x:float) =
if coef.Length <> 2 then
Expand Down

0 comments on commit 16b9628

Please sign in to comment.