Calculate Option Implied Volatility In Python
文章推薦指數: 80 %
This tutorial covers two methods on how to calculate option implied volatility using Python: brute force and Newton Raphson Method.
SkiptocontentThistutorialcoverstwomethodsonhowtocalculateoptionimpliedvolatilityusingPython:bruteforceandNewtonRaphsonMethod.Asingleoptionchaincontainsthousandsofvaluesfordifferentoptions.Ifwewanttocalculatetheimpliedvolatilitiesforalloptions,wehavetousesomethingfast.Forpracticalreasons,wewouldpreferNewton’smethod,andbruteforceisjustforcomparison.Inreality,optionpricesaredeterminedbydemandandsupply.Foragivenoption,when:Morebuyersthansellers,thepricegoesupMoresellerthanbuyers,thepricegoesdownTheBlackScholesformulagivesaframeworktomodeltheoptionpricesandrisksassociatedwithanoption.Alltheinputvaluesarereadilyobservableexceptforone–theimpliedvolatility.Asitsnamesuggests–itisimpliedandnotexplicitlyknown.BlackScholesOptionPricingFormulaInPythonInthistutorial,weimplementedtheBSformulainPythontocalculateoptionsprices.Belowisthecode:importscipy.stats
fromnumpyimportsqrt,log,exp,pi
N=scipy.stats.norm.cdf
d1=(log(S/K)+(r+sigma**2/2)*t)/(sigma*sqrt(t))
d2=d1-sigma*sqrt(t)
defbs_price(c_p,S,K,r,t,sigma):
ifc_p=='c':
returnN(d1)*S-N(d2)*K*exp(-r*t)
elifc_p=='p':
returnN(-d2)*K*exp(-r*t)-N(-d1)*S
else:
return"Pleasespecifycallorputoptions."TheBSpricingformulasortofhaswhatweneedbutdoesnotexactlysolveourproblem.Insteadofcalculatinganoptionprice,wealreadyknowitThevolatility(sigma)isunknownandweneedtocalculateitCalculatingImpliedVolatilityInPythonBruteForceMethodA“bruteforce”methodbasicallyattemptstousemanydifferentsigma(volatility)valuestocalculatetheoptionprice.Whenacalculatedpriceiscloseenoughtotheobservedprice,thecorrespondingsigmaisconsideredtobethe“root”.Thismethodwillperformmanycalculationssincewedon’tknowwhentherewillbeamatch.Usually,bruteforceisthelastresortwheneverythingelsefails.Luckily,forourproblem,there’samoreefficientsolution.Ifyoudon’twanttoreadaboutthebruteforcemethod,skipaheadtothenextsectiontolearnhowtoimplementthemoreefficientmethod–TheNewtonRasphonMethod.Tostartthebruteforcecalculation,weneedastartingsigmavalue.There’snorightorwrongnumberforthisstartingvalue,butIchosetouse0.5,or50%volatility.Notethatmanystockscanhavevolatilitywellover100%sothe0.5isn’treallythemidpoint.Theprocessgoeslikethis:DefineathresholdONE_CENTonwhentoacceptthesolution,e.g.whenthecalculatedpriceandtheactualmarketpricearewithin1cent.Defineastepvariabletoadjustthesigmavalues.Useaninitialguess(_sigma)tocalculatetheBlackScholesoptionpriceCalculatethedifference(diff)betweentheBSpriceandtheactualmarketpriceIfthediffislargerthanthethresholdONE_CENT,adjustthesigmavalueandre-calculatethedifferenceRepeat#4-5untilthediffislessthanorequaltothethresholdToavoidaninfiniteloop,let’salsosetalimitonthemaxnumberofcalculationstoperform.ONE_CENT=0.01
step=0.0001
defbrute_force(c_p,S,K,r,t,market_price):
_sigma=0.5
foriinrange(10000):#maxnumberofcalculationsis10000
bs_price=bs_price(c_p,S,K,r,t,sigma=_sigma)
diff=market_price-bs_price
ifdiff>ONE_CENT:
_sigma=_sigma+step
elifdiff<0andabs(diff)>ONE_CENT:
_sigma=_sigma-step
elifabs(diff)
延伸文章資訊
- 1Introduction to Options - Historical Volatility and Implied Volatility
Calculation. Here we use the bisection method to solve the BSM pricing equation and find the root...
- 2Implied Volatility for European Call with Python - Codearmo
Learn how to calculate the implied volatility of a European call option using the Newton-Raphson ...
- 3Fast Implied Volatility Calculation in Python - Stack Overflow
I am looking for a library which i can use for faster way to calculate implied volatility in pyth...
- 4Calculate Implied Volatility or any Options Greek in just 3 lines ...
We will be using a python library — mibian, which could solve our purpose. Mibian can be used to ...
- 5Implied Volatility Calculations with Python