Skip to content

OptimizationLBFGSB.LBFGSB() produces wrong results with retcode: Success #1094

@pmc4

Description

@pmc4

Describe the bug 🐞

Hi everyone. I've run into this runtime error that worries me a bit. I was minimizing a typical chi squared function using OptimizationLBFGSB.LBFGSB(). The optimization returns success but the result is wrong.

I've discovered this behaviour with the last 4.x version of Optimization.jl using Optimization.LBFGS(), but here I provide a MWE for version 5.1.0 that reproduces the bug.

Image

As you can see on the image, the minimum is around -0.05, but when I call the method LBFGSB() from OptimizationLBFGSB package,

sol = solve(prob, OptimizationLBFGSB.LBFGSB())

I get

retcode: Success
u: 1-element Vector{Float64}:
 0.0

which is a success retcode with a minimum around 0.0. Looking at the figure, that's obviously wrong. There are no warnings whatsoever.

If I use Optim's LBFGS() method instead, everything works:

sol = solve(prob, Optim.LBFGS())

returns

retcode: Success
u: 1-element Vector{Float64}:
 -0.051422488572892285

which is the correct value.

Expected behavior

I should be able to call OptimizationLBFGSB.LBFGSB() and get a minimum around -0.05 with a retcode: Success.

Minimal Reproducible Example 👇

I've pregenerated the data for ease of use. The function I'm minimizing is a Poissonian likelyhood function with some nuisance parameters (the ones to be minimized over).

using Optimization, OptimizationOptimJL, OptimizationLBFGSB, ForwardDiff, CairoMakie

"Experimental number of events."
const ne = [47.79016955366216, 54.63792672045489, 60.6817081483305, 65.85497617511764,
70.10163120593351, 73.38214524249031, 75.6679992088981, 76.9449555834446, 77.20918553594912,
76.46915655141063, 74.74232402982297, 72.05645354279015, 68.44666004402201, 63.956275455187374,
58.63429503572871, 52.53457348342481, 45.677188154035534, 38.7314886941981, 34.19260165003558,
30.784750516477747, 27.387995197108484, 24.00145774130718, 20.687966069161696, 17.637911910103306,
14.93245414792653, 12.4489653347761, 10.181537719538321, 8.148303027435635, 6.361941181550973,
4.829627414524656, 3.552527822049159, 2.5264917405123013, 1.7421104640301057, 1.1877485347281902,
0.8467200821260764, 0.6560958454993128]

"Theoretical number of events."
const nt = [49.00928880337846, 56.09394530248748, 62.37909255858915, 67.79690028474204,
72.28975301404762, 75.8162261685694, 78.3457614258788, 79.86188067534194, 80.35848905789322,
79.84174847321458, 78.32689914964128, 75.83959322777797, 72.41302839568749, 68.0888342687985,
62.914584567966486, 56.941011087706656, 50.01050515306522, 41.89887406295331, 36.503137537870884,
32.96797889924509, 29.466821579372933, 25.959186733715818, 22.509873814723917, 19.311591400637997,
16.448043988519792, 13.799326941778979, 11.362380032036715, 9.158257568882474, 7.202570686822948,
5.505387789445479, 4.070660173093367, 2.8968757912937115, 1.9770626122012342, 1.3024644039013222,
0.8738309918479059, 0.6573023347540636]

function chi2(alpha, n_exp, n_th)
    total_chi = 0.0
    # Add nuisance parameters
    n_th = (1 + alpha[1]) * n_th

    total_chi = 0.0
    for i in eachindex(n_exp)
        if n_exp[i] == 0.0
            total_chi += 2 * n_th[i]
        else
            total_chi += 2 * (n_th[i] - n_exp[i] + n_exp[i] * log(n_exp[i] / n_th[i]))
        end
    end

    return total_chi
end


function main()
    f(u, p) = chi2(u, ne, nt)

    u0 = [0.0,]
    lb = [-1.0,]
    ub = [1.0,]

    optf = OptimizationFunction(f, AutoForwardDiff())
    prob = OptimizationProblem(optf, u0; lb = lb, ub = ub)
    sol = solve(prob, OptimizationLBFGSB.LBFGSB())

    return sol
end

# Returns retcode: Success with minimum at 0.0, wrong result
sol = main()
sol.objective

Next we call our main function with Optim.LBFGS():

function main2()
    f(u, p) = chi2(u, ne, nt)

    u0 = [0.0,]
    lb = [-1.0,]
    ub = [1.0,]

    optf = OptimizationFunction(f, AutoForwardDiff())
    prob = OptimizationProblem(optf, u0; lb = lb, ub = ub)
    sol = solve(prob, Optim.LBFGS())

    return sol
end

# Returns retcode: Success with minimum at around -0.05, correct result
sol2 = main2()
sol2.objective

Error & Stacktrace ⚠️

There is no error nor stacktrace, but a runtime error.

Environment (please complete the following information):

  • Output of using Pkg; Pkg.status()
Status `~/mwe/Project.toml`
  [13f3f980] CairoMakie v0.15.6
  [f6369f11] ForwardDiff v1.3.0
  [7f7a1694] Optimization v5.1.0
  [22f7324a] OptimizationLBFGSB v1.2.1
  [36348300] OptimizationOptimJL v0.4.8
  • Output of using Pkg; Pkg.status(; mode = PKGMODE_MANIFEST)
Status `~/mwe/Manifest.toml`
  [47edcb42] ADTypes v1.19.0
  [621f4979] AbstractFFTs v1.5.0
  [1520ce14] AbstractTrees v0.4.5
  [7d9f7c33] Accessors v0.1.42
  [79e6a3ab] Adapt v4.4.0
  [35492f91] AdaptivePredicates v1.2.0
  [66dad0bd] AliasTables v1.1.3
  [27a7e980] Animations v0.4.2
  [4fba245c] ArrayInterface v7.22.0
  [67c07d97] Automa v1.1.0
  [13072b0f] AxisAlgorithms v1.1.0
  [39de3d68] AxisArrays v0.4.8
  [18cc8868] BaseDirs v1.3.2
  [fa961155] CEnum v0.5.0
  [96374032] CRlibm v1.0.2
  [159f3aea] Cairo v1.1.1
  [13f3f980] CairoMakie v0.15.6
  [d360d2e6] ChainRulesCore v1.26.0
  [a2cac450] ColorBrewer v0.4.2
  [35d6a980] ColorSchemes v3.31.0
  [3da002f7] ColorTypes v0.12.1
  [c3611d14] ColorVectorSpace v0.11.0
  [5ae59095] Colors v0.13.1
  [38540f10] CommonSolve v0.2.4
  [bbf7d656] CommonSubexpressions v0.3.1
  [34da2185] Compat v4.18.1
  [a33af91c] CompositionsBase v0.1.2
  [95dc2771] ComputePipeline v0.1.4
  [88cd18e8] ConsoleProgressMonitor v0.1.2
  [187b0558] ConstructionBase v1.6.0
  [d38c429a] Contour v0.6.3
  [9a962f9c] DataAPI v1.16.0
  [864edb3b] DataStructures v0.19.3
  [e2d170a0] DataValueInterfaces v1.0.0
  [927a84f5] DelaunayTriangulation v1.6.5
  [163ba53b] DiffResults v1.1.0
  [b552c78f] DiffRules v1.15.1
  [a0c0ee7d] DifferentiationInterface v0.7.12
  [31c24e10] Distributions v0.25.122
  [ffbed154] DocStringExtensions v0.9.5
  [4e289a0a] EnumX v1.0.5
  [429591f6] ExactPredicates v2.2.9
  [e2ba6199] ExprTools v0.1.10
  [55351af7] ExproniconLite v0.10.14
  [411431e0] Extents v0.1.6
  [7a1cc6ca] FFTW v1.10.0
  [9aa1b823] FastClosures v0.3.2
  [5789e2e9] FileIO v1.17.1
⌅ [8fc22ac5] FilePaths v0.8.3
  [48062228] FilePathsBase v0.9.24
  [1a297f60] FillArrays v1.15.0
  [6a86dc24] FiniteDiff v2.29.0
  [53c48c17] FixedPointNumbers v0.8.5
  [1fa38f19] Format v1.3.7
  [f6369f11] ForwardDiff v1.3.0
  [b38be410] FreeType v4.1.1
  [663a7486] FreeTypeAbstraction v0.10.8
  [069b7b12] FunctionWrappers v1.1.3
  [77dc65aa] FunctionWrappersWrappers v0.1.3
  [46192b85] GPUArraysCore v0.2.0
  [5c1252a2] GeometryBasics v0.5.10
  [a2bd30eb] Graphics v1.1.3
  [3955a311] GridLayoutBase v0.11.2
  [42e2da0e] Grisu v1.0.2
  [34004b35] HypergeometricFunctions v0.3.28
  [2803e5a7] ImageAxes v0.6.12
  [c817782e] ImageBase v0.1.7
  [a09fc81d] ImageCore v0.10.5
  [82e4d734] ImageIO v0.6.9
  [bc367c6b] ImageMetadata v0.9.10
  [9b13fd28] IndirectArrays v1.0.0
  [d25df0c9] Inflate v0.1.5
  [a98d9a8b] Interpolations v0.16.2
  [d1acc4aa] IntervalArithmetic v1.0.2
  [8197267c] IntervalSets v0.7.12
  [3587e190] InverseFunctions v0.1.17
  [92d709cd] IrrationalConstants v0.2.6
  [f1662d9f] Isoband v0.1.1
  [c8e1da08] IterTools v1.10.0
  [82899510] IteratorInterfaceExtensions v1.0.0
  [692b3bcd] JLLWrappers v1.7.1
  [682c06a0] JSON v1.3.0
  [ae98c720] Jieko v0.2.1
  [b835a17e] JpegTurbo v0.1.6
  [5ab0869b] KernelDensity v0.6.10
  [5be7bae1] LBFGSB v0.4.1
  [b964fa9f] LaTeXStrings v1.4.0
  [8cdb02fc] LazyModules v0.3.1
  [1d6d02ad] LeftChildRightSiblingTrees v0.2.1
  [d3d80556] LineSearches v7.4.0
  [2ab3a3ac] LogExpFunctions v0.3.29
  [e6f89c97] LoggingExtras v1.2.0
  [1914dd2f] MacroTools v0.5.16
  [ee78f7c6] Makie v0.24.6
  [dbb5928d] MappedArrays v0.4.2
  [0a4f8689] MathTeXEngine v0.6.7
  [e1d29d7a] Missings v1.2.0
  [e94cdb99] MosaicViews v0.3.4
  [2e0e35c7] Moshi v0.3.7
  [d41bc354] NLSolversBase v7.10.0
  [77ba4419] NaNMath v1.1.3
  [f09324ee] Netpbm v1.1.1
  [510215fc] Observables v0.5.5
  [6fe1bfb0] OffsetArrays v1.17.0
  [52e1d378] OpenEXR v0.3.3
  [429524aa] Optim v1.13.2
  [7f7a1694] Optimization v5.1.0
  [bca83a33] OptimizationBase v4.0.2
  [22f7324a] OptimizationLBFGSB v1.2.1
  [36348300] OptimizationOptimJL v0.4.8
  [bac558e1] OrderedCollections v1.8.1
  [90014a1f] PDMats v0.11.36
  [f57f5aa1] PNGFiles v0.4.4
  [19eb6ba3] Packing v0.5.1
  [5432bcbf] PaddedViews v0.5.12
  [d96e819e] Parameters v0.12.3
  [69de0a69] Parsers v2.8.3
  [eebad327] PkgVersion v0.3.3
  [995b91a9] PlotUtils v1.4.4
  [647866c9] PolygonOps v0.1.2
  [85a6dd25] PositiveFactorizations v0.2.4
  [d236fae5] PreallocationTools v0.4.34
  [aea7be01] PrecompileTools v1.3.3
  [21216c6a] Preferences v1.5.0
  [33c8b6b6] ProgressLogging v0.1.5
  [92933f4c] ProgressMeter v1.11.0
  [43287f4e] PtrArrays v1.3.0
  [4b34888f] QOI v1.0.1
  [1fd47b50] QuadGK v2.11.2
  [b3c3ace0] RangeArrays v0.3.2
  [c84ed2f1] Ratios v0.4.5
  [3cdcf5f2] RecipesBase v1.3.4
  [731186ca] RecursiveArrayTools v3.39.0
  [189a3867] Reexport v1.2.2
  [05181044] RelocatableFolders v1.0.1
  [ae029012] Requires v1.3.1
  [79098fc4] Rmath v0.9.0
  [5eaf0fd0] RoundingEmulator v0.2.1
  [7e49a35a] RuntimeGeneratedFunctions v0.5.16
  [fdea26ae] SIMD v3.7.2
  [0bca4576] SciMLBase v2.127.0
  [a6db7da4] SciMLLogging v1.5.0
  [c0aeaf25] SciMLOperators v1.11.0
  [431bcebd] SciMLPublic v1.0.0
  [53ae85a6] SciMLStructures v1.7.0
  [6c6a2e73] Scratch v1.3.0
  [efcf1570] Setfield v1.1.2
  [65257c39] ShaderAbstractions v0.5.0
  [992d4aef] Showoff v1.0.3
  [73760f76] SignedDistanceFields v0.4.0
  [699a6c99] SimpleTraits v0.9.5
  [45858cf5] Sixel v0.1.5
  [a2af1166] SortingAlgorithms v1.2.2
  [9f842d2f] SparseConnectivityTracer v1.1.3
  [0a514795] SparseMatrixColorings v0.4.23
  [276daf66] SpecialFunctions v2.6.1
  [860ef19b] StableRNGs v1.0.4
  [cae243ae] StackViews v0.1.2
  [90137ffa] StaticArrays v1.9.15
  [1e83bf80] StaticArraysCore v1.4.4
  [10745b16] Statistics v1.11.1
  [82ae8749] StatsAPI v1.7.1
  [2913bbd2] StatsBase v0.34.8
  [4c63d2b9] StatsFuns v1.5.2
  [09ab397b] StructArrays v0.7.2
  [ec057cc2] StructUtils v2.6.0
  [2efcf032] SymbolicIndexingInterface v0.3.46
  [3783bdb8] TableTraits v1.0.1
  [bd369af6] Tables v1.12.1
  [62fd8b95] TensorCore v0.1.1
  [5d786b92] TerminalLoggers v0.1.7
  [731e570b] TiffImages v0.11.6
  [3bb67fe8] TranscodingStreams v0.11.3
  [981d1d27] TriplotBase v0.1.0
  [3a884ed6] UnPack v1.0.2
  [1cfade01] UnicodeFun v0.4.1
  [1986cc42] Unitful v1.25.1
  [e3aaa7dc] WebP v0.1.3
  [efce3f68] WoodburyMatrices v1.0.0
  [6e34b625] Bzip2_jll v1.0.9+0
  [4e9b3aee] CRlibm_jll v1.0.1+0
  [83423d85] Cairo_jll v1.18.5+0
  [5ae413db] EarCut_jll v2.2.4+0
  [2e619515] Expat_jll v2.7.3+0
⌅ [b22a6f82] FFMPEG_jll v6.1.3+0
  [f5851436] FFTW_jll v3.3.11+0
  [a3f928ae] Fontconfig_jll v2.17.1+0
  [d7e528f0] FreeType2_jll v2.13.4+0
  [559328eb] FriBidi_jll v1.0.17+0
  [b0724c58] GettextRuntime_jll v0.22.4+0
  [59f7168a] Giflib_jll v5.2.3+0
  [7746bdde] Glib_jll v2.86.0+0
  [3b182d85] Graphite2_jll v1.3.15+0
  [2e76f6c2] HarfBuzz_jll v8.5.1+0
  [905a6f67] Imath_jll v3.1.11+0
  [1d5cc7b8] IntelOpenMP_jll v2025.2.0+0
  [aacddb02] JpegTurbo_jll v3.1.3+0
  [c1c5ebd0] LAME_jll v3.100.3+0
  [88015f11] LERC_jll v4.0.1+0
  [1d63c593] LLVMOpenMP_jll v18.1.8+0
  [dd4b983a] LZO_jll v2.10.3+0
  [81d17ec3] L_BFGS_B_jll v3.0.1+0
  [e9f186c6] Libffi_jll v3.4.7+0
  [7e76a0d4] Libglvnd_jll v1.7.1+1
  [94ce4f54] Libiconv_jll v1.18.0+0
  [4b2f31a3] Libmount_jll v2.41.2+0
  [89763e89] Libtiff_jll v4.7.2+0
  [38a345b3] Libuuid_jll v2.41.2+0
  [856f044c] MKL_jll v2025.2.0+0
  [e7412a2a] Ogg_jll v1.3.6+0
  [6cdc7f73] OpenBLASConsistentFPCSR_jll v0.3.29+0
  [18a262bb] OpenEXR_jll v3.2.4+0
  [efe28fd5] OpenSpecFun_jll v0.5.6+0
  [91d4177d] Opus_jll v1.5.2+0
  [36c8627f] Pango_jll v1.57.0+0
⌅ [30392449] Pixman_jll v0.44.2+0
  [f50d1b31] Rmath_jll v0.5.1+0
  [ffd25f8a] XZ_jll v5.8.1+0
  [4f6342f7] Xorg_libX11_jll v1.8.12+0
  [0c0b7dd1] Xorg_libXau_jll v1.0.13+0
  [a3789734] Xorg_libXdmcp_jll v1.1.6+0
  [1082639a] Xorg_libXext_jll v1.3.7+0
  [ea2f1a96] Xorg_libXrender_jll v0.9.12+0
  [c7cfdc94] Xorg_libxcb_jll v1.17.1+0
  [c5fb5394] Xorg_xtrans_jll v1.6.0+0
  [3161d3a3] Zstd_jll v1.5.7+1
  [9a68df92] isoband_jll v0.2.3+0
  [a4ae2306] libaom_jll v3.13.1+0
  [0ac62f75] libass_jll v0.17.4+0
  [f638f0a6] libfdk_aac_jll v2.0.4+0
  [b53b4c65] libpng_jll v1.6.50+0
  [075b6546] libsixel_jll v1.10.5+0
  [f27f6e37] libvorbis_jll v1.3.8+0
  [c5f90fcd] libwebp_jll v1.6.0+0
  [1317d2d5] oneTBB_jll v2022.0.0+1
  [1270edf5] x264_jll v10164.0.1+0
  [dfaa095f] x265_jll v4.1.0+0
  [0dad84c5] ArgTools v1.1.2
  [56f22d72] Artifacts v1.11.0
  [2a0f44e3] Base64 v1.11.0
  [8bf52ea8] CRC32c v1.11.0
  [ade2ca70] Dates v1.11.0
  [8ba89e20] Distributed v1.11.0
  [f43a241f] Downloads v1.6.0
  [7b1f6079] FileWatching v1.11.0
  [9fa8497b] Future v1.11.0
  [b77e0a4c] InteractiveUtils v1.11.0
  [ac6e5ff7] JuliaSyntaxHighlighting v1.12.0
  [4af54fe1] LazyArtifacts v1.11.0
  [b27032c2] LibCURL v0.6.4
  [76f85450] LibGit2 v1.11.0
  [8f399da3] Libdl v1.11.0
  [37e2e46d] LinearAlgebra v1.12.0
  [56ddb016] Logging v1.11.0
  [d6f4376e] Markdown v1.11.0
  [a63ad114] Mmap v1.11.0
  [ca575930] NetworkOptions v1.3.0
  [44cfe95a] Pkg v1.12.0
  [de0858da] Printf v1.11.0
  [3fa0cd96] REPL v1.11.0
  [9a3f8284] Random v1.11.0
  [ea8e919c] SHA v0.7.0
  [9e88b42a] Serialization v1.11.0
  [1a1011a3] SharedArrays v1.11.0
  [6462fe0b] Sockets v1.11.0
  [2f01184e] SparseArrays v1.12.0
  [f489334b] StyledStrings v1.11.0
  [4607b0f0] SuiteSparse
  [fa267f1f] TOML v1.0.3
  [a4e569a6] Tar v1.10.0
  [8dfed614] Test v1.11.0
  [cf7118a7] UUIDs v1.11.0
  [4ec0a83e] Unicode v1.11.0
  [e66e0078] CompilerSupportLibraries_jll v1.3.0+1
  [deac9b47] LibCURL_jll v8.11.1+1
  [e37daf67] LibGit2_jll v1.9.0+0
  [29816b5a] LibSSH2_jll v1.11.3+1
  [14a3606d] MozillaCACerts_jll v2025.5.20
  [4536629a] OpenBLAS_jll v0.3.29+0
  [05823500] OpenLibm_jll v0.8.7+0
  [458c3c95] OpenSSL_jll v3.5.1+0
  [efcefdf7] PCRE2_jll v10.44.0+1
  [bea87d4a] SuiteSparse_jll v7.8.3+2
  [83775a58] Zlib_jll v1.3.1+2
  [8e850b90] libblastrampoline_jll v5.13.1+1
  [8e850ede] nghttp2_jll v1.64.0+1
  [3f19e933] p7zip_jll v17.5.0+2
Info Packages marked with ⌅ have new versions available but compatibility constraints restrict them from upgrading. To see why use `status --outdated -m`
  • Output of versioninfo()
Julia Version 1.12.0
Commit b907bd0600f (2025-10-07 15:42 UTC)
Build Info:
  Official https://julialang.org release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 20 × 12th Gen Intel(R) Core(TM) i7-12700H
  WORD_SIZE: 64
  LLVM: libLLVM-18.1.7 (ORCJIT, alderlake)
  GC: Built with stock GC
Threads: 1 default, 1 interactive, 1 GC (on 20 virtual cores)
Environment:
  JULIA_EDITOR = code
  JULIA_VSCODE_REPL = 1

Additional context

I've tried a derivative-free method like Optim.NelderMead() and get the correct result, too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions