Python for Finance. Control Flow, data structures and first application (part 2) Andras Niedermayer

Similar documents
CSC Advanced Scientific Computing, Fall Numpy

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Python for Finance. Introduction and Basics of Python. Andras Niedermayer

Python for Finance. Advanced Features. Andras Niedermayer

Outline. 1 If Statement. 2 While Statement. 3 For Statement. 4 Nesting. 5 Applications. 6 Other Conditional and Loop Constructs 2 / 19

Problem Based Learning 2018

NumPy quick reference

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Python Basics. Nakul Gopalan With help from Cam Allen-Lloyd

Types, lists & functions

Programming for Engineers in Python

PYTHON FOR KIDS A Pl ayfu l I ntrodu ctio n to Prog r am m i ng J a s o n R. B r i g g s

Python: Short Overview and Recap

ENGR 102 Engineering Lab I - Computation

Introduction to Machine Learning. Useful tools: Python, NumPy, scikit-learn

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Teaching London Computing

Exercise: Introduction to NumPy arrays

The Big Python Guide

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python

A. Python Crash Course

Short Introduction to Python Machine Learning Course Laboratory

Table of Contents. Preface... xxi

PYTHON FOR MEDICAL PHYSICISTS. Radiation Oncology Medical Physics Cancer Care Services, Royal Brisbane & Women s Hospital

Part IV. More on Python. Tobias Neckel: Scripting with Bash and Python Compact Max-Planck, February 16-26,

Sequences and iteration in Python

cs1114 REVIEW of details test closed laptop period

Variable and Data Type I

DSC 201: Data Analysis & Visualization

A Little Python Part 2

Course May 18, Advanced Computational Physics. Course Hartmut Ruhl, LMU, Munich. People involved. SP in Python: 3 basic points

Introduction to Python Programming

Chapter 2 Writing Simple Programs

Introduction to NumPy

(DRAFT) PYTHON FUNDAMENTALS II: NUMPY & MATPLOTLIB

Python for Finance. Introduction and Basics of Python. Andras Niedermayer

DSC 201: Data Analysis & Visualization

Numerical Methods. Centre for Mathematical Sciences Lund University. Spring 2015

Play with Python: An intro to Data Science

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

Programming to Python

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

Introduction to Programming in Turing. Input, Output, and Variables

Visualize ComplexCities

Introduction to Python, Cplex and Gurobi

Python Programming Exercises 3

Introduction to Python

Table of Contents EVALUATION COPY

Data type built into Python. Dictionaries are sometimes found in other languages as associative memories or associative arrays.

Basic Python Revision Notes With help from Nitish Mittal

Math Day 2 Programming: How to make computers do math for you

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming

Chapter 1. Data types. Data types. In this chapter you will: learn about data types. learn about tuples, lists and dictionaries

Subject: Computer Science

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

Variable and Data Type I

Beyond Blocks: Python Session #1

COMP1730/COMP6730 Programming for Scientists. Sequence types, part 2

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

NumPy. Daniël de Kok. May 4, 2017

Built-in Types of Data

Jython. secondary. memory

Programming for Data Science Syllabus

Chapter 2. Python Programming for Physicists. Soon-Hyung Yook. March 31, Soon-Hyung Yook Chapter 2 March 31, / 52

1. BASICS OF PYTHON. JHU Physics & Astronomy Python Workshop Lecturer: Mubdi Rahman

Downloaded from Chapter 2. Functions

Python Lists: Example 1: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2]

6.S189 Homework 2. What to turn in. Exercise 3.1 Defining A Function. Exercise 3.2 Math Module.

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

Exercise: The basics - variables and types

ENGR (Socolofsky) Week 07 Python scripts

Exam 1 Format, Concepts, What you should be able to do, and Sample Problems

Python Tutorial. CS/CME/BioE/Biophys/BMI 279 Oct. 17, 2017 Rishi Bedi

Python. Jae-Gil Lee Based on the slides by K. Naik, M. Raju, and S. Bhatkar. December 28, Outline

Java Outline (Upto Exam 2)

Python Games. Session 1 By Declan Fox

Physics 514 Basic Python Intro

Scientific Computing: Lecture 3

SI Networked Computing: Storage, Communication, and Processing, Winter 2009

Introduction to Python! Lecture 2

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

CIS192 Python Programming

MICROPROCESSOR SYSTEMS INTRODUCTION TO PYTHON

Text Input and Conditionals

Part III Appendices 165

Programming with Python

Handling arrays in Python (numpy)

Python Crash Course Numpy, Scipy, Matplotlib

Key Differences Between Python and Java

CS61A Lecture 16. Amir Kamil UC Berkeley February 27, 2013

DM536 / DM550 Part 1 Introduction to Programming. Peter Schneider-Kamp.

CS 1110 Final, December 8th, Question Points Score Total: 100

The power of logarithmic computations. Recursive x y. Calculate x y. Can we reduce the number of multiplications?

Intelligente Datenanalyse Intelligent Data Analysis

EASY

Data structure and algorithm in Python

Turn in a printout of your code exercises stapled to your answers to the written exercises at 2:10 PM on Thursday, January 13th.

PYTHON NUMPY TUTORIAL CIS 581

Transcription:

Python for Finance Control Flow, data structures and first application (part 2) Andras Niedermayer

Outline 1 Control Flow 2 Modules 3 Data types and structures. Working with arrays and matrices. 4 Numpy functions Andras Niedermayer - Université Paris-Dauphine 2/34

Functions definition with parameters In [1]: def hello ( name ):... : print ( Hello + name ) usage: In [1]: hello ( Alice ) In [2]: hello ( Bob ) even better: document your code with a docstring In [1]: def hello ( name ):... : """... : A function that says hello... : to name... : """... : print ( Hello + name ) In [2]: help ( hello ) Andras Niedermayer - Université Paris-Dauphine 3/34

Functions return values def get_answers ( answer_number ): if answer_number == 1: return You are the first. elif answer_number == 2: return Twice as good. else : return Something else than one \ or two usage: In [1]: print ( get_answer (2)) Andras Niedermayer - Université Paris-Dauphine 4/34

Functions Or pick a random one: import random fortune = get_answer ( random. randint (1,9)) print ( fortune ) Andras Niedermayer - Université Paris-Dauphine 5/34

Variable Scope one cannot use variables outside of their scope def spam (): eggs = 31337 print ( str ( eggs )) spam () print ( eggs ) Andras Niedermayer - Université Paris-Dauphine 6/34

Variable Scope This also true for calls to other functions: def spam (): eggs = 31337 bacon () print ( eggs ) def bacon (): ham = 101 eggs = 0 spam () Andras Niedermayer - Université Paris-Dauphine 7/34

Variable Scope In constrast to this, one can use global variables in a funtion: def spam (): print ( eggs ) eggs = 31337 spam () Andras Niedermayer - Université Paris-Dauphine 8/34

Functions The following is bad style, but feasible: def spam (): eggs = spam local print ( eggs ) def bacon (): eggs = bacon local print ( eggs ) spam () print ( eggs ) eggs = global bacon () Andras Niedermayer - Université Paris-Dauphine 9/34

Variable Scope Usage of the global keyword def spam (): global eggs eggs = spam eggs = global spam () print ( eggs ) Andras Niedermayer - Université Paris-Dauphine 10/34

Our Own Module Definition in Python documentation: A module is a file containing Python definitions and statements. The file name is the module name with the suffix.py appended. A module is essentially a file that one imports. The file contains the functions we can call. Here is an example of what we may try to achieve: In [ 1]: import sequences In [ 2]: # Import the file sequences. py In [3]: dir ( sequences ) Out [3]: [ name, fib, fib2 ] Andras Niedermayer - Université Paris-Dauphine 11/34

Outline 1 Control Flow 2 Modules 3 Data types and structures. Working with arrays and matrices. 4 Numpy functions Andras Niedermayer - Université Paris-Dauphine 12/34

Our Own Module Let us create our module sequences and save it as sequences.py """ A module with different sequences """ def fib (n): """ prints the Fibonacci sequence from 1 to n """ a, b = 0, 1 while b < n: print (b, end=" ") a, b = b, a+b print () def fib2 (n): """ returns the Fibonacci sequence until n """ res = [] a, b = 0, 1 while b < n: res. append (b) a, b = b, a+b return res Andras Niedermayer - Université Paris-Dauphine 13/34

Our Own Module using the module In [ 1]: import sequences In [2]: sequences. name Out [2]: sequences In [3]: sequences. fib (1000) 1 1 2 3 5 8 13 21 34 55 144 233 377 610 987 In [4]: help ( sequences ) In [5]: help ( sequences. fib ) to automatically reload changes: In [1]: % load_ext autoreload In [2]: % autoreload 2 Andras Niedermayer - Université Paris-Dauphine 14/34

Our Own Module making the module executable because you want to test the modul because you want to run your module without starting the Python interpreter add the following to the end of the sequences.py file: if name == " main ": import sys # Usage : python sequences. py < ENTER > fib ( int ( sys. argv [1])) start the Anaconda prompt, change to the folder in which you saved sequences.py, e.g. if the folder name is C:\some\path, type cd C:\some\path then type python sequences.py 1000 Andras Niedermayer - Université Paris-Dauphine 15/34

Excercises 14 Create a program that picks a random number between 1 and 2. The player has to guess the number. The player has 6 attempts to guess. After each guess, tell the player whether his number was too high or too low. 15 Solve the Syracuse problem numerically: Take an integer n 1, repeat the following operation: if the number is even then divide it by two if the number is odd then multiply it by 3 and add 1 Does the sequence always reach 1? ( Solve is in quotation marks, because proving this result in general is an unsolved problem so far, see https://en.wikipedia.org/wiki/collatz_conjecture. However, we can calculate the solution of the problem for different numerical examples.) Andras Niedermayer - Université Paris-Dauphine 16/34

Excercise 14 Guessing game guess.py: """ Picks a random number and lets the player guess """ # imports # ask six times # was the number too high? # was the number too low? # if neither, exit # if success... # or if failure... Andras Niedermayer - Université Paris-Dauphine 17/34

Excercise 14 Guessing game guess.py """ Picks a random number and lets the player guess """ import random secret = random. randint (1,20) print ("I thought of a number between 1 and 20. ") # Ask the player six times for number_guess in range (1,7): print ( What number? ) guess = int ( input ()) # number was too low if guess < secret : print ( Your guess is too low. ) # number was too high elif guess > secret : print ( Your guess is too high. ) else : break # otherwise exit if guess == secret : # if success print ( Well played. ) else : # or if failure... print ( No luck! ) Andras Niedermayer - Université Paris-Dauphine 18/34

Exercise 15 define following function, run with different values of starting value and number steps def syracuse ( starting_ value, number_ steps ): u = starting_ value print (u, end =" ") for n in range ( number_ steps ): if u % 2 == 0: u = u // 2 else : u = 3 * u + 1 print (u, end =" ") if u == 1: print ( Converged to 1 after {} \ steps!. format (n)) return print ( Did not converge in {} steps! \. format ( number_steps )) Andras Niedermayer - Université Paris-Dauphine 19/34

Outline 1 Control Flow 2 Modules 3 Data types and structures. Working with arrays and matrices. 4 Numpy functions Andras Niedermayer - Université Paris-Dauphine 20/34

Data types in Python Type Name Example Notes Integer int a=10 arbitrarily large; a//4=? Floats float b=0.35 precision issues; b+0.1=? Strings string c= it is a string Interesting methods for strings: c.capitalize(), c.split(), c.replace(a,b)... Andras Niedermayer - Université Paris-Dauphine 21/34

Data structures tuple Simple collection of arbitrary objects. Limited methods. t=(1, 2.5, " data ") # t=1, 2.5, " data " Note that indexing starts at zero: t[0]=1. Two methods: 1 Count: t.count("data")=1 2 Index: t.index(2.5)=1 Andras Niedermayer - Université Paris-Dauphine 22/34

Data structures list A collection of arbitrary objects; many methods available. l=[1, 2.5, " data "] Can convert tuples into lists: l=list(t)? Multiple methods: 1 Append (even another list): l.append([4,3])=[1,2.5,"data",[4,3]] 2 Insert before index: l.insert(1, insert )=[1, insert, 2.5,"data",[4,3]] 3 Remove first occurence: l.remove(2.5)=[1, insert,"data",[4,3]] 4 Slice : l[1:3]=[ insert,"data"] 5 Sort data: l.sort(), or non-mutating version l2=sorted(l) Andras Niedermayer - Université Paris-Dauphine 23/34

Data structures Excercise: Play hangman rules The computer chooses a word. In each round the player chooses a letter If the letter is in the word, it appears. If not, then the counter increases and the game appraoches its end. Andras Niedermayer - Université Paris-Dauphine 24/34

Solution def play_hangman (word, n): guess = len ( word )*[ _ ] while n >0: letters = input ( {}. guesses left : \. format (n)) letter = letters [0] if letter in word : for i in range ( len ( word )): if word [ i] == letter : guess [ i] = letter print (. join ( guess )) if _ not in guess : print ( success! ) return else : n -= 1 print ( wrong. {} guesses left. \. format (n)) print ( failure! ) Andras Niedermayer - Université Paris-Dauphine 25/34

Data structures dict Dictionaries with key-value stores. Unordered and un-sortable. Maps (generally) strings into strings or numbers. d={ Last : Doe, First : John, Country : UK } Multiple methods: 1 d.keys()=[ Last, First, Country ] 2 d.values()=[ Doe, John, England ] 3 Mapping in a dictionary: d[ Last ]= Doe. 4 Setting an item: d[ Country ]=US Andras Niedermayer - Université Paris-Dauphine 26/34

Data structures set Mathematical sets: unordered collections of objects, repeated only once. s1=set ([ a, b, c, d ]) s2=set ([ e, b, c, f ]) Multiple methods: 1 s1.union(s2)={ a, b, c, d, e, f } 2 s1.intersection(s2)={ b, c } 3 s1.difference(s2)={ a, d } 4 s1.symmetric difference(s2)={ a, d, e, f } Andras Niedermayer - Université Paris-Dauphine 27/34

Working with matrices: List arrays v =[0.5, 0.75, 1.0, 1.5, 2. 0] # vector m=[v,v,v] # a 3-by -3 matrix m=[ [0.5, 0.75, 1.0, 1.5, 2.0] [0.5, 0.75, 1.0, 1.5, 2.0] [0.5, 0.75, 1.0, 1.5, 2.0]] 1 Easy to select rows or single elements. Example: m[1] is second row, m[1][0] first element of the second row. 2 Not easy to select columns! (a row is the primary element of the list matrix) 3 Works by reference pointers changes in v are copied everywhere in m. Example: v[0] = 2. Try out m =? Andras Niedermayer - Université Paris-Dauphine 28/34

Numpy arrays We will import the numerical Python library: numpy. import numpy as np v1=np. array ([0.5, 0.75, 1.0, 1.5, 2.0]) # ndarray. Vector methods for numpy.ndarray: 1 Sum of elements: v1.sum()=5.75. 2 Standard deviation: v1.std()=0.53. 3 Cumulative sum: v1.cumsum()=array([0.5, 1.25, 2.25, 3.75, 5.75]) 4 Scalar multiplication, powers, square root...: v1*2 = array([1, 1.5, 2.0, 3.0, 4.0]) v1**2 =array([0.25, 0.5625, 1., 2.25, 4.]) Andras Niedermayer - Université Paris-Dauphine 29/34

Matrix operations m1=np. array ([v1, v1 *2]) m1=np. array ([[ 0.5, 0.75, 1., 1.5, 2.], [ 1., 1.5, 2., 3., 4.]]) 1 Indexing is (row, column): m1[0, 2] is third element of first row. 2 Column sum: m1.sum(axis=0)=array([1.5, 2.25, 3., 4.5, 6.]) Row sum: m1.sum(axis=1)=? 3 Cumulative sum: v1.cumsum()=array([0.5, 1.25, 2.25, 3.75, 5.75]) 4 Initializing a matrix: np.zeros((r,c,z), dtype= f, order= C ) or np.ones((r,c,z), dtype= f, order= C ). Types (optional): i is integer, f is float, b is boolean... Order (optional): how to store elements in memory C is row-wise, F is column-wise. Andras Niedermayer - Université Paris-Dauphine 30/34

Matrix operations (more) m1=np. array ([v1, v1 *2]) m1= array ([[0.5, 0.75, 1., 1.5, 2.], [1., 1.5, 2., 3., 4.]]) 1 Flattening: m1.ravel()=array([0.5, 0.75, 1., 1.5, 2., 1., 1.5, 2., 3., 4.]) 2 Matrix size: m1.shape=(2, 5) 3 Reshape: m1.reshape(5,-1)=array([[0.5, 0.75], [1., 1.5], [2., 1.], [1.5, 2.], [3., 4.]]) 4 Vertical and horizontal stacking: vstack and hstack. Andras Niedermayer - Université Paris-Dauphine 31/34

Vectorization Advantages of vectorization: 1 compact code, easy to read and understand. 2 faster execution. v20 =np. array ([2,3,5]) v21 =np. array ([0.5,0.6,0.2]) # element - by - element sum v20 + v21 = array ([2.5, 3.6, 5.2]) # broadcasting the scalar. 2* v20 +3= array ([7, 9, 13]) Andras Niedermayer - Université Paris-Dauphine 32/34

Outline 1 Control Flow 2 Modules 3 Data types and structures. Working with arrays and matrices. 4 Numpy functions Andras Niedermayer - Université Paris-Dauphine 33/34

Numpy functions Documentation: www.docs.scipy.org/doc/numpy/reference/routines.html Name np.dot(a, b) np.linalg.det(a) np.linalg.solve(a, b) np.linalg.eig(a) np.sin(x), np.cos(x).. np.exp(x), np.log(x), np.power(x1, x2), np.sqrt(x) np.median(a), np.mean(a) np.std(a), np.corrcoef (a, b) Description Dot product of a and b Determinant of array a Solve linear system ax = b Eigenvalues of matrix a Trigonometric functions Arithmetic, exponents, logarithms Summary stats of an array Andras Niedermayer - Université Paris-Dauphine 34/34