Skip to main content

Volatility Class Architecture

The Volatility class architecture is designed for modeling the volatility of historical price series using various methods, such as GARCH or standard deviation. The master class Volatility is an abstract class, and specific volatility modeling techniques are implemented in its subclasses.

Volatility (Abstract Class)

Overview

Volatility is an abstract base class that defines a standard interface for volatility calculations. Classes that extend Volatility must implement the calculate method.

calculate Method

The calculate method is an abstract method that must be implemented by subclasses. It is expected to return a tuple of two numpy arrays, one for volatility of returns and the other for volatility of prices.

from abc import ABC, abstractmethod
import numpy as np
from typing import Tuple

class Volatility(ABC):
@abstractmethod
def calculate(self) -> Tuple[np.ndarray, np.ndarray]:
raise NotImplementedError('"Volatility" is an abstract class')

StandardDeviation (Subclass of Volatility)

Overview

StandardDeviation is a subclass of Volatility that implements the volatility calculation using the standard deviation method.

Constructor

The constructor takes several parameters including prices, steps, and configuration settings to initialize the object.

calculate Method Implementation

This method implements the core logic for calculating the volatility based on standard deviation. It computes the matrix of returns and prices, and optionally applies rescaling based on the configured volatility range.

import numpy as np
from typing import Tuple

class StandardDeviation(Volatility):
# Constructor definition...

def calculate(self) -> Tuple[np.ndarray, np.ndarray]:
# Implementation...

Creating Your Own Volatility Modeler

To create your own custom volatility modeler, extend the Volatility class and implement the calculate method.

  1. Define the Subclass: Create a new class that inherits from Volatility.
  2. Implement the calculate Method: This method should contain the logic for your volatility calculation and return a tuple of numpy arrays.
  3. Initialize Your Class: Add any initialization parameters your model requires.

Example Template

import numpy as np
from typing import Tuple

class CustomVolatility(Volatility):
def __init__(self, _custom_parameters):
# Initialization code

def calculate(self) -> Tuple[np.ndarray, np.ndarray]:
# Custom calculation logic
return matrix_of_returns, matrix_of_prices

Use this template as a starting point for creating your own volatility modeling techniques.