0% found this document useful (0 votes)
47 views22 pages

Python & R in Quantitative Finance

This document discusses using Python and R for quantitative finance. It proposes combining the two languages, with Python as the main server-side driver and R used for statistics and math via the rpy2 library. Examples show sharing memory/arrays between Python and R, and a simple stock statistics server in Python that exposes an RPC interface and uses R packages to calculate rolling averages.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views22 pages

Python & R in Quantitative Finance

This document discusses using Python and R for quantitative finance. It proposes combining the two languages, with Python as the main server-side driver and R used for statistics and math via the rpy2 library. Examples show sharing memory/arrays between Python and R, and a simple stock statistics server in Python that exposes an RPC interface and uses R packages to calculate rolling averages.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • Introduction
  • Context
  • Python and R
  • Installation
  • Examples
  • System Requirements
  • References

Python and R for

Quantitative Finance
An Introduction
Luca Sbardella

[Link]@[Link]
@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

Combine two of the most used and powerful opensource


languagestocreateatrulydynamicapplicationframework.
Benefitfromanactivecommunityofdevelopersandavast
arrayofreadyavailablelibraries.
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++([Link],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 = [Link](verbosity = 1)
[Link]([Link]())

3Examples
1)Sharingmemoryandarrays
2)Smallserverforrollingstatisticscalculation
3)AtimeseriesDSL(DomainSpecificLanguage).Not
presented.

3.1MemoryandVectors

Whenusingrpy2PythonandRdomainsarecoexisting
Pythonmanagesobjectspointingtodatastoredand
administeredintheRspace
RvariablesareexistingwithinanembeddedRworkspace,
andcanbeaccessedfromPythonthroughtheirpython
objectrepresentations(Sexpandsubclasses).

3.1MemoryandVectors
import [Link] as ri
import numpy as ny
# Create an integer array in R domain
rx = [Link]([1,2,3,4], [Link])
# Create a local copy in Python domain
nx = [Link](rx)
# Proxy to R vector without coping
nx_nc = [Link](rx)

3.2Asmallserver

AminimalistClientserverapplication
ThePythonserverusesRpackagestoperformtimeseries
analysisonhistoricalstockprices
TheserverexposesaRPCJSONinterfacetotheclient
Implementonefunctionwhichcalculateasimplerolling
movingavarage

Codestructure
Code can be found at [Link]
[Link]
londonr1109/
__init__.py
[Link]
[Link]
[Link]
jsonrpc/
__init__.py
[Link]
[Link]
[Link]

SystemRequirements

Python2.6orabove

R2.8orabove

rpy2andnumpy(corePythonRpackages)

matplotlib(forplottingclient)

proformanceAnalytics(Rpackage)

quantmod(Rpackage)

To start the server type


$ python [Link] 8080
The server is now ready to listen to requests on port 8080
To test the server the [Link] client can be used
To plot Google moving averages with rolloing window of 60
days type
$ python [Link] GOOG 60

References

Python: [Link]

R:[Link]

rpy2:[Link]

numpy:[Link]

matplotlib:[Link]

twisted:[Link]

boost:[Link]

 
 
Python and R for 
Quantitative Finance
An Introduction
Luca Sbardella
luca.sbardella@gmail.com
@lsbardel
LondonR - Nov 09
 
 
Overview
1) Putting things into context
2) Python and R
3) Examples
 
 
How can quantitative finance pratictioners best 
leverage their expertise without reinventing the 
wheel and spending lot
 
 
Current Approach
✔Extensive use of large propriety C, C++ or Java libraries.
✔Server­side applications written in
✔.Net/C
 
 
Problems
✔Full development cycle in low­level languages is expensive
C++ : Python = 7 : 1
✔Writing code already developed
 
 
A different approach
✔Limit low­level development to critical number­crunching 
components
✔Use available high­standard o
 
 
A proposed solution
✔Use of Python as main server side driver
✔Legacy C, C++ as Python modules (Boost­Python)
✔Interactio
 
 
2 ­ Python and R
✔Combine  two  of  the  most  used  and  powerful  opensource 
languages to create a truly dynamic appli
 
 
Python?
✔General-purpose high-level 
programming language
✔Named after Monty Python
✔by Guido Van Rossum
✔in the late 198
 
 
Python Features
✔Dynamically typed
✔Multi­paradigm design
✔Object Oriented
✔Metaprogramming (reflection)
✔Functional prog

You might also like