Black-Scholes Formula and Python Implementation - Aaron ...
文章推薦指數: 80 %
The Black-Scholes model was first introduced by Fischer Black and Myron Scholes in 1973 in the paper "The Pricing of Options and Corporate ... Home Projects TheBlack-ScholesmodelwasfirstintroducedbyFischerBlackandMyronScholesin1973inthepaper"ThePricingofOptionsandCorporateLiabilities".Sincebeingpublished,themodelhasbecomeawidelyusedtoolbyinvestorsandisstillregardedasoneofthebestwaystodeterminefairpricesofoptions. ThepurposeofthemodelistodeterminethepriceofavanillaEuropeancallandputoptions(optionthatcanonlybeexercisedattheendofitsmaturity)basedonpricevariationovertimeandassumingtheassethasalognormaldistribution. Assumptions¶ TodeterminethepriceofvanillaEuropeanoptions,severalassumptionsaremade: Europeanoptionscanonlybeexercisedatexpiration Nodividendsarepaidduringtheoption'slife Marketmovementscannotbepredicted Therisk-freerateandvolatilityareconstant Followsalognormaldistribution Non-DividendPayingBlack-ScholesFormula¶ InBlack-Scholesformulas,thefollowingparametersaredefined. $S$,thespotpriceoftheassetattime$t$ $T$,thematurityoftheoption.Timetomaturityisdefinedas$T-t$ $K$,strikepriceoftheoption $r$,therisk-freeinterestrate,assumedtobeconstantbetween$t$and$T$ $\sigma$,volatilityofunderlyingasset,thestandarddeviationoftheassetreturns $N(d)$isthecumulativedistributionofthestandardnormalvariableZ¶ $$N(d)=\frac{1}{\sqrt{2\pi}}\int_{-\infty}^de^{-\frac{1}{2}x^2}dx$$ $C(S,t)$isthevalueattime$t$ofacalloptionand$P(S,t)$isthevalueattime$t$ofaputoption. TheBlack-Scholescallformulaisgivenas: $$C(S,t)=SN(d_1)-Ke^{-r(T-t)}N(d_2)$$ Theputformulaisgiven: $$P(S,t)=Ke^{-r(T-t)}N(-d_2)-SN(-d_1)$$ Where: $$d_1=\frac{\ln\left(\frac{S}{K}\right)+\left(r+\frac{\sigma^2}{2}\right)(T-t)}{\sigma\sqrt{T-t}}$$ $$d_2=d_1-\sigma\sqrt{T-t}=\frac{\ln\left(\frac{S}{K}\right)+\left(r-\frac{\sigma^2}{2}\right)(T-t)}{\sigma\sqrt{T-t}}$$ PythonImplementationofBlack-Scholesformulafornon-dividendpayingoptions¶ In [1]: importnumpyasnp importscipy.statsassi importsympyassy fromsympy.statsimportNormal,cdf fromsympyimportinit_printing init_printing() In [2]: defeuro_vanilla_call(S,K,T,r,sigma): #S:spotprice #K:strikeprice #T:timetomaturity #r:interestrate #sigma:volatilityofunderlyingasset d1=(np.log(S/K)+(r+0.5*sigma**2)*T)/(sigma*np.sqrt(T)) d2=(np.log(S/K)+(r-0.5*sigma**2)*T)/(sigma*np.sqrt(T)) call=(S*si.norm.cdf(d1,0.0,1.0)-K*np.exp(-r*T)*si.norm.cdf(d2,0.0,1.0)) returncall In [3]: euro_vanilla_call(50,100,1,0.05,0.25) Out[3]: $\displaystyle0.027352509369436617$ In [4]: defeuro_vanilla_put(S,K,T,r,sigma): #S:spotprice #K:strikeprice #T:timetomaturity #r:interestrate #sigma:volatilityofunderlyingasset d1=(np.log(S/K)+(r+0.5*sigma**2)*T)/(sigma*np.sqrt(T)) d2=(np.log(S/K)+(r-0.5*sigma**2)*T)/(sigma*np.sqrt(T)) put=(K*np.exp(-r*T)*si.norm.cdf(-d2,0.0,1.0)-S*si.norm.cdf(-d1,0.0,1.0)) returnput In [5]: euro_vanilla_put(50,100,1,0.05,0.25) Out[5]: $\displaystyle45.15029495944084$ Thenextfunctioncanbecalledwith'call'or'put'fortheoptionparametertocalculatethedesiredoption In [6]: defeuro_vanilla(S,K,T,r,sigma,option='call'): #S:spotprice #K:strikeprice #T:timetomaturity #r:interestrate #sigma:volatilityofunderlyingasset d1=(np.log(S/K)+(r+0.5*sigma**2)*T)/(sigma*np.sqrt(T)) d2=(np.log(S/K)+(r-0.5*sigma**2)*T)/(sigma*np.sqrt(T)) ifoption=='call': result=(S*si.norm.cdf(d1,0.0,1.0)-K*np.exp(-r*T)*si.norm.cdf(d2,0.0,1.0)) ifoption=='put': result=(K*np.exp(-r*T)*si.norm.cdf(-d2,0.0,1.0)-S*si.norm.cdf(-d1,0.0,1.0)) returnresult In [7]: euro_vanilla(50,100,1,0.05,0.25,option='put') Out[7]: $\displaystyle45.15029495944084$ SympyimplementationforExactResults In [8]: defeuro_call_sym(S,K,T,r,sigma): #S:spotprice #K:strikeprice #T:timetomaturity #r:interestrate #sigma:volatilityofunderlyingasset N=Normal('x',0.0,1.0) d1=(sy.ln(S/K)+(r+0.5*sigma**2)*T)/(sigma*sy.sqrt(T)) d2=(sy.ln(S/K)+(r-0.5*sigma**2)*T)/(sigma*sy.sqrt(T)) call=(S*cdf(N)(d1)-K*sy.exp(-r*T)*cdf(N)(d2)) returncall In [9]: euro_call_sym(50,100,1,0.05,0.25) Out[9]: $\displaystyle-25\operatorname{erf}{\left(1.22379436111989\sqrt{2}\right)}-22.5614712250357+47.5614712250357\operatorname{erf}{\left(1.34879436111989\sqrt{2}\right)}$ In [10]: defeuro_put_sym(S,K,T,r,sigma): #S:spotprice #K:strikeprice #T:timetomaturity #r:interestrate #sigma:volatilityofunderlyingasset N=systats.Normal(0.0,1.0) d1=(sy.ln(S/K)+(r+0.5*sigma**2)*T)/(sigma*sy.sqrt(T)) d2=(sy.ln(S/K)+(r-0.5*sigma**2)*T)/(sigma*sy.sqrt(T)) put=(K*sy.exp(-r*T)*N.cdf(-d2)-S*N.cdf(-d1)) returnput Sympyimplementationoftheabovefunctionthatenablesonetospecifyacallorputresult. In [11]: defsym_euro_vanilla(S,K,T,r,sigma,option='call'): #S:spotprice #K:strikeprice #T:timetomaturity #r:interestrate #sigma:volatilityofunderlyingasset N=Normal('x',0.0,1.0) d1=(sy.ln(S/K)+(r+0.5*sigma**2)*T)/(sigma*sy.sqrt(T)) d2=(sy.ln(S/K)+(r-0.5*sigma**2)*T)/(sigma*sy.sqrt(T)) ifoption=='call': result=(S*cdf(N)(d1)-K*sy.exp(-r*T)*cdf(N)(d2)) ifoption=='put': result=(K*sy.exp(-r*T)*cdf(N)(-d2)-S*cdf(N)(-d1)) returnresult In [12]: sym_euro_vanilla(50,100,1,0.05,0.25,option='put') Out[12]: $\displaystyle-25\operatorname{erf}{\left(1.22379436111989\sqrt{2}\right)}+22.5614712250357+47.5614712250357\operatorname{erf}{\left(1.34879436111989\sqrt{2}\right)}$ DividendPayingBlack-ScholesFormula¶ Forassetsthatpaydividends,theBlack-Scholesformulaisrathersimilartothenon-dividendpayingassetformula;however,anewparameter$q$,isadded. $S$,thespotpriceoftheassetattime$t$ $T$,thematurityoftheoption.Timetomaturityisdefinedas$T-t$ $K$,strikepriceoftheoption $r$,therisk-freeinterestrate,assumedtobeconstantbetween$t$and$T$ $\sigma$,volatilityofunderlyingasset,thestandarddeviationoftheassetreturns $q$,thedividendrateoftheasset.Thisisassumedtopaydividendsatacontinuousrate Inthiscase,the$q$parameterisnowincludedin$C(S,t)$and$P(S,t)$. $$C(S,t)=Se^{-q(T-t)}N(d_1)-Ke^{-r(T-t)}N(d_2)$$ $$P(S,t)=Ke^{-r(T-t)}N(-d_2)-Se^{-q(T-t)}N(-d_1)$$ Then,$d_1$and$d_2$areslightlymodifiedtoincludethecontinuousdividends $$d_1=\frac{ln\left(\frac{S}{K}\right)+\left(r-q+\frac{\sigma^2}{2}\right)(T-t)}{\sigma\sqrt{T-t}}$$ $$d_2=d_1-\sigma\sqrt{T-t}=\frac{ln(\frac{S}{K})+(r-q-\frac{\sigma^2}{2})(T-t)}{\sigma\sqrt{T-t}}$$ PythonImplementation¶ In [13]: defblack_scholes_call_div(S,K,T,r,q,sigma): #S:spotprice #K:strikeprice #T:timetomaturity #r:interestrate #q:rateofcontinuousdividendpayingasset #sigma:volatilityofunderlyingasset d1=(np.log(S/K)+(r-q+0.5*sigma**2)*T)/(sigma*np.sqrt(T)) d2=(np.log(S/K)+(r-q-0.5*sigma**2)*T)/(sigma*np.sqrt(T)) call=(S*np.exp(-q*T)*si.norm.cdf(d1,0.0,1.0)-K*np.exp(-r*T)*si.norm.cdf(d2,0.0,1.0)) returncall In [14]: defblack_scholes_put_div(S,K,T,r,q,sigma): #S:spotprice #K:strikeprice #T:timetomaturity #r:interestrate #q:rateofcontinuousdividendpayingasset #sigma:volatilityofunderlyingasset d1=(np.log(S/K)+(r-q+0.5*sigma**2)*T)/(sigma*np.sqrt(T)) d2=(np.log(S/K)+(r-q-0.5*sigma**2)*T)/(sigma*np.sqrt(T)) put=(K*np.exp(-r*T)*si.norm.cdf(-d2,0.0,1.0)-S*np.exp(-q*T)*si.norm.cdf(-d1,0.0,1.0)) returnput Implementationthatcanbeusedtodeterminetheputorcalloptionpricedependingonspecification In [15]: defeuro_vanilla_dividend(S,K,T,r,q,sigma,option='call'): #S:spotprice #K:strikeprice #T:timetomaturity #r:interestrate #q:rateofcontinuousdividendpayingasset #sigma:volatilityofunderlyingasset d1=(np.log(S/K)+(r-q+0.5*sigma**2)*T)/(sigma*np.sqrt(T)) d2=(np.log(S/K)+(r-q-0.5*sigma**2)*T)/(sigma*np.sqrt(T)) ifoption=='call': result=(S*np.exp(-q*T)*si.norm.cdf(d1,0.0,1.0)-K*np.exp(-r*T)*si.norm.cdf(d2,0.0,1.0)) ifoption=='put': result=(K*np.exp(-r*T)*si.norm.cdf(-d2,0.0,1.0)-S*np.exp(-q*T)*si.norm.cdf(-d1,0.0,1.0)) returnresult SympyImplementationofBlack-ScholeswithDividend-payingasset In [16]: defblack_scholes_call_div_sym(S,K,T,r,q,sigma): #S:spotprice #K:strikeprice #T:timetomaturity #r:interestrate #q:rateofcontinuousdividendpayingasset #sigma:volatilityofunderlyingasset N=Normal('x',0.0,1.0) d1=(sy.ln(S/K)+(r-q+0.5*sigma**2)*T)/(sigma*sy.sqrt(T)) d2=(sy.ln(S/K)+(r-q-0.5*sigma**2)*T)/(sigma*sy.sqrt(T)) call=S*sy.exp(-q*T)*cdf(N)(d1)-K*sy.exp(-r*T)*cdf(N)(d2) returncall In [17]: defblack_scholes_call_put_sym(S,K,T,r,q,sigma): #S:spotprice #K:strikeprice #T:timetomaturity #r:interestrate #q:rateofcontinuousdividendpayingasset #sigma:volatilityofunderlyingasset N=Normal('x',0.0,1.0) d1=(sy.ln(S/K)+(r-q+0.5*sigma**2)*T)/(sigma*sy.sqrt(T)) d2=(sy.ln(S/K)+(r-q-0.5*sigma**2)*T)/(sigma*sy.sqrt(T)) put=K*sy.exp(-r*T)*cdf(N)(-d2)-S*sy.exp(-q*T)*cdf(N)(-d1) returnput SympyimplementationofpricingaEuropeanputorcalloptiondependingonspecification In [18]: defsym_euro_vanilla_dividend(S,K,T,r,q,sigma,option='call'): #S:spotprice #K:strikeprice #T:timetomaturity #r:interestrate #q:rateofcontinuousdividendpayingasset #sigma:volatilityofunderlyingasset N=Normal('x',0.0,1.0) d1=(sy.ln(S/K)+(r-q+0.5*sigma**2)*T)/(sigma*sy.sqrt(T)) d2=(sy.ln(S/K)+(r-q-0.5*sigma**2)*T)/(sigma*sy.sqrt(T)) ifoption=='call': result=S*sy.exp(-q*T)*cdf(N)(d1)-K*sy.exp(-r*T)*cdf(N)(d2) ifoption=='put': result=K*sy.exp(-r*T)*cdf(N)(-d2)-S*sy.exp(-q*T)*cdf(N)(-d1) returnresult References¶ Sherbin,A.(2015).Howtopriceandtradeoptions:identify,analyze,andexecutethebesttradeprobabilities. Hoboken,NJ:JohnWiley&Sons,Inc. Ursone,P.(2015).HowtocalculateoptionspricesandtheirGreeks:exploringtheBlackScholesmodelfromDelta toVega.Chichester:Wiley. RelatedPosts TheGeneralizedBlack-ScholesFormulaforEuropeanOptions MeasuringSensitivitytoDerivativesPricingChangeswiththe"Greeks"andPython ImpliedVolatilityCalculationswithPython Put-CallParityofVanillaEuropeanOptionsandPythonImplementation MatrixNormsandInequalitieswithPython Categories Analysis Calculus DataScience Finance LinearAlgebra MachineLearning nasapy petpy poetpy Python R SQL Statistics RecentPosts MedianTest Chi-SquareTestofIndependenceforRxCContingencyTables Wald-WolfowitzTwo-SampleRunsTest MatrixNormsandInequalitieswithPython VectorNormsandInequalitieswithPython Games-HowellPost-HocMultipleComparisonsTestwithPython Blogroll R-Bloggers
延伸文章資訊
- 1Implementing the Black-Scholes in Python
the Black-Scholes Partial Differential Equation given below: ... We successfully implemented the ...
- 2Black Scholes Model in Python for Predicting Options Premiums
The Black Scholes model is considered to be one of the best ways of determining fair prices of op...
- 3Python財金應用:Black-Scholes選擇權訂價模型(1)
經典的量化金融案例,也是每天在交易室會碰到無數次的內容,推導Black-Scholes formula就不是本篇的重點,有興趣我會在文章最底下附上推薦書單。
- 4Black Scholes Model: Formula, Limitations, Python ...
Black Scholes formula · S = Stock price · N() = Cumulative Standard normal distribution · K = Str...
- 5Black Scholes Model Python - Codearmo
Black Scholes Model Python · 1) Interest rate is known and constant through time. · 2) The stock ...