%pip install -q validmind
Quickstart for knockout option pricing model documentation
Welcome! Let’s get you started with the basic process of documenting models with ValidMind.
A knockout option is a barrier option that ceases to exist if the underlying asset hits a predetermined price, known as the “barrier.” This barrier level, set above or below the current market price, determines whether the option will “knock out” before its expiration date. There are two types: “up-and-out” and “down-and-out.” In an up-and-out knockout option, the option expires if the asset price rises above the barrier, while in a down-and-out, it expires if the asset price falls below. Knockout options generally offer a lower premium than standard options since there is a chance they will expire worthless if the barrier is reached.
Pricing knockout options involves accounting for the proximity of the asset’s price to the barrier, as well as market volatility and the option’s time to expiration. High volatility and longer expiry increase the likelihood of the barrier being triggered, which reduces the option’s value. Models like modified Black-Scholes are used for simpler cases, while Monte Carlo simulations or binomial trees handle complex scenarios. Knockout options are useful for hedging or cost-effective investment strategies, allowing investors to save on premiums but with the risk of losing the option entirely if the barrier is hit.
You will learn how to initialize the ValidMind Library, develop a option pricing model, and then write custom tests that can be used for sensitivity and stress testing to quickly generate documentation about model.
Contents
About ValidMind
ValidMind is a suite of tools for managing model risk, including risk associated with AI and statistical models.
You use the ValidMind Library to automate documentation and validation tests, and then use the ValidMind Platform to collaborate on model documentation. Together, these products simplify model risk management, facilitate compliance with regulations and institutional standards, and enhance collaboration between yourself and model validators.
Before you begin
This notebook assumes you have basic familiarity with Python, including an understanding of how functions work. If you are new to Python, you can still run the notebook but we recommend further familiarizing yourself with the language.
If you encounter errors due to missing modules in your Python environment, install the modules with pip install
, and then re-run the notebook. For more help, refer to Installing Python Modules.
New to ValidMind?
If you haven’t already seen our Get started with the ValidMind Library, we recommend you explore the available resources for developers at some point. There, you can learn more about documenting models, find code samples, or read our developer reference.
Signing up is FREE — Register with ValidMind
Key concepts
Model documentation: A structured and detailed record pertaining to a model, encompassing key components such as its underlying assumptions, methodologies, data sources, inputs, performance metrics, evaluations, limitations, and intended uses. It serves to ensure transparency, adherence to regulatory requirements, and a clear understanding of potential risks associated with the model’s application.
Documentation template: Functions as a test suite and lays out the structure of model documentation, segmented into various sections and sub-sections. Documentation templates define the structure of your model documentation, specifying the tests that should be run, and how the results should be displayed.
Tests: A function contained in the ValidMind Library, designed to run a specific quantitative test on the dataset or model. Tests are the building blocks of ValidMind, used to evaluate and document models and datasets, and can be run individually or as part of a suite defined by your model documentation template.
Custom tests: Custom tests are functions that you define to evaluate your model or dataset. These functions can be registered via the ValidMind Library to be used with the ValidMind Platform.
Inputs: Objects to be evaluated and documented in the ValidMind Library. They can be any of the following:
- model: A single model that has been initialized in ValidMind with
vm.init_model()
. - dataset: Single dataset that has been initialized in ValidMind with
vm.init_dataset()
. - models: A list of ValidMind models - usually this is used when you want to compare multiple models in your custom test.
- datasets: A list of ValidMind datasets - usually this is used when you want to compare multiple datasets in your custom test. See this example for more information.
Parameters: Additional arguments that can be passed when running a ValidMind test, used to pass additional information to a test, customize its behavior, or provide additional context.
Outputs: Custom tests can return elements like tables or plots. Tables may be a list of dictionaries (each representing a row) or a pandas DataFrame. Plots may be matplotlib or plotly figures.
Test suites: Collections of tests designed to run together to automate and generate model documentation end-to-end for specific use-cases.
Example: the classifier_full_suite
test suite runs tests from the tabular_dataset
and classifier
test suites to fully document the data and model sections for binary classification model use-cases.
Install the ValidMind Library
To install the library:
Initialize the ValidMind Library
ValidMind generates a unique code snippet for each registered model to connect with your developer environment. You initialize the ValidMind Library with this code snippet, which ensures that your documentation and tests are uploaded to the correct model when you run the notebook.
Get your code snippet
In a browser, log in to ValidMind.
In the left sidebar, navigate to Model Inventory and click + Register Model.
Enter the model details and click Continue. (Need more help?)
For example, to register a model for use with this notebook, select:
- Documentation template:
Capital markets
You can fill in other options according to your preference.
- Documentation template:
Go to Getting Started and click Copy snippet to clipboard.
Next, load your model identifier credentials from an .env
file or replace the placeholder with your own code snippet:
# Load your model identifier credentials from an `.env` file
%load_ext dotenv
%dotenv .env
# Or replace with your code snippet
import validmind as vm
vm.init(# api_host="...",
# api_key="...",
# api_secret="...",
# model="...",
)
Initialize the Python environment
Next, let’s import the necessary libraries and set up your Python environment for data analysis:
%matplotlib inline
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import minimize
from validmind.tests import run_test
Preview the documentation template
A template predefines sections for your model documentation and provides a general outline to follow, making the documentation process much easier.
You will upload documentation and test results into this template later on. For now, take a look at the structure that the template provides with the vm.preview_template()
function from the ValidMind library and note the empty sections:
vm.preview_template()
Model development
class OptionPricing:
def __init__(self, S0, K, T, r):
self.S0 = S0
self.K = K
self.T = T
self.r = r
def monte_carlo_simulation(self, N, M):
raise NotImplementedError("Must be implemented by subclasses")
def price_option(self, N, M):
raise NotImplementedError("Must be implemented by subclasses")
class BlackScholesModel(OptionPricing):
def __init__(self, S0, K, T, r, sigma):
super().__init__(S0, K, T, r)
self.sigma = sigma
def monte_carlo_simulation(self, N, M):
= self.T / M
dt = np.zeros((N, M + 1))
price_paths 0] = self.S0
price_paths[:, for t in range(1, M + 1):
= np.random.standard_normal(N)
Z = price_paths[:, t - 1] * np.exp((self.r - 0.5 * self.sigma**2) * dt + self.sigma * np.sqrt(dt) * Z)
price_paths[:, t] return price_paths
def price_option(self, N, M):
= self.monte_carlo_simulation(N, M)
price_paths = np.maximum(price_paths[:, -1] - self.K, 0)
payoffs return np.exp(-self.r * self.T) * np.mean(payoffs)
def calibrate(self, market_prices, strikes, maturities):
def objective_function(params):
self.sigma = params[0]
for K, T in zip(strikes, maturities):
self.K = K
self.T = T
self.price_option(10000, 100))
model_prices.append(return np.sum((np.array(market_prices) - np.array(model_prices))**2)
= minimize(objective_function, [self.sigma], bounds=[(0.01, 1.0)])
result self.sigma = result.x[0]
class StochasticVolatilityModel(OptionPricing):
def __init__(self, S0, K, T, r, v0, kappa, theta, xi, rho):
super().__init__(S0, K, T, r)
self.v0 = v0
self.kappa = kappa
self.theta = theta
self.xi = xi
self.rho = rho
def monte_carlo_simulation(self, N, M):
= self.T / M
dt = np.zeros((N, M + 1))
price_paths = np.zeros((N, M + 1))
vol_paths 0] = self.S0
price_paths[:, 0] = self.v0
vol_paths[:, for t in range(1, M + 1):
= np.random.standard_normal(N)
Z1 = np.random.standard_normal(N)
Z2 = Z1
W1 = self.rho * Z1 + np.sqrt(1 - self.rho**2) * Z2
W2 = np.abs(vol_paths[:, t - 1] + self.kappa * (self.theta - vol_paths[:, t - 1]) * dt + self.xi * np.sqrt(vol_paths[:, t - 1] * dt) * W1)
vol_paths[:, t] = price_paths[:, t - 1] * np.exp((self.r - 0.5 * vol_paths[:, t - 1]) * dt + np.sqrt(vol_paths[:, t - 1] * dt) * W2)
price_paths[:, t] return price_paths
def price_option(self, N, M):
= self.monte_carlo_simulation(N, M)
price_paths = np.maximum(price_paths[:, -1] - self.K, 0)
payoffs return np.exp(-self.r * self.T) * np.mean(payoffs)
def calibrate(self, market_prices, strikes, maturities):
def objective_function(params):
self.v0, self.kappa, self.theta, self.xi, self.rho = params
= []
model_prices for K, T in zip(strikes, maturities):
self.K = K
self.T = T
self.price_option(10000, 100))
model_prices.append(
return np.sum((np.array(market_prices) - np.array(model_prices))**2)
= [self.v0, self.kappa, self.theta, self.xi, self.rho]
initial_guess = [(0.01, 1.0), (0.01, 5.0), (0.01, 1.0), (0.01, 1.0), (-1.0, 1.0)]
bounds = minimize(objective_function, initial_guess, bounds=bounds)
result self.v0, self.kappa, self.theta, self.xi, self.rho = result.x
class KnockoutOption:
def __init__(self, model, S0, K, T, r, barrier):
self.model = model
self.S0 = S0
self.K = K
self.T = T
self.r = r
self.barrier = barrier
def price_knockout_option(self, N, M):
= self.T / M
dt = np.zeros((N, M + 1))
price_paths = np.zeros((N, M + 1)) if isinstance(self.model, StochasticVolatilityModel) else None
vol_paths 0] = self.S0
price_paths[:, if vol_paths is not None:
0] = self.model.v0
vol_paths[:,
for t in range(1, M + 1):
= np.random.standard_normal(N)
Z1 if vol_paths is None:
# Black-Scholes Model
= price_paths[:, t - 1] * np.exp(
price_paths[:, t] self.r - 0.5 * self.model.sigma**2) * dt + self.model.sigma * np.sqrt(dt) * Z1
(
)else:
# Stochastic Volatility Model
= np.random.standard_normal(N)
Z2 = Z1
W1 = self.model.rho * Z1 + np.sqrt(1 - self.model.rho**2) * Z2
W2 = np.abs(vol_paths[:, t - 1] + self.model.kappa * (self.model.theta - vol_paths[:, t - 1]) * dt + self.model.xi * np.sqrt(vol_paths[:, t - 1] * dt) * W1)
vol_paths[:, t] = price_paths[:, t - 1] * np.exp(
price_paths[:, t] self.r - 0.5 * vol_paths[:, t - 1]) * dt + np.sqrt(vol_paths[:, t - 1] * dt) * W2
(
)
# Knockout condition
>= self.barrier] = 0
price_paths[:, t][price_paths[:, t] = np.maximum(price_paths[:, -1] - self.K, 0)
payoffs return np.exp(-self.r * self.T) * np.mean(payoffs)
Data Preparation
def generate_synthetic_market_data(model, strikes, maturities):
= []
market_prices = []
market_data for K, T in zip(strikes, maturities):
= K
model.K = T
model.T 10000, 100))
market_prices.append(model.price_option("strike": K, "option_price": model.price_option(10000, 100)})
market_data.append({return market_prices, market_data
# Parameters for synthetic data
= 100
S0 = 100
K = 1
T = 0.05
r # BlackSholes
= 0.2
true_sigma
# Stochastic Volatility
= 0.2
true_v0 = 2.0
true_kappa = 0.2
true_theta = 0.1
true_xi = -0.5
true_rho
# Synthetic data generation parameters
= list(np.linspace(75, 130, 25))
strikes = list(np.linspace(0.2, 3.0, 25))
maturities
# Generate synthetic market data using the true parameters
= BlackScholesModel(S0, K, T, r, true_sigma)
bs_model = generate_synthetic_market_data(bs_model, strikes, maturities)
bs_market_prices, bs_market_data
= StochasticVolatilityModel(S0, K, T, r, true_v0, true_kappa, true_theta, true_xi, true_rho)
sv_model = generate_synthetic_market_data(sv_model, strikes, maturities) sv_market_prices, sv_market_data
Initialize the ValidMind datasets
Before you can run tests, you must first initialize a ValidMind dataset object using the init_dataset
function from the ValidMind (vm
) module.
= pd.DataFrame(bs_market_data)
bs_market_data_df = vm.init_dataset(
vm_bs_market_data =bs_market_data_df,
dataset="sv_market_data",
input_id
)
= pd.DataFrame(sv_market_data)
sv_market_data_df = vm.init_dataset(
vm_sv_market_data =sv_market_data_df,
dataset="sv_market_data",
input_id )
Data Quality
Let’s check quality of the data using outliers and missing data tests.
Outliers detection using IQR method
Let’s visualizes the distribution of outliers in the option_price feature using the Interquartile Range (IQR) method.
= run_test(
result "validmind.data_validation.IQROutliersBarPlot:BlackScholes",
={
inputs"dataset": vm_bs_market_data,
},="Outliers detection using IQR method for BlackScholes",
title
) result.log()
= run_test(
result "validmind.data_validation.IQROutliersTable:BlackScholes",
={
inputs"dataset": vm_bs_market_data,
},="Outliers table using IQR method for BlackScholes",
title
) result.log()
= run_test(
result "validmind.data_validation.IQROutliersBarPlot:StochasticVolatility",
={
inputs"dataset": vm_sv_market_data,
},="Outliers detection using IQR method for StochasticVolatility",
title
) result.log()
= run_test(
result "validmind.data_validation.IQROutliersTable:StochasticVolatility",
={
inputs"dataset": vm_sv_market_data,
},="Outliers table using IQR method for StochasticVolatility",
title
) result.log()
Isolation Forest Outliers Test
Let’s detects anomalies in the dataset using the Isolation Forest algorithm, visualized through scatter plots.
= run_test(
result "validmind.data_validation.IsolationForestOutliers:BlackScholes",
={
inputs"dataset": vm_bs_market_data,
},="Outliers detection using Isolation Forest for BlackScholes",
title
) result.log()
= run_test(
result "validmind.data_validation.IsolationForestOutliers:StochasticVolatility",
={
inputs"dataset": vm_sv_market_data,
},="Outliers detection using Isolation Forest for StochasticVolatility",
title
) result.log()
Missing Values Test
Let’s evaluates dataset quality by ensuring the missing value ratio across all features does not exceed a set threshold.
= run_test(
result "validmind.data_validation.MissingValues:BlackScholes",
={
inputs"dataset": vm_bs_market_data,
},="Missing Values detection for BlackScholes",
title
) result.log()
= run_test(
result "validmind.data_validation.MissingValues:StochasticVolatility",
={
inputs"dataset": vm_sv_market_data,
},="MissingValues detection for StochasticVolatility",
title
) result.log()
### Model Calibration * Clearly state the purpose of the calibration process. For example, in the context of an option pricing model, calibration aims to adjust model parameters to fit market data (e.g., market option prices, volatility surfaces). * Specify whether the calibration is to historical data, current market data, or a blend of both.
import pandas as pd
@vm.test("my_custom_tests.SyntheticDataCalibrationTest")
def generate_synthetic_data_summary(option_pricing_model, strikes, maturities, synthetic_prices):
"""
This function will use synthetic prices to calibrate each model
and then generate derived prices based on the calibrated parameters.
It will output a DataFrame summarizing the strikes, maturities,
synthetic and derived prices, and the model parameters.
"""
= []
derived_prices for K, T in zip(strikes, maturities):
= K
option_pricing_model.K = T
option_pricing_model.T 10000, 100))
derived_prices.append(option_pricing_model.price_option(
= type(option_pricing_model).__name__
model_type = {
data "Strike": strikes,
"Maturity": maturities,
"Synthetic_Price": synthetic_prices,
"Derived_Price": derived_prices,
"Model_Type": model_type,
"S0": [option_pricing_model.S0] * len(strikes),
"K": [option_pricing_model.K] * len(strikes),
"T": [option_pricing_model.T] * len(strikes),
"r": [option_pricing_model.r] * len(strikes)
}
if model_type == "BlackScholesModel":
"sigma"] = [option_pricing_model.sigma] * len(strikes)
data[elif model_type == "StochasticVolatilityModel":
"v0"] = [option_pricing_model.v0] * len(strikes)
data["kappa"] = [option_pricing_model.kappa] * len(strikes)
data["theta"] = [option_pricing_model.theta] * len(strikes)
data["xi"] = [option_pricing_model.xi] * len(strikes)
data["rho"] = [option_pricing_model.rho] * len(strikes)
data[
= pd.DataFrame(data)
df return df
Synthetic Data Calibration Test
Let’s evaluates the accuracy of a stochastic volatility model by comparing synthetic prices with derived prices after model calibration.
= run_test(
result "my_custom_tests.SyntheticDataCalibrationTest",
={
params"option_pricing_model": sv_model,
"strikes": strikes,
"maturities": maturities,
"synthetic_prices": sv_market_prices
},
) result.log()
#### Benchmark Testing * Compare the model’s performance with alternative models or industry-standard models to assess its relative effectiveness. * Ensure that the model is competitive in pricing, accuracy, and computational efficiency.
@vm.test("my_custom_tests.BenchmarkTest")
def benchmark_test(bs_model, sv_model, strikes, maturities):
"""
Comparison between Black Scholes and stochastic volatility model
"""
= type(bs_model).__name__
bs_model_type = type(sv_model).__name__
sv_model_type
= []
bs_derived_prices = []
sv_derived_prices for K in strikes:
= K
bs_model.K 10000, 100))
bs_derived_prices.append(bs_model.price_option(= K
sv_model.K 10000, 100))
sv_derived_prices.append(sv_model.price_option(
= {
data "Strike": strikes,
"Maturities": [sv_model.T] * len(strikes),
"bs_model_price": bs_derived_prices,
"sv_model_price": sv_derived_prices,
}= pd.DataFrame(data)
df1
= []
bs_derived_prices = []
sv_derived_prices for T in maturities:
= T
bs_model.T 10000, 100))
bs_derived_prices.append(bs_model.price_option(= T
sv_model.T 10000, 100))
sv_derived_prices.append(sv_model.price_option(
= {
data "Strike": [sv_model.K] * len(maturities),
"Maturities": maturities,
"bs_model_price": bs_derived_prices,
"sv_model_price": sv_derived_prices,
}
= pd.DataFrame(data)
df2
return {"strikes variation benchmarking": df1}, {"maturities variation benchmarking": df2}
= run_test(
result "my_custom_tests.BenchmarkTest",
={
params"sv_model": sv_model,
"bs_model": bs_model,
"strikes": strikes,
"maturities": maturities,
},
) result.log()
Surface Volatility Test
Let’s calculates the implied volatility across different strikes and maturities based on market prices
import numpy as np
import pandas as pd
from scipy.optimize import minimize
import plotly.graph_objects as go
@vm.test("my_custom_tests.ImpliedVolSurface")
def implied_vol_surface(market_prices, strikes, maturities, S0, r, barrier, N=10000, M=100):
"""
This is a test to compute the implied volatility surface for a given set of market prices,
strikes, and maturities.
"""
def implied_volatility(market_price, N, M, initial_guess=0.2):
def objective_function(sigma):
= sigma
model.sigma = model.price_option(N, M)
model_price return (model_price - market_price) ** 2
= minimize(objective_function, initial_guess, bounds=[(0.01, 1.0)])
result return result.x[0]
= np.zeros((len(strikes), len(maturities)))
implied_vols
for i, K in enumerate(strikes):
for j, T in enumerate(maturities):
= market_prices[i]
market_price = BlackScholesModel(S0, K, T, r, sigma=0.2)
model
= implied_volatility(market_price, N, M)
implied_vol = implied_vol
implied_vols[i, j]
# Create the 3D surface plot
= np.meshgrid(strikes, maturities)
X, Y = implied_vols.T # Transpose to match the meshgrid orientation
Z
= go.Figure(data=[go.Surface(x=X, y=Y, z=Z)])
fig
# Update the layout
fig.update_layout(=f'3D Surface Plot of Implied Volatility',
title=dict(
scene='Strike',
xaxis_title='Maturity',
yaxis_title='Implied Volatility',
zaxis_title=dict(
camera=dict(x=0, y=0, z=1),
up=dict(x=0, y=0, z=0),
center=dict(x=1.5, y=1.5, z=1.5)
eye
)
),=900,
width=700,
height=dict(l=65, r=50, b=65, t=90)
margin
)
return fig
= run_test(
result "my_custom_tests.ImpliedVolSurface",
={
params"market_prices": sv_market_prices,
"strikes": strikes,
"maturities": maturities,
"S0": S0,
"r": r,
"barrier": 120
}
) result.log()
@vm.test("my_custom_tests.Sensitivity")
def sensitivity_test(model_type, S0, T, r, N, M, strike=None, barrier=None, sigma=None, v0=None, kappa=None,theta=None, xi=None, rho=None):
"""
This is sensitivity test
"""
if model_type == 'BS':
= BlackScholesModel(S0, strike, T, r, sigma)
model else:
= StochasticVolatilityModel(S0, strike, T, r, v0, kappa, theta, xi, rho)
model
= KnockoutOption(model, S0, strike, T, r, barrier)
knockout_option = knockout_option.price_knockout_option(N, M)
price
return pd.DataFrame({"Option price": [price]})
Initialise parameters
= (min(strikes), max(strikes))
strike_range = (100, 120) barrier_range
Common plot function
Let’s create a line plot using the default result output data and log it by passing the function through the post_process_fn
parameter in the run_test()
method.
from plotly_express import bar
from validmind.vm_models.figure import Figure
from validmind.vm_models.result import TestResult
import plotly.graph_objects as go
import random
def process_results(result: TestResult):
# Convert to DataFrame
= pd.DataFrame(result.tables[0].data)
df
# Get the first two column names
= df.columns[0]
x_col = df.columns[1]
y_col
# Create figure
= go.Figure()
fig
fig.add_trace(
go.Scatter(=df[x_col],
x=df[y_col],
y='lines',
mode=y_col # Use y-axis column name as trace name
name
)
)
fig.update_layout(=x_col,
xaxis_title=y_col,
yaxis_title=True,
showlegend="plotly_white"
template
)
result.add_figure(
Figure(=fig,
figure="sensitivity_plot_" + str(random.randint(0, 1000000)),
key=result.ref_id,
ref_id
)
)
return result
Strike sensitivity Test
Let’s evaluates the sensitivity of a model’s output value to changes in the strike price, while keeping other parameters constant. This test is crucial for understanding how variations in strike prices affect the valuation of financial derivatives, particularly options.
= run_test(
result "my_custom_tests.Sensitivity:S0",
={
param_grid"model_type": ['SV'],
"N": [N],
"M": [M],
"strike":[strike_range[0]],
"barrier": [barrier_range[0]],
"S0": list(np.linspace(S0-20, S0+20, 20)),
"T": [T],
"r": [r],
"v0": [0.2],
"kappa": [2],
"theta": [0.2],
"xi": [0.1],
"rho": [-0.5],
},= process_results
post_process_fn
) result.log()
= run_test(
result "my_custom_tests.Sensitivity:ToStrike",
={
param_grid"model_type": ['SV'],
"N": [N],
"M": [M],
"strike": list(np.linspace(strike_range[0], strike_range[1], 20)),
"barrier": [barrier_range[0]],
"S0": [S0],
"T": [T],
"r": [r],
"v0": [0.2],
"kappa": [2],
"theta": [0.2],
"xi": [0.1],
"rho": [-0.5],
},= process_results
post_process_fn
) result.log()
Barrier Sensitivity Test
Let’s evaluates the sensitivity of a model’s output to changes in the barrier level of a financial derivative, specifically a barrier option. This test is crucial for understanding how small changes in the barrier can impact the option’s valuation, which is essential for risk management and pricing strategies.
= run_test(
result "my_custom_tests.Sensitivity:ToBarrier",
={
param_grid"model_type": ['SV'],
"N": [N],
"M": [M],
"strike": [strike_range[0]],
"barrier": list(np.linspace(barrier_range[0], barrier_range[1], 20)),
"S0": [S0],
"T": [T],
"r": [r],
"v0": [0.2],
"kappa": [2],
"theta": [0.2],
"xi": [0.1],
"rho": [-0.5],
},=process_results
post_process_fn
) result.log()
#### Greeks These Greeks are crucial for traders and risk managers as they provide insights into the risk and potential price movements of options and derivatives, allowing for more informed decision-making and risk management strategies.
Delta
Let’s measures the sensitivity of the option’s price to a change in the price of the underlying asset. It indicates how much the price of an option is expected to move per $1 change in the underlying asset’s price.
@vm.test("my_custom_tests.GreeksDelta")
def calculate_delta(model_type, S0, T, r, N, M, strike=None, barrier=None,
=None, v0=None, kappa=None, theta=None, xi=None, rho=None,
sigma=0.001): # h is the step size for finite difference
h"""
Calculate delta using finite difference method.
Delta = (V(S0 + h) - V(S0 - h)) / (2h)
where V is the option price and h is a small increment
"""
# Initialize the model with S0 + h
if model_type == 'BS':
= BlackScholesModel(S0 + h, strike, T, r, sigma)
model_up = BlackScholesModel(S0 - h, strike, T, r, sigma)
model_down else:
= StochasticVolatilityModel(S0 + h, strike, T, r, v0, kappa, theta, xi, rho)
model_up = StochasticVolatilityModel(S0 - h, strike, T, r, v0, kappa, theta, xi, rho)
model_down
# Calculate option prices for up and down moves
= KnockoutOption(model_up, S0 + h, strike, T, r, barrier)
knockout_up = KnockoutOption(model_down, S0 - h, strike, T, r, barrier)
knockout_down
= knockout_up.price_knockout_option(N, M)
price_up = knockout_down.price_knockout_option(N, M)
price_down
# Calculate delta using central difference
= (price_up - price_down) / (2 * h)
delta = pd.DataFrame({"Delta": [delta], "Price_Up": [price_up], "Price_Down": [price_down], "h": [h]})
df return df
# To analyze delta sensitivity to underlying price changes
= run_test(
result "my_custom_tests.GreeksDelta",
={
param_grid"model_type": ['SV'],
"N": [1000000],
"M": [M],
"strike":[strike_range[0]],
"barrier": [barrier_range[0]],
"S0": list(np.linspace(S0-20, S0+20, 20)),
"T": [T],
"r": [r],
"v0": [0.2],
"kappa": [2],
"theta": [0.2],
"xi": [0.1],
"rho": [-0.5],
"h": [0.001]
},=process_results # Using the plotting function defined earlier
post_process_fn
) result.log()
Gamma
Let’s measures the rate of change of Delta with respect to changes in the underlying asset’s price. It indicates the curvature of the option’s price relative to the underlying asset’s price.
@vm.test("my_custom_tests.GreeksGamma")
def calculate_gamma(model_type, S0, T, r, N, M, strike=None, barrier=None,
=None, v0=None, kappa=None, theta=None, xi=None, rho=None,
sigma=0.01): # h is the step size for finite difference
h"""
Calculate gamma using finite difference method.
Gamma = (V(S0 + h) - 2V(S0) + V(S0 - h)) / h^2
where V is the option price and h is a small increment
"""
# Initialize the models with S0 + h, S0, and S0 - h
if model_type == 'BS':
= BlackScholesModel(S0 + h, strike, T, r, sigma)
model_up = BlackScholesModel(S0, strike, T, r, sigma)
model_center = BlackScholesModel(S0 - h, strike, T, r, sigma)
model_down else:
= StochasticVolatilityModel(S0 + h, strike, T, r, v0, kappa, theta, xi, rho)
model_up = StochasticVolatilityModel(S0, strike, T, r, v0, kappa, theta, xi, rho)
model_center = StochasticVolatilityModel(S0 - h, strike, T, r, v0, kappa, theta, xi, rho)
model_down
# Calculate option prices for up, center, and down moves
= KnockoutOption(model_up, S0 + h, strike, T, r, barrier)
knockout_up = KnockoutOption(model_center, S0, strike, T, r, barrier)
knockout_center = KnockoutOption(model_down, S0 - h, strike, T, r, barrier)
knockout_down
= knockout_up.price_knockout_option(N, M)
price_up = knockout_center.price_knockout_option(N, M)
price_center = knockout_down.price_knockout_option(N, M)
price_down
# Calculate gamma using second-order central difference
= (price_up - 2*price_center + price_down) / (h * h)
gamma
= pd.DataFrame({
df "Gamma": [gamma],
"Price_Up": [price_up],
"Price_Center": [price_center],
"Price_Down": [price_down],
"h": [h]
})return df
# To analyze gamma sensitivity to underlying price changes
= run_test(
result "my_custom_tests.GreeksGamma",
={
param_grid"model_type": ['SV'],
"N": [1000000],
"M": [M],
"strike":[strike_range[0]],
"barrier": [barrier_range[0]],
"S0": list(np.linspace(S0-20, S0+20, 20)),
"T": [T],
"r": [r],
"v0": [0.2],
"kappa": [2],
"theta": [0.2],
"xi": [0.1],
"rho": [-0.5],
"h": [0.1]
},=process_results # Using the plotting function defined earlier
post_process_fn
) result.log()
Theta
Let’s measures the sensitivity of the option’s price to the passage of time, also known as time decay. It indicates how much the price of an option is expected to decrease as the option approaches its expiration date.
@vm.test("my_custom_tests.GreeksTheta")
def calculate_theta(model_type, S0, T, r, N, M, strike=None, barrier=None,
=None, v0=None, kappa=None, theta=None, xi=None, rho=None,
sigma=1/365): # dt is typically one day
dt"""
Calculate theta using finite difference method.
Theta = (V(t + dt) - V(t)) / dt
where V is the option price and dt is a small time increment (typically 1 day)
"""
# Initialize the models with T and T + dt
if model_type == 'BS':
= BlackScholesModel(S0, strike, T, r, sigma)
model_current = BlackScholesModel(S0, strike, T + dt, r, sigma)
model_future else:
= StochasticVolatilityModel(S0, strike, T, r, v0, kappa, theta, xi, rho)
model_current = StochasticVolatilityModel(S0, strike, T + dt, r, v0, kappa, theta, xi, rho)
model_future
# Calculate option prices for current and future time
= KnockoutOption(model_current, S0, strike, T, r, barrier)
knockout_current = KnockoutOption(model_future, S0, strike, T + dt, r, barrier)
knockout_future
= knockout_current.price_knockout_option(N, M)
price_current = knockout_future.price_knockout_option(N, M)
price_future
# Calculate theta using forward difference
# Note: We divide by dt and multiply by -1 since theta represents the negative rate of change
= -1 * (price_future - price_current) / dt
theta_value
= pd.DataFrame({
df "Theta": [theta_value],
"Price_Current": [price_current],
"Price_Future": [price_future],
"dt": [dt]
})return df
# Example usage to analyze theta sensitivity across different underlying prices
= run_test(
result "my_custom_tests.GreeksTheta",
={
param_grid"model_type": ['SV'],
"N": [1000000],
"M": [M],
"strike":[strike_range[0]],
"barrier": [barrier_range[0]],
"S0": list(np.linspace(S0-20, S0+20, 20)),
"T": [T],
"r": [r],
"v0": [0.2],
"kappa": [2],
"theta": [0.2],
"xi": [0.1],
"rho": [-0.5],
"dt": [1/365] # One day time step
},=process_results # Using the plotting function defined earlier
post_process_fn
) result.log()
Vega
Let’s measures the sensitivity of the option’s price to changes in the volatility of the underlying asset. It indicates how much the price of an option is expected to change with a 1% change in the underlying asset’s volatility.
@vm.test("my_custom_tests.GreeksVega")
def calculate_vega(model_type, S0, T, r, N, M, strike=None, barrier=None,
=None, v0=None, kappa=None, theta=None, xi=None, rho=None,
sigma=0.001): # h is the step size for finite difference
h"""
Calculate vega using finite difference method.
For Black-Scholes: Vega = (V(σ + h) - V(σ - h)) / (2h)
For Stochastic Vol: Vega = (V(v0 + h) - V(v0 - h)) / (2h)
where V is the option price and h is a small increment in volatility
"""
if model_type == 'BS':
# For Black-Scholes, perturb sigma
= BlackScholesModel(S0, strike, T, r, sigma + h)
model_up = BlackScholesModel(S0, strike, T, r, sigma - h)
model_down else:
# For Stochastic Volatility, perturb v0
= StochasticVolatilityModel(S0, strike, T, r, v0 + h, kappa, theta, xi, rho)
model_up = StochasticVolatilityModel(S0, strike, T, r, v0 - h, kappa, theta, xi, rho)
model_down
# Calculate option prices for up and down moves in volatility
= KnockoutOption(model_up, S0, strike, T, r, barrier)
knockout_up = KnockoutOption(model_down, S0, strike, T, r, barrier)
knockout_down
= knockout_up.price_knockout_option(N, M)
price_up = knockout_down.price_knockout_option(N, M)
price_down
# Calculate vega using central difference
= (price_up - price_down) / (2 * h)
vega
= pd.DataFrame({
df "Vega": [vega],
"Price_Up": [price_up],
"Price_Down": [price_down],
"h": [h]
})return df
# Example usage to analyze vega sensitivity across different underlying prices
= run_test(
result "my_custom_tests.GreeksVega",
={
param_grid"model_type": ['SV'],
"N": [1000000],
"M": [M],
"strike":[strike_range[0]],
"barrier": [barrier_range[0]],
"S0": list(np.linspace(S0-20, S0+20, 20)),
"T": [T],
"r": [r],
"v0": [0.2],
"kappa": [2],
"theta": [0.2],
"xi": [0.1],
"rho": [-0.5],
"h": [0.0001] # Small step size for better accuracy
},=process_results # Using the plotting function defined earlier
post_process_fn
) result.log()
Rho
Let’s measures the sensitivity of the option’s price to changes in the interest rate. It indicates how much the price of an option is expected to change with a 1% change in interest rates.
@vm.test("my_custom_tests.GreeksRho")
def calculate_rho(model_type, S0, T, r, N, M, strike=None, barrier=None,
=None, v0=None, kappa=None, theta=None, xi=None, rho=None,
sigma=0.0001): # h is the step size for finite difference
h"""
Calculate rho using finite difference method.
Rho = (V(r + h) - V(r - h)) / (2h)
where V is the option price and h is a small increment in interest rate
"""
# Initialize the models with r + h and r - h
if model_type == 'BS':
= BlackScholesModel(S0, strike, T, r + h, sigma)
model_up = BlackScholesModel(S0, strike, T, r - h, sigma)
model_down else:
= StochasticVolatilityModel(S0, strike, T, r + h, v0, kappa, theta, xi, rho)
model_up = StochasticVolatilityModel(S0, strike, T, r - h, v0, kappa, theta, xi, rho)
model_down
# Calculate option prices for up and down moves in interest rate
= KnockoutOption(model_up, S0, strike, T, r + h, barrier)
knockout_up = KnockoutOption(model_down, S0, strike, T, r - h, barrier)
knockout_down
= knockout_up.price_knockout_option(N, M)
price_up = knockout_down.price_knockout_option(N, M)
price_down
# Calculate rho using central difference
= (price_up - price_down) / (2 * h)
rho_value
= pd.DataFrame({
df "Rho": [rho_value],
"Price_Up": [price_up],
"Price_Down": [price_down],
"h": [h]
})return df
# Example usage to analyze rho sensitivity across different underlying prices
= run_test(
result "my_custom_tests.GreeksRho",
={
param_grid"model_type": ['SV'],
"N": [1000000],
"M": [M],
"strike":[strike_range[0]],
"barrier": [barrier_range[0]],
"S0": list(np.linspace(S0-20, S0+20, 20)),
"T": [T],
"r": [r],
"v0": [0.2],
"kappa": [2],
"theta": [0.2],
"xi": [0.1],
"rho": [-0.5],
"h": [0.0001] # Small step size for better accuracy
},=process_results # Using the plotting function defined earlier
post_process_fn
) result.log()
@vm.test("my_custom_tests.Stressing")
def sensitivity_test(model_type, S0, T, r, N, M, strike=None, barrier=None, sigma=None, v0=None, kappa=None,theta=None, xi=None, rho=None):
"""
This is stress test
"""
if model_type == 'BS':
= BlackScholesModel(S0, strike, T, r, sigma)
model else:
= StochasticVolatilityModel(S0, strike, T, r, v0, kappa, theta, xi, rho)
model
= KnockoutOption(model, S0, strike, T, r, barrier)
knockout_option = knockout_option.price_knockout_option(N, M)
price
return pd.DataFrame({"Option price": [price]})
Rho (correlation) and Theta (long term vol) stress test
First, we create a surface plot to visualize the option price with respect to two variables.
def two_parameters_stress_surface_plot(result: TestResult):
import plotly.graph_objects as go
import numpy as np
import pandas as pd
# Convert to DataFrame
= pd.DataFrame(result.tables[0].data)
data
# Get column names (assuming first column is x, next two are y1 and y2)
= data.columns[2]
z_col = data.columns[0]
x_col = data.columns[1]
y_col
# Get unique values for x and y
= np.sort(data[x_col].unique())
x_unique = np.sort(data[y_col].unique())
y_unique
# Create meshgrid
= np.meshgrid(x_unique, y_unique)
X, Y
# Create Z matrix
= np.zeros_like(X)
Z for i, x_val in enumerate(x_unique):
for j, y_val in enumerate(y_unique):
= (data[x_col] == x_val) & (data[y_col] == y_val)
mask if mask.any():
= data.loc[mask, z_col].iloc[0]
Z[j, i]
# Create the 3D surface plot
= go.Figure(data=[go.Surface(x=X, y=Y, z=Z)])
fig
# Update the layout
fig.update_layout(=f'3D Surface Plot of {z_col}',
title=dict(
scene=x_col,
xaxis_title=y_col,
yaxis_title=z_col,
zaxis_title=dict(
camera=dict(x=0, y=0, z=1),
up=dict(x=0, y=0, z=0),
center=dict(x=1.5, y=1.5, z=1.5)
eye
)
),=900,
width=700,
height=dict(l=65, r=50, b=65, t=90)
margin
)
result.add_figure(
Figure(=fig,
figure="sensitivity_plot_" + str(random.randint(0, 1000000)),
key=result.ref_id,
ref_id
)
)
return result
Let’s evaluates the sensitivity of a model’s output to changes in the correlation parameter (rho) and the long-term variance parameter (theta) within a stochastic volatility framework.
This test is useful for understanding how variations in these parameters affect the model’s valuation, which is crucial for risk management and model validation.
= run_test(
result "my_custom_tests.Stressing:TheRhoAndThetaParameters",
={
param_grid"model_type": ['SV'],
"N": [N],
"M": [M],
"strike": [strike_range[0]],
"barrier": [barrier_range[0]],
"S0": [S0],
"T": [T],
"r": [r],
"v0": [0.2],
"kappa": [2],
"theta": list(np.linspace(0,0.8, 10)),
"xi": [0.1],
"rho": list(np.linspace(-1,0.8, 10)),
},=two_parameters_stress_surface_plot
post_process_fn
) result.log()
Rho (correlation) and Xi (vol of vol) stress test
= run_test(
result "my_custom_tests.Stressing:TheRhoAndXiParameters",
={
param_grid"model_type": ['SV'],
"N": [N],
"M": [M],
"strike": [strike_range[0]],
"barrier": [barrier_range[0]],
"S0": [S0],
"T": [T],
"r": [r],
"v0": [0.2],
"kappa": [2],
"theta": [0.2],
"xi": list(np.linspace(0,0.8, 10)),
"rho": list(np.linspace(-1,0.8, 10)),
},=two_parameters_stress_surface_plot
post_process_fn
) result.log()
Sigma stress test
evaluates the sensitivity of a model’s output to changes in the volatility parameter, sigma. This test is crucial for understanding how variations in market volatility impact the model’s valuation of financial instruments, particularly options.
This test is useful for risk management and model validation, as it helps identify the robustness of the model under different market conditions. By analyzing the changes in the model’s output as sigma varies, stakeholders can assess the model’s stability and reliability.
= run_test(
result "my_custom_tests.Stressing:TheSigmaParameter",
={
param_grid"model_type": ['BS'],
"N": [N],
"M": [M],
"strike": [strike_range[0]],
"barrier": [barrier_range[0]],
"S0": [S0],
"T": [T],
"r": [r],
"sigma": list(np.linspace(0.2, 0.8, 10)),
},=process_results
post_process_fn
) result.log()
Stress kappa
Let’s evaluates the sensitivity of a model’s output to changes in the kappa parameter, which is a mean reversion rate in stochastic volatility models.
= run_test(
result "my_custom_tests.Stressing:TheKappaParameter",
={
param_grid"model_type": ['SV'],
"N": [N],
"M": [M],
"strike": [strike_range[0]],
"barrier": [barrier_range[0]],
"S0": [S0],
"T": [T],
"r": [r],
"v0": [0.2],
"kappa": list(np.linspace(0, 8, 10)),
"theta": [0.2],
"xi": [0.1],
"rho": [-0.5],
},=process_results
post_process_fn
) result.log()
Stress theta
Stress Theta evaluates the sensitivity of a model’s output to changes in the parameter theta, which represents the long-term variance in a stochastic volatility model
= run_test(
result "my_custom_tests.Stressing:TheThetaParameter",
={
param_grid"model_type": ['SV'],
"N": [N],
"M": [M],
"strike": [strike_range[0]],
"barrier": [barrier_range[0]],
"S0": [S0],
"T": [T],
"r": [r],
"v0": [0.2],
"kappa": [2],
"theta": list(np.linspace(0, 0.8, 10)),
"xi": [0.1],
"rho": [-0.5],
},=process_results
post_process_fn
) result.log()
Stress xi
Stress Xi evaluates the sensitivity of a model’s output to changes in the parameter xi, which represents the volatility of volatility in a stochastic volatility model. This test is crucial for understanding how variations in xi impact the model’s valuation, particularly in financial derivatives pricing.
= run_test(
result "my_custom_tests.Stressing:TheXiParameter",
={
param_grid"model_type": ['SV'],
"N": [N],
"M": [M],
"strike": [strike_range[0]],
"barrier": [barrier_range[0]],
"S0": [S0],
"T": [T],
"r": [r],
"v0": [0.2],
"kappa": [2],
"theta": [0.2],
"xi": list(np.linspace(0.05, 0.95, 10)),
"rho": [-0.5],
},=process_results
post_process_fn
) result.log()
Stress rho
Stress rho test evaluates the sensitivity of a model’s output to changes in the correlation parameter, rho, within a stochastic volatility (SV) model framework. This test is crucial for understanding how variations in rho, which represents the correlation between the asset price and its volatility, impact the model’s valuation output.
= run_test(
result "my_custom_tests.Stressing:TheRhoParameter",
={
param_grid"model_type": ['SV'],
"N": [N],
"M": [M],
"strike": [strike_range[0]],
"barrier": [barrier_range[0]],
"S0": [S0],
"T": [T],
"r": [r],
"v0": [0.2],
"kappa": [2],
"theta": [0.2],
"xi": [0.1],
"rho": list(np.linspace(-1.0, 1.0, 20)),
},=process_results
post_process_fn
) result.log()
Next steps
You can look at the results of this test suite right in the notebook where you ran the code, as you would expect. But there is a better way — use the ValidMind Platform to work with your model documentation.
Work with your model documentation
From the Model Inventory in the ValidMind Platform, go to the model you registered earlier. (Need more help?)
Click and expand the Model Development section.
What you see is the full draft of your model documentation in a more easily consumable version. From here, you can make qualitative edits to model documentation, view guidelines, collaborate with validators, and submit your model documentation for approval when it’s ready. Learn more …
Discover more learning resources
We offer many interactive notebooks to help you document models:
Or, visit our documentation to learn more about ValidMind.