fslaborg / FSharp.Stats

statistical testing, linear algebra, machine learning, fitting and signal processing in F#
https://fslab.org/FSharp.Stats/
Other
205 stars 54 forks source link

[Feature Request] Remove restriction of matrix nullspace calculation #194

Closed bvenn closed 2 years ago

bvenn commented 2 years ago

Is your feature request related to a problem? Please describe. As pointed out in the fslab discord channel there is no requirement for Matrix.nullspace to be guarded by the HasService query. The SVD works even if no service is available.

Describe the solution you'd like The condition could simply be removed. The results may be different for different SVD implementations. Unit tests should be added to ensure the validity of the nullspace result.

Additional context The matrix multiplied with its nullspace must result in zeros. It's up to discussion what the threshold for the singular value filtering should be (currently it is set to 1E-05).

let m = 
    [|
        [|1.;1.;2.|]
        [|3.;2.;1.;|]
    |]
    |> matrix 

let ns = Algebra.LinearAlgebra.nullspace m
m * ns 
val it: Matrix<float> = matrix [[-3.330669074e-16]
                                [-3.885780586e-16]]
bvenn commented 2 years ago

After some discussion on the fslab Discord I'm going to add a accuracy parameter so the user can define the threshold manually.

pkg threshold
numpy eps
r rankMatrix max(dim(A)) * eps