gpt4 book ai didi

optimization - 优化错误: Box constraint optimization (Julia Optim.jl)

转载 作者:行者123 更新时间:2023-12-03 09:06:49 31 4
gpt4 key购买 nike

我正在尝试运行以下代码片段,以使曲线适合一些经验数据,但在Julia Optim.jl包中,optimize()方法一直存在问题。我正在使用Julia v1.1.0,并安装了所有正确的软件包。我不断收到的错误是:

ERROR: LoadError: MethodError: no method matching optimize(::getfield(Main, Symbol("##13#14")), ::Array{Float64,1}, ::Array{Int32,1}, ::Array{Float64,1}, ::Fminbox{LBFGS{Nothing,LineSearches.InitialStatic{Float64},LineSearches.HagerZhang{Float64,Base.RefValue{Bool}},getfield(Optim, Symbol("##19#21"))},Float64,getfield(Optim, Symbol("##43#45"))})

这是我的代码:
# Loading in dependencies
using Distributions # To use probability & statistics library
using Plots # To visualize results
using Optim # For minimization (curve fitting)

# Empirical data for curve fitting
IM = [1, 2, 3, 4] # x axis variables
pfs = [0.0, 0.0, 0.13, 0.23] # associated probabilities y-axis
n = 1000 # assume this number of independent trials for each x value

# Create functions to evaluate fit between theoretical values and empirical values
theor_vals = x -> cdf.(LogNormal(log(x[1]), x[2]), IM) # Assume lognormal shape and construct CDF with arbitrary fit parameters
likelihood = x -> [pdf(Binomial(n,xx[1]), round(xx[2])) for xx in zip(theor_vals(x),n.*pfs)] # getting likelihood values from binomial distribution for n trials
log_likelihood = x -> log.([xi > 0 ? xi : 1e-30 for xi in likelihood(x)]) # getting log value of likelihood
min_function = x -> -sum(log_likelihood(x)) # summing and switching sign for optimization


# Set inputs for minimization - first index is for the median and second index is for the dispersion (uncertainty)
init_guess = [median(IM), 0.5] # reasonable initial guess
lx = [0.001, 5.0] # lower bound
ux = [5,10] # upper bound

# Using Optim to optimize the objective function and get best curve fit
result = optimize(min_function, lx, ux, init_guess, Fminbox(LBFGS())) # call optimize function
theta, beta_a = result.minimizer # retrieve lognormal fit params

我仍然很熟悉julia语言,因此很可能我只是没有正确地理解文档。在此先感谢您提供的任何帮助或指导!

最佳答案

您必须修复代码中的两件事,所有东西都可以工作:

  • ux必须包含浮点数,因此您应将其定义更改为ux = [5.0,10.0]
  • init_guess必须在优化范围内,这样您就可以将其设置为init_guess = (lx+ux)/2

  • 有了这些更改,您就可以运行代码。这是我得到的结果(我没有从优化规范方面检查过您的问题-我认为这是正确的;我只是提出了使您的示例可运行的更改):
    julia> result = optimize(min_function, lx, ux, init_guess, Fminbox(LBFGS()))
    * Status: success

    * Candidate solution
    Minimizer: [5.00e+00, 5.00e+00]
    Minimum: 1.417223e+03

    * Found with
    Algorithm: Fminbox with L-BFGS
    Initial Point: [2.50e+00, 7.50e+00]

    * Convergence measures
    |x - x'| = 8.88e-16 ≰ 0.0e+00
    |x - x'|/|x'| = 1.26e-16 ≰ 0.0e+00
    |f(x) - f(x')| = 0.00e+00 ≤ 0.0e+00
    |f(x) - f(x')|/|f(x')| = 0.00e+00 ≤ 0.0e+00
    |g(x)| = 8.87e+01 ≰ 1.0e-08

    * Work counters
    Seconds run: 0 (vs limit Inf)
    Iterations: 6
    f(x) calls: 2571
    ∇f(x) calls: 2571

    关于optimization - 优化错误: Box constraint optimization (Julia Optim.jl),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59537399/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com