An Introduction to the Python Programming...

44
An Introduction to the Python Programming Language Daniel Toppo Pictet Asset Management 20192 – Financial Econometrics 2 Spring 2017

Transcript of An Introduction to the Python Programming...

Page 1: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

An Introduction to the Python Programming Language

Daniel ToppoPictet Asset Management

20192 – Financial Econometrics 2

Spring 2017

Page 2: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

Part IIntroduction

Python

2Intro Lecture

Page 4: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

What is Python ?

4Intro Lecture

PYTHONProgramming

Language

High-level

Open Source

Multi-paradigm

General-purpose

Interactive

Cross-platform

Page 5: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

What is Python ?

5Intro Lecture

A high-level, general-purpose, interactive programming languageo Designed to communicate instructions to a computero Allows to humans to express what they want the computer to

execute for them

High-levelo Python uses English keywords (aka natural language

elements)o Strong abstraction from the details of the computero Makes the process of creating a program much simpler

General-purposeo Used to create software in a wide variety of domainso Extended through an extensive ecosystem of librarieso Can be used for rapid code development as well as for

building large applications

Page 6: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

What is Python ?

6Intro Lecture

Interactiveo Processed at runtime by Python interpreter (Matlab-like)o The interpreter is a program that reads a program and

carries out its instructionso It translates the code at runtime to executable byte code (i.e.

binary code that the computer can understand and execute)

Cross-platformo Available for the most important Operating Systems

(Windows, Mac OS, Linux, etc.)o Used to build web applications, desktop applications, etc.

Multi-paradigmo Supports different ways to programo Object-oriented, Functional, Imperative, Procedural

programming

Page 8: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

Where to find Python ?

8Intro Lecture

Domainso Software Developmento Art (Movies)o Businesso Educationo Governmento Scienceo Engineering

Available platformso Windowso Mac OSXo Linuxo MS-DOSo Solariso Raspberryo Mobile phone (Android, iOS, Windows)

Page 9: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

What is Python used for?

9Intro Lecture

Web Development

Data Analysis

Machine Learning

Page 10: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

What is Python used for ?

10Intro Lecture

Internet of Things Games

Mobile Development

And LOT

more!

Page 11: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

What is Python used for ?

11Intro Lecture

Web Developmento To create websites with dynamic contento Some popular web framework (packages which allow developers

to write web applications): Django, TurboGears, web2py

Data analysiso Large and excellent scientific libraries: NumPy, SciPy, Statsmodels,

Pandas, Matplotlib, Seaborn, etc.o Specifically, Pandas is probably the most useful data analysis in

Python: easy-to-use data structures and data analysis tools

Machine Learningo Machine Learning is a branch of computing science that studies

algorithms allowing computers to learn without being explicitly programmed

o It is an application of a broader concept of Artificial Intelligence

Page 12: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

What is Python used for ?

12Intro Lecture

Internet of Thingso Python can be used to control and automate your entire

home!o Python can act as a brain for robots to perform actions or act

& react to the environmento Tiny computer like Raspberry Pi can be programmed in

Python

Games Developmentso Entire video games are coded in Pythono From graphical interface, to artificial intelligence algorithmso Specifically, the Pygame library is used to program games

Mobile Developmentso Some library (like Kivy, or PyMob) allow for rapid

development of mobile applications o Still a very young technology though, but might improve

drastically in the near future

Page 13: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

Who uses Python ?

13Intro Lecture

Page 15: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

Python Tools & Infrastructure

15Intro Lecture

Python tools and infrastructure are all the components (software & hardware) that allow for increasing productivity

Anaconda is one of the most popular Python distribution for data scienceo To install Python and scientific librarieso Over 100 of the most important libraries o Cross-platform (Windows, Mac, Linux)

Page 20: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

Why use Python ?

20Intro Lecture

Python is easy ! o Easy to read – Easy to learn – Easy to digest!

o In C++ (another very famous programming language) :

o In Python :print (“Ciao Italia!")

#include <iostream.h>void main() {

cout << “Ciao Italia!" << endl;}

Page 21: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

Why use Python ?

21Intro Lecture

Python's ecosystem is one of the largest out of any programming community:

Pyth

on’s

ec

osys

temTools

External libraries

Frameworks

Documentation

Books

Websites

Tutorials

PeopleDevelopers

Trainers

Page 22: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

Why use Python ?

22Intro Lecture

Widely taught in universitieso Top-ranked tech universities (MIT, Berkeley, etc.) switched

their introductory courses to Pythono The largest MOOC (massive open online course) providers

(edX, Coursera, Udacity) offer introductory programming course in Python

Open sourceo Development model that encourages open collaborationo Python source code is freely available to the publico Higher security, higher quality, higher customization

One of the top-ten programming languageso IEEE Spectrum: 3rd out of 20 (July 2016) 1

o TIOBE Programming Community : 5th out of 20 (Feb. 2017) 2

1 http://spectrum.ieee.org/computing/software/the-2016-top-programming-languages2 http://www.tiobe.com/tiobe-index/

Page 23: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

Python vs Matlab

23Intro Lecture

Python Advantageso Open and freeo Low learning curveo Object orientedo High portability: run the

code everywhereo Extensive ecosystem:

libraries (external modules) GUI toolkits (tools to build

Graphical User Interfaces) IDEs (Integrated

Development Environment)

Disadvantageso Must include libraries to

extend standard functionalities

Matlab Advantageso Include IDE, GUI builder,

matrix algebra, data processing, plotting in standard package

o Available and extensive documentation

o Large scientific community

Disadvantageso Proprietary: hidden codeo Expensive: Toolboxes are

usually not freeo Low portability (Matlab

code doesn't run exactly the same way on different platforms)

Page 24: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

Python vs Matlab

24Intro Lecture

Python MatlabCore

Programming language Interpreter Standard library

Libraries

Numpy Scipy Matplotlib Pandas Scikit PyQt4 Etc.

IDEs

Spyder PyCharm Wing IDE PyStudio IDLE Etc.

Core Programming language Interpreter Standard library IDE GUI builder

Toolkits | Toolboxes

Simulink (simulation & modelling)

Parallel computing Image processing Signal processing Etc.

Page 26: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

Python in Finance

26Intro Lecture

Financial institutions are evolving into technology companieso Banking and financial institutions is the industry that spends

the most in technology (often spending billions $US per year)o Large banks employ thousands of developers to create and

maintain IT systems

Technology leads to competitive advantageso Increase of speed of trades executionso Increase and automation of controlso Ease of increasing data volumes managemento Financial engineering modelling

However, skilled people are scarce and hard to findo Expert not only need to know technology, but also finance!o Skills shortages are a threat to competitiveness and growth

Page 27: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

Python in Finance

27Intro Lecture

Non-exhaustive list of uses of Python in financeo Analyzing datao Backtesting trades and modelso Financial modelingo Asset pricingo Risk management

Examples of software built with Python in the financial Industryo JP Morgan’s Athena: risk management & analysis, asset

pricing, trading management systemo Bank of America Merrill Lynch’s Quartz: asset pricing,

positions management, risk managemento Bellco Credit Union’s SAFE: online banking systemo Altis Investment Management: risk management system

Page 28: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

Python in Finance

28Intro Lecture

Most requested languages in coding interviews in the financial space (from the HackerRank1 website)

1 https://blog.hackerrank.com/emerging-languages-still-overshadowed-by-incumbents-java-python-in-coding-interviews/

Page 29: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

Python

29Intro Lecture

Part IIExamples

Page 30: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

Python Examples – Basic

30Intro Lecture

Simple mathematical expression and its evaluation: Compound Interest

𝐹𝐹 = 𝑃𝑃 � 1 + 𝑟𝑟 𝑡𝑡

# Defines a function with parameters 'p', 'r' and 't'def compoundInterest (p, r, t) :

return p*(1+r)**t # Return the function result

# Let's initialize the parameters…p = 100r = 0.1t = 10

# … and call the function !f = compoundInterest (p, r, t) # The variable 'f ' yields the

# function result (259.374246…)

Page 31: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

Python Examples – Basic

31Intro Lecture

What does this very simple example shows us:o Python syntax is very similar to the mathematical

syntaxo Python uses English keywords for expressing

scientific and mathematical problemso Python elegant syntax clearly separates blocs of

statements (the function in our example) using adequate indentation

Any financial (or scientific) problem can thus be very easily expressed and translated into algorithms

Page 32: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

Examples – Optimization

32Intro Lecture

Let’s try to minimize the following function over the [-1, 1] x [-1, 1] space:

𝑓𝑓 𝑥𝑥,𝑦𝑦 =12

sin 𝑝𝑝𝑝𝑝 � 𝑥𝑥 � 𝑦𝑦 + 𝑥𝑥2 − 𝑒𝑒𝑥𝑥

Page 33: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

Examples – Optimization

33Intro Lecture

From the plot, minima are located at around:o (x = 1.0, y = -0.5)o (x = -0.5, y = 1.0)

# Defines the function def f(params) :

x, y = paramsreturn sin(pi*x*y) + x**2 - exp(x)

# Let’s fix our search space to [-1, 1]:[-1, 1] bnds = ((-1, 1), (-1, 1))# And our first initial guess for the optimizerinitGuess = [-0.5, 0.5]

# Finally, optimize ‘f ’ using the ‘minimize’ functionoptimization = optimize.minimize(f, initGuess, method="SLSQP", bounds=bnds)

Page 34: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

Examples – Optimization

34Intro Lecture

Results# Prints the result print (optimization)

fun: -2.417917900528793message: 'Optimization terminated successfully.‘nit: 5success: Truex: array([-0.423265, 1.0])

The optimization results is successful, with results o x = [-0.42, 1.0], close to what we were expectingo yielding function value: -2.42

We can do the same exercise with another initial guesses to find another minimum (e.g. [0.75, -0.75])

Page 35: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

Examples – Linear Regression

35Intro Lecture

Dataset is about salaries of professors. The data were collected to monitor salary differences between male and female members

397 observations on the following 6 variableso rank: AssocProf, AsstProf, Profo discipline: A (theoretical), B (applied)

departmento yrs.since.phd: years since PhDo yrs.service: years of serviceo sex: Female, Maleo salary: nine-month salary, in $US

Dependent variable: salary Independent variables: years service, years since PhD

Page 36: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

Examples – Linear Regression

36Intro Lecture

Linear regression function# Read the data series from “salaries.xlsx” Excel filexslx = ExcelFile("salaries.xlsx")# Parse the “salaries” sheet and store the data into the “data” variabledata = xslx.parse("salaries")

regressor = "salary" # responsepredictor = "yearsOfService" # predictor

y = data[regressor] X = data[predictor]X = add_constant(X) # Adds a constant term to the predictor

# OLS is the Ordinary Least Squares functionmodel = OLS(y, X).fit()

Page 37: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

Examples – Linear Regression

37Intro Lecture

Result# Prints the regression modelprint (model)

Page 38: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

Examples – Linear Regression

38Intro Lecture

Plotting salaries regression against years of service

Page 39: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

Examples – Linear Regression

39Intro Lecture

Plot – separating women & men salaries

Page 40: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

Examples – Linear Regression

40Intro Lecture

Plot – separating theoretical to practical departments

Page 42: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

Python - References

42Intro Lecture

Online documentation Official Python website: https://docs.python.org/3/ Wiki-Book: https://en.wikibooks.org/wiki/Python_Programming Byte of Python: http://www.swaroopch.com/notes/Python Python Bibliotheca: http://openbookproject.net/pybiblio/ Tutorialspoint: https://www.tutorialspoint.com/python Google: https://developers.google.com/edu/python/ Pythonspot: https://pythonspot.com/ Pythonlearn: http://www.pythonlearn.com/book.php Python for Fun: http://www.openbookproject.net/py4fun/ Pandas: http://pandas.pydata.org/ Numpy: http://www.numpy.org/ Scipy: https://www.scipy.org/

Page 43: An Introduction to the Python Programming Languagedidattica.unibocconi.it/mypage/dwload.php?nomefile=... · An Introduction to the Python Programming Language Daniel Toppo ... is

Python - References

43Intro Lecture

BooksMark LutzLearning Python, 5th EditionO'Reilly Media; 5 edition (July 6, 2013)

Paul JonesPython: The Fundamentals Of Python ProgrammingCreateSpace Publishing Platform (Oct. 26, 2016)

Eric MatthesPython Crash Course: A Hands-On, Project-Based Introduction to ProgrammingNo Starch Press; 1 edition (November 30, 2015)