Skip to content

Commit 1638f88

Browse files
committed
first phase of reducing dependencies
1 parent 8ce9442 commit 1638f88

File tree

35 files changed

+465
-82
lines changed

35 files changed

+465
-82
lines changed
6.39 KB
Loading

content/posts/finance/monte_carlo/Black-Scholes/index.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,13 @@ For a European call option, the Black-Scholes formula is:
3030

3131
$$
3232
C = S₀N(d_1) - Ke^{-rT}N(d_2)
33-
3433
$$
3534

3635
Where:
3736

3837
$$
3938
d_1 = \frac{(ln(S_0/K) + (r + σ²/2)T)}{(σ\sqrt{T})}, \quad
4039
d_2 = d_1 - \sigma \sqrt{T}
41-
4240
$$
4341

4442
- C: Call option price
@@ -86,9 +84,9 @@ Monte Carlo methods use repeated random sampling to obtain numerical results. In
8684

8785
For option pricing, we model the stock price movement using a stochastic differential equation:
8886

89-
```
90-
dS = μSdt + σSdW
91-
```
87+
$$
88+
dS = \mu Sdt + \sigma SdW
89+
$$
9290

9391
Where:
9492

hugo.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ module:
1818
# # target: data
1919
- source: assets
2020
target: assets
21-
- source: static
22-
target: static
21+
# - source: static
22+
# target: static
2323
mounts:
2424
- source: static/files
2525
target: static/files

public/categories/finance/index.html

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,50 @@
288288

289289

290290

291+
<div class="post-card">
292+
<div class="card">
293+
<div class="card-head">
294+
<a href="/posts/finance/monte_carlo/black-scholes/" class="post-card-link">
295+
<img class="card-img-top" src='/posts/finance/monte_carlo/black-scholes/Option-Pricing-Models-1.jpg' alt="Hero Image">
296+
</a>
297+
</div>
298+
<div class="card-body">
299+
<a href="/posts/finance/monte_carlo/black-scholes/" class="post-card-link">
300+
<h5 class="card-title">Monte Carlo Simulation for Option Pricing</h5>
301+
<p class="card-text post-summary">1. Introduction In the dynamic world of finance, options play a crucial role in risk management, speculation, and portfolio optimization. An option is a contract that gives the holder the right, but not the obligation, to buy (call option) or sell (put option) an underlying asset at a predetermined price (strike price) within a specific time frame. The challenge lies in accurately pricing these financial instruments, given the uncertainties in market movements.</p>
302+
</a>
303+
304+
<div class="tags">
305+
<ul style="padding-left: 0;">
306+
307+
308+
<li class="rounded"><a href="/tags/finance/" class="btn btn-sm btn-info">Finance</a></li>
309+
310+
311+
<li class="rounded"><a href="/tags/options/" class="btn btn-sm btn-info">Options</a></li>
312+
313+
314+
<li class="rounded"><a href="/tags/statistics/" class="btn btn-sm btn-info">Statistics</a></li>
315+
316+
</ul>
317+
</div>
318+
319+
320+
</div>
321+
<div class="card-footer">
322+
<span class="float-start">
323+
Sunday, June 23, 2024
324+
| 6 minutes </span>
325+
<a
326+
href="/posts/finance/monte_carlo/black-scholes/"
327+
class="float-end btn btn-outline-info btn-sm">Read</a>
328+
</div>
329+
</div>
330+
</div>
331+
332+
333+
334+
291335
<div class="post-card">
292336
<div class="card">
293337
<div class="card-head">

public/categories/finance/index.xml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,16 @@
66
<description>Recent content in Finance on Stefano Giannini</description>
77
<generator>Hugo -- gohugo.io</generator>
88
<language>en</language>
9-
<lastBuildDate>Sun, 16 Jun 2024 00:00:00 +0100</lastBuildDate><atom:link href="http://localhost:1313/categories/finance/index.xml" rel="self" type="application/rss+xml" /><item>
9+
<lastBuildDate>Sun, 23 Jun 2024 00:08:25 +0100</lastBuildDate><atom:link href="http://localhost:1313/categories/finance/index.xml" rel="self" type="application/rss+xml" /><item>
10+
<title>Monte Carlo Simulation for Option Pricing</title>
11+
<link>http://localhost:1313/posts/finance/monte_carlo/black-scholes/</link>
12+
<pubDate>Sun, 23 Jun 2024 00:08:25 +0100</pubDate>
13+
14+
<guid>http://localhost:1313/posts/finance/monte_carlo/black-scholes/</guid>
15+
<description>1. Introduction In the dynamic world of finance, options play a crucial role in risk management, speculation, and portfolio optimization. An option is a contract that gives the holder the right, but not the obligation, to buy (call option) or sell (put option) an underlying asset at a predetermined price (strike price) within a specific time frame. The challenge lies in accurately pricing these financial instruments, given the uncertainties in market movements.</description>
16+
</item>
17+
18+
<item>
1019
<title>MSFT Stock Prediction using LSTM or GRU</title>
1120
<link>http://localhost:1313/posts/finance/stock_prediction/gru/</link>
1221
<pubDate>Sun, 16 Jun 2024 00:00:00 +0100</pubDate>

public/categories/index.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
<description>Recent content in Categories on Stefano Giannini</description>
77
<generator>Hugo -- gohugo.io</generator>
88
<language>en</language>
9-
<lastBuildDate>Sun, 16 Jun 2024 00:00:00 +0100</lastBuildDate><atom:link href="http://localhost:1313/categories/index.xml" rel="self" type="application/rss+xml" />
9+
<lastBuildDate>Sun, 23 Jun 2024 00:08:25 +0100</lastBuildDate><atom:link href="http://localhost:1313/categories/index.xml" rel="self" type="application/rss+xml" />
1010
</channel>
1111
</rss>

public/index.html

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -951,11 +951,6 @@ <h1 class="text-center">
951951

952952

953953

954-
<div class="logo-holder">
955-
<img class="company-logo light-logo" src="/images/sections/experience/stellantis_logo.png" alt="" />
956-
957-
</div>
958-
959954

960955

961956

@@ -1039,11 +1034,6 @@ <h6 class="text-heading">Responsibilities:</h6>
10391034

10401035

10411036

1042-
<div class="logo-holder">
1043-
<img class="company-logo light-logo" src="/images/sections/experience/smairthero.png" alt="" />
1044-
1045-
</div>
1046-
10471037

10481038

10491039

@@ -1115,11 +1105,6 @@ <h6 class="text-heading">Responsibilities:</h6>
11151105

11161106

11171107

1118-
<div class="logo-holder">
1119-
<img class="company-logo light-logo" src="/images/sections/experience/Soave_logo.jpg" alt="" />
1120-
1121-
</div>
1122-
11231108

11241109

11251110

public/index.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

public/posts/data-science/index.html

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,23 @@
267267

268268

269269

270-
<li><a class=" list-link" href="/posts/finance/monte_carlo/" title="Monte Carlo">Monte Carlo</a></li>
270+
<li>
271+
<i data-feather="plus-circle"></i><a class=" list-link" href="/posts/finance/monte_carlo/"> Monte Carlo</a>
272+
273+
<ul class="">
274+
275+
276+
277+
278+
279+
280+
281+
<li><a class=" list-link" href="/posts/finance/monte_carlo/black-scholes/" title="Option Pricing">Option Pricing</a></li>
282+
283+
284+
285+
</ul>
286+
</li>
271287

272288

273289

Loading
Loading

public/posts/finance/monte_carlo/black-scholes/index.html

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -412,15 +412,16 @@ <h1>Monte Carlo Simulation for Option Pricing</h1>
412412
<h2 id="1-introduction">1. Introduction</h2>
413413
<p>In the dynamic world of finance, options play a crucial role in risk management, speculation, and portfolio optimization. An option is a contract that gives the holder the right, but not the obligation, to buy (call option) or sell (put option) an underlying asset at a predetermined price (strike price) within a specific time frame. The challenge lies in accurately pricing these financial instruments, given the uncertainties in market movements.</p>
414414
<p>Traditional analytical methods, while powerful, often struggle with complex option structures or realistic market conditions. This is where Monte Carlo simulation steps in, offering a flexible and robust approach to option pricing. By leveraging the power of computational methods, Monte Carlo simulations can handle a wide array of option types and market scenarios, making it an indispensable tool in a quantitative analyst&rsquo;s toolkit.</p>
415+
<p>For further explanation about <em>options pricing</em>, check <a href="https://www.investopedia.com/articles/optioninvestor/07/options_beat_market.asp" target="_blank" rel="noopener">Investopedia</a>.</p>
415416
<h2 id="2-the-black-scholes-model">2. The Black-Scholes Model</h2>
416417
<p>Before diving into Monte Carlo methods, it&rsquo;s crucial to understand the Black-Scholes model, a cornerstone in option pricing theory. Developed by Fischer Black, Myron Scholes, and Robert Merton in the early 1970s, this model revolutionized the field of quantitative finance.</p>
417418
<h3 id="the-black-scholes-formula">The Black-Scholes Formula</h3>
418419
<p>For a European call option, the Black-Scholes formula is:</p>
419420
<p>$$
420421
C = S₀N(d_1) - Ke^{-rT}N(d_2)
421-
$$
422-
Where:
423-
$$
422+
$$</p>
423+
<p>Where:</p>
424+
<p>$$
424425
d_1 = \frac{(ln(S_0/K) + (r + σ²/2)T)}{(σ\sqrt{T})}, \quad
425426
d_2 = d_1 - \sigma \sqrt{T}
426427
$$</p>
@@ -447,24 +448,12 @@ <h3 id="assumptions-of-the-black-scholes-model">Assumptions of the Black-Scholes
447448
<h3 id="limitations-of-the-black-scholes-model">Limitations of the Black-Scholes Model</h3>
448449
<p>While groundbreaking, the Black-Scholes model has several limitations:</p>
449450
<ol>
450-
<li>
451-
<p><strong>Constant Volatility</strong>: The model assumes volatility is constant, which doesn&rsquo;t hold in real markets where volatility can change dramatically.</p>
452-
</li>
453-
<li>
454-
<p><strong>Log-normal Distribution</strong>: It assumes stock returns are normally distributed, which doesn&rsquo;t account for the fat-tailed distributions observed in reality.</p>
455-
</li>
456-
<li>
457-
<p><strong>Continuous Trading</strong>: The model assumes continuous trading is possible, which isn&rsquo;t realistic in practice.</p>
458-
</li>
459-
<li>
460-
<p><strong>No Dividends</strong>: It doesn&rsquo;t account for dividends, which can significantly affect option prices.</p>
461-
</li>
462-
<li>
463-
<p><strong>European Options Only</strong>: The original model only prices European-style options, not American or exotic options.</p>
464-
</li>
465-
<li>
466-
<p><strong>Risk-free Rate</strong>: It assumes a constant, known risk-free rate, which can vary in reality.</p>
467-
</li>
451+
<li><strong>Constant Volatility</strong>: The model assumes volatility is constant, which doesn&rsquo;t hold in real markets where volatility can change dramatically.</li>
452+
<li><strong>Log-normal Distribution</strong>: It assumes stock returns are normally distributed, which doesn&rsquo;t account for the fat-tailed distributions observed in reality.</li>
453+
<li><strong>Continuous Trading</strong>: The model assumes continuous trading is possible, which isn&rsquo;t realistic in practice.</li>
454+
<li><strong>No Dividends</strong>: It doesn&rsquo;t account for dividends, which can significantly affect option prices.</li>
455+
<li><strong>European Options Only</strong>: The original model only prices European-style options, not American or exotic options.</li>
456+
<li><strong>Risk-free Rate</strong>: It assumes a constant, known risk-free rate, which can vary in reality.</li>
468457
</ol>
469458
<p>These limitations highlight why more flexible approaches like Monte Carlo simulation are valuable in option pricing.</p>
470459
<h2 id="3-monte-carlo-simulation-theoretical-background">3. Monte Carlo Simulation: Theoretical Background</h2>
@@ -473,8 +462,10 @@ <h3 id="basic-principles">Basic Principles</h3>
473462
<p>Monte Carlo methods use repeated random sampling to obtain numerical results. In the context of option pricing, we simulate many possible price paths for the underlying asset and then calculate the option&rsquo;s payoff for each path.</p>
474463
<h3 id="application-to-option-pricing">Application to Option Pricing</h3>
475464
<p>For option pricing, we model the stock price movement using a stochastic differential equation:</p>
476-
<pre tabindex="0"><code>dS = μSdt + σSdW
477-
</code></pre><p>Where:</p>
465+
<p>$$
466+
dS = \mu Sdt + \sigma SdW
467+
$$</p>
468+
<p>Where:</p>
478469
<ul>
479470
<li>S: Stock price</li>
480471
<li>μ: Expected return</li>
@@ -491,14 +482,14 @@ <h2 id="4-implementing-monte-carlo-simulation-in-python">4. Implementing Monte C
491482
</span></span><span style="display:flex;"><span> dt <span style="color:#f92672">=</span> T <span style="color:#f92672">/</span> num_steps
492483
</span></span><span style="display:flex;"><span> paths <span style="color:#f92672">=</span> np<span style="color:#f92672">.</span>zeros((num_simulations, num_steps <span style="color:#f92672">+</span> <span style="color:#ae81ff">1</span>))
493484
</span></span><span style="display:flex;"><span> paths[:, <span style="color:#ae81ff">0</span>] <span style="color:#f92672">=</span> S0
494-
</span></span><span style="display:flex;"><span>
485+
</span></span><span style="display:flex;"><span>
495486
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">for</span> t <span style="color:#f92672">in</span> range(<span style="color:#ae81ff">1</span>, num_steps <span style="color:#f92672">+</span> <span style="color:#ae81ff">1</span>):
496487
</span></span><span style="display:flex;"><span> z <span style="color:#f92672">=</span> np<span style="color:#f92672">.</span>random<span style="color:#f92672">.</span>standard_normal(num_simulations)
497488
</span></span><span style="display:flex;"><span> paths[:, t] <span style="color:#f92672">=</span> paths[:, t<span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>] <span style="color:#f92672">*</span> np<span style="color:#f92672">.</span>exp((r <span style="color:#f92672">-</span> <span style="color:#ae81ff">0.5</span> <span style="color:#f92672">*</span> sigma<span style="color:#f92672">**</span><span style="color:#ae81ff">2</span>) <span style="color:#f92672">*</span> dt <span style="color:#f92672">+</span> sigma <span style="color:#f92672">*</span> np<span style="color:#f92672">.</span>sqrt(dt) <span style="color:#f92672">*</span> z)
498-
</span></span><span style="display:flex;"><span>
489+
</span></span><span style="display:flex;"><span>
499490
</span></span><span style="display:flex;"><span> option_payoffs <span style="color:#f92672">=</span> np<span style="color:#f92672">.</span>maximum(paths[:, <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>] <span style="color:#f92672">-</span> K, <span style="color:#ae81ff">0</span>)
500491
</span></span><span style="display:flex;"><span> option_price <span style="color:#f92672">=</span> np<span style="color:#f92672">.</span>exp(<span style="color:#f92672">-</span>r <span style="color:#f92672">*</span> T) <span style="color:#f92672">*</span> np<span style="color:#f92672">.</span>mean(option_payoffs)
501-
</span></span><span style="display:flex;"><span>
492+
</span></span><span style="display:flex;"><span>
502493
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">return</span> option_price, paths
503494
</span></span><span style="display:flex;"><span>
504495
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Example usage</span>
@@ -528,7 +519,31 @@ <h2 id="5-visualization-and-analysis">5. Visualization and Analysis</h2>
528519
</span></span><span style="display:flex;"><span>plt<span style="color:#f92672">.</span>xlabel(<span style="color:#e6db74">&#34;Stock Price&#34;</span>)
529520
</span></span><span style="display:flex;"><span>plt<span style="color:#f92672">.</span>ylabel(<span style="color:#e6db74">&#34;Frequency&#34;</span>)
530521
</span></span><span style="display:flex;"><span>plt<span style="color:#f92672">.</span>show()
531-
</span></span></code></pre></div><p>These visualizations show the range of possible stock price paths and the distribution of final stock prices, providing insight into the option&rsquo;s potential outcomes.</p>
522+
</span></span></code></pre></div><img src="%5cposts%5cfinance%5cmonte_carlo%5cBlack-Scholes%5cimages%5csimulation_path.png"
523+
524+
alt="Results"
525+
526+
527+
528+
529+
530+
class="center"
531+
532+
>
533+
534+
<img src="/posts/finance/monte_carlo/Black-Scholes/images/simulation_histogram.png"
535+
536+
alt="Results"
537+
538+
539+
540+
541+
542+
class="center"
543+
544+
>
545+
546+
<p>These visualizations show the range of possible stock price paths and the distribution of final stock prices, providing insight into the option&rsquo;s potential outcomes.</p>
532547
<h2 id="6-comparison-with-analytical-solutions">6. Comparison with Analytical Solutions</h2>
533548
<p>To validate our Monte Carlo results, we can compare them with the Black-Scholes analytical solution:</p>
534549
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#f92672">from</span> scipy.stats <span style="color:#f92672">import</span> norm
@@ -543,6 +558,11 @@ <h2 id="6-comparison-with-analytical-solutions">6. Comparison with Analytical So
543558
</span></span><span style="display:flex;"><span>print(<span style="color:#e6db74">f</span><span style="color:#e6db74">&#34;Monte Carlo price: </span><span style="color:#e6db74">{</span>price<span style="color:#e6db74">:</span><span style="color:#e6db74">.2f</span><span style="color:#e6db74">}</span><span style="color:#e6db74">&#34;</span>)
544559
</span></span><span style="display:flex;"><span>print(<span style="color:#e6db74">f</span><span style="color:#e6db74">&#34;Difference: </span><span style="color:#e6db74">{</span>abs(bs_price <span style="color:#f92672">-</span> price)<span style="color:#e6db74">:</span><span style="color:#e6db74">.4f</span><span style="color:#e6db74">}</span><span style="color:#e6db74">&#34;</span>)
545560
</span></span></code></pre></div><p>The difference between the two methods gives us an idea of the Monte Carlo simulation&rsquo;s accuracy.</p>
561+
<blockquote>
562+
<p>Black-Scholes price: 11.270</p>
563+
<p>Monte Carlo price: 11.445</p>
564+
<p>Difference: 0.1744</p>
565+
</blockquote>
546566
<h2 id="7-advanced-topics-and-extensions">7. Advanced Topics and Extensions</h2>
547567
<p>Monte Carlo simulation&rsquo;s flexibility allows for various extensions:</p>
548568
<ol>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Sun Jun 23 11:30:47 2024
4+
5+
@author: stefa
6+
"""
7+
8+
import numpy as np
9+
import matplotlib.pyplot as plt
10+
11+
def monte_carlo_option_pricing(S0, K, T, r, sigma, num_simulations, num_steps):
12+
dt = T / num_steps
13+
paths = np.zeros((num_simulations, num_steps + 1))
14+
paths[:, 0] = S0
15+
16+
for t in range(1, num_steps + 1):
17+
z = np.random.standard_normal(num_simulations)
18+
paths[:, t] = paths[:, t-1] * np.exp((r - 0.5 * sigma**2) * dt + sigma * np.sqrt(dt) * z)
19+
20+
option_payoffs = np.maximum(paths[:, -1] - K, 0)
21+
option_price = np.exp(-r * T) * np.mean(option_payoffs)
22+
23+
return option_price, paths
24+
25+
# Example usage
26+
S0 = 100 # Initial stock price
27+
K = 98.5 # Strike price
28+
T = 1 # Time to maturity (in years)
29+
r = 0.05 # Risk-free rate
30+
sigma = 0.2 # Volatility
31+
num_simulations = 10000
32+
num_steps = 252 # Number of trading days in a year
33+
34+
price, paths = monte_carlo_option_pricing(S0, K, T, r, sigma, num_simulations, num_steps)
35+
print(f"Estimated option price: {price:.3f}")
36+
37+
## Visualization
38+
plt.figure(figsize=(10, 6))
39+
plt.plot(paths[:100, :].T)
40+
plt.title("Sample Stock Price Paths")
41+
plt.xlabel("Time Steps")
42+
plt.ylabel("Stock Price")
43+
plt.show()
44+
plt.savefig("images/simulation_path.png", dpi=300)
45+
46+
plt.figure(figsize=(10, 6))
47+
plt.hist(paths[:, -1], bins=100)
48+
plt.title("Distribution of Final Stock Prices")
49+
plt.xlabel("Stock Price")
50+
plt.ylabel("Frequency")
51+
plt.vlines(S0, 0, 350, colors='r', linestyles='--', label=r'$S_0$')
52+
plt.legend()
53+
plt.show()
54+
plt.savefig("images/simulation_histogram.png", dpi=300)
55+
56+
57+
from scipy.stats import norm
58+
59+
def black_scholes_call(S0, K, T, r, sigma):
60+
d1 = (np.log(S0 / K) + (r + 0.5 * sigma**2) * T) / (sigma * np.sqrt(T))
61+
d2 = d1 - sigma * np.sqrt(T)
62+
return S0 * norm.cdf(d1) - K * np.exp(-r * T) * norm.cdf(d2)
63+
64+
65+
bs_price = black_scholes_call(S0, K, T, r, sigma)
66+
print(f"Black-Scholes price: {bs_price:.3f}")
67+
print(f"Monte Carlo price: {price:.3f}")
68+
print(f"Difference: {abs(bs_price - price):.4f}")

public/posts/finance/stock_prediction/index.html

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,23 @@
270270

271271

272272

273-
<li><a class=" list-link" href="/posts/finance/monte_carlo/" title="Monte Carlo">Monte Carlo</a></li>
273+
<li>
274+
<i data-feather="plus-circle"></i><a class=" list-link" href="/posts/finance/monte_carlo/"> Monte Carlo</a>
275+
276+
<ul class="">
277+
278+
279+
280+
281+
282+
283+
284+
<li><a class=" list-link" href="/posts/finance/monte_carlo/black-scholes/" title="Option Pricing">Option Pricing</a></li>
285+
286+
287+
288+
</ul>
289+
</li>
274290

275291

276292

0 commit comments

Comments
 (0)