隐含波动率模型-增量搜寻算法-python实现

隐含波动率模型-增量搜寻算法-python实现

在这里插入图片描述

import numpy as np

def incremental_search(f,a,b,dx):
    fa=f(a)
    c=a+dx
    fc=f(c)
    n=1
    while np.sign(fa)==np.sign(fc):
        if a>=b:
            return a-dx,n
        a=c
        fa=fc
        c=a+dx
        fc=f(c)
        n+=1
    if fa==0:
        return a,n
    elif fc==0:
        return c,n
    else:
        return (a+c)/2,n
    
y=lambda x:x**3+2.0*x**2-5
root,iterations=incremental_search(y,-5.,5.,0.001)
print("Root is:",root)
print( "Iterations: ",iterations)
©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页