SlideShare a Scribd company logo
1 of 22
Download to read offline
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




             
1 ­ Context


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



                   
Current Approach

✔   Extensive use of large propriety C, C++ or Java libraries.
✔   Server­side applications written in
        ✔    .Net/C# ­ Windows servers
        ✔    Java – Windows & Linux/Unix
✔   VBA/Excel on the client side :(
✔   Some web/based clients :)
✔   Limited if inexistent use of powerful opensource 
      technologies/libraries.



                           
Problems

✔   Full development cycle in low­level languages is expensive
             C++ : Python = 7 : 1
✔   Writing code already developed many times before
✔   Difficult to adjust to new technologies as they become 
     available
✔   Often libraries are deployed on the client machine
✔   Some technologies (.Net) force a decision on the platform 
     to be used (Windows)



                            
A different approach

✔   Limit low­level development to critical number­crunching 
      components
✔   Use available high­standard opensource technologies
✔   Flexible, multiplatform server­side configuration
✔   Multi­language support
✔   Abstraction when possible
✔   Remote procedure calls (RPC)




                          
A proposed solution

✔   Use of Python as main server side driver
✔   Legacy C, C++ as Python modules (Boost­Python)
✔   Interaction with other technologies
        ✔    R ­ for statistics
        ✔    Erlang/Haskell – functional programming and 
               concurrency
        ✔    q – kdb+ timeseries database (commercial)
        ✔    Javascript/flex – web GUI front­end



                              
2 ­ Python and R

✔   Combine  two  of  the  most  used  and  powerful  opensource 
    languages to create a truly dynamic application framework.
✔   Benefit from an active community of developers and a vast 
    array of ready available libraries.
✔   Seemingless integration with C/C++ libraries




                          
Python?
✔   General-purpose high-level
      programming language
✔   Named after Monty Python
✔   by Guido Van Rossum
✔   in the late 1980s.
✔   Large community




                          
Python Features

✔   Dynamically typed
✔   Multi­paradigm design
        ✔   Object Oriented
        ✔   Metaprogramming (reflection)
        ✔   Functional programming
✔   Emphasizes semplicity
✔   Extendibility via C or C++ (boost.python, Cpython)




                          
Where?

✔   Fast Prototyping
✔   Scientific programming (numpy, scipy, …)
✔   Database abstraction (pyodbc, django)
✔   RPC servers (JSON, XML, Binary, ...)
✔   Domain Specific Languages (DSP)
✔   Web servers (mod_python, twisted, ...)
✔   Web Frameworks (django, zope, ...)
✔   Glue for anything you can think of


                         
R language

✔   Open source implementation of S/SPLUS
✔   Scripting language that excels in
        ✔    data analysis
        ✔    statistics and econometric
        ✔    graphics
✔   Huge collection of statistical packages available
✔   Can be used as stand­alone server side language but not 
     ideal



                              
Using R on the server­side

✔   Use R only for what has been design for
        ✔   Statistics & Math
✔   Embed R within Python domain




                           
Installing
✔   Install Python
✔   Install R and any R library of your choice
✔   Install numpy and rpy2
✔   Launch Python and run rpy2 test


$ python

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



                           
3 ­ Examples

1) Sharing memory and arrays
2) Small server for rolling statistics calculation
3) A timeseries DSL (Domain Specific Language). Not 
    presented.




                         
3.1 Memory and Vectors

✔   When using rpy2 Python and R domains are co­existing
✔   Python manages objects pointing to data stored and 
     administered in the R space
✔   R variables are existing within an embedded R workspace, 
      and can be accessed from Python through their python 
      object representations (Sexp and subclasses).




                         
3.1 – Memory and Vectors
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.2 A small server

✔   A minimalist Client­server application
✔   The Python server uses R packages to perform time­series 
     analysis on historical stock prices
✔   The server exposes a RPC­JSON interface to the client
✔   Implement one function which calculate a simple rolling 
      moving avarage




                         
Code structure
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




                            
System Requirements

✔   Python 2.6 or above
✔   R 2.8 or above
✔   rpy2 and numpy (core Python­R packages)
✔   matplotlib (for plotting client)
✔   proformanceAnalytics (R package)
✔   quantmod (R package)




                           
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.r­project.org
✔   rpy2: http://rpy.sourceforge.net/
✔   numpy: www.numpy.org
✔   matplotlib: http://matplotlib.sourceforge.net
✔   twisted: http://twistedmatrix.com
✔   boost: http://www.boost.org/




                          

More Related Content

What's hot

Top 5 Python Libraries For Data Science | Python Libraries Explained | Python...
Top 5 Python Libraries For Data Science | Python Libraries Explained | Python...Top 5 Python Libraries For Data Science | Python Libraries Explained | Python...
Top 5 Python Libraries For Data Science | Python Libraries Explained | Python...Simplilearn
 
Big data in fintech ecosystem
Big data in fintech ecosystemBig data in fintech ecosystem
Big data in fintech ecosystemBBVA API Market
 
AWS Elastic Beanstalk - Running Microservices and Docker
AWS Elastic Beanstalk - Running Microservices and DockerAWS Elastic Beanstalk - Running Microservices and Docker
AWS Elastic Beanstalk - Running Microservices and DockerAmazon Web Services
 
Write Smart Contracts with Truffle Framework
Write Smart Contracts with Truffle FrameworkWrite Smart Contracts with Truffle Framework
Write Smart Contracts with Truffle FrameworkShun Shiku
 
How Banking as a Service Will Keep Banks Digitally Relevant and Growing
How Banking as a Service Will Keep Banks Digitally Relevant and GrowingHow Banking as a Service Will Keep Banks Digitally Relevant and Growing
How Banking as a Service Will Keep Banks Digitally Relevant and GrowingCognizant
 
Amazon EKS - Elastic Container Service for Kubernetes
Amazon EKS - Elastic Container Service for KubernetesAmazon EKS - Elastic Container Service for Kubernetes
Amazon EKS - Elastic Container Service for KubernetesAmazon Web Services
 
Amazon Redshift Tutorial | AWS Tutorial for Beginners | AWS Certification Tra...
Amazon Redshift Tutorial | AWS Tutorial for Beginners | AWS Certification Tra...Amazon Redshift Tutorial | AWS Tutorial for Beginners | AWS Certification Tra...
Amazon Redshift Tutorial | AWS Tutorial for Beginners | AWS Certification Tra...Edureka!
 
AWS Application Migration Service-Hands-On Guide
AWS Application Migration Service-Hands-On GuideAWS Application Migration Service-Hands-On Guide
AWS Application Migration Service-Hands-On GuideManas Mondal
 
Open Banking - The Digital Transformation Opportunity in Disguise
Open Banking - The Digital Transformation Opportunity in Disguise Open Banking - The Digital Transformation Opportunity in Disguise
Open Banking - The Digital Transformation Opportunity in Disguise WSO2
 
AWS 기반의 마이크로 서비스 아키텍쳐 구현 방안 :: 김필중 :: AWS Summit Seoul 20
AWS 기반의 마이크로 서비스 아키텍쳐 구현 방안 :: 김필중 :: AWS Summit Seoul 20AWS 기반의 마이크로 서비스 아키텍쳐 구현 방안 :: 김필중 :: AWS Summit Seoul 20
AWS 기반의 마이크로 서비스 아키텍쳐 구현 방안 :: 김필중 :: AWS Summit Seoul 20Amazon Web Services Korea
 
The role of IAM in OpenBanking and where do we stand
The role of IAM in OpenBanking and where do we stand The role of IAM in OpenBanking and where do we stand
The role of IAM in OpenBanking and where do we stand Pushpalanka Jayawardhana
 
Introduction to data analysis using python
Introduction to data analysis using pythonIntroduction to data analysis using python
Introduction to data analysis using pythonGuido Luz Percú
 
Python NumPy Tutorial | NumPy Array | Edureka
Python NumPy Tutorial | NumPy Array | EdurekaPython NumPy Tutorial | NumPy Array | Edureka
Python NumPy Tutorial | NumPy Array | EdurekaEdureka!
 
Databases - Choosing the right Database on AWS
Databases - Choosing the right Database on AWSDatabases - Choosing the right Database on AWS
Databases - Choosing the right Database on AWSAmazon Web Services
 

What's hot (20)

Top 5 Python Libraries For Data Science | Python Libraries Explained | Python...
Top 5 Python Libraries For Data Science | Python Libraries Explained | Python...Top 5 Python Libraries For Data Science | Python Libraries Explained | Python...
Top 5 Python Libraries For Data Science | Python Libraries Explained | Python...
 
Big data in fintech ecosystem
Big data in fintech ecosystemBig data in fintech ecosystem
Big data in fintech ecosystem
 
AWS Elastic Beanstalk - Running Microservices and Docker
AWS Elastic Beanstalk - Running Microservices and DockerAWS Elastic Beanstalk - Running Microservices and Docker
AWS Elastic Beanstalk - Running Microservices and Docker
 
Write Smart Contracts with Truffle Framework
Write Smart Contracts with Truffle FrameworkWrite Smart Contracts with Truffle Framework
Write Smart Contracts with Truffle Framework
 
How Banking as a Service Will Keep Banks Digitally Relevant and Growing
How Banking as a Service Will Keep Banks Digitally Relevant and GrowingHow Banking as a Service Will Keep Banks Digitally Relevant and Growing
How Banking as a Service Will Keep Banks Digitally Relevant and Growing
 
AWS IAM Introduction
AWS IAM IntroductionAWS IAM Introduction
AWS IAM Introduction
 
Amazon EKS - Elastic Container Service for Kubernetes
Amazon EKS - Elastic Container Service for KubernetesAmazon EKS - Elastic Container Service for Kubernetes
Amazon EKS - Elastic Container Service for Kubernetes
 
AWS Account Best Practices
AWS Account Best PracticesAWS Account Best Practices
AWS Account Best Practices
 
Amazon Redshift Tutorial | AWS Tutorial for Beginners | AWS Certification Tra...
Amazon Redshift Tutorial | AWS Tutorial for Beginners | AWS Certification Tra...Amazon Redshift Tutorial | AWS Tutorial for Beginners | AWS Certification Tra...
Amazon Redshift Tutorial | AWS Tutorial for Beginners | AWS Certification Tra...
 
AWS Application Migration Service-Hands-On Guide
AWS Application Migration Service-Hands-On GuideAWS Application Migration Service-Hands-On Guide
AWS Application Migration Service-Hands-On Guide
 
Introduction to Amazon EC2
Introduction to Amazon EC2Introduction to Amazon EC2
Introduction to Amazon EC2
 
Open Banking - The Digital Transformation Opportunity in Disguise
Open Banking - The Digital Transformation Opportunity in Disguise Open Banking - The Digital Transformation Opportunity in Disguise
Open Banking - The Digital Transformation Opportunity in Disguise
 
Amazon S3 Masterclass
Amazon S3 MasterclassAmazon S3 Masterclass
Amazon S3 Masterclass
 
AWS 기반의 마이크로 서비스 아키텍쳐 구현 방안 :: 김필중 :: AWS Summit Seoul 20
AWS 기반의 마이크로 서비스 아키텍쳐 구현 방안 :: 김필중 :: AWS Summit Seoul 20AWS 기반의 마이크로 서비스 아키텍쳐 구현 방안 :: 김필중 :: AWS Summit Seoul 20
AWS 기반의 마이크로 서비스 아키텍쳐 구현 방안 :: 김필중 :: AWS Summit Seoul 20
 
Open banking as a service
Open banking as a serviceOpen banking as a service
Open banking as a service
 
The role of IAM in OpenBanking and where do we stand
The role of IAM in OpenBanking and where do we stand The role of IAM in OpenBanking and where do we stand
The role of IAM in OpenBanking and where do we stand
 
Introduction to data analysis using python
Introduction to data analysis using pythonIntroduction to data analysis using python
Introduction to data analysis using python
 
AWS Lambda
AWS LambdaAWS Lambda
AWS Lambda
 
Python NumPy Tutorial | NumPy Array | Edureka
Python NumPy Tutorial | NumPy Array | EdurekaPython NumPy Tutorial | NumPy Array | Edureka
Python NumPy Tutorial | NumPy Array | Edureka
 
Databases - Choosing the right Database on AWS
Databases - Choosing the right Database on AWSDatabases - Choosing the right Database on AWS
Databases - Choosing the right Database on AWS
 

Similar to Python and R for quantitative finance

Python | What is Python | History of Python | Python Tutorial
Python | What is Python | History of Python | Python TutorialPython | What is Python | History of Python | Python Tutorial
Python | What is Python | History of Python | Python TutorialQA TrainingHub
 
Introduction of python programming
Introduction of python programmingIntroduction of python programming
Introduction of python programmingNitin Kumar Kashyap
 
Scylla Summit 2022: Learning Rust the Hard Way for a Production Kafka+ScyllaD...
Scylla Summit 2022: Learning Rust the Hard Way for a Production Kafka+ScyllaD...Scylla Summit 2022: Learning Rust the Hard Way for a Production Kafka+ScyllaD...
Scylla Summit 2022: Learning Rust the Hard Way for a Production Kafka+ScyllaD...ScyllaDB
 
Approaches to debugging mixed-language HPC apps
Approaches to debugging mixed-language HPC appsApproaches to debugging mixed-language HPC apps
Approaches to debugging mixed-language HPC appsRogue Wave Software
 
Python workshop
Python workshopPython workshop
Python workshopShiraz LUG
 
Streaming Data in R
Streaming Data in RStreaming Data in R
Streaming Data in RRory Winston
 
Python quick guide1
Python quick guide1Python quick guide1
Python quick guide1Kanchilug
 
Open source analytics
Open source analyticsOpen source analytics
Open source analyticsAjay Ohri
 
Cloud native IPC for Microservices Workshop @ Containerdays 2022
Cloud native IPC for Microservices Workshop @ Containerdays 2022Cloud native IPC for Microservices Workshop @ Containerdays 2022
Cloud native IPC for Microservices Workshop @ Containerdays 2022QAware GmbH
 
Ali alshehri c++_comparison between c++&python
Ali alshehri c++_comparison between c++&pythonAli alshehri c++_comparison between c++&python
Ali alshehri c++_comparison between c++&pythonAliAAAlshehri
 
An overview of data and web-application development with Python
An overview of data and web-application development with PythonAn overview of data and web-application development with Python
An overview of data and web-application development with PythonSivaranjan Goswami
 
R as supporting tool for analytics and simulation
R as supporting tool for analytics and simulationR as supporting tool for analytics and simulation
R as supporting tool for analytics and simulationAlvaro Gil
 
Python tutorial for beginners - Tib academy
Python tutorial for beginners - Tib academyPython tutorial for beginners - Tib academy
Python tutorial for beginners - Tib academyTIB Academy
 

Similar to Python and R for quantitative finance (20)

Python | What is Python | History of Python | Python Tutorial
Python | What is Python | History of Python | Python TutorialPython | What is Python | History of Python | Python Tutorial
Python | What is Python | History of Python | Python Tutorial
 
Introduction of python programming
Introduction of python programmingIntroduction of python programming
Introduction of python programming
 
Go at uber
Go at uberGo at uber
Go at uber
 
Pyhton-1a-Basics.pdf
Pyhton-1a-Basics.pdfPyhton-1a-Basics.pdf
Pyhton-1a-Basics.pdf
 
Scylla Summit 2022: Learning Rust the Hard Way for a Production Kafka+ScyllaD...
Scylla Summit 2022: Learning Rust the Hard Way for a Production Kafka+ScyllaD...Scylla Summit 2022: Learning Rust the Hard Way for a Production Kafka+ScyllaD...
Scylla Summit 2022: Learning Rust the Hard Way for a Production Kafka+ScyllaD...
 
Approaches to debugging mixed-language HPC apps
Approaches to debugging mixed-language HPC appsApproaches to debugging mixed-language HPC apps
Approaches to debugging mixed-language HPC apps
 
Python workshop
Python workshopPython workshop
Python workshop
 
Python workshop
Python workshopPython workshop
Python workshop
 
Learn python
Learn pythonLearn python
Learn python
 
PyData Boston 2013
PyData Boston 2013PyData Boston 2013
PyData Boston 2013
 
Python Online From EasyLearning Guru
Python Online From EasyLearning GuruPython Online From EasyLearning Guru
Python Online From EasyLearning Guru
 
Streaming Data in R
Streaming Data in RStreaming Data in R
Streaming Data in R
 
Python quick guide1
Python quick guide1Python quick guide1
Python quick guide1
 
Open source analytics
Open source analyticsOpen source analytics
Open source analytics
 
Cloud native IPC for Microservices Workshop @ Containerdays 2022
Cloud native IPC for Microservices Workshop @ Containerdays 2022Cloud native IPC for Microservices Workshop @ Containerdays 2022
Cloud native IPC for Microservices Workshop @ Containerdays 2022
 
Ali alshehri c++_comparison between c++&python
Ali alshehri c++_comparison between c++&pythonAli alshehri c++_comparison between c++&python
Ali alshehri c++_comparison between c++&python
 
An overview of data and web-application development with Python
An overview of data and web-application development with PythonAn overview of data and web-application development with Python
An overview of data and web-application development with Python
 
R as supporting tool for analytics and simulation
R as supporting tool for analytics and simulationR as supporting tool for analytics and simulation
R as supporting tool for analytics and simulation
 
Introduction To Python
Introduction To PythonIntroduction To Python
Introduction To Python
 
Python tutorial for beginners - Tib academy
Python tutorial for beginners - Tib academyPython tutorial for beginners - Tib academy
Python tutorial for beginners - Tib academy
 

Recently uploaded

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 

Recently uploaded (20)

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

Python and R for quantitative finance

  • 1. Python and R for Quantitative Finance An Introduction Luca Sbardella luca.sbardella@gmail.com @lsbardel LondonR - Nov 09    
  • 3. 1 ­ Context 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    
  • 4. Current Approach ✔ Extensive use of large propriety C, C++ or Java libraries. ✔ Server­side applications written in ✔ .Net/C# ­ Windows servers ✔ Java – Windows & Linux/Unix ✔ VBA/Excel on the client side :( ✔ Some web/based clients :) ✔ Limited if inexistent use of powerful opensource  technologies/libraries.    
  • 5. Problems ✔ Full development cycle in low­level languages is expensive C++ : Python = 7 : 1 ✔ Writing code already developed many times before ✔ Difficult to adjust to new technologies as they become  available ✔ Often libraries are deployed on the client machine ✔ Some technologies (.Net) force a decision on the platform  to be used (Windows)    
  • 6. A different approach ✔ Limit low­level development to critical number­crunching  components ✔ Use available high­standard opensource technologies ✔ Flexible, multiplatform server­side configuration ✔ Multi­language support ✔ Abstraction when possible ✔ Remote procedure calls (RPC)    
  • 7. A proposed solution ✔ Use of Python as main server side driver ✔ Legacy C, C++ as Python modules (Boost­Python) ✔ Interaction with other technologies ✔ R ­ for statistics ✔ Erlang/Haskell – functional programming and  concurrency ✔ q – kdb+ timeseries database (commercial) ✔ Javascript/flex – web GUI front­end    
  • 8. 2 ­ Python and R ✔ Combine  two  of  the  most  used  and  powerful  opensource  languages to create a truly dynamic application framework. ✔ Benefit from an active community of developers and a vast  array of ready available libraries. ✔ Seemingless integration with C/C++ libraries    
  • 9. Python? ✔ General-purpose high-level programming language ✔ Named after Monty Python ✔ by Guido Van Rossum ✔ in the late 1980s. ✔ Large community    
  • 10. Python Features ✔ Dynamically typed ✔ Multi­paradigm design ✔ Object Oriented ✔ Metaprogramming (reflection) ✔ Functional programming ✔ Emphasizes semplicity ✔ Extendibility via C or C++ (boost.python, Cpython)    
  • 11. Where? ✔ Fast Prototyping ✔ Scientific programming (numpy, scipy, …) ✔ Database abstraction (pyodbc, django) ✔ RPC servers (JSON, XML, Binary, ...) ✔ Domain Specific Languages (DSP) ✔ Web servers (mod_python, twisted, ...) ✔ Web Frameworks (django, zope, ...) ✔ Glue for anything you can think of    
  • 12. R language ✔ Open source implementation of S/SPLUS ✔ Scripting language that excels in ✔ data analysis ✔ statistics and econometric ✔ graphics ✔ Huge collection of statistical packages available ✔ Can be used as stand­alone server side language but not  ideal    
  • 13. Using R on the server­side ✔ Use R only for what has been design for ✔ Statistics & Math ✔ Embed R within Python domain    
  • 14. Installing ✔ Install Python ✔ Install R and any R library of your choice ✔ Install numpy and rpy2 ✔ Launch Python and run rpy2 test $ python >>> from rpy2 import tests >>> import unittest >>> tr = unittest.TextTestRunner(verbosity = 1) >>> tr.run(tests.suite())    
  • 16. 3.1 Memory and Vectors ✔ When using rpy2 Python and R domains are co­existing ✔ Python manages objects pointing to data stored and  administered in the R space ✔ R variables are existing within an embedded R workspace,  and can be accessed from Python through their python  object representations (Sexp and subclasses).    
  • 17. 3.1 – Memory and Vectors 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)    
  • 18. 3.2 A small server ✔ A minimalist Client­server application ✔ The Python server uses R packages to perform time­series  analysis on historical stock prices ✔ The server exposes a RPC­JSON interface to the client ✔ Implement one function which calculate a simple rolling  moving avarage    
  • 19. Code structure 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    
  • 20. System Requirements ✔ Python 2.6 or above ✔ R 2.8 or above ✔ rpy2 and numpy (core Python­R packages) ✔ matplotlib (for plotting client) ✔ proformanceAnalytics (R package) ✔ quantmod (R package)    
  • 21. 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    
  • 22. References ✔ Python: www.python.org ✔ R: www.r­project.org ✔ rpy2: http://rpy.sourceforge.net/ ✔ numpy: www.numpy.org ✔ matplotlib: http://matplotlib.sourceforge.net ✔ twisted: http://twistedmatrix.com ✔ boost: http://www.boost.org/