简体   繁体   English

使用SCIPY.OPTIMIZE.FMIN_CG提取Weibull分布参数

[英]Using SCIPY.OPTIMIZE.FMIN_CG to extract Weibull distribution parameters

I am attempting to extract Weibull distribution parameters (shape 'k' and scale 'lambda') that satisfy a certain mean and variance. 我试图提取满足一定均值和方差的Weibull分布参数(形状'k'和比例'lambda')。 In this example, the mean is 4 and the variance is 8. It is a 2-unknowns and 2-equations type of problem. 在这个例子中,均值是4,方差是8.它是一个2-unknowns和2-equation类型的问题。

Since this algorithm works with Excel 2010's GRG Solver, I am certain it is about the way I am framing the problem, or potentially, the libraries I am using. 由于此算法适用于Excel 2010的GRG求解器,因此我确信它是关于我构建问题的方式,或者可能是我正在使用的库。 I am not overly familiar with optimization libraries, so please let me know where the error is. 我对优化库并不太熟悉,所以请告诉我错误的位置。

Below is the script: 以下是脚本:

from scipy.optimize import fmin_cg
import math

def weibull_mu(k, lmda):                  #Formula can be found on wikipedia
    return lmda*math.gamma(1+1/k)
def weibull_var(k, lmda):                 #Formula can be found on wikipedia
    return lmda**2*math.gamma(1+2/k)-weibull_mu(k, lmda)**2

def min_function(arggs):
    actual_mean = 4                          # specific to this example
    actual_var = 8                           # specific to this example
    k = arggs[0]
    lmda = arggs[1]
    output = [weibull_mu(k, lmda)-(var_wei)]
    output.append(weibull_var(k, lmda)-(actual_var)**2-(actual_mean)**2)
    return output

print fmin(min_function, [1,1])

This script gives me the following error: 这个脚本给我以下错误:

[...]
  File "C:\Program Files\Python27\lib\site-packages\scipy\optimize\optimize.py", line 278, in fmin
    fsim[0] = func(x0)
ValueError: setting an array element with a sequence.

As far as I can tell, min_function returns a multi-dimensional list, but fmin and fmin_cg does expect that the objective function returns a scalar, if I am not mistaken. 据我所知, min_function返回一个多维列表,但fminfmin_cg确实期望目标函数返回一个标量,如果我没有弄错的话。

If you are searching the root of the two-equations problem, I suppose it is better that you apply the root function instead. 如果您正在搜索双方程问题的根,我认为最好应用函数。 As far as I have been able to find out, scipy does not provide any general optimizers for vector functions. 据我所知scipy没有为矢量函数提供任何通用的优化器。

I managed to get it to work thanks to Anders Gustafsson's comment (thank you). 由于Anders Gustafsson的评论(谢谢),我设法让它工作。 This script now works if one returns only a scalar (in this case I used something along the lines of least-squares). 如果只返回一个标量,这个脚本现在可以工作(在这种情况下,我使用的是最小二乘方式的东西)。 Also, bounds were added by changing the optimization function to "fmin_l_bfgs_b" (again, thanks to Anders Gustafsson). 此外,通过将优化函数更改为“fmin_l_bfgs_b”来添加边界(再次,感谢Anders Gustafsson)。

I only changed the min_function definition relative to the question. 我只改变了相对于问题的min_function定义。

from scipy.optimize import fmin_l_bfgs_b
import math

def weibull_mu(k, lmda):
    return lmda*math.gamma(1+1/k)
def weibull_var(k, lmda):
    return lmda**2*math.gamma(1+2/k)-weibull_mu(k, lmda)**2

def min_function(arggs):
    actual_mean = 4.                    # specific to this example
    actual_var = 8.                     # specific to this example
    k = arggs[0]
    lmda = arggs[1]
    extracted_var = weibull_var(k, lmda)
    extracted_mean = weibull_mu(k, lmda)
    output = (extracted_var - actual_var)**2 + (extracted_mean - actual_mean)**2
    return output

print fmin_l_bfgs_b(min_function, best_guess, approx_grad = True, bounds = [(.0000001,None),(.0000001,None)], disp = False)

Note: Please feel free to use this script for your own or professional use. 注意:请随意使用此脚本供您自己或专业人士使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM