Skip to content

Commit 3a5fd9c

Browse files
committed
3rd articles completed and reviewed
1 parent 6148cc4 commit 3a5fd9c

File tree

9 files changed

+26
-19
lines changed

9 files changed

+26
-19
lines changed
Loading
Loading

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def monte_carlo_option_pricing(S0, K, T, r, sigma, num_simulations, num_steps):
121121

122122
# Example usage
123123
S0 = 100 # Initial stock price
124-
K = 100 # Strike price
124+
K = 98.5 # Strike price
125125
T = 1 # Time to maturity (in years)
126126
r = 0.05 # Risk-free rate
127127
sigma = 0.2 # Volatility
@@ -154,9 +154,11 @@ plt.ylabel("Frequency")
154154
plt.show()
155155
```
156156

157-
{{< img src="\posts\finance\monte_carlo\Black-Scholes\images\simulation_path.png" align="center" title="Results">}}
157+
{{< img src="/posts/finance/monte_carlo/Black-Scholes/images/simulation_path.png" align="center">}}
158158

159-
{{< img src="/posts/finance/monte_carlo/Black-Scholes/images/simulation_histogram.png" align="center" title="Results">}}
159+
---
160+
161+
{{< img src="/posts/finance/monte_carlo/Black-Scholes/images/simulation_histogram.png" align="center" title="Histogram">}}
160162

161163
These visualizations show the range of possible stock price paths and the distribution of final stock prices, providing insight into the option's potential outcomes.
162164

@@ -173,8 +175,8 @@ def black_scholes_call(S0, K, T, r, sigma):
173175
return S0 * norm.cdf(d1) - K * np.exp(-r * T) * norm.cdf(d2)
174176

175177
bs_price = black_scholes_call(S0, K, T, r, sigma)
176-
print(f"Black-Scholes price: {bs_price:.2f}")
177-
print(f"Monte Carlo price: {price:.2f}")
178+
print(f"Black-Scholes price: {bs_price:.3f}")
179+
print(f"Monte Carlo price: {price:.3f}")
178180
print(f"Difference: {abs(bs_price - price):.4f}")
179181
```
180182

content/posts/finance/monte_carlo/Black-Scholes/options.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
import numpy as np
99
import matplotlib.pyplot as plt
10+
import seaborn as sns
11+
sns.set_style('whitegrid')
12+
plt.style.use("fivethirtyeight")
1013

1114
def monte_carlo_option_pricing(S0, K, T, r, sigma, num_simulations, num_steps):
1215
dt = T / num_steps
@@ -35,15 +38,15 @@ def monte_carlo_option_pricing(S0, K, T, r, sigma, num_simulations, num_steps):
3538
print(f"Estimated option price: {price:.3f}")
3639

3740
## Visualization
38-
plt.figure(figsize=(10, 6))
39-
plt.plot(paths[:100, :].T)
41+
plt.figure(figsize=(12, 8))
42+
plt.plot(paths[:100, :].T, linewidth=1)
4043
plt.title("Sample Stock Price Paths")
4144
plt.xlabel("Time Steps")
4245
plt.ylabel("Stock Price")
4346
plt.show()
4447
plt.savefig("images/simulation_path.png", dpi=300)
4548

46-
plt.figure(figsize=(10, 6))
49+
plt.figure(figsize=(12, 8))
4750
plt.hist(paths[:, -1], bins=100)
4851
plt.title("Distribution of Final Stock Prices")
4952
plt.xlabel("Stock Price")

public/index.json

+1-1
Large diffs are not rendered by default.
Loading
Loading

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ <h2 id="4-implementing-monte-carlo-simulation-in-python">4. Implementing Monte C
494494
</span></span><span style="display:flex;"><span>
495495
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Example usage</span>
496496
</span></span><span style="display:flex;"><span>S0 <span style="color:#f92672">=</span> <span style="color:#ae81ff">100</span> <span style="color:#75715e"># Initial stock price</span>
497-
</span></span><span style="display:flex;"><span>K <span style="color:#f92672">=</span> <span style="color:#ae81ff">100</span> <span style="color:#75715e"># Strike price</span>
497+
</span></span><span style="display:flex;"><span>K <span style="color:#f92672">=</span> <span style="color:#ae81ff">98.5</span> <span style="color:#75715e"># Strike price</span>
498498
</span></span><span style="display:flex;"><span>T <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span> <span style="color:#75715e"># Time to maturity (in years)</span>
499499
</span></span><span style="display:flex;"><span>r <span style="color:#f92672">=</span> <span style="color:#ae81ff">0.05</span> <span style="color:#75715e"># Risk-free rate</span>
500500
</span></span><span style="display:flex;"><span>sigma <span style="color:#f92672">=</span> <span style="color:#ae81ff">0.2</span> <span style="color:#75715e"># Volatility</span>
@@ -519,9 +519,7 @@ <h2 id="5-visualization-and-analysis">5. Visualization and Analysis</h2>
519519
</span></span><span style="display:flex;"><span>plt<span style="color:#f92672">.</span>xlabel(<span style="color:#e6db74">&#34;Stock Price&#34;</span>)
520520
</span></span><span style="display:flex;"><span>plt<span style="color:#f92672">.</span>ylabel(<span style="color:#e6db74">&#34;Frequency&#34;</span>)
521521
</span></span><span style="display:flex;"><span>plt<span style="color:#f92672">.</span>show()
522-
</span></span></code></pre></div><img src="%5cposts%5cfinance%5cmonte_carlo%5cBlack-Scholes%5cimages%5csimulation_path.png"
523-
524-
alt="Results"
522+
</span></span></code></pre></div><img src="/posts/finance/monte_carlo/Black-Scholes/images/simulation_path.png"
525523

526524

527525

@@ -531,9 +529,10 @@ <h2 id="5-visualization-and-analysis">5. Visualization and Analysis</h2>
531529

532530
>
533531

532+
<hr>
534533
<img src="/posts/finance/monte_carlo/Black-Scholes/images/simulation_histogram.png"
535534

536-
alt="Results"
535+
alt="Histogram"
537536

538537

539538

@@ -554,8 +553,8 @@ <h2 id="6-comparison-with-analytical-solutions">6. Comparison with Analytical So
554553
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">return</span> S0 <span style="color:#f92672">*</span> norm<span style="color:#f92672">.</span>cdf(d1) <span style="color:#f92672">-</span> K <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> norm<span style="color:#f92672">.</span>cdf(d2)
555554
</span></span><span style="display:flex;"><span>
556555
</span></span><span style="display:flex;"><span>bs_price <span style="color:#f92672">=</span> black_scholes_call(S0, K, T, r, sigma)
557-
</span></span><span style="display:flex;"><span>print(<span style="color:#e6db74">f</span><span style="color:#e6db74">&#34;Black-Scholes price: </span><span style="color:#e6db74">{</span>bs_price<span style="color:#e6db74">:</span><span style="color:#e6db74">.2f</span><span style="color:#e6db74">}</span><span style="color:#e6db74">&#34;</span>)
558-
</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>)
556+
</span></span><span style="display:flex;"><span>print(<span style="color:#e6db74">f</span><span style="color:#e6db74">&#34;Black-Scholes price: </span><span style="color:#e6db74">{</span>bs_price<span style="color:#e6db74">:</span><span style="color:#e6db74">.3f</span><span style="color:#e6db74">}</span><span style="color:#e6db74">&#34;</span>)
557+
</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">.3f</span><span style="color:#e6db74">}</span><span style="color:#e6db74">&#34;</span>)
559558
</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>)
560559
</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>
561560
<blockquote>

public/posts/finance/monte_carlo/black-scholes/options.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
import numpy as np
99
import matplotlib.pyplot as plt
10+
import seaborn as sns
11+
sns.set_style('whitegrid')
12+
plt.style.use("fivethirtyeight")
1013

1114
def monte_carlo_option_pricing(S0, K, T, r, sigma, num_simulations, num_steps):
1215
dt = T / num_steps
@@ -35,15 +38,15 @@ def monte_carlo_option_pricing(S0, K, T, r, sigma, num_simulations, num_steps):
3538
print(f"Estimated option price: {price:.3f}")
3639

3740
## Visualization
38-
plt.figure(figsize=(10, 6))
39-
plt.plot(paths[:100, :].T)
41+
plt.figure(figsize=(12, 8))
42+
plt.plot(paths[:100, :].T, linewidth=1)
4043
plt.title("Sample Stock Price Paths")
4144
plt.xlabel("Time Steps")
4245
plt.ylabel("Stock Price")
4346
plt.show()
4447
plt.savefig("images/simulation_path.png", dpi=300)
4548

46-
plt.figure(figsize=(10, 6))
49+
plt.figure(figsize=(12, 8))
4750
plt.hist(paths[:, -1], bins=100)
4851
plt.title("Distribution of Final Stock Prices")
4952
plt.xlabel("Stock Price")

0 commit comments

Comments
 (0)