1+ {
2+ "cells" : [
3+ {
4+ "cell_type" : " markdown" ,
5+ "metadata" : {},
6+ "source" : [
7+ " # Quantitative Finance with Python\n " ,
8+ " \n " ,
9+ " This notebook demonstrates the usage of various quantitative finance functions implemented in `financial_utils.py`. We'll cover:\n " ,
10+ " \n " ,
11+ " 1. Time Value of Money Calculations\n " ,
12+ " 2. Investment Performance Metrics\n " ,
13+ " 3. Risk Measures\n " ,
14+ " 4. Options Pricing\n " ,
15+ " \n " ,
16+ " Fixes #36"
17+ ]
18+ },
19+ {
20+ "cell_type" : " code" ,
21+ "execution_count" : null ,
22+ "metadata" : {},
23+ "source" : [
24+ " from financial_utils import *\n " ,
25+ " import numpy as np\n " ,
26+ " import matplotlib.pyplot as plt"
27+ ]
28+ },
29+ {
30+ "cell_type" : " markdown" ,
31+ "metadata" : {},
32+ "source" : [
33+ " ## 1. Time Value of Money\n " ,
34+ " \n " ,
35+ " Let's explore how money changes value over time using Present Value (PV) and Future Value (FV) calculations."
36+ ]
37+ },
38+ {
39+ "cell_type" : " code" ,
40+ "execution_count" : null ,
41+ "metadata" : {},
42+ "source" : [
43+ " # Example: Calculate present value of $10,000 received in 5 years with 5% interest rate\n " ,
44+ " future_amount = 10000\n " ,
45+ " rate = 0.05\n " ,
46+ " years = 5\n " ,
47+ " \n " ,
48+ " pv = present_value(future_amount, rate, years)\n " ,
49+ " print(f\" Present value of ${future_amount:,} in {years} years at {rate:.1%} interest: ${pv:,.2f}\" )\n " ,
50+ " \n " ,
51+ " # Verify with future value calculation\n " ,
52+ " fv = future_value(pv, rate, years)\n " ,
53+ " print(f\" Future value verification: ${fv:,.2f}\" )"
54+ ]
55+ },
56+ {
57+ "cell_type" : " markdown" ,
58+ "metadata" : {},
59+ "source" : [
60+ " ## 2. Investment Performance Analysis\n " ,
61+ " \n " ,
62+ " Let's analyze a series of stock prices and calculate returns."
63+ ]
64+ },
65+ {
66+ "cell_type" : " code" ,
67+ "execution_count" : null ,
68+ "metadata" : {},
69+ "source" : [
70+ " # Sample stock price data\n " ,
71+ " stock_prices = [100, 102, 99, 105, 103, 106, 109, 108, 110, 112]\n " ,
72+ " \n " ,
73+ " # Calculate returns\n " ,
74+ " returns = calculate_returns(stock_prices)\n " ,
75+ " \n " ,
76+ " # Plot the prices and returns\n " ,
77+ " fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 8))\n " ,
78+ " \n " ,
79+ " ax1.plot(stock_prices, marker='o')\n " ,
80+ " ax1.set_title('Stock Price Over Time')\n " ,
81+ " ax1.set_ylabel('Price ($)')\n " ,
82+ " \n " ,
83+ " ax2.bar(range(len(returns)), [r * 100 for r in returns])\n " ,
84+ " ax2.set_title('Daily Returns')\n " ,
85+ " ax2.set_ylabel('Return (%)')\n " ,
86+ " \n " ,
87+ " plt.tight_layout()\n " ,
88+ " plt.show()"
89+ ]
90+ },
91+ {
92+ "cell_type" : " markdown" ,
93+ "metadata" : {},
94+ "source" : [
95+ " ## 3. Risk Analysis\n " ,
96+ " \n " ,
97+ " Let's calculate some risk metrics including the Sharpe Ratio and Beta."
98+ ]
99+ },
100+ {
101+ "cell_type" : " code" ,
102+ "execution_count" : null ,
103+ "metadata" : {},
104+ "source" : [
105+ " # Calculate Sharpe ratio\n " ,
106+ " risk_free_rate = 0.02 # 2% annual risk-free rate\n " ,
107+ " sharpe = sharpe_ratio(returns, risk_free_rate)\n " ,
108+ " print(f\" Sharpe Ratio: {sharpe:.2f}\" )\n " ,
109+ " \n " ,
110+ " # Calculate Beta using simulated market returns\n " ,
111+ " market_returns = [0.01, -0.005, 0.02, 0.015, -0.01, 0.02, 0.01, -0.005, 0.015]\n " ,
112+ " beta_value = beta(returns, market_returns)\n " ,
113+ " print(f\" Beta: {beta_value:.2f}\" )"
114+ ]
115+ },
116+ {
117+ "cell_type" : " markdown" ,
118+ "metadata" : {},
119+ "source" : [
120+ " ## 4. Options Pricing\n " ,
121+ " \n " ,
122+ " Let's explore option pricing using the Black-Scholes model."
123+ ]
124+ },
125+ {
126+ "cell_type" : " code" ,
127+ "execution_count" : null ,
128+ "metadata" : {},
129+ "source" : [
130+ " # Parameters for Black-Scholes\n " ,
131+ " S = 100 # Current stock price\n " ,
132+ " K = 100 # Strike price\n " ,
133+ " T = 1.0 # Time to expiration (1 year)\n " ,
134+ " r = 0.05 # Risk-free rate (5%)\n " ,
135+ " sigma = 0.2 # Volatility (20%)\n " ,
136+ " \n " ,
137+ " # Calculate call and put option prices\n " ,
138+ " call_price = black_scholes(S, K, T, r, sigma, 'call')\n " ,
139+ " put_price = black_scholes(S, K, T, r, sigma, 'put')\n " ,
140+ " \n " ,
141+ " print(f\" Call Option Price: ${call_price:.2f}\" )\n " ,
142+ " print(f\" Put Option Price: ${put_price:.2f}\" )\n " ,
143+ " \n " ,
144+ " # Plot option prices for different strike prices\n " ,
145+ " strikes = np.linspace(80, 120, 41)\n " ,
146+ " call_prices = [black_scholes(S, k, T, r, sigma, 'call') for k in strikes]\n " ,
147+ " put_prices = [black_scholes(S, k, T, r, sigma, 'put') for k in strikes]\n " ,
148+ " \n " ,
149+ " plt.figure(figsize=(10, 6))\n " ,
150+ " plt.plot(strikes, call_prices, label='Call Option')\n " ,
151+ " plt.plot(strikes, put_prices, label='Put Option')\n " ,
152+ " plt.axvline(x=S, color='gray', linestyle='--', alpha=0.5)\n " ,
153+ " plt.xlabel('Strike Price')\n " ,
154+ " plt.ylabel('Option Price')\n " ,
155+ " plt.title('Option Prices vs Strike Price')\n " ,
156+ " plt.legend()\n " ,
157+ " plt.grid(True)\n " ,
158+ " plt.show()"
159+ ]
160+ }
161+ ],
162+ "metadata" : {
163+ "kernelspec" : {
164+ "display_name" : " Python 3" ,
165+ "language" : " python" ,
166+ "name" : " python3"
167+ },
168+ "language_info" : {
169+ "codemirror_mode" : {
170+ "name" : " ipython" ,
171+ "version" : 3
172+ },
173+ "file_extension" : " .py" ,
174+ "mimetype" : " text/x-python" ,
175+ "name" : " python" ,
176+ "nbconvert_exporter" : " python" ,
177+ "pygments_lexer" : " ipython3" ,
178+ "version" : " 3.8.0"
179+ }
180+ },
181+ "nbformat" : 4 ,
182+ "nbformat_minor" : 4
183+ }
0 commit comments