Calculate Option Implied Volatility In Python

文章推薦指數: 80 %
投票人數:10人

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)impliestheputoptioniscurrentlyITM,withanintrinsicvalueof$4.Withstill5dayslefttoexpiration,I’dassumetherearestillsome(althoughverylittle)extrinsicvalueintheoption,meaningthegivenoptionshouldbetraded>$4.Let’sdoaquicktestbyusingyourgiveninput,andasigmavalueof0:bs_price(c_p,S,K,r,t,0)=$3.94.Veryclosetotheintrinsicvalueof$4,kindaweirdbutit’satheoreticalequationandverycloseto$4,soI’mokaywithit.Infact,ifweplugt=0intotheequation,wegetexactly$4.Bysettingsigma=0(i.e.theminimalpossiblevalueforIV),wesettheminimumvalueoftheoption,whichshouldbeatleast3.94(basedontheBSequation).However,themarketpriceisonly$3.8….Thisissomeseriousmis-priceaccordingtoBSMframework.However,BSMisonlytheoretical,theoptionpricesaredeterminedbythebuyersandsellersinreality.Forthisreason,I’dsuggestusingBSMasaguide/referencepointinsteadofrelyingitoneverything.Anotherpossibilityforthis“misprice”isthatyoumightbeusingendofdaydata,whichcouldbeinaccurate(e.g.stockpricemovedaftermarketclose,butthelastoptionpricedidn’tchange,etc.)Goingbacktothecodingsidewiththehandling,wecanprobablydosomethinglikethis(pseudocode):ifintrinsicvalue



請為這篇文章評分?