Package: calcbsimpvol
calcbsimpvolCalculate Black-Scholes Implied Volatility - Vectorwise:)native python code:)lightweight footprint:)sample data included:(not suited for single / low number of options:(code reads un-pythonic:(not yet thoroughly testedGetting startedRequirementsPython 3.x (currently) or PyPy3NumPySciPy(MatPlotLib to visualize results in some examples)InstallationWhile the code consists of single digit functions,
I recommend using thepip installway to get the code.
That way you would take advantage of bug fixes, updates,
and possible extensions.$pipinstallcalcbsimpvolExamplePass yourargsbundled in adict.fromcalcbsimpvolimportcalcbsimpvolimportnumpyasnpS=np.asarray(100)K_value=np.arange(40,160,25)K=np.ones((np.size(K_value),1))K[:,0]=K_valuetau_value=np.arange(0.25,1.01,0.25)tau=np.ones((np.size(tau_value),1))tau[:,0]=tau_valuer=np.asarray(0.01)q=np.asarray(0.03)cp=np.asarray(1)P=[[59.35,34.41,10.34,0.50,0.01],[58.71,33.85,10.99,1.36,0.14],[58.07,33.35,11.50,2.12,0.40],[57.44,32.91,11.90,2.77,0.70]]P=np.asarray(P)[K,tau]=np.meshgrid(K,tau)sigma=calcbsimpvol(dict(cp=cp,P=P,S=S,K=K,tau=tau,r=r,q=q))print(sigma)# [[      nan,       nan,  0.20709362, 0.21820954, 0.24188675],# [       nan, 0.22279836, 0.20240934, 0.21386148, 0.23738982],# [       nan, 0.22442837, 0.1987048 , 0.21063506, 0.23450013],# [       nan, 0.22188111, 0.19564657, 0.20798285, 0.23045406]]More usage examples are available inexample3.py(additional sample data required which is  available atGitHub RepoPerformanceDesign a test. 
Get the results you want.k_max = 10(default)tolerance = 10E-12(default)linear regression steps are commented out (default)# assuming you did install it alreadygitclonehttps://github.com/erkandem/calcbsimpvol.gitcdcalcbsimpvol
pythonexamples/example3.py--steps100--modereference15 µs per option41 ms per surfacetested with 3.6, 3.7 and PyPy3matlab-nodisplay-nosplash-nodesktop-r"run('mlb_reference_example.m');"12 µs per option34 ms per surfaceObviously, these values are per core (i5 4210U 1.7 GHz).NotesGood Python code reads like a novel. Right? So should math.
I preferred short math-like variable names in this case.
That makes the code less readable compared to other Python code
but the docstrings should make up for the lack of readability.Originally, I left the camelCase function name and spelling in place but eventually got annoyed.calcbsimpvol it isCode Originfirst thought of by Li (2006) (see References)implemented and published by Mark Whirdy as MATLAB .m-code (see References)numpyified from.mto.pyby meContactemail:erkan.dem@pm.medocumentation:erkandem.github.io/calcbsimpvol/source:github.com/erkandem/calcbsimpvolissues:github.com/erkandem/calcbsimpvol/issuesToDosmake the code compatible withPython 2make itPyPycompatibleReferencesLi, 2006, "You Don't Have to Bother Newton for Implied Volatility"http://papers.ssrn.com/sol3/papers.cfm?abstract_id=952727MATLAB source code available at:https://www.mathworks.com/matlabcentral/fileexchange/41473-calcbsimpvol-cp-p-s-k-t-r-qLicenseThe included Python code is licensed underMITLicenseThe Code by Mark Whirdy is licensed underMITLicenseThe translation is not related or endorsed by the original author.