【发布时间】:2020-10-02 20:00:41
【问题描述】:
我试图将输入(1 个变量)最小化为输出为概率的非线性函数。
如,如果输入为 5,则输出概率为 40%,输入 10,则概率变为 93%。计算概率的函数是确定性的。
现在我想最小化输入,使得概率大于 80% 。有没有使用 scipy 库在 python 中执行此操作的简单方法?
【问题讨论】:
标签: python optimization scipy minimization nonlinear-functions
我试图将输入(1 个变量)最小化为输出为概率的非线性函数。
如,如果输入为 5,则输出概率为 40%,输入 10,则概率变为 93%。计算概率的函数是确定性的。
现在我想最小化输入,使得概率大于 80% 。有没有使用 scipy 库在 python 中执行此操作的简单方法?
【问题讨论】:
标签: python optimization scipy minimization nonlinear-functions
以下有帮助吗?
import math
from scipy.optimize import minimize,NonlinearConstraint
def fmin(x):
# this is the function you are minimizing
# in your case this function just returns x
return x
def fprob(x):
# this is the function defining your probability as a function of x
# this function is maximized at x=3, and its max value is 1
return math.exp(-(x-3)*(x-3))
# these are your nonlinear constraints
# since you want to find input such that your probability is > 0.8
# the lower limit is 0.8. Since probabilty cannot be >1, upper limit is 1
nlc = NonlinearConstraint(fprob,0.8,1)
# the zero is your initial guess
res = minimize(fmin,0,method='SLSQP',constraints=nlc)
# this is your answer
print(f'{res.x=}')
【讨论】: