Brownian motion and stochastic calculus are fundamental concepts in finance, particularly in the modeling of asset prices and derivatives pricing. Below, I'll provide an example of simulating geometric Brownian motion (GBM) using R programming language and how it can be applied to model stock prices. We'll also include a brief demonstration of stochastic calculus in the context of option pricing using the Black-Scholes-Merton model.
```R
# Load required libraries
library(ggplot2)
# Parameters
S0 <- 100 # Initial stock price
mu <- 0.05 # Drift
sigma <- 0.2 # Volatility
dt <- 1/252 # Time step (daily)
T <- 1 # Time horizon (1 year)
N <- T/dt # Number of time steps
n_paths <- 5 # Number of simulated paths
# Function to simulate geometric Brownian motion
simulate_gbm <- function(S0, mu, sigma, dt, N, n_paths) {
paths <- matrix(NA, nrow = N + 1, ncol = n_paths)
paths[1,] <- S0
for (i in 1:n_paths) {
for (j in 2:(N + 1)) {
paths[j, i] <- paths[j - 1, i] * exp((mu - 0.5 * sigma^2) * dt +
sigma * sqrt(dt) * rnorm(1))
}
}
return(paths)
}
# Simulate GBM paths
paths <- simulate_gbm(S0, mu, sigma, dt, N, n_paths)
# Plot simulated paths
ggplot() +
geom_line(aes(x = 0:N * dt, y = paths), color = "blue") +
labs(title = "Simulated Geometric Brownian Motion Paths",
x = "Time",
y = "Stock Price")
```
This code simulates multiple paths of a stock price process governed by geometric Brownian motion. Each path represents a potential evolution of the stock price over time.
Now, let's briefly demonstrate the application of stochastic calculus in finance, specifically in option pricing using the Black-Scholes-Merton model:
```R
# Black-Scholes-Merton option pricing formula
bsm_call <- function(S0, K, T, r, sigma) {
d1 <- (log(S0/K) + (r + 0.5 * sigma^2) * T) / (sigma * sqrt(T))
d2 <- d1 - sigma * sqrt(T)
C <- S0 * pnorm(d1) - K * exp(-r * T) * pnorm(d2)
return(C)
}
# Parameters for option pricing
S0 <- 100 # Initial stock price
K <- 105 # Strike price
T <- 0.5 # Time to expiration (in years)
r <- 0.05 # Risk-free interest rate
sigma <- 0.2 # Volatility
# Calculate call option price
call_price <- bsm_call(S0, K, T, r, sigma)
print(paste("Black-Scholes-Merton Call Option Price:", round(call_price, 2)))
```
This code calculates the price of a European call option using the Black-Scholes-Merton formula. It takes into account the current stock price, strike price, time to expiration, risk-free interest rate, and volatility. This is just a basic demonstration; in practice, more sophisticated models and techniques are used for option pricing and risk management.

No comments:
Post a Comment