R & Python
R & Python
Quantitative Finance
An Introduction
Luca Sbardella
luca.sbardella@gmail.com
@lsbardel
LondonR - Nov 09
Overview
1)Puttingthingsintocontext
2)PythonandR
3)Examples
1Context
How can quantitative finance pratictioners best
leverage their expertise without reinventing the
wheel and spending lots of their precious time
writing low level code?
opensource technologies
CurrentApproach
ExtensiveuseoflargeproprietyC,C++orJavalibraries.
Serversideapplicationswrittenin
.Net/C#Windowsservers
JavaWindows&Linux/Unix
VBA/Excelontheclientside:(
Someweb/basedclients:)
Limitedifinexistentuseofpowerfulopensource
technologies/libraries.
Problems
Fulldevelopmentcycleinlowlevellanguagesisexpensive
C++:Python=7:1
Writingcodealreadydevelopedmanytimesbefore
Difficulttoadjusttonewtechnologiesastheybecome
available
Oftenlibrariesaredeployedontheclientmachine
Sometechnologies(.Net)forceadecisionontheplatform
tobeused(Windows)
Adifferentapproach
Limitlowleveldevelopmenttocriticalnumbercrunching
components
Useavailablehighstandardopensourcetechnologies
Flexible,multiplatformserversideconfiguration
Multilanguagesupport
Abstractionwhenpossible
Remoteprocedurecalls(RPC)
Aproposedsolution
UseofPythonasmainserversidedriver
LegacyC,C++asPythonmodules(BoostPython)
Interactionwithothertechnologies
Rforstatistics
Erlang/Haskellfunctionalprogrammingand
concurrency
qkdb+timeseriesdatabase(commercial)
Javascript/flexwebGUIfrontend
2PythonandR
Python?
General-purpose high-level
programming language
Large community
PythonFeatures
Dynamicallytyped
Multiparadigmdesign
ObjectOriented
Metaprogramming(reflection)
Functionalprogramming
Emphasizessemplicity
ExtendibilityviaCorC++(boost.python,Cpython)
Where?
FastPrototyping
Scientificprogramming(numpy,scipy,)
Databaseabstraction(pyodbc,django)
RPCservers(JSON,XML,Binary,...)
DomainSpecificLanguages(DSP)
Webservers(mod_python,twisted,...)
WebFrameworks(django,zope,...)
Glueforanythingyoucanthinkof
Rlanguage
OpensourceimplementationofS/SPLUS
Scriptinglanguagethatexcelsin
dataanalysis
statisticsandeconometric
graphics
Hugecollectionofstatisticalpackagesavailable
Canbeusedasstandaloneserversidelanguagebutnot
ideal
UsingRontheserverside
UseRonlyforwhathasbeendesignfor
Statistics&Math
EmbedRwithinPythondomain
Installing
InstallPython
InstallRandanyRlibraryofyourchoice
Installnumpyandrpy2
LaunchPythonandrunrpy2test
$ python
>>>
>>>
>>>
>>>
3Examples
1)Sharingmemoryandarrays
2)Smallserverforrollingstatisticscalculation
3)AtimeseriesDSL(DomainSpecificLanguage).Not
presented.
3.1MemoryandVectors
Whenusingrpy2PythonandRdomainsarecoexisting
Pythonmanagesobjectspointingtodatastoredand
administeredintheRspace
RvariablesareexistingwithinanembeddedRworkspace,
andcanbeaccessedfromPythonthroughtheirpython
objectrepresentations(Sexpandsubclasses).
3.1MemoryandVectors
import rpy2.rinterface as ri
import numpy as ny
# Create an integer array in R domain
rx = ri.SexpVector([1,2,3,4], ri.INTSXP)
# Create a local copy in Python domain
nx = ny.array(rx)
# Proxy to R vector without coping
nx_nc = ny.asarray(rx)
3.2Asmallserver
AminimalistClientserverapplication
ThePythonserverusesRpackagestoperformtimeseries
analysisonhistoricalstockprices
TheserverexposesaRPCJSONinterfacetotheclient
Implementonefunctionwhichcalculateasimplerolling
movingavarage
Codestructure
Code can be found at http://github.com
http://github.com/lsbardel/statplay/tree/master/examples/londonr1109/
londonr1109/
__init__.py
roll.py
plot.py
server1.py
jsonrpc/
__init__.py
jsonlib.py
proxy.py
server.py
SystemRequirements
Python2.6orabove
R2.8orabove
rpy2andnumpy(corePythonRpackages)
matplotlib(forplottingclient)
proformanceAnalytics(Rpackage)
quantmod(Rpackage)
References
Python: www.python.org
R:www.rproject.org
rpy2:http://rpy.sourceforge.net/
numpy:www.numpy.org
matplotlib:http://matplotlib.sourceforge.net
twisted:http://twistedmatrix.com
boost:http://www.boost.org/