Skip to content

Instantly share code, notes, and snippets.

@yhilpisch
Last active September 11, 2022 07:50
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save yhilpisch/d06a4e0987e45c0a8219136f027c3769 to your computer and use it in GitHub Desktop.
Save yhilpisch/d06a4e0987e45c0a8219136f027c3769 to your computer and use it in GitHub Desktop.

Intermediate Python for Finance Training

This is the Gist for Day 1 of the Intermediate Python for Finance Training in London, 28. November 2017

Agenda

Module 1 — Python

This module is a review of Python infrastructure elements and basic Python idioms.

The goal of this module is to be able to set up a proper Python environment, to know about basic tools and to be aware of basic Python functionality.

Module 2 — NumPy

This module covers numerical operations and algorithms with NumPy for finance.

NumPy is a central package in numerical and financial analysis. It allows for the performant implementation of financial algorithms based on concise, vectorized code.

The goal of this module is to understand the benefits of NumPy compared to pure Python code in finance.

Notebooks & Code Files

Below you find the all the notebooks and code files as worked on during this day.

The Python Quants

http://twitter.com/dyjh

team@tpq.io | http://tpq.io

http://pyalgo.tpq.io | http://certificate.tpq.io

Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src='http://hilpisch.com/tpq_logo.png' width=\"300px\" align=\"right\">"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Intermediate Python &mdash; Day 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercises**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Module 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 01"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create an environment for **Python 2.7** with `numpy, pandas, ipython`. Call it `py27`.\n",
"\n",
"Activate it, start `ipython` and import `numpy` and `pandas` in `ipython`.\n",
"\n",
"Terminate `ipython`. Deactivate the environment. Remove the environment."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 02"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Implement a Python function that returns the $n-$th **Fibonacci number**. The algorithm to create such number is given as follows for any natural number $n\\geq0$:\n",
"\n",
"$$\n",
"f_0=0 \\\\ f_1=1 \\\\\n",
"f_n = f_{n-2} + f_{n-1}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 03"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Implement a routine that collects (and prints) the **Fibonacci numbers** up to $n=15$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 04"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Implement a Python function that checks whether a given natural number $N>2$ is a **prime number**. A natural number is prime if it is only divided by itself and 1."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 05"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Implement a routine that collects (and prints) the **prime numbers** up to 100,000."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 06"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a list object with $N$ standard normally distributed pseudo-random numbers.\n",
"\n",
"Select all those numbers $r_n, n \\in \\{1, 2, ..., N\\}$, that satisfy $r_n>0.5$.\n",
"\n",
"Count the number of such values, calculate the ratio of their frequency to the total number (\"relative frequency\") and their average."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 07"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Calculate the **net present value** of a project that has cash flows:\n",
"\n",
"$$\n",
"\\begin{eqnarray}\n",
"C &=& (C_0, C_1, C_2, C_3, C_4, C_5)\\\\\n",
"&=& (-10, -5, 5, 5, 6, 6)\n",
"\\end{eqnarray}\n",
"$$\n",
"\n",
"The time intervals are all of length \"1 year\".\n",
"\n",
"The relevant interest rate is 5% p.a (continuously compounded).\n",
"\n",
"Is the project still profitable for an increased rate of 12%?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Module 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 01"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Draw 1,000,000 distributed numbers with a mean of 100 and a standard deviation of 20.\n",
"\n",
"* with the Python `random` module\n",
"* with the `numpy.random` modlue\n",
"\n",
"In both cases calculate the mean and the standard deviation of the resulting data sets.\n",
"\n",
"Compare the performance of the two approaches."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 02"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create an array with $N$ **standard normally distributed random numbers** using `numpy`.\n",
"\n",
"Calculate the 1st and 2nd moment of the random numbers.\n",
"\n",
"Correct the 1st moment by subtracting the mean value from all random numbers.\n",
"\n",
"Correct the 2nd moment by dividing all random numbers by their standard deviation."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 03"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Estimate the value of the following integral by Monte Carlo Simulation using `numpy`:\n",
"\n",
"$$\n",
"\\int_2^4 e^x dx\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 04"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The future price of a stock $S_T, T>0,$ with an intial price today of $S_0$ in the **Black-Scholes-Merton (1973)** model is given in a simulation context by:\n",
"\n",
"$$\n",
"S_t = S_0 e^{\\left(r - \\frac{\\sigma^2}{2}\\right)T + \\sigma \\sqrt{T}z}\n",
"$$\n",
"\n",
"Here, $r$ is the constant short rate, $\\sigma$ the constant volatility factor and $z$ a standard normally distributed number.\n",
"\n",
"Assume:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"S0 = 36.\n",
"r = 0.06\n",
"T = 1.0\n",
"sigma = 0.2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Simulate 100,000 values for $S_T$.\n",
"\n",
"Estimate the price of a **European put option** with strike price of $K = 40$ and payoff function of\n",
"\n",
"$$\n",
"h^i = \\max[K - S_T^i, 0]\n",
"$$\n",
"\n",
"Estimate the price of a **European call option** with the same strike."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src='http://hilpisch.com/tpq_logo.png' width=\"300px\" align=\"right\">"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment