├── README.md ├── advanced_python_concepts.py ├── basic_python_concepts.py ├── data_visualization.py ├── financial_libraries.py ├── monte_carlo_simulations.py ├── numpy_for_quants.py ├── pandas_for_quants.py └── time_series_analysis.py /README.md: -------------------------------------------------------------------------------- 1 | ![GitHub stars](https://img.shields.io/github/stars/AIM-IT4/Python-For-Quants) 2 | ![GitHub forks](https://img.shields.io/github/forks/AIM-IT4/Python-For-Quants) 3 | # Introduction to Python for Quants 4 | 5 | This repository provides a foundational introduction to Python tailored for quantitative analysts and researchers (quants). Each section is demonstrated with Python code examples to help users understand and apply the concepts in practice. 6 | 7 | ## Table of Contents 8 | 9 | 1. **Basic Python Concepts** 10 | - Learn about variables, data types, operators, control structures, and functions. 11 | - [View Code](basic_python_concepts.py) 12 | 13 | 2. **Advanced Python Concepts** 14 | - Dive into classes, objects, modules, packages, and exception handling. 15 | - [View Code](advanced_python_concepts.py) 16 | 17 | 3. **NumPy for Quants** 18 | - Understand array creation, mathematical operations, and statistical functions using NumPy. 19 | - [View Code](numpy_for_quants.py) 20 | 21 | 4. **Pandas for Quants** 22 | - Explore Series, DataFrames, data cleaning, and time-series analysis using Pandas. 23 | - [View Code](pandas_for_quants.py) 24 | 25 | 5. **Data Visualization** 26 | - Discover basic plotting with Matplotlib and advanced visualizations with Seaborn. 27 | - [View Code](data_visualization.py) 28 | 29 | 6. **Financial Libraries** 30 | - Get introduced to popular financial libraries like QuantLib and Pyfolio. 31 | - [View Code](financial_libraries.py) 32 | 33 | 7. **Monte Carlo Simulations** 34 | - Understand the basics of Monte Carlo and its applications in finance. 35 | - [View Code](monte_carlo_simulations.py) 36 | 37 | 8. **Time-Series Analysis** 38 | - Delve into time series forecasting using models like ARIMA. 39 | - [View Code](time_series_analysis.py) 40 | 41 | ## Getting Started 42 | 43 | 1. Clone the repository. 44 | 2. Ensure you have Python and the necessary libraries installed. 45 | 3. Navigate to the desired section and run the corresponding Python file to see the concepts in action. 46 | 47 | ## Contributions 48 | 49 | Feel free to fork the repository, make changes, and submit pull requests. Feedback and improvements are always welcome! 50 | -------------------------------------------------------------------------------- /advanced_python_concepts.py: -------------------------------------------------------------------------------- 1 | 2 | # Classes and objects 3 | class Stock: 4 | def __init__(self, ticker, price): 5 | self.ticker = ticker 6 | self.price = price 7 | def display(self): 8 | print(f"Stock: {self.ticker}, Price: ${self.price}") 9 | apple = Stock("AAPL", 150) 10 | apple.display() 11 | 12 | # Modules and packages (Assuming a module 'example_module' exists) 13 | # from example_module import example_function 14 | # example_function() 15 | 16 | # Exceptions 17 | try: 18 | result = 10 / 0 19 | except ZeroDivisionError: 20 | print("Cannot divide by zero!") 21 | -------------------------------------------------------------------------------- /basic_python_concepts.py: -------------------------------------------------------------------------------- 1 | 2 | # Variables and data types 3 | integer_var = 10 4 | float_var = 10.5 5 | string_var = "Hello, Quant" 6 | print(integer_var, float_var, string_var) 7 | 8 | # Operators and expressions 9 | sum_result = integer_var + float_var 10 | print(sum_result) 11 | 12 | # Control structures 13 | if integer_var > 5: 14 | print("Integer is greater than 5") 15 | else: 16 | print("Integer is less than or equal to 5") 17 | 18 | for i in range(5): 19 | print(i) 20 | 21 | # Functions 22 | def add_numbers(a, b): 23 | return a + b 24 | print(add_numbers(5, 3)) 25 | -------------------------------------------------------------------------------- /data_visualization.py: -------------------------------------------------------------------------------- 1 | 2 | import matplotlib.pyplot as plt 3 | import seaborn as sns 4 | 5 | # Matplotlib basics 6 | x = [1, 2, 3, 4, 5] 7 | y = [1, 4, 9, 16, 25] 8 | plt.plot(x, y) 9 | plt.title("Matplotlib Example") 10 | plt.xlabel("X-axis") 11 | plt.ylabel("Y-axis") 12 | plt.show() 13 | 14 | # Seaborn for advanced visualizations 15 | sns.set_theme() 16 | tips = sns.load_dataset("tips") 17 | sns.relplot(x="total_bill", y="tip", hue="smoker", style="smoker", size="size", data=tips) 18 | plt.title("Seaborn Example: Tips Dataset") 19 | plt.show() 20 | -------------------------------------------------------------------------------- /financial_libraries.py: -------------------------------------------------------------------------------- 1 | 2 | # NOTE: QuantLib and Pyfolio need to be installed to run this script. 3 | # For demonstration purposes, I'll outline basic usage, but this won't run without the libraries installed. 4 | 5 | # QuantLib: Introduction and basic examples 6 | import QuantLib as ql 7 | 8 | # Setting up a basic date and calendar 9 | today_date = ql.Date(17, 9, 2023) 10 | ql.Settings.instance().evaluationDate = today_date 11 | calendar = ql.UnitedStates() 12 | 13 | next_business_day = calendar.advance(today_date, ql.Period(1, ql.Days)) 14 | print("Next Business Day:", next_business_day) 15 | -------------------------------------------------------------------------------- /monte_carlo_simulations.py: -------------------------------------------------------------------------------- 1 | 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Basics of Monte Carlo 6 | def monte_carlo_estimation_pi(num_samples): 7 | inside_circle = 0 8 | for _ in range(num_samples): 9 | x, y = np.random.uniform(-1, 1, 2) 10 | distance_to_origin = x**2 + y**2 11 | if distance_to_origin <= 1: 12 | inside_circle += 1 13 | return (inside_circle / num_samples) * 4 14 | 15 | estimated_pi = monte_carlo_estimation_pi(100000) 16 | print(f"Estimated value of pi using Monte Carlo: {estimated_pi}") 17 | -------------------------------------------------------------------------------- /numpy_for_quants.py: -------------------------------------------------------------------------------- 1 | 2 | import numpy as np 3 | 4 | # Creating arrays 5 | arr1 = np.array([1, 2, 3, 4, 5]) 6 | print("Array:", arr1) 7 | 8 | # Mathematical operations 9 | arr2 = np.array([5, 4, 3, 2, 1]) 10 | sum_arr = arr1 + arr2 11 | print("Sum of arrays:", sum_arr) 12 | 13 | # Statistical functions 14 | mean_val = np.mean(arr1) 15 | std_val = np.std(arr1) 16 | print("Mean:", mean_val, "Standard Deviation:", std_val) 17 | -------------------------------------------------------------------------------- /pandas_for_quants.py: -------------------------------------------------------------------------------- 1 | 2 | import pandas as pd 3 | 4 | # Series and DataFrames 5 | s = pd.Series([1, 2, 3, 4, 5], name="Example Series") 6 | print(s) 7 | 8 | data = { 9 | 'A': [1, 2, 3], 10 | 'B': [4, 5, 6], 11 | 'C': [7, 8, 9] 12 | } 13 | df = pd.DataFrame(data) 14 | print(df) 15 | 16 | # Data cleaning and preparation 17 | df['D'] = df['A'] + df['B'] 18 | print("After adding a new column D:", df) 19 | -------------------------------------------------------------------------------- /time_series_analysis.py: -------------------------------------------------------------------------------- 1 | 2 | # NOTE: To run this file, you'll need to have statsmodels and related libraries installed. 3 | 4 | import pandas as pd 5 | import numpy as np 6 | import matplotlib.pyplot as plt 7 | from statsmodels.tsa.arima.model import ARIMA 8 | 9 | # Generating a simple time series with trend and seasonality 10 | time = np.arange(100) 11 | time_series = 5 * time + 10 * np.sin(0.1 * time) + np.random.normal(size=100) 12 | 13 | plt.plot(time, time_series) 14 | plt.title("Generated Time Series") 15 | plt.show() 16 | 17 | # ARIMA model 18 | model = ARIMA(time_series, order=(5,1,0)) 19 | model_fit = model.fit(disp=0) 20 | print(model_fit.summary()) 21 | 22 | # Forecast 23 | forecast = model_fit.forecast(steps=10) 24 | print(f"10-step forecast: {forecast}") 25 | --------------------------------------------------------------------------------