Implied Volatility for European Call with Python - Codearmo

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

Learn how to calculate the implied volatility of a European call option using the Newton-Raphson method in Python. A brute force approach is ... ImpliedVolatilityforEuropeanCallwithPython John | September08,2020   Inordertounderstandwhattheimpliedvolatilityofacalloptionisandthencalculateit,considertheBlack-ScholesformulaforpricingaEuropeancalloptionbelow. Inthisarticlewewillfocusonthemathematicsbehindsolvingfortheimpliedvolatilityofacalloption.Ifyouaremoreinterestedinsimplygettingthenumberinafastmanner,thisarticlewhichusesScipy'sminimizemodulemaybemoresuitable.    \(Call=S_{0}N(d1)-N(d2)Ke^{-rT}\) Where: \(\\d1=\frac{ln(\frac{S}{K})+(r+\frac{\sigma^2}{2})T}{\sigma\sqrt{T}}\\\\d2=d1-\sigma\sqrt{T}\)   \(S\) :currentassetprice \(K\):strikepriceoftheoption \(r\):riskfreerate  \(T\) :timeuntiloptionexpiration  \(\sigma\):annualizedvolatilityoftheasset'sreturns  \(N(x)\):isthecumulativedistributionfunctionforastandardnormaldistribution      importnumpyasnp fromscipy.statsimportnorm N_prime=norm.pdf N=norm.cdf defblack_scholes_call(S,K,T,r,sigma): ''' :paramS:Assetprice :paramK:Strikeprice :paramT:Timetomaturity :paramr:risk-freerate(treasurybills) :paramsigma:volatility :return:callprice ''' ###standardblack-scholesformula d1=(np.log(S/K)+(r+sigma**2/2)*T)/(sigma*np.sqrt(T)) d2=d1-sigma*np.sqrt(T) call=S*N(d1)-N(d2)*K*np.exp(-r*T) returncall      Considerthefollowingexampletodemonstrateimpliedvolatility.Youobserveastock inthemarketwithacurrentpriceof$100,thereisanoptionforsale,fortherighttobuythestockinexactly1yearfromnowfor$115,theoptioncosts$18andtheriskfreerate is5%.Noticeweknoweverythingexceptthevolatility(\(\sigma\))fromtheequationabove.Observetheexample inequationform:   \(18=100N(\frac{ln(\frac{100}{115})+(0.05+\frac{\sigma^2}{2})1}{\sigma\sqrt{1}})-N(\frac{ln(\frac{100}{115})+(0.05+\frac{\sigma^2}{2})1}{\sigma\sqrt{1}}-\sigma\sqrt{1})115e^{-0.05*1}\)     The \(\sigma\) parameterfromaboveisthevolatilityatwhichtheBlack-Scholesformulawouldreturnavalueof$18.Soessentiallyoncewesolvefor \(\sigma\) intheequationabovewehavetheimpliedvolatilityoftheoptionprice.Sincetheformulaabovecannotbesolvedexplicitlywemustresorttoiterativemeasures.   BruteForce Let'suseabruteforcemethodbelowtodemonstratewhatiterativemethodsactuallyare.Thenumpyarrayofvolatilitycandidates containsapproximately40,000possiblesolutionstotheequationdescribedabove,belowwesimplyiteratethroughthemallinordertofindthatonethatminimizestheabsolutedifferencebetweentheobservedpriceandtheBlack-Scholesprice,orinmathtermswearefindingtherootofthefunction.   volatility_candidates=np.arange(0.01,4,0.0001) price_differences=np.zeros_like(volatility_candidates) observed_price=18 S=100 K=115 r=0.05 T=1 foriinrange(len(volatility_candidates)): candidate=volatility_candidates[i] price_differences[i]=observed_price-black_scholes_call(S,K,T,r,candidate) idx=np.argmin(abs(price_differences)) implied_volatility=volatility_candidates[idx] print('Impliedvolatilityforoptionis:',implied_volatility) Impliedvolatilityforoptionis:0.5427999999999968   YoucanverifytheiterativealgorithmworkedbypluggingtheimpliedvolatilitynumberbackintotheBlack-Scholesformula.   S=100 K=115 r=0.05 T=1 sigma=implied_volatility price=black_scholes_call(S,K,T,r,sigma) print(price) 17.9983104365487   Noticethatthevaluewegetaboveisslightlydifferent fromthe$18weexpected.Thisisinherentfeatureofiterativemethods,thinkofthe0.0001in volatility_candidates=np.arange(0.01,4,0.0001)asatoleranceforerror,changingthisvalueupordownwillincreaseordecrease theerrorrespectively.    Whilstthemethoddescribedaboveworksprettywell,youmayhavenoticedthatittakesquiteawhiletofinish.Imaginehavingsay1000optionsforwhichyouwantedtocalculatetheimpliedvolatility.Ittookroughly5secondsfortheabovescripttofinish,multiplyingthatby1000equatestowelloveranhourofwaiting.    Alsonoticethesearchareawedesignatedforourbruteforcemethodwasboundedbetween1%and400%,thereisnoreasonwhyanoptionwouldn'tbepricedhigherthanthatwhichwouldleadtrouble!   NewtonRaphsonAlgorithm  TheNewtonRaphsonmethodisawidelyusedalgorithmforcalculatingtheimpliedvolatilityofanoption.Thesteps forthegenericalgorithmareasfollows: 1)Defineourfunctionas \(f(x)\) forwhichwewanttosolve \(f(x)=0\) 2)Chooseaninitialguess  3)Iterateasfollows:  \(x_{n+1}=x_n-\frac{f(x_n)}{f'(x_n)}\) 4)Breakif: \(|f(x_n)|



請為這篇文章評分?