我正在尝试在Python上使用牛顿算法来获取函数的根。即使我更改精度级别,我也会出现运行时错误。您能帮我了解如何改进它吗?
最好, GB
在我的“简单”代码和根查找部分下面:
from scipy.stats import norm
import numpy as np
import matplotlib.pyplot as plt
import scipy.optimize as opt
def vega_callspread(r,S,T,d,sigma,q,K1,K2):
d1 = (np.log(S/K1)+(r+sigma*sigma/2)*T)/(sigma*np.sqrt(T))
d2 = (np.log(S/K2)+(r+sigma*sigma/2)*T)/(sigma*np.sqrt(T))
u1 = S*np.exp(-q*T)*norm.pdf(d1,0,1)
u2 = S*np.exp(-q*T)*norm.pdf(d2,0,1)
return u1-u2;
x0=112
r=0
T=1
d=0
sigma=0.2
q=0
K1=110
K2=130
res2= opt.newton(vega_callspread, x0, args=(r,T,d,sigma,q,K1,K2,),tol=10**(-1),maxiter=1000000)
the error i get is: res2= opt.zeros.newton(vega_callspread, x0, args=(r,T,d,sigma,q,K1,K2,),tol=10**(-1),maxiter=1000000)
/Users/anaconda/lib/python3.5/site-packages/scipy/optimize/zeros.py:173: RuntimeWarning: Tolerance of 0.011300000000005639 reached
warnings.warn(msg, RuntimeWarning)
最佳答案
在如此稀疏的背景下很难给出建议。但一些评论:
答:
您没有使用牛顿,如所述 here :
The Newton-Raphson method is used if the derivative fprime of func is provided, otherwise the secant method is used.
B:
您的错误来自here我会说:
由于这些 tol 值是硬编码的,我认为这不应该发生!
# Secant method
p0 = x0
if x0 >= 0:
p1 = x0*(1 + 1e-4) + 1e-4
else:
p1 = x0*(1 + 1e-4) - 1e-4
q0 = func(*((p0,) + args))
q1 = func(*((p1,) + args))
for iter in range(maxiter):
if q1 == q0:
if p1 != p0:
msg = "Tolerance of %s reached" % (p1 - p0)
warnings.warn(msg, RuntimeWarning)
return (p1 + p0)/2.0
含义:您的代码可能有问题!
C:
让我们尝试更慢但更安全的 bisection-method :
# brackets not tuned! it's just some trial!
res2 = opt.bisect(vega_callspread, 0, 200, args=(r,T,d,sigma,q,K1,K2))
输出:
X:\so_newton.py:9: RuntimeWarning: divide by zero encountered in log
d1 = (np.log(S/K1)+(r+sigma*sigma/2)*T)/(sigma*np.sqrt(T))
X:\so_newton.py:10: RuntimeWarning: divide by zero encountered in log
d2 = (np.log(S/K2)+(r+sigma*sigma/2)*T)/(sigma*np.sqrt(T))
这是一个坏兆头。
D(跟随 C):
你的函数如下:
def vega_callspread(r,S,T,d,sigma,q,K1,K2):
d1 = (np.log(S/K1)+(r+sigma*sigma/2)*T)/(sigma*np.sqrt(T))
d2 = (np.log(S/K2)+(r+sigma*sigma/2)*T)/(sigma*np.sqrt(T))
u1 = S*np.exp(-q*T)*norm.pdf(d1,0,1)
u2 = S*np.exp(-q*T)*norm.pdf(d2,0,1)
return u1-u2;
你打电话的方式是:
x0=112
r=0
T=1
d=0
sigma=0.2
q=0
K1=110
K2=130
args=(r,T,d,sigma,q,K1,K2,)
没有S!
因此,要么您危险地重命名变量,要么您的端出现了一些错误!
S
的值是log(S/K1)
和co中的问题。
这不是关于S/K1
而是关于这个:
import numpy as np
np.log(0)
# __main__:1: RuntimeWarning: divide by zero encountered in log
# -inf
这是我从 this SO-answer 得到的.
您现在可以尝试解释这对您的函数做了什么,因为此 log-eval 将获取 -np.inf
的值。
E scipy 如何处理这个问题(遵循 D):
我懒得阅读 args-handling 的文档/源代码,但让我们检查一下(向 func 添加打印;像之前一样使用二分法):
def vega_callspread(r,S,T,d,sigma,q,K1,K2):
print('S: ', S)
d1 = (np.log(S/K1)+(r+sigma*sigma/2)*T)/(sigma*np.sqrt(T))
d2 = (np.log(S/K2)+(r+sigma*sigma/2)*T)/(sigma*np.sqrt(T))
u1 = S*np.exp(-q*T)*norm.pdf(d1,0,1)
u2 = S*np.exp(-q*T)*norm.pdf(d2,0,1)
return u1-u2;
输出:
S: 0
X:\so_newton.py:11: RuntimeWarning: divide by zero encountered in log
d1 = (np.log(S/K1)+(r+sigma*sigma/2)*T)/(sigma*np.sqrt(T))
X:\so_newton.py:12: RuntimeWarning: divide by zero encountered in log
d2 = (np.log(S/K2)+(r+sigma*sigma/2)*T)/(sigma*np.sqrt(T))
S: 0
因此,arg-handling 似乎是通过 arg-name 进行的(而不仅仅是调用中的 param-position ;我在这里缺少正确的编程语言术语;再次懒惰!)
这(S=0)可能非常糟糕(不是您想要的),这是您的错误!
编辑
在您发表评论后,您似乎尝试优化S
。这让我清楚地知道,您正在使用某种优化算法来优化 x
,而您的函数中没有 x
!
我不是在这里分析你的任务,但你可能想让你的函数使用一些x
(由x0
初始化),因为这是一般的想法scipy.optimize
。我们可以保留名称S
,但它需要是函数的第一个参数。这一切都在文档中进行了解释。
所以:
def vega_callspread(S, r,T,d,sigma,q,K1,K2): # S now first argument !!!
...
res2= opt.newton(vega_callspread, x0, args=(r,T,d,sigma,q,K1,K2,),tol=10**(-1),maxiter=1000000) # S removed from args; S is init by x0 -> read docs!
输出:
117.214682594
关于python - python/scipy 中的牛顿法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46265502/