Source code for naginterfaces.library.examples.roots.contfn_brent_interval_ex

#!/usr/bin/env python3
"``naginterfaces.library.roots.contfn_brent_interval`` Python Example."

# NAG Copyright 2018-2019.

# pylint: disable=invalid-name

from naginterfaces.library import roots, specfun

[docs]def main(): """ Example for :func:`naginterfaces.library.roots.contfn_brent_interval`. Finds a zero of a continuous function using Brent's algorithm. >>> main() naginterfaces.library.roots.contfn_brent_interval Python Example Results. Implied volatility for an option with a given target price. The implied volatility for the target price of 12.35008695 is 0.09000000. """ print('naginterfaces.library.roots.contfn_brent_interval ' 'Python Example Results.') print('Implied volatility for an option with a given target price.') # Initial guess for sigma: sigma = 0.15 # Option-pricing parameters: S = 100. # Spot price of the underlying asset; K = 90. # Strike price; T = 1.5 # Time to expiry; r = 0.03 # Annual risk-free rate of interest; q = 0.015 # Annual continuous yield rate. # Target option price: target = 12.35008695 # The (Black--Scholes--Merton European option) volatility function: bsm_f = lambda sigma: ( specfun.opt_bsm_price('c', [K], S, [T], sigma, r, q)[0, 0] - target ) print( "The implied volatility for the target price of " "{:1.8f} is {:1.8f}.".format( target, roots.contfn_brent_interval(sigma, bsm_f).x ) )
if __name__ == '__main__': import doctest import sys sys.exit( doctest.testmod( None, verbose=True, report=False, optionflags=doctest.REPORT_NDIFF, ).failed )