Top Banner
  Python and R for Quantitative Finance An Introduction Luca Sbardella [email protected] @lsbardel LondonR - Nov 09
22

R & Python

Nov 10, 2015

Download

Documents

Python and R for Quant Finance
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • Python and R for Quantitative Finance

    An Introduction

    Luca [email protected]

    @lsbardel

    LondonR - Nov 09

  • Overview

    1)Puttingthingsintocontext2)PythonandR3)Examples

  • 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

    1Context

  • 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

    Limitlowleveldevelopmenttocriticalnumbercrunchingcomponents

    Useavailablehighstandardopensourcetechnologies Flexible,multiplatformserversideconfiguration Multilanguagesupport Abstractionwhenpossible Remoteprocedurecalls(RPC)

  • Aproposedsolution

    UseofPythonasmainserversidedriver LegacyC,C++asPythonmodules(BoostPython) Interactionwithothertechnologies

    Rforstatistics Erlang/Haskellfunctionalprogrammingand

    concurrency qkdb+timeseriesdatabase(commercial) Javascript/flexwebGUIfrontend

  • 2PythonandR

    Combine two of the most used and powerful opensourcelanguagestocreateatrulydynamicapplicationframework.

    Benefitfromanactivecommunityofdevelopersandavastarrayofreadyavailablelibraries.

    SeeminglessintegrationwithC/C++libraries

  • Python?

    General-purpose high-level programming language

    Named after Monty Python by Guido Van Rossum in the late 1980s. 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

    >>> from rpy2 import tests>>> import unittest>>> tr = unittest.TextTestRunner(verbosity = 1)>>> tr.run(tests.suite())

  • 3Examples

    1)Sharingmemoryandarrays

    2)Smallserverforrollingstatisticscalculation

    3)AtimeseriesDSL(DomainSpecificLanguage).Notpresented.

  • 3.1MemoryandVectors

    Whenusingrpy2PythonandRdomainsarecoexisting Pythonmanagesobjectspointingtodatastoredand

    administeredintheRspace RvariablesareexistingwithinanembeddedRworkspace,

    andcanbeaccessedfromPythonthroughtheirpythonobjectrepresentations(Sexpandsubclasses).

  • 3.1MemoryandVectors

    import rpy2.rinterface as riimport numpy as ny

    # Create an integer array in R domainrx = ri.SexpVector([1,2,3,4], ri.INTSXP)

    # Create a local copy in Python domainnx = ny.array(rx)

    # Proxy to R vector without copingnx_nc = ny.asarray(rx)

  • 3.2Asmallserver

    AminimalistClientserverapplication ThePythonserverusesRpackagestoperformtimeseries

    analysisonhistoricalstockprices TheserverexposesaRPCJSONinterfacetotheclient Implementonefunctionwhichcalculateasimplerolling

    movingavarage

  • londonr1109/__init__.pyroll.pyplot.pyserver1.pyjsonrpc/

    __init__.pyjsonlib.pyproxy.pyserver.py

    Codestructure

    Code can be found at http://github.com

    http://github.com/lsbardel/statplay/tree/master/examples/londonr1109/

  • SystemRequirements

    Python2.6orabove R2.8orabove rpy2andnumpy(corePythonRpackages) matplotlib(forplottingclient) proformanceAnalytics(Rpackage) quantmod(Rpackage)

  • To start the server type

    $ python server1.py 8080

    The server is now ready to listen to requests on port 8080

    To test the server the plot.py client can be used

    To plot Google moving averages with rolloing window of 60 days type

    $ python plot.py GOOG 60

  • 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/

    Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22