Skip to content

Commit

Permalink
new article
Browse files Browse the repository at this point in the history
  • Loading branch information
rageSpin committed Jun 22, 2024
1 parent 562f4ef commit b401e01
Show file tree
Hide file tree
Showing 56 changed files with 4,153 additions and 336 deletions.
Binary file removed assets/images/background.webp
Binary file not shown.
Binary file removed assets/images/site/icon.webp
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
195 changes: 195 additions & 0 deletions content/posts/finance/monte_carlo/Black-Scholes/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
---
title: "Monte Carlo Simulation for Option Pricing"
date: 2024-06-23T00:08:25+01:00
description: Physical Process of Percolation
menu:
sidebar:
name: Option Pricing
identifier: MonteCarlo_optionpricing
parent: monte_carlo
weight: 9
hero: Option-Pricing-Models-1.jpg
tags: ["Finance", "Options", "Statistics"]
categories: ["Finance"]
---

## 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.

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's toolkit.

## 2. The Black-Scholes Model

Before diving into Monte Carlo methods, it'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.

### The Black-Scholes Formula

For a European call option, the Black-Scholes formula is:

$$
C = S₀N(d_1) - Ke^{-rT}N(d_2)
$$
Where:
$$
d_1 = \frac{(ln(S_0/K) + (r + σ²/2)T)}{(σ\sqrt{T})}, \quad
d_2 = d_1 - \sigma \sqrt{T}
$$
- C: Call option price
- S₀: Current stock price
- K: Strike price
- r: Risk-free interest rate
- T: Time to maturity
- σ: Volatility of the underlying asset
- N(x): Cumulative standard normal distribution function

### Assumptions of the Black-Scholes Model

The Black-Scholes model rests on several key assumptions:

1. The stock price follows a geometric Brownian motion with constant drift and volatility.
2. No arbitrage opportunities exist in the market.
3. It's possible to buy and sell any amount of stock or options (including fractional amounts).
4. There are no transaction costs or taxes.
5. All securities are perfectly divisible.
6. The risk-free interest rate is constant and known.
7. The underlying stock does not pay dividends.

### Limitations of the Black-Scholes Model

While groundbreaking, the Black-Scholes model has several limitations:

1. **Constant Volatility**: The model assumes volatility is constant, which doesn't hold in real markets where volatility can change dramatically.

2. **Log-normal Distribution**: It assumes stock returns are normally distributed, which doesn't account for the fat-tailed distributions observed in reality.

3. **Continuous Trading**: The model assumes continuous trading is possible, which isn't realistic in practice.

4. **No Dividends**: It doesn't account for dividends, which can significantly affect option prices.

5. **European Options Only**: The original model only prices European-style options, not American or exotic options.

6. **Risk-free Rate**: It assumes a constant, known risk-free rate, which can vary in reality.

These limitations highlight why more flexible approaches like Monte Carlo simulation are valuable in option pricing.

## 3. Monte Carlo Simulation: Theoretical Background

Monte Carlo simulation addresses many of the Black-Scholes model's limitations by using computational power to model a wide range of possible future scenarios.

### Basic Principles

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's payoff for each path.

### Application to Option Pricing

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

```
dS = μSdt + σSdW
```

Where:
- S: Stock price
- μ: Expected return
- σ: Volatility
- dW: Wiener process (random walk)

This equation is then discretized for simulation purposes.

## 4. Implementing Monte Carlo Simulation in Python

Let's implement a basic Monte Carlo simulation for pricing a European call option:

```python
import numpy as np
import matplotlib.pyplot as plt

def monte_carlo_option_pricing(S0, K, T, r, sigma, num_simulations, num_steps):
dt = T / num_steps
paths = np.zeros((num_simulations, num_steps + 1))
paths[:, 0] = S0

for t in range(1, num_steps + 1):
z = np.random.standard_normal(num_simulations)
paths[:, t] = paths[:, t-1] * np.exp((r - 0.5 * sigma**2) * dt + sigma * np.sqrt(dt) * z)

option_payoffs = np.maximum(paths[:, -1] - K, 0)
option_price = np.exp(-r * T) * np.mean(option_payoffs)

return option_price, paths

# Example usage
S0 = 100 # Initial stock price
K = 100 # Strike price
T = 1 # Time to maturity (in years)
r = 0.05 # Risk-free rate
sigma = 0.2 # Volatility
num_simulations = 10000
num_steps = 252 # Number of trading days in a year

price, paths = monte_carlo_option_pricing(S0, K, T, r, sigma, num_simulations, num_steps)
print(f"Estimated option price: {price:.2f}")
```

This code simulates multiple stock price paths, calculates the option payoff for each path, and then averages these payoffs to estimate the option price.

## 5. Visualization and Analysis

Visualizing the results helps in understanding the distribution of possible outcomes:

```python
plt.figure(figsize=(10, 6))
plt.plot(paths[:100, :].T)
plt.title("Sample Stock Price Paths")
plt.xlabel("Time Steps")
plt.ylabel("Stock Price")
plt.show()

plt.figure(figsize=(10, 6))
plt.hist(paths[:, -1], bins=50)
plt.title("Distribution of Final Stock Prices")
plt.xlabel("Stock Price")
plt.ylabel("Frequency")
plt.show()
```

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.

## 6. Comparison with Analytical Solutions

To validate our Monte Carlo results, we can compare them with the Black-Scholes analytical solution:

```python
from scipy.stats import norm

def black_scholes_call(S0, K, T, r, sigma):
d1 = (np.log(S0 / K) + (r + 0.5 * sigma**2) * T) / (sigma * np.sqrt(T))
d2 = d1 - sigma * np.sqrt(T)
return S0 * norm.cdf(d1) - K * np.exp(-r * T) * norm.cdf(d2)

bs_price = black_scholes_call(S0, K, T, r, sigma)
print(f"Black-Scholes price: {bs_price:.2f}")
print(f"Monte Carlo price: {price:.2f}")
print(f"Difference: {abs(bs_price - price):.4f}")
```

The difference between the two methods gives us an idea of the Monte Carlo simulation's accuracy.

## 7. Advanced Topics and Extensions

Monte Carlo simulation's flexibility allows for various extensions:

1. **Variance Reduction Techniques**: Methods like antithetic variates can improve accuracy without increasing computational cost.
2. **Exotic Options**: Monte Carlo can price complex options like Asian or barrier options, which are challenging for analytical methods.
3. **Incorporating Dividends**: We can easily modify the simulation to account for dividend payments.
4. **Stochastic Volatility**: Models like Heston can be implemented to account for changing volatility.

## 8. Conclusion

Monte Carlo simulation offers a powerful and flexible approach to option pricing, addressing many limitations of analytical methods like the Black-Scholes model. While it can be computationally intensive, it handles complex scenarios and option structures with relative ease.

The method's ability to incorporate various market dynamics, such as changing volatility or dividend payments, makes it invaluable in real-world financial modeling. As computational power continues to increase, Monte Carlo methods are likely to play an even more significant role in quantitative finance.

However, it's important to remember that any model, including Monte Carlo simulation, is only as good as its underlying assumptions. Careful consideration of these assumptions and regular validation against market data remain crucial in applying these techniques effectively in practice.

9 changes: 9 additions & 0 deletions content/posts/finance/monte_carlo/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Monte Carlo
menu:
sidebar:
name: Monte Carlo
identifier: monte_carlo
parent: finance
weight: 10
---
7 changes: 6 additions & 1 deletion content/posts/finance/stock_prediction/GRU/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,16 @@ print(final_capital, bench_capital)
```

> returns
>
>
> True 81
> False 72
>
> Name: count, dtype: int64
> 10535.325897548326 9617.616876598737
> 10535.325 9617.617
## Conclusion

As showed in the previous section, these two simple Deep Learning models exhibits interesting positive results both regarding regression and trading metrics.
The latter is particularly important, indeed a return of **5%** is obtained while the stock price decreased of approximately 4%. This also lead to a very high sharpe and colmar ratio.
1 change: 1 addition & 0 deletions data/en/sections/skills.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ skills:
- name: Apache Cassandra DB
logo: /images/sections/skills/cassandra.png
summary: "Experience with NoSQL and distributed database"
url: https://cassandra.apache.org/_/index.html

- name: Apache Spark
logo: /images/sections/skills/spark.png
Expand Down
30 changes: 18 additions & 12 deletions hugo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ module:
- path: github.com/hugo-toha/hugo-toha.github.io
disable: false
mounts:
#- source: content
# target: content
- source: layouts
target: layouts
#- source: data
# target: data
- source: assets
# # - source: content
# # target: content
# # - source: layouts
# # target: layouts
# #- source: data
# # target: data
- source: assets
target: assets
- source: static
target: static
Expand All @@ -29,6 +29,12 @@ module:
target: static/files
- source: ./node_modules/katex/dist/fonts
target: static/fonts
- source: content
target: content
- source: assets
target: assets
# - source: static
# target: static

# Manage languages
# For any more details, you can check the official documentation: https://gohugo.io/content-management/multilingual/
Expand All @@ -38,11 +44,11 @@ languages:
languageName: English
title: "Stefano Giannini"
weight: 1
it:
languageCode: it
languageName: Italiano
title: "Stefano Giannini"
weight: 2
# it:
# languageCode: it
# languageName: Italiano
# title: "Stefano Giannini"
# weight: 2
# se:
# languageCode: se
# languageName: Svenska
Expand Down
34 changes: 0 additions & 34 deletions public/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -200,40 +200,6 @@







<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="languageSelector" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">


<span class="flag-icon flag-icon-gb"></span>

English
</a>
<div class="dropdown-menu" aria-labelledby="languageSelector">

<a class="dropdown-item nav-link languages-item" href="/404.html">


<span class="flag-icon flag-icon-gb"></span>

English
</a>

<a class="dropdown-item nav-link languages-item" href="/it/404.html">


<span class="flag-icon flag-icon-it"></span>

Italiano
</a>

</div>
</li>



<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="themeSelector" role="button"
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions public/categories/finance/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,6 @@






<section class="sidebar-section" id="sidebar-section">
<div class="sidebar-holder">
Expand Down
36 changes: 0 additions & 36 deletions public/categories/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -200,40 +200,6 @@







<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="languageSelector" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">


<span class="flag-icon flag-icon-gb"></span>

English
</a>
<div class="dropdown-menu" aria-labelledby="languageSelector">

<a class="dropdown-item nav-link languages-item" href="/categories">


<span class="flag-icon flag-icon-gb"></span>

English
</a>

<a class="dropdown-item nav-link languages-item" href="/it/categories">


<span class="flag-icon flag-icon-it"></span>

Italiano
</a>

</div>
</li>



<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="themeSelector" role="button"
Expand Down Expand Up @@ -272,8 +238,6 @@






<section class="sidebar-section" id="sidebar-section">
<div class="sidebar-holder">
Expand Down
2 changes: 0 additions & 2 deletions public/categories/physics/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,6 @@






<section class="sidebar-section" id="sidebar-section">
<div class="sidebar-holder">
Expand Down
Loading

0 comments on commit b401e01

Please sign in to comment.