Apakaļ pie Kurss

Monte Carlo Methods in Finance Engineering

0% Pabeigts
0/0 Soļi
  1. Course Introduction

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

Run the code to see how different methods of the variance reduction works

import numpy as np
from math import log, sqrt, exp, erf

# ---- parameters ----
S0 = 50.0
K  = 50.0
T  = 2.0
sigma = 0.3
r = 0.05
option = "call"
# --------------------

def norm_cdf(x):
    return 0.5 * (1.0 + erf(x / sqrt(2.0)))

def bs_call(S0, K, r, sigma, T):
    d1 = (log(S0 / K) + (r + 0.5 * sigma**2) * T) / (sigma * sqrt(T))
    d2 = d1 - sigma * sqrt(T)
    return S0 * norm_cdf(d1) - K * exp(-r*T) * norm_cdf(d2)

def payoff_call(ST, K):
    return np.maximum(ST - K, 0.0)

def mc_plain(n=200000, seed=0):
    rng = np.random.default_rng(seed)
    Z = rng.standard_normal(n)
    ST = S0 * np.exp((r - 0.5*sigma**2)*T + sigma*np.sqrt(T)*Z)
    Y = np.exp(-r*T) * payoff_call(ST, K)
    return Y.mean(), Y.std(ddof=1)/np.sqrt(n)

print("BS reference:", bs_call(S0, K, r, sigma, T))
print("MC plain:", mc_plain())

def mc_antithetic(n=200000, seed=0):
    n2 = n // 2
    rng = np.random.default_rng(seed)
    Z = rng.standard_normal(n2)

    drift = (r - 0.5*sigma**2)*T
    vol = sigma*np.sqrt(T)

    ST1 = S0 * np.exp(drift + vol*Z)
    ST2 = S0 * np.exp(drift - vol*Z)  # uses -Z

    disc = np.exp(-r*T)
    Y = 0.5 * (disc*payoff_call(ST1, K) + disc*payoff_call(ST2, K))
    return Y.mean(), Y.std(ddof=1)/np.sqrt(n2)

print("MC antithetic:", mc_antithetic())

def mc_control_variate_ST(n=200000, seed=0):
    rng = np.random.default_rng(seed)
    Z = rng.standard_normal(n)

    ST = S0 * np.exp((r - 0.5*sigma**2)*T + sigma*np.sqrt(T)*Z)
    disc = np.exp(-r*T)

    Y = disc * payoff_call(ST, K)   # target payoff
    X = disc * ST                   # control variate
    EX = S0                         # known expectation

    Xc = X - X.mean()
    Yc = Y - Y.mean()
    b_hat = (Xc @ Yc) / (Xc @ Xc)   # regression slope

    Ycv = Y - b_hat * (X - EX)
    return Ycv.mean(), Ycv.std(ddof=1)/np.sqrt(n), b_hat

print("MC control variate:", mc_control_variate_ST())

def mc_moment_matching(n=200000, seed=0):
    rng = np.random.default_rng(seed)
    Z = rng.standard_normal(n)
    Z = (Z - Z.mean()) / Z.std(ddof=1)  # match N(0,1) moments

    ST = S0 * np.exp((r - 0.5*sigma**2)*T + sigma*np.sqrt(T)*Z)
    Y = np.exp(-r*T) * payoff_call(ST, K)
    return Y.mean(), Y.std(ddof=1)/np.sqrt(n)

print("MC moment matching:", mc_moment_matching())


def mc_importance_sampling_shift(n=200000, mu=1.5, seed=0):
    rng = np.random.default_rng(seed)
    Zp = rng.standard_normal(n) + mu  # Z' ~ N(mu, 1)

    ST = S0 * np.exp((r - 0.5*sigma**2)*T + sigma*np.sqrt(T)*Zp)
    Y = np.exp(-r*T) * payoff_call(ST, K)

    # likelihood ratio for shifting N(0,1) -> N(mu,1)
    w = np.exp(-mu*Zp + 0.5*mu**2)

    Yis = Y * w
    return Yis.mean(), Yis.std(ddof=1)/np.sqrt(n)

print("MC importance sampling:", mc_importance_sampling_shift(mu=1.5))