quantdata.utils.backbone#

Abstract Numerical Backbone#

Consistent wrapper around numpy and torch, and in the future jax for being able to write unified model code where sensible (e.g. outside jax.scan loops).

The backbone also has a black scholes function and a verify function.

class quantdata.utils.backbone.Backbone[source]#

Bases: object

Generalized compute backbone: numpy, torch, (soon) jax.

class Array[source]#

Bases: object

The array type

static are_broadcastable(*shapes)[source]#

Check if shapes are broadcastable according to numpy broadcasting rules.

BS_EPS = 1e-10#
EPS_VOL = 1e-10#
bs_price(strikes, sqrtVars, *, mask=None, spot=None, logK=None, high_precision=False, debug_verify_bounds=False, eps=1e-06)[source]#

Black-Scholes price of call options with given spot, strikes and sqrtVars.

Parameters:
  • strikes (Array) – Strike prices for each option.

  • sqrtVars (Array) – Square root of variance for each option.

  • mask (Array | None) – Optional boolean mask indicating which options are valid. If provided, the function will return 0 for invalid options and compute prices only for valid options. This can be used to handle cases where some options are not traded or not relevant without affecting the computed prices of valid options.

  • spot (Array | None) – Spot prices, or None for 1.

  • high_precision (bool) – If True, perform calculations in higher precision (float64) to improve accuracy for deep OTM options. The output will be cast back to the input dtype.

  • debug_verify_bounds (bool) – If True, perform additional checks to verify that the computed option prices are within theoretical bounds (intrinsic value and spot price).

  • eps (float) – Tolerance for numerical checks when debug_verify_bounds is True.

Returns:

prices – The Black-Scholes price of the call options.

Return type:

Array

quantdata.utils.backbone.sigmoid(x)[source]#

Numpy Sigmoid function 1/(1+exp(-x))

quantdata.utils.backbone.relu(x)[source]#

Numpy ReLU function max(0,x)

quantdata.utils.backbone.are_broadcastable(*shapes)[source]#

Check if shapes or arrays are broadcastable according to torch/numpy broadcasting rules.

class quantdata.utils.backbone.BackboneNumpy[source]#

Bases: Backbone

Numpy compute backbone

to_array(data, dtype=None)[source]#

Create an array; do not copy if data is already an array of the correct dtype

to_numpy(arr)[source]#
norm_cdf(arr)[source]#
verify(condition, message, exception=<class 'RuntimeError'>)[source]#

Verify a condition, raise an error with message if not satisfied.

stop_gradient(x)[source]#

Stop gradient calculation. Does NOT make a copy

sigmoid(x)[source]#

Sigmoid function 1/(1+exp(-x))

relu(x)[source]#

ReLU function max(0,x)

are_broadcastable(*shapes)[source]#

Check if shapes or arrays are broadcastable according to torch/numpy broadcasting rules.

stack(arrays, axis=0, dtype=None)[source]#

Stack arrays along a new axis. If dtype is provided, cast to dtype after stacking.