Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash if the Jacobian is singular #238

Open
mbaz opened this issue Jun 29, 2023 · 0 comments
Open

Crash if the Jacobian is singular #238

mbaz opened this issue Jun 29, 2023 · 0 comments

Comments

@mbaz
Copy link

mbaz commented Jun 29, 2023

I am running curve_fit on a large number of sinusoidal signals with random parameters. Sometimes this causes curve_fit to crash:

ERROR: LinearAlgebra.SingularException(4)
Stacktrace:
  [1] checknonsingular
    @ ~/bin/julia-1.9.0/share/julia/stdlib/v1.9/LinearAlgebra/src/factorization.jl:19 [inlined]
  [2] checknonsingular
    @ ~/bin/julia-1.9.0/share/julia/stdlib/v1.9/LinearAlgebra/src/factorization.jl:22 [inlined]
  [3] #lu!#170
    @ ~/bin/julia-1.9.0/share/julia/stdlib/v1.9/LinearAlgebra/src/lu.jl:82 [inlined]
  [4] lu!
    @ ~/bin/julia-1.9.0/share/julia/stdlib/v1.9/LinearAlgebra/src/lu.jl:80 [inlined]
  [5] #lu#176
    @ ~/bin/julia-1.9.0/share/julia/stdlib/v1.9/LinearAlgebra/src/lu.jl:299 [inlined]
  [6] lu (repeats 2 times)
    @ ~/bin/julia-1.9.0/share/julia/stdlib/v1.9/LinearAlgebra/src/lu.jl:298 [inlined]
  [7] \(A::Matrix{Float64}, B::Vector{Float64})
    @ LinearAlgebra ~/bin/julia-1.9.0/share/julia/stdlib/v1.9/LinearAlgebra/src/generic.jl:1115
  [8] levenberg_marquardt(df::NLSolversBase.OnceDifferentiable{Vector{Float64}, Matrix{Float64}, Vector{Float64}}, initial_x::Vector{Float64}; x_tol::Float64, g_tol::Float64, maxIter::Int64, lambda::Float64, tau::Float64, lambda_increase::Float64, lambda_decrease::Float64, min_step_quality::Float64, good_step_quality::Float64, show_trace::Bool, lower::Vector{Float64}, upper::Vector{Float64}, avv!::Nothing)
    @ LsqFit ~/.julia/packages/LsqFit/BBrNp/src/levenberg_marquardt.jl:126

Line 126 of levenberg_marquardt.jl is

v .= JJ \ n_buffer

The crash comes from Julia's LinearAlgebra library:

julia> [1 1; 2 2] \ [1,2]
ERROR: SingularException(2)
Stacktrace:
 [1] checknonsingular
   @ ~/bin/julia-1.9.0/share/julia/stdlib/v1.9/LinearAlgebra/src/factorization.jl:19 [inlined]

My question is if it would be a good idea for curve_fit to identify the problem and report a convergence failure instead of crashing.

Here's a (contrived) MWE:

@. model(t, p) = p[2] + p[3]*sin(2π*p[1]*t) + p[4]*cos(2π*p[1]*t)
x = 0.0:0.01:0.99
y = [-1.292216825464793, -1.2952938565190915, -1.2551966338159637, -1.2594439105244135, -1.3076054335159406, -1.3149351825115647, -1.312209749725464, -1.3269535160643398, -1.2984183842790722, -1.303705622851796, -1.3240442209719487, -1.3185467134476756, -1.2920091086995027, -1.3508679319812065, -1.3138831361533125, -1.3288156603748804, -1.3374467671365593, -1.3214155571007546, -1.3345566676824927, -1.3099685384899566, -1.3383345778026294, -1.3424184979594376, -1.3659379865515553, -1.3666092119332536, -1.3546244224167894, -1.3461938039663555, -1.3744705639413841, -1.3558035734301572, -1.366282907440218, -1.384523372856264, -1.3547780867475971, -1.3534927204414966, -1.3784457260898326, -1.3849878335512766, -1.3705499109266617, -1.4039611248225348, -1.3638808575182875, -1.3996096373182176, -1.3695277942522208, -1.3956773456560045, -1.3944176582906345, -1.3888745957965418, -1.4019836833612387, -1.3807867183801328, -1.3670239495026903, -1.368947317022084, -1.4031411232441346, -1.395581690288087, -1.4120342024349177, -1.4041289343717054,-1.3839689705528686, -1.4002894263556378, -1.4184162622846428, -1.405149489054179, -1.4114871418858892, -1.4222275035322312, -1.4256980150295218, -1.4075697904186666, -1.3991503262399105, -1.3897641047882807, -1.4114617303708386, -1.392394110993734, -1.4110963331027842, -1.4015406700955888, -1.4385967619129176, -1.424500270734283, -1.3883848675018182, -1.4163251768143987, -1.412981869672577, -1.3994066129962857, -1.3902008709251659, -1.4099019279995255, -1.3989053027341685, -1.389705906745122, -1.3989551114198944, -1.4303485320691924, -1.4043753587199719, -1.4379813803381887, -1.39796758362759, -1.4140632282920367, -1.4173650734536793, -1.396226753254768, -1.4314186046971933, -1.4160899972739944, -1.4299401762694293, -1.4033402272213507, -1.406893342242255, -1.4029985469222355, -1.4063905371082013, -1.4066009638861554, -1.4246921390814296, -1.3931986016522087, -1.387359465544153, -1.4287405658403294, -1.4137967618095058, -1.3894842928027942, -1.4131847625397986, -1.4126843863647254, -1.4152401545011943, -1.3952122566090701]
curve_fit(model, x, y, [0.5, 1798.4487118441707, -21.805084064105603, -1799.7304824749904])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant