Back to Course

Monte Carlo Methods in Finance Engineering

0% Complete
0/0 Steps
  1. Course Introduction

    Introduction
    2 Topics
    |
    1 Quiz
  2. Lesson 1. Linear Congruental Generator
    2 Topics
    |
    1 Quiz
  3. Lesson 2. Normally Distributed Random Variables and Vectors Generation
    2 Topics
    |
    1 Quiz
  4. Lesson 3. Integral Estimation.
    2 Topics
    |
    1 Quiz
  5. Lesson 4. Expected Present Value
    1 Quiz
  6. Lesson 5. Brownian motion. Geometric Brownian motion
    2 Topics
    |
    1 Quiz
  7. Lesson 6. GBM. Path-Dependent Options
    1 Topic
  8. Lesson 7. The effectiveness of simulation evaluation
    2 Topics
    |
    1 Quiz
  9. Lesson 8. Variance reduction methods
    3 Topics
    |
    1 Quiz
  10. Lesson 9. Introduction to RTU High Performance Computing (HPC) Environment
Lesson Progress
0% Complete

Run the code with the different number of iterations and stock parameters

import numpy as np


S0 = 50        
K = 55         
T = 1          
mu = r = 0.05  
sigma = 0.2    

# Funkcija Montekarlo simulācijai
def asian_option_mc(S0, K, T, r, sigma, n, m, average_type='arithmetic'):

    dt = T / n
    discount_factor = np.exp(-r * T)

    # Izveido m simulētās cenas ceļus (m x n)
    Z = np.random.randn(m, n)
    S_paths = np.zeros((m, n))
    S_paths[:, 0] = S0 * np.exp((r - 0.5 * sigma**2) * dt + sigma * np.sqrt(dt) * Z[:, 0])

    for i in range(1, n):
        S_paths[:, i] = S_paths[:, i-1] * np.exp((r - 0.5 * sigma**2) * dt + sigma * np.sqrt(dt) * Z[:, i])

    if average_type == 'arithmetic':
        S_avg = np.mean(S_paths, axis=1)
    elif average_type == 'geometric':
        S_avg = np.exp(np.mean(np.log(S_paths), axis=1))
    else:
        raise ValueError("Invalid average_type: choose 'arithmetic' or 'geometric'")

    payoff = np.maximum(S_avg - K, 0)
    option_price = discount_factor * np.mean(payoff)
    return option_price


n_values = [300, 600]
m_values = [100, 10000]

for n in n_values:
    for m in m_values:
        arith_price = asian_option_mc(S0, K, T, r, sigma, n, m, 'arithmetic')
        geom_price = asian_option_mc(S0, K, T, r, sigma, n, m, 'geometric')
        print(f"n={n}, m={m} \t->  Aritmētiskā: {arith_price:.4f}, Ģeometriskā: {geom_price:.4f}")