Singular Exceptions on Windows OS

When I use LsqFit to loop some curve fitting, I come across some singular exceptions. Anyone could give me some guidance on this problem. My os is window 32. Thank you.

func(x, p) = 1 ./ (p[1]+p[2] * exp.(-p[3] * x ))

# averaged_vi: [-3.15, -2.25, -1.95, -1.65, -1.35, -1.05, -0.75, -0.45, -0.15, 0.15, 0.45, 0.75, 1.05, 1.35, 1.65, 1.95, 2.55]
#F_VarY [0.1, 0.65, 0.88, 0.883333, 1.94286, 1.28, 0.766667, 1.36071, 1.65405, 2.425, 3.23333, 3.80286, 6.23333, 8.1375, 21.9, 1.3, 24.6]

p0 = [0.01, 0.5, 0.5]
fit = LsqFit.curve_fit(func, F_averaged_vi, F_VarY, p0, lower = [0.005,realmin(Float64),realmin(Float64)],
upper = [0.05,typemax(Float64),typemax(Float64)],maxIter=1000)


 [1] A_ldiv_B! at .\linalg\lu.jl:238 [inlined]
 [2] \(::Base.LinAlg.LU{Float64,Array{Float64,2}}, ::Array{Float64,1}) at .\linalg\factorization.jl:48
 [3] \(::Array{Float64,2}, ::Array{Float64,1}) at .\linalg\generic.jl:832
 [4] macro expansion at C:\Users\wangfeng\.julia\v0.6\LsqFit\src\levenberg_marquardt.jl:111 [inlined]
 [5] macro expansion at .\simdloop.jl:73 [inlined]
 [6] #levenberg_marquardt#1(::Float64, ::Float64, ::Int32, ::Float64, ::Float64, ::Float64, ::Float64, ::Float64, ::Bool, ::Array{Float64,1}, ::Array{Float64,1}, ::LsqFit.#levenberg_marquardt, ::LsqFit.#f#5{#func,Array{Float64,1},Array{Float64,1}}, ::Calculus.#g#5{LsqFit.#f#5{#func,Array{Float64,1},Array{Float64,1}},Symbol}, ::Array{Float64,1}) at C:\Users\wangfeng\.julia\v0.6\LsqFit\src\levenberg_marquardt.jl:110
 [7] (::LsqFit.#kw##levenberg_marquardt)(::Array{Any,1}, ::LsqFit.#levenberg_marquardt, ::Function, ::Function, ::Array{Float64,1}) at .\<missing>:0
 [8] #lmfit#2(::Array{Any,1}, ::Function, ::LsqFit.#f#5{#func,Array{Float64,1},Array{Float64,1}}, ::Calculus.#g#5{LsqFit.#f#5{#func,Array{Float64,1},Array{Float64,1}},Symbol}, ::Array{Float64,1}, ::Array{Float64,1}) at C:\Users\wangfeng\.julia\v0.6\LsqFit\src\curve_fit.jl:13
 [9] (::LsqFit.#kw##lmfit)(::Array{Any,1}, ::LsqFit.#lmfit, ::Function, ::Function, ::Array{Float64,1}, ::Array{Float64,1}) at .\<missing>:0
 [10] #lmfit#3(::Array{Any,1}, ::Function, ::Function, ::Array{Float64,1}, ::Array{Float64,1}) at C:\Users\wangfeng\.julia\v0.6\LsqFit\src\curve_fit.jl:38
 [11] (::LsqFit.#kw##lmfit)(::Array{Any,1}, ::LsqFit.#lmfit, ::Function, ::Array{Float64,1}, ::Array{Float64,1}) at .\<missing>:0
 [12] #curve_fit#4(::Array{Any,1}, ::Function, ::#func, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Float64,1}) at C:\Users\wangfeng\.julia\v0.6\LsqFit\src\curve_fit.jl:45
 [13] (::LsqFit.#kw##curve_fit)(::Array{Any,1}, ::LsqFit.#curve_fit, ::Function, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Float64,1}) at .\<missing>:100:

My Julia Information
Julia Version 0.6.3
Commit d55cadc350* (2018-05-28 20:20 UTC)
Platform Info:
  OS: Windows (i686-w64-mingw32)
  CPU: Intel(R) Core(TM) i5-4300M CPU @ 2.60GHz
  BLAS: libopenblas (DYNAMIC_ARCH NO_AFFINITY Nehalem)
  LAPACK: libopenblas
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, haswell)

Most likely you are solving a system with a singular matrix.

Providing a self-contained minimal working example could get you more help, but it may be an issue with your algorithm and/or conditioning.

1 Like

Dear@Tamas_Papp, Thank you very much for your help, Is there some methods to throw this kind of exceptions in Julia? In my data analysis, I need to loop 1326 cases, in which this kind of exceptions may happen seven times, so I would like to throw them off.

Thank you again.

You are looking for exception handling, but I would recommend understanding what is going on instead.

1 Like

Thank you very much for your quick reply, It may depends on my data analysis method, I will deeply analyze the data to see how the exceptional case influence the result.
Thank you!

Maybe the LsqFit.curve_fit() has some explanations to this issues. I will check it and hope to find the solution.

As @Tamas_Papp suggested, a simple example would help. I suspect that the algorithm is trying to solve an under-identified system of equations. In a least-squares fit, this is typically because there is insufficient independent variation between two (or more) of your columns.

1 Like

Thank you for your help. In my analysis, maybe sometimes the vectors input into curve_fit(), was transformed into singular matrix. I do not fully understand how the least squares fitting was conducted.

If you look at the source of the LsqFit package, you will notice that it uses the Levenberg-Marquardt
algorithm, but for practical purposes it should not matter: the fact that your problem is (occasionally) singular should be investigated, because even when it is not caught as a singularity, it could just be ill-conditioned. Recall the folk theorem of statistical computing:

When you have computational problems, often there’s a problem with your model.

1 Like

Thank you for your great help.