Tải bản đầy đủ (.pdf) (566 trang)

Python for finance

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (10.57 MB, 566 trang )

www.it-ebooks.info


PythonforFinance
YvesHilpisch
Beijing•Cambridge•Farnham•Köln•Sebastopol•Tokyo

www.it-ebooks.info


Preface
Nottoolongago,Pythonasaprogramminglanguageandplatformtechnologywas
consideredexotic—ifnotcompletelyirrelevant—inthefinancialindustry.Bycontrast,
in2014therearemanyexamplesoflargefinancialinstitutions—likeBankofAmerica
MerrillLynchwithitsQuartzproject,orJPMorganChasewiththeAthenaproject—that
strategicallyusePythonalongsideotherestablishedtechnologiestobuild,enhance,and
maintainsomeoftheircoreITsystems.Thereisalsoamultitudeoflargerandsmaller
hedgefundsthatmakeheavyuseofPython’scapabilitieswhenitcomestoefficient
financialapplicationdevelopmentandproductivefinancialanalyticsefforts.
Similarly,manyoftoday’sMasterofFinancialEngineeringprograms(orprograms
awardingsimilardegrees)usePythonasoneofthecorelanguagesforteachingthe
translationofquantitativefinancetheoryintoexecutablecomputercode.Educational
programsandtrainingstargetedtofinanceprofessionalsarealsoincreasingly
incorporatingPythonintotheircurricula.Somenowteachitasthemainimplementation
language.
TherearemanyreasonswhyPythonhashadsuchrecentsuccessandwhyitseemsitwill
continuetodosointhefuture.Amongthesereasonsareitssyntax,theecosystemof
scientificanddataanalyticslibrariesavailabletodevelopersusingPython,itseaseof
integrationwithalmostanyothertechnology,anditsstatusasopensource.(SeeChapter1
forafewmoreinsightsinthisregard.)
Forthatreason,thereisanabundanceofgoodbooksavailablethatteachPythonfrom


differentanglesandwithdifferentfocuses.Thisbookisoneofthefirsttointroduceand
teachPythonforfinance—inparticular,forquantitativefinanceandforfinancial
analytics.Theapproachisapracticalone,inthatimplementationandillustrationcome
beforetheoreticaldetails,andthebigpictureisgenerallymorefocusedonthanthemost
arcaneparameterizationoptionsofacertainclassorfunction.
Mostofthisbookhasbeenwritteninthepowerful,interactive,browser-basedIPython
Notebookenvironment(explainedinmoredetailinChapter2).Thismakesitpossibleto
providethereaderwithexecutable,interactiveversionsofalmostallexamplesusedinthis
book.
Thosewhowanttoimmediatelygetstartedwithafull-fledged,interactivefinancial
analyticsenvironmentforPython(and,forinstance,RandJulia)shouldgoto
andtryoutthePythonQuantPlatform(incombination
withtheIPythonNotebookfilesandcodethatcomewiththisbook).Youshouldalso
havealookatDXanalytics,aPython-basedfinancialanalyticslibrary.Myotherbook,
DerivativesAnalyticswithPython(WileyFinance),presentsmoredetailsonthetheory
andnumericalmethodsforadvancedderivativesanalytics.Italsoprovidesawealthof
readilyusablePythoncode.Furthermaterial,and,inparticular,slidedecksandvideosof
talksaboutPythonforQuantFinancecanbefoundonmyprivatewebsite.
IfyouwanttogetinvolvedinPythonforQuantFinancecommunityevents,thereare
opportunitiesinthefinancialcentersoftheworld.Forexample,Imyself(co)organize
meetupgroupswiththisfocusinLondon(cf. />

Finance-London/)andNewYorkCity(cf.TherearealsoForPythonQuantsconferencesandworkshopsseveral
timesayear(cf.and).
IamreallyexcitedthatPythonhasestablisheditselfasanimportanttechnologyinthe
financialindustry.Iamalsosurethatitwillplayanevenmoreimportantrolethereinthe
future,infieldslikederivativesandriskanalyticsorhighperformancecomputing.My
hopeisthatthisbookwillhelpprofessionals,researchers,andstudentsalikemakethe
mostofPythonwhenfacingthechallengesofthisfascinatingfield.


www.it-ebooks.info


ConventionsUsedinThisBook
Thefollowingtypographicalconventionsareusedinthisbook:
Italic
Indicatesnewterms,URLs,andemailaddresses.
Constantwidth

Usedforprogramlistings,aswellaswithinparagraphstorefertosoftwarepackages,
programminglanguages,fileextensions,filenames,programelementssuchas
variableorfunctionnames,databases,datatypes,environmentvariables,statements,
andkeywords.
Constantwidthitalic

Showstextthatshouldbereplacedwithuser-suppliedvaluesorbyvaluesdetermined
bycontext.
TIP
Thiselementsignifiesatiporsuggestion.

WARNING
Thiselementindicatesawarningorcaution.

www.it-ebooks.info


UsingCodeExamples
Supplementalmaterial(inparticular,IPythonNotebooksandPythonscripts/modules)is
availablefordownloadat.
Thisbookisheretohelpyougetyourjobdone.Ingeneral,ifexamplecodeisoffered

withthisbook,youmayuseitinyourprogramsanddocumentation.Youdonotneedto
contactusforpermissionunlessyou’rereproducingasignificantportionofthecode.For
example,writingaprogramthatusesseveralchunksofcodefromthisbookdoesnot
requirepermission.SellingordistributingaCD-ROMofexamplesfromO’Reillybooks
doesrequirepermission.Answeringaquestionbycitingthisbookandquotingexample
codedoesnotrequirepermission.Incorporatingasignificantamountofexamplecode
fromthisbookintoyourproduct’sdocumentationdoesrequirepermission.
Weappreciate,butdonotrequire,attribution.Anattributionusuallyincludesthetitle,
author,publisher,andISBN.Forexample:“PythonforFinancebyYvesHilpisch
(O’Reilly).Copyright2015YvesHilpisch,978-1-491-94528-5.”
Ifyoufeelyouruseofcodeexamplesfallsoutsidefairuseorthepermissiongivenabove,
feelfreetocontactusat

www.it-ebooks.info


Safari®BooksOnline
NOTE
SafariBooksOnlineisanon-demanddigitallibrarythatdeliversexpertcontentinbothbookandvideoformfrom
theworld’sleadingauthorsintechnologyandbusiness.

Technologyprofessionals,softwaredevelopers,webdesigners,andbusinessandcreative
professionalsuseSafariBooksOnlineastheirprimaryresourceforresearch,problem
solving,learning,andcertificationtraining.
SafariBooksOnlineoffersarangeofplansandpricingforenterprise,government,
education,andindividuals.
Membershaveaccesstothousandsofbooks,trainingvideos,andprepublication
manuscriptsinonefullysearchabledatabasefrompublisherslikeO’ReillyMedia,
PrenticeHallProfessional,Addison-WesleyProfessional,MicrosoftPress,Sams,Que,
PeachpitPress,FocalPress,CiscoPress,JohnWiley&Sons,Syngress,Morgan

Kaufmann,IBMRedbooks,Packt,AdobePress,FTPress,Apress,Manning,NewRiders,
McGraw-Hill,Jones&Bartlett,CourseTechnology,andhundredsmore.Formore
informationaboutSafariBooksOnline,pleasevisitusonline.

www.it-ebooks.info


HowtoContactUs
Pleaseaddresscommentsandquestionsconcerningthisbooktothepublisher:
O’ReillyMedia,Inc.
1005GravensteinHighwayNorth
Sebastopol,CA95472
800-998-9938(intheUnitedStatesorCanada)
707-829-0515(internationalorlocal)
707-829-0104(fax)

Wehaveawebpageforthisbook,wherewelisterrata,examples,andanyadditional
information.Youcanaccessthispageat />Tocommentorasktechnicalquestionsaboutthisbook,sendemailto

Formoreinformationaboutourbooks,courses,conferences,andnews,seeourwebsiteat
.
FindusonFacebook: />FollowusonTwitter: />WatchusonYouTube: />
www.it-ebooks.info


Acknowledgments
Iwanttothankallthosewhohelpedtomakethisbookareality,inparticularthosewho
haveprovidedhonestfeedbackorevencompletelyworkedoutexamples,likeBenLerner,
JamesPowell,MichaelSchwed,ThomasWieckiorFelixZumstein.Similarly,Iwould
liketothankreviewersHughBrown,JenniferPierce,KevinSheppard,andGalen

Wilkerson.Thebookbenefitedfromtheirvaluablefeedbackandthemanysuggestions.
ThebookhasalsobenefitedsignificantlyasaresultoffeedbackIreceivedfromthe
participantsofthemanyconferencesandworkshopsIwasabletopresentatin2013and
2014:PyData,ForPythonQuants,BigDatainQuantFinance,EuroPython,EuroScipy,
PyConDE,PyConIreland,ParallelDataAnalysis,BudapestBIForumandCodeJam.I
alsogotvaluablefeedbackduringmymanypresentationsatPythonmeetupsinBerlin,
London,andNewYorkCity.
Lastbutnotleast,Iwanttothankmyfamily,whichfullyacceptsthatIdowhatIlove
doingmostandthis,ingeneral,ratherintensively.Writingandfinishingabookofthis
lengthoverthecourseofayearrequiresalargetimecommitment—ontopofmyusually
heavyworkloadandpackedtravelschedule—andmakesitnecessarytositsometimes
morehoursinsolitudeinfrontthecomputerthanexpected.Therefore,thankyouSandra,
Lilli,andHenryforyourunderstandingandsupport.Idedicatethisbooktomylovely
wifeSandra,whoistheheartofourfamily.
YvesSaarland,November2014

www.it-ebooks.info


PartI.PythonandFinance
ThispartintroducesPythonforfinance.Itconsistsofthreechapters:

Chapter1brieflydiscussesPythoningeneralandargueswhyPythonisindeedwell
suitedtoaddressthetechnologicalchallengesinthefinanceindustryandinfinancial
(data)analytics.
Chapter2,onPythoninfrastructureandtools,ismeanttoprovideaconciseoverview
ofthemostimportantthingsyouhavetoknowtogetstartedwithinteractive
analyticsandapplicationdevelopmentinPython;therelatedAppendixAsurveys
someselectedbestpracticesforPythondevelopment.
Chapter3immediatelydivesintothreespecificfinancialexamples;itillustrateshow

tocalculateimpliedvolatilitiesofoptionswithPython,howtosimulateafinancial
modelwithPythonandthearraylibraryNumPy,andhowtoimplementabacktesting
foratrend-basedinvestmentstrategy.Thischaptershouldgivethereaderafeeling
forwhatitmeanstousePythonforfinancialanalytics—detailsarenotthat
importantatthisstage;theyareallexplainedinPartII.

www.it-ebooks.info


Chapter1.WhyPythonforFinance?
Banksareessentiallytechnologyfirms.
—HugoBanziger

www.it-ebooks.info


WhatIsPython?
Pythonisahigh-level,multipurposeprogramminglanguagethatisusedinawiderangeof
domainsandtechnicalfields.OnthePythonwebsiteyoufindthefollowingexecutive

summary(cf. />Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its highlevel built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for
RapidApplicationDevelopment,aswellasforuseasascriptingorgluelanguagetoconnectexistingcomponents
together.Python’ssimple,easytolearnsyntaxemphasizesreadabilityandthereforereducesthecostofprogram
maintenance.Pythonsupportsmodulesandpackages,whichencouragesprogrammodularityandcodereuse.The
Pythoninterpreterandtheextensivestandardlibraryareavailableinsourceorbinaryformwithoutchargeforall
majorplatforms,andcanbefreelydistributed.

ThisprettywelldescribeswhyPythonhasevolvedintooneofthemajorprogramming
languagesasoftoday.Nowadays,Pythonisusedbythebeginnerprogrammeraswellas
bythehighlyskilledexpertdeveloper,atschools,inuniversities,atwebcompanies,in

largecorporationsandfinancialinstitutions,aswellasinanyscientificfield.
Amongothers,Pythonischaracterizedbythefollowingfeatures:
Opensource
Pythonandthemajorityofsupportinglibrariesandtoolsavailableareopensource

andgenerallycomewithquiteflexibleandopenlicenses.
Interpreted
ThereferenceCPythonimplementationisaninterpreterofthelanguagethat
translatesPythoncodeatruntimetoexecutablebytecode.
Multiparadigm
Pythonsupportsdifferentprogrammingandimplementationparadigms,suchas

objectorientationandimperative,functional,orproceduralprogramming.
Multipurpose
Pythoncanbeusedforrapid,interactivecodedevelopmentaswellasforbuilding

largeapplications;itcanbeusedforlow-levelsystemsoperationsaswellasforhighlevelanalyticstasks.
Cross-platform
Pythonisavailableforthemostimportantoperatingsystems,suchasWindows,
Linux,andMacOS;itisusedtobuilddesktopaswellaswebapplications;itcanbe

usedonthelargestclustersandmostpowerfulserversaswellasonsuchsmall
devicesastheRaspberryPi(cf.).
Dynamicallytyped
TypesinPythonareingeneralinferredduringruntimeandnotstaticallydeclaredas
inmostcompiledlanguages.
Indentationaware
Incontrasttothemajorityofotherprogramminglanguages,Pythonusesindentation
www.it-ebooks.info



formarkingcodeblocksinsteadofparentheses,brackets,orsemicolons.
Garbagecollecting
Pythonhasautomatedgarbagecollection,avoidingtheneedfortheprogrammerto

managememory.
WhenitcomestoPythonsyntaxandwhatPythonisallabout,PythonEnhancement
Proposal20—i.e.,theso-called“ZenofPython”—providesthemajorguidelines.Itcan
beaccessedfromeveryinteractiveshellwiththecommandimportthis:
$ipython
Python2.7.6|Anaconda1.9.1(x86_64)|(default,Jan102014,11:23:15)
Type“copyright”,“credits”or“license”formoreinformation.
IPython2.0.0—AnenhancedInteractivePython.
?->IntroductionandoverviewofIPython’sfeatures.
%quickref->Quickreference.
help->Python’sownhelpsystem.
object?->Detailsabout‘object’,use‘object??’forextradetails.
In[1]:importthis
TheZenofPython,byTimPeters
Beautifulisbetterthanugly.
Explicitisbetterthanimplicit.
Simpleisbetterthancomplex.
Complexisbetterthancomplicated.
Flatisbetterthannested.
Sparseisbetterthandense.
Readabilitycounts.
Specialcasesaren’tspecialenoughtobreaktherules.
Althoughpracticalitybeatspurity.
Errorsshouldneverpasssilently.
Unlessexplicitlysilenced.

Inthefaceofambiguity,refusethetemptationtoguess.
Thereshouldbeone—andpreferablyonlyone—obviouswaytodoit.
Althoughthatwaymaynotbeobviousatfirstunlessyou’reDutch.
Nowisbetterthannever.
Althoughneverisoftenbetterthan*right*now.
Iftheimplementationishardtoexplain,it’sabadidea.
Iftheimplementationiseasytoexplain,itmaybeagoodidea.
Namespacesareonehonkinggreatidea—let’sdomoreofthose!

BriefHistoryofPython
AlthoughPythonmightstillhavetheappealofsomethingnewtosomepeople,ithasbeen
aroundforquitealongtime.Infact,developmenteffortsbeganinthe1980sbyGuidovan
RossumfromtheNetherlands.HeisstillactiveinPythondevelopmentandhasbeen
awardedthetitleofBenevolentDictatorforLifebythePythoncommunity(cf.
Thefollowingcanbeconsidered
milestonesinthedevelopmentofPython:

Python0.9.0releasedin1991(firstrelease)
Python1.0releasedin1994
Python2.0releasedin2000
Python2.6releasedin2008
Python2.7releasedin2010
Python3.0releasedin2008
Python3.3releasedin2010
Python3.4releasedin2014

www.it-ebooks.info


Itisremarkable,andsometimesconfusingtoPythonnewcomers,thattherearetwomajor

versionsavailable,stillbeingdevelopedand,moreimportantly,inparallelusesince2008.
Asofthiswriting,thiswillkeeponforquiteawhilesinceneitheristhere100%code
compatibilitybetweentheversions,norareallpopularlibrariesavailableforPython3.x.
ThemajorityofcodeavailableandinproductionisstillPython2.6/2.7,andthisbookis
basedonthe2.7.xversion,althoughthemajorityofcodeexamplesshouldworkwith
versions3.xaswell.

ThePythonEcosystem
AmajorfeatureofPythonasanecosystem,comparedtojustbeingaprogramming
language,istheavailabilityofalargenumberoflibrariesandtools.Theselibrariesand
toolsgenerallyhavetobeimportedwhenneeded(e.g.,aplottinglibrary)orhavetobe
startedasaseparatesystemprocess(e.g.,aPythondevelopmentenvironment).Importing
meansmakingalibraryavailabletothecurrentnamespaceandthecurrentPython
interpreterprocess.
Pythonitselfalreadycomeswithalargesetoflibrariesthatenhancethebasicinterpreter

indifferentdirections.Forexample,basicmathematicalcalculationscanbedonewithout
anyimporting,whilemorecomplexmathematicalfunctionsneedtobeimportedthrough
themathlibrary:
In[2]:100*2.5+50
Out[2]:300.0
In[3]:log(1)

NameError:name‘log’isnotdefined
In[4]:frommathimport*
In[5]:log(1)
Out[5]:0.0

Althoughtheso-called“starimport”(i.e.,thepracticeofimportingeverythingfroma
libraryviafromlibraryimport*)issometimesconvenient,oneshouldgenerallyuse

analternativeapproachthatavoidsambiguitywithregardtonamespacesand
relationshipsoffunctionstolibraries.Thisthentakesontheform:
In[6]:importmath
In[7]:math.log(1)
Out[7]:0.0

WhilemathisastandardPythonlibraryavailablewithanyinstallation,therearemany
morelibrariesthatcanbeinstalledoptionallyandthatcanbeusedintheverysame
fashionasthestandardlibraries.Suchlibrariesareavailablefromdifferent(web)sources.
However,itisgenerallyadvisabletouseaPythondistributionthatmakessurethatall
librariesareconsistentwitheachother(seeChapter2formoreonthistopic).
ThecodeexamplespresentedsofaralluseIPython(cf.),whichis
probablythemostpopularinteractivedevelopmentenvironment(IDE)forPython.
Althoughitstartedoutasanenhancedshellonly,ittodayhasmanyfeaturestypically
foundinIDEs(e.g.,supportforprofilinganddebugging).Thosefeaturesmissingare
typicallyprovidedbyadvancedtext/codeeditors,likeSublimeText(cf.
).Therefore,itisnotunusualtocombineIPythonwithone’s
www.it-ebooks.info


text/codeeditorofchoicetoformthebasictoolsetforaPythondevelopmentprocess.
IPythonisalsosometimescalledthekillerapplicationofthePythonecosystem.It

enhancesthestandardinteractiveshellinmanyways.Forexample,itprovidesimproved
command-linehistoryfunctionsandallowsforeasyobjectinspection.Forinstance,the
helptextforafunctionisprintedbyjustaddinga?behindthefunctionname(adding??
willprovideevenmoreinformation):
In[8]:math.log?
Type:builtin_function_or_method
StringForm:<built-infunctionlog>

Docstring:
log(x[,base])
Returnthelogarithmofxtothegivenbase.
Ifthebasenotspecified,returnsthenaturallogarithm(basee)ofx.
In[9]:

IPythoncomesinthreedifferentversions:ashellversion,onebasedonaQTgraphical
userinterface(theQTconsole),andabrowser-basedversion(theNotebook).Thisisjust

meantasateaser;thereisnoneedtoworryaboutthedetailsnowsinceChapter2
introducesIPythoninmoredetail.

PythonUserSpectrum
Pythondoesnotonlyappealtoprofessionalsoftwaredevelopers;itisalsoofuseforthe

casualdeveloperaswellasfordomainexpertsandscientificdevelopers.
Professionalsoftwaredevelopersfindallthattheyneedtoefficientlybuildlarge
applications.Almostallprogrammingparadigmsaresupported;therearepowerful
developmenttoolsavailable;andanytaskcan,inprinciple,beaddressedwithPython.
Thesetypesofuserstypicallybuildtheirownframeworksandclasses,alsoworkonthe
fundamentalPythonandscientificstack,andstrivetomakethemostoftheecosystem.
Scientificdevelopersordomainexpertsaregenerallyheavyusersofcertainlibrariesand
frameworks,havebuilttheirownapplicationsthattheyenhanceandoptimizeovertime,
andtailortheecosystemtotheirspecificneeds.Thesegroupsofusersalsogenerally
engageinlongerinteractivesessions,rapidlyprototypingnewcodeaswellasexploring
andvisualizingtheirresearchand/ordomaindatasets.
CasualprogrammersliketousePythongenerallyforspecificproblemstheyknowthat
Pythonhasitsstrengthsin.Forexample,visitingthegallerypageofmatplotlib,copying
acertainpieceofvisualizationcodeprovidedthere,andadjustingthecodetotheirspecific
needsmightbeabeneficialusecaseformembersofthisgroup.

ThereisalsoanotherimportantgroupofPythonusers:beginnerprogrammers,i.e.,those
thatarejuststartingtoprogram.Nowadays,Pythonhasbecomeaverypopularlanguage
atuniversities,colleges,andevenschoolstointroducestudentstoprogramming.[1]A
majorreasonforthisisthatitsbasicsyntaxiseasytolearnandeasytounderstand,even
forthenondeveloper.Inaddition,itishelpfulthatPythonsupportsalmostall
programmingstyles.[2]

TheScientificStack
Thereisacertainsetoflibrariesthatiscollectivelylabeledthescientificstack.Thisstack
www.it-ebooks.info


comprises,amongothers,thefollowinglibraries:
NumPy
NumPyprovidesamultidimensionalarrayobjecttostorehomogenousor

heterogeneousdata;italsoprovidesoptimizedfunctions/methodstooperateonthis
arrayobject.
SciPy
SciPyisacollectionofsublibrariesandfunctionsimplementingimportantstandard

functionalityoftenneededinscienceorfinance;forexample,youwillfindfunctions
forcubicsplinesinterpolationaswellasfornumericalintegration.
matplotlib

ThisisthemostpopularplottingandvisualizationlibraryforPython,providingboth
2Dand3Dvisualizationcapabilities.
PyTables
PyTablesisapopularwrapperfortheHDF5datastoragelibrary(cf.


itisalibrarytoimplementoptimized,disk-based
I/Ooperationsbasedonahierarchicaldatabase/fileformat.
pandas
pandasbuildsonNumPyandprovidesricherclassesforthemanagementandanalysis
oftimeseriesandtabulardata;itistightlyintegratedwithmatplotlibforplotting
andPyTablesfordatastorageandretrieval.

Dependingonthespecificdomainorproblem,thisstackisenlargedbyadditional
libraries,whichmoreoftenthannothaveincommonthattheybuildontopofoneormore
ofthesefundamentallibraries.However,theleastcommondenominatororbasicbuilding
blockingeneralistheNumPyndarrayclass(cf.Chapter4).
TakingPythonasaprogramminglanguagealone,thereareanumberofotherlanguages
availablethatcanprobablykeepupwithitssyntaxandelegance.Forexample,Rubyis
quiteapopularlanguageoftencomparedtoPython.Onthelanguage’swebsiteyoufind
thefollowingdescription:
A dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant
syntaxthatisnaturaltoreadandeasytowrite.

ThemajorityofpeopleusingPythonwouldprobablyalsoagreewiththeexactsame
statementbeingmadeaboutPythonitself.However,whatdistinguishesPythonformany
usersfromequallyappealinglanguageslikeRubyistheavailabilityofthescientificstack.
ThismakesPythonnotonlyagoodandelegantlanguagetouse,butalsoonethatis
capableofreplacingdomain-specificlanguagesandtoolsetslikeMatlaborR.Inaddition,
itprovidesbydefaultanythingthatyouwouldexpect,say,asaseasonedwebdeveloperor
systemsadministrator.

www.it-ebooks.info


TechnologyinFinance

NowthatwehavesomeroughideasofwhatPythonisallabout,itmakessensetostep
backabitandtobrieflycontemplatetheroleoftechnologyinfinance.Thiswillputusin
apositiontobetterjudgetherolePythonalreadyplaysand,evenmoreimportantly,will
probablyplayinthefinancialindustryofthefuture.
Inasense,technologyperseisnothingspecialtofinancialinstitutions(ascompared,for
instance,toindustrialcompanies)ortothefinancefunction(ascomparedtoother
corporatefunctions,likelogistics).However,inrecentyears,spurredbyinnovationand
alsoregulation,banksandotherfinancialinstitutionslikehedgefundshaveevolvedmore
andmoreintotechnologycompaniesinsteadofbeingjustfinancialintermediaries.
Technologyhasbecomeamajorassetforalmostanyfinancialinstitutionaroundthe
globe,havingthepotentialtoleadtocompetitiveadvantagesaswellasdisadvantages.
Somebackgroundinformationcanshedlightonthereasonsforthisdevelopment.

TechnologySpending
Banksandfinancialinstitutionstogetherformtheindustrythatspendsthemoston
technologyonanannualbasis.Thefollowingstatementthereforeshowsnotonlythat
technologyisimportantforthefinancialindustry,butthatthefinancialindustryisalso
reallyimportanttothetechnologysector:
Bankswillspend4.2%moreontechnologyin2014thantheydidin2013,accordingtoIDCanalysts.OverallIT
spend in financial services globally will exceed $430 billion in 2014 and surpass $500 billion by 2020, the
analystssay.
—Crosman2013

Large,multinationalbankstodaygenerallyemploythousandsofdevelopersthatmaintain
existingsystemsandbuildnewones.Largeinvestmentbankswithheavytechnological
requirementsshowtechnologybudgetsoftenofseveralbillionUSDperyear.

TechnologyasEnabler
Thetechnologicaldevelopmenthasalsocontributedtoinnovationsandefficiency
improvementsinthefinancialsector:

Technologicalinnovationshavecontributedsignificantlytogreaterefficiencyinthederivativesmarket.Through
innovationsintradingtechnology,tradesatEurexaretodayexecutedmuchfasterthantenyearsagodespitethe
strong increase in trading volume and the number of quotes … These strong improvements have only been
possibleduetotheconstant,highITinvestmentsbyderivativesexchangesandclearinghouses.
—DeutscheBörseGroup2008

Asasideeffectoftheincreasingefficiency,competitiveadvantagesmustoftenbelooked
forinevermorecomplexproductsortransactions.Thisinturninherentlyincreasesrisks
andmakesriskmanagementaswellasoversightandregulationmoreandmoredifficult.
Thefinancialcrisisof2007and2008tellsthestoryofpotentialdangersresultingfrom
suchdevelopments.Inasimilarvein,“algorithmsandcomputersgonewild”also
representapotentialrisktothefinancialmarkets;thismaterializeddramaticallyinthesocalledflashcrashofMay2010,whereautomatedsellingledtolargeintradaydropsin
certainstocksandstockindices(cf. />
TechnologyandTalentasBarrierstoEntry
www.it-ebooks.info


Ontheonehand,technologyadvancesreducecostovertime,ceterisparibus.Ontheother
hand,financialinstitutionscontinuetoinvestheavilyintechnologytobothgainmarket
shareanddefendtheircurrentpositions.Tobeactiveincertainareasinfinancetoday
oftenbringswithittheneedforlarge-scaleinvestmentsinbothtechnologyandskilled
staff.Asanexample,considerthederivativesanalyticsspace(seealsothecasestudyin
PartIIIofthebook):
Aggregatedoverthetotalsoftwarelifecycle,firmsadoptingin-housestrategiesforOTC[derivatives]pricingwill
require investments between $25 million and $36 million alone to build, maintain, and enhance a complete
derivativeslibrary.
—Ding2010

Notonlyisitcostlyandtime-consumingtobuildafull-fledgedderivativesanalytics
library,butyoualsoneedtohaveenoughexpertstodoso.Andtheseexpertshavetohave

therighttoolsandtechnologiesavailabletoaccomplishtheirtasks.
AnotherquoteabouttheearlydaysofLong-TermCapitalManagement(LTCM),formerly
oneofthemostrespectedquantitativehedgefunds—which,however,wentbustinthe
late1990s—furthersupportsthisinsightabouttechnologyandtalent:
Meriwetherspent$20milliononastate-of-the-artcomputersystemandhiredacrackteamoffinancialengineers
toruntheshowatLTCM,whichsetupshopinGreenwich,Connecticut.Itwasriskmanagementonanindustrial
level.
—Patterson2010

ThesamecomputingpowerthatMeriwetherhadtobuyformillionsofdollarsistoday
probablyavailableforthousands.Ontheotherhand,trading,pricing,andrisk
managementhavebecomesocomplexforlargerfinancialinstitutionsthattodaytheyneed
todeployITinfrastructureswithtensofthousandsofcomputingcores.

Ever-IncreasingSpeeds,Frequencies,DataVolumes
Thereisonedimensionofthefinanceindustrythathasbeeninfluencedmostby
technologicaladvances:thespeedandfrequencywithwhichfinancialtransactionsare
decidedandexecuted.TherecentbookbyLewis(2014)describesso-calledflashtrading
—i.e.,tradingatthehighestspeedspossible—invividdetail.
Ontheonehand,increasingdataavailabilityonever-smallerscalesmakesitnecessaryto
reactinrealtime.Ontheotherhand,theincreasingspeedandfrequencyoftradingletthe
datavolumesfurtherincrease.Thisleadstoprocessesthatreinforceeachotherandpush
theaveragetimescaleforfinancialtransactionssystematicallydown:
Renaissance’s Medallion fund gained an astonishing 80 percent in 2008, capitalizing on the market’s extreme
volatility with its lightning-fast computers. Jim Simons was the hedge fund world’s top earner for the year,
pocketingacool$2.5billion.
—Patterson2010

Thirtyyears’worthofdailystockpricedataforasinglestockrepresentsroughly7,500
quotes.Thiskindofdataiswhatmostoftoday’sfinancetheoryisbasedon.Forexample,

theorieslikethemodernportfoliotheory(MPT),thecapitalassetpricingmodel(CAPM),
andvalue-at-risk(VaR)allhavetheirfoundationsindailystockpricedata.
Incomparison,onatypicaltradingdaythestockpriceofAppleInc.(AAPL)isquoted
around15,000times—twotimesasmanyquotesasseenforend-of-dayquotingovera

www.it-ebooks.info


timespanof30years.Thisbringswithitanumberofchallenges:
Dataprocessing
Itdoesnotsufficetoconsiderandprocessend-of-dayquotesforstocksorother
financialinstruments;“toomuch”happensduringthedayforsomeinstruments
during24hoursfor7daysaweek.
Analyticsspeed
Decisionsoftenhavetobemadeinmillisecondsorevenfaster,makingitnecessary
tobuildtherespectiveanalyticscapabilitiesandtoanalyzelargeamountsofdatain
realtime.
Theoreticalfoundations
Althoughtraditionalfinancetheoriesandconceptsarefarfrombeingperfect,they
havebeenwelltested(andsometimeswellrejected)overtime;forthemillisecond
scalesimportantasoftoday,consistentconceptsandtheoriesthathaveproventobe
somewhatrobustovertimearestillmissing.
Allthesechallengescaninprincipleonlybeaddressedbymoderntechnology.Something
thatmightalsobealittlebitsurprisingisthatthelackofconsistenttheoriesoftenis
addressedbytechnologicalapproaches,inthathigh-speedalgorithmsexploitmarket
microstructureelements(e.g.,orderflow,bid-askspreads)ratherthanrelyingonsome
kindoffinancialreasoning.

TheRiseofReal-TimeAnalytics
Thereisonedisciplinethathasseenastrongincreaseinimportanceinthefinance

industry:financialanddataanalytics.Thisphenomenonhasacloserelationshiptothe
insightthatspeeds,frequencies,anddatavolumesincreaseatarapidpaceintheindustry.
Infact,real-timeanalyticscanbeconsideredtheindustry’sanswertothistrend.
Roughlyspeaking,“financialanddataanalytics”referstothedisciplineofapplying
softwareandtechnologyincombinationwith(possiblyadvanced)algorithmsandmethods
togather,process,andanalyzedatainordertogaininsights,tomakedecisions,orto
fulfillregulatoryrequirements,forinstance.Examplesmightincludetheestimationof
salesimpactsinducedbyachangeinthepricingstructureforafinancialproductinthe
retailbranchofabank.Anotherexamplemightbethelarge-scaleovernightcalculationof
creditvalueadjustments(CVA)forcomplexportfoliosofderivativestradesofan
investmentbank.
Therearetwomajorchallengesthatfinancialinstitutionsfaceinthiscontext:
Bigdata
Banksandotherfinancialinstitutionshadtodealwithmassiveamountsofdataeven
beforetheterm“bigdata”wascoined;however,theamountofdatathathastobe
processedduringsingleanalyticstaskshasincreasedtremendouslyovertime,
demandingbothincreasedcomputingpowerandever-largermemoryandstorage
capacities.
Real-timeeconomy
www.it-ebooks.info


Inthepast,decisionmakerscouldrelyonstructured,regularplanning,decision,and
(risk)managementprocesses,whereastheytodayfacetheneedtotakecareofthese
functionsinrealtime;severaltasksthathavebeentakencareofinthepastvia
overnightbatchrunsinthebackofficehavenowbeenmovedtothefrontofficeand
areexecutedinrealtime.
Again,onecanobserveaninterplaybetweenadvancesintechnologyand
financial/businesspractice.Ontheonehand,thereistheneedtoconstantlyimprove
analyticsapproachesintermsofspeedandcapabilitybyapplyingmoderntechnologies.

Ontheotherhand,advancesonthetechnologysideallownewanalyticsapproachesthat
wereconsideredimpossible(orinfeasibleduetobudgetconstraints)acoupleofyearsor
evenmonthsago.
Onemajortrendintheanalyticsspacehasbeentheutilizationofparallelarchitectureson
theCPU(centralprocessingunit)sideandmassivelyparallelarchitecturesontheGPGPU
(general-purposegraphicalprocessingunits)side.CurrentGPGPUsoftenhavemorethan
1,000computingcores,makingnecessaryasometimesradicalrethinkingofwhat
parallelismmightmeantodifferentalgorithms.Whatisstillanobstacleinthisregardis
thatusersgenerallyhavetolearnnewparadigmsandtechniquestoharnessthepowerof
suchhardware.[3]

www.it-ebooks.info


PythonforFinance
Theprevioussectiondescribessomeselectedaspectscharacterizingtheroleoftechnology
infinance:

Costsfortechnologyinthefinanceindustry
Technologyasanenablerfornewbusinessandinnovation
Technologyandtalentasbarrierstoentryinthefinanceindustry
Increasingspeeds,frequencies,anddatavolumes
Theriseofreal-timeanalytics
Inthissection,wewanttoanalyzehowPythoncanhelpinaddressingseveralofthe
challengesimpliedbytheseaspects.Butfirst,onamorefundamentallevel,letusexamine
Pythonforfinancefromalanguageandsyntaxstandpoint.

FinanceandPythonSyntax
MostpeoplewhomaketheirfirststepswithPythoninafinancecontextmayattackan
algorithmicproblem.Thisissimilartoascientistwho,forexample,wantstosolvea

differentialequation,wantstoevaluateanintegral,orsimplywantstovisualizesomedata.
Ingeneral,atthisstage,thereisonlylittlethoughtspentontopicslikeaformal
developmentprocess,testing,documentation,ordeployment.However,thisespecially
seemstobethestagewhenpeoplefallinlovewithPython.Amajorreasonforthismight
bethatthePythonsyntaxisgenerallyquiteclosetothemathematicalsyntaxusedto
describescientificproblemsorfinancialalgorithms.
Wecanillustratethisphenomenonbyasimplefinancialalgorithm,namelythevaluation
ofaEuropeancalloptionbyMonteCarlosimulation.WewillconsideraBlack-ScholesMerton(BSM)setup(seealsoChapter3)inwhichtheoption’sunderlyingriskfactor
followsageometricBrownianmotion.
Supposewehavethefollowingnumericalparametervaluesforthevaluation:

InitialstockindexlevelS0=100
StrikepriceoftheEuropeancalloptionK=105
Time-to-maturityT=1year
Constant,risklessshortrater=5%
Constantvolatility=20%
IntheBSMmodel,theindexlevelatmaturityisarandomvariable,givenbyEquation1-1
withzbeingastandardnormallydistributedrandomvariable.
Equation1-1.Black-Scholes-Merton(1973)indexlevelatmaturity

www.it-ebooks.info


ThefollowingisanalgorithmicdescriptionoftheMonteCarlovaluationprocedure:

1. DrawI(pseudo)randomnumbersz(i),i∈{1,2,…,I},fromthestandardnormal
distribution.
2. CalculateallresultingindexlevelsatmaturityST(i)forgivenz(i)andEquation1-1.
3. CalculateallinnervaluesoftheoptionatmaturityashT(i)=max(ST(i)–K,0).
4. EstimatetheoptionpresentvalueviatheMonteCarloestimatorgiveninEquation12.

Equation1-2.MonteCarloestimatorforEuropeanoption

WearenowgoingtotranslatethisproblemandalgorithmintoPythoncode.Thereader
mightfollowthesinglestepsbyusing,forexample,IPython—thisis,however,not
reallynecessaryatthisstage.
First,letusstartwiththeparametervalues.Thisisreallyeasy:
S0=100.
K=105.
T=1.0
r=0.05
sigma=0.2

Next,thevaluationalgorithm.Here,wewillforthefirsttimeuseNumPy,whichmakeslife
quiteeasyforoursecondtask:
fromnumpyimport*
I=100000
z=random.standard_normal(I)
ST=S0*exp((r-0.5*sigma**2)*T+sigma*sqrt(T)*z)
hT=maximum(ST-K,0)
C0=exp(-r*T)*sum(hT)/I

Third,weprinttheresult:
print“ValueoftheEuropeanCallOption%5.3f”%C0

Theoutputmightbe:[4]
ValueoftheEuropeanCallOption8.019

Threeaspectsareworthhighlighting:
Syntax
ThePythonsyntaxisindeedquiteclosetothemathematicalsyntax,e.g.,whenit

comestotheparametervalueassignments.
Translation
Everymathematicaland/oralgorithmicstatementcangenerallybetranslatedintoa
www.it-ebooks.info


singlelineofPythoncode.
Vectorization
OneofthestrengthsofNumPyisthecompact,vectorizedsyntax,e.g.,allowingfor
100,000calculationswithinasinglelineofcode.
ThiscodecanbeusedinaninteractiveenvironmentlikeIPython.However,codethatis
meanttobereusedregularlytypicallygetsorganizedinso-calledmodules(orscripts),
whicharesinglePython(i.e.,text)fileswiththesuffix.py.Suchamodulecouldinthis
caselooklikeExample1-1andcouldbesavedasafilenamedbsm_mcs_euro.py.
Example1-1.MonteCarlovaluationofEuropeancalloption
#
#MonteCarlovaluationofEuropeancalloption
#inBlack-Scholes-Mertonmodel
#bsm_mcs_euro.py
#
importnumpyasnp
#ParameterValues
S0=100.#initialindexlevel
K=105.#strikeprice
T=1.0#time-to-maturity
r=0.05#risklessshortrate
sigma=0.2#volatility
I=100000#numberofsimulations
#ValuationAlgorithm
z=np.random.standard_normal(I)#pseudorandomnumbers

ST=S0*np.exp((r-0.5*sigma**2)*T+sigma*np.sqrt(T)*z)
#indexvaluesatmaturity
hT=np.maximum(ST-K,0)#innervaluesatmaturity
C0=np.exp(-r*T)*np.sum(hT)/I#MonteCarloestimator
#ResultOutput
print“ValueoftheEuropeanCallOption%5.3f”%C0

TherathersimplealgorithmicexampleinthissubsectionillustratesthatPython,withits
verysyntax,iswellsuitedtocomplementtheclassicduoofscientificlanguages,English
andMathematics.ItseemsthataddingPythontothesetofscientificlanguagesmakesit
morewellrounded.Wehave

Englishforwriting,talkingaboutscientificandfinancialproblems,etc.
Mathematicsforconciselyandexactlydescribingandmodelingabstractaspects,
algorithms,complexquantities,etc.
Pythonfortechnicallymodelingandimplementingabstractaspects,algorithms,
complexquantities,etc.
MATHEMATICSANDPYTHONSYNTAX
ThereishardlyanyprogramminglanguagethatcomesasclosetomathematicalsyntaxasPython.Numerical
algorithmsarethereforesimpletotranslatefromthemathematicalrepresentationintothePythonic
implementation.Thismakesprototyping,development,andcodemaintenanceinsuchareasquiteefficientwith
Python.

Insomeareas,itiscommonpracticetousepseudocodeandtherewithtointroducea
fourthlanguagefamilymember.Theroleofpseudocodeistorepresent,forexample,
financialalgorithmsinamoretechnicalfashionthatisbothstillclosetothemathematical

www.it-ebooks.info



representationandalreadyquiteclosetothetechnicalimplementation.Inadditiontothe
algorithmitself,pseudocodetakesintoaccounthowcomputersworkinprinciple.
Thispracticegenerallyhasitscauseinthefactthatwithmostprogramminglanguagesthe
technicalimplementationisquite“faraway”fromitsformal,mathematicalrepresentation.
Themajorityofprogramminglanguagesmakeitnecessarytoincludesomanyelements
thatareonlytechnicallyrequiredthatitishardtoseetheequivalencebetweenthe
mathematicsandthecode.
Nowadays,Pythonisoftenusedinapseudocodewaysinceitssyntaxisalmostanalogous
tothemathematicsandsincethetechnical“overhead”iskepttoaminimum.Thisis
accomplishedbyanumberofhigh-levelconceptsembodiedinthelanguagethatnotonly
havetheiradvantagesbutalsocomeingeneralwithrisksand/orothercosts.However,itis
safetosaythatwithPythonyoucan,whenevertheneedarises,followthesamestrict
implementationandcodingpracticesthatotherlanguagesmightrequirefromtheoutset.In
thatsense,Pythoncanprovidethebestofbothworlds:high-levelabstractionandrigorous
implementation.

EfficiencyandProductivityThroughPython
Atahighlevel,benefitsfromusingPythoncanbemeasuredinthreedimensions:
Efficiency
HowcanPythonhelpingettingresultsfaster,insavingcosts,andinsavingtime?
Productivity
HowcanPythonhelpingettingmoredonewiththesameresources(people,assets,
etc.)?
Quality
WhatdoesPythonallowustodothatwecouldnotdowithalternativetechnologies?
Adiscussionoftheseaspectscanbynaturenotbeexhaustive.However,itcanhighlight
someargumentsasastartingpoint.
Shortertime-to-results
AfieldwheretheefficiencyofPythonbecomesquiteobviousisinteractivedataanalytics.
ThisisafieldthatbenefitsstronglyfromsuchpowerfultoolsasIPythonandlibrarieslike

pandas.
Considerafinancestudent,writinghermaster’sthesisandinterestedinGooglestock
prices.Shewantstoanalyzehistoricalstockpriceinformationfor,say,fiveyearstosee
howthevolatilityofthestockpricehasfluctuatedovertime.Shewantstofindevidence
thatvolatility,incontrasttosometypicalmodelassumptions,fluctuatesovertimeandis
farfrombeingconstant.Theresultsshouldalsobevisualized.Shemainlyhastodothe
following:

DownloadGooglestockpricedatafromtheWeb.
Calculatetherollingstandarddeviationofthelogreturns(volatility).
www.it-ebooks.info


Plotthestockpricedataandtheresults.
Thesetasksarecomplexenoughthatnottoolongagoonewouldhaveconsideredthemto
besomethingforprofessionalfinancialanalysts.Today,eventhefinancestudentcan
easilycopewithsuchproblems.Letusseehowexactlythisworks—withoutworrying
aboutsyntaxdetailsatthisstage(everythingisexplainedindetailinsubsequentchapters).
First,makesuretohaveavailableallnecessarylibraries:
In[1]:importnumpyasnp
importpandasaspd
importpandas.io.dataasweb

Second,retrievethedatafrom,say,Googleitself:
In[2]:goog=web.DataReader(‘GOOG’,data_source=‘google’,
start=‘3/14/2009’,end=‘4/14/2014’)
goog.tail()
Out[2]:OpenHighLowCloseVolume
Date
2014-04-08542.60555.00541.61554.903152406

2014-04-09559.62565.37552.95564.143324742
2014-04-10565.00565.00539.90540.954027743
2014-04-11532.55540.00526.53530.603916171
2014-04-14538.25544.10529.56532.522568020
5rows×5columns

Third,implementthenecessaryanalyticsforthevolatilities:
In[3]:goog[‘Log_Ret’]=np.log(goog[‘Close’]/goog[‘Close’].shift(1))
goog[‘Volatility’]=pd.rolling_std(goog[‘Log_Ret’],
window=252)*np.sqrt(252)

Fourth,plottheresults.Togenerateaninlineplot,weusetheIPythonmagiccommand
%matplotlibwiththeoptioninline:
In[4]:%matplotlibinline
goog[[‘Close’,‘Volatility’]].plot(subplots=True,color=‘blue’,
figsize=(8,6))

Figure1-1showsthegraphicalresultofthisbriefinteractivesessionwithIPython.Itcan
beconsideredalmostamazingthatfourlinesofcodesufficetoimplementthreerather
complextaskstypicallyencounteredinfinancialanalytics:datagathering,complexand
repeatedmathematicalcalculations,andvisualizationofresults.Thisexampleillustrates
thatpandasmakesworkingwithwholetimeseriesalmostassimpleasdoing
mathematicaloperationsonfloating-pointnumbers.

www.it-ebooks.info


Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×