tensorquant.markethandles package

Submodules

tensorquant.markethandles.bootstrapping module

class tensorquant.markethandles.bootstrapping.CurveBootstrap(evaluation_date: date, daycount_convention: DayCounterConvention, market_env: MarketEnvironment)[source]

Bases: object

Class to bootstrap interest rate curves from market quotes and instrument generators.

evaluation_date

The date of curve evaluation.

Type:

datetime.date

day_counter

The day count convention used for the curve.

Type:

DayCounter

market_env

The market environment used for pricing during bootstrap.

Type:

MarketEnvironment

eur_generator_map

A map of instrument generators for EUR instruments.

Type:

dict

strip(generators: list[str], maturities: list[str], quotes: list[float], curve_name: str, currency: Currency, interpolation: str = 'LINEAR', is_spread_curve: bool = False, daycounter_convention=DayCounterConvention.ActualActual)[source]

Bootstraps an interest rate curve from market quotes and instrument generators.

The bootstrapped curve is registered in the market environment under curve_name so that subsequent strip() calls can reference it as a discount / forward curve.

Parameters:
  • generators (list[str]) – A list of instrument generators (e.g., “Dp” for deposit, “Os” for OIS).

  • maturities (list[str]) – A list of instrument maturities (e.g., “1M”, “6M”, “1Y”).

  • quotes (list[float]) – A list of market quotes corresponding to the instruments.

  • curve_name (str) – The market key for the bootstrapped curve (e.g., “IR:EUR:ESTR:SPOT”).

  • currency (Currency) – The currency of the curve (e.g., EUR).

  • interpolation (str, optional) – The type of interpolation (default is “LINEAR”).

  • is_spread_curve (bool, optional) – Whether the curve is a spread curve (default is False).

  • daycounter_convention (DayCounterConvention, optional) – The day count convention for the curve (default is ActualActual).

Returns:

The bootstrapped interest rate curve.

Return type:

RateCurve

Raises:

KeyError – If the generator key does not exist in the generator map.

class tensorquant.markethandles.bootstrapping.ObjectiveFunction(rate_curve: RateCurve, products: list[Product], pricers: list[Pricer], market_env: MarketEnvironment)[source]

Bases: object

Global objective function for curve bootstrapping.

Each call prices all N instruments at the current rate vector and returns the NPV vector together with the NxN Jacobian computed via TensorFlow autodiff (GradientTape). None gradients (rates not used by a given instrument) are treated as zero.

rate_curve

The rate curve being bootstrapped.

Type:

RateCurve

products

A list of products to price during the bootstrap.

Type:

list[Product]

pricers

A list of pricers for the given products.

Type:

list[Pricer]

market_env

The market environment used for pricing.

Type:

MarketEnvironment

__call__(x: ndarray) tuple[ndarray, ndarray][source]

Price all instruments at rates x and return NPVs and their Jacobian.

Parameters:

x (numpy.ndarray) – Current rate vector (one entry per pillar).

Returns:

  • res: NPV vector of shape (N,).

  • jac: Jacobian matrix of shape (N, N) where jac[i, j] = dNPV_i / dr_j. Entries corresponding to unused rates (None autodiff gradients) are set to zero.

Return type:

tuple[numpy.ndarray, numpy.ndarray]

tensorquant.markethandles.interestrate module

class tensorquant.markethandles.interestrate.InterestRate(r: float, daycounter: DayCounter, compounding: CompoundingType, frequency: Frequency)[source]

Bases: object

Represents an interest rate with associated day count convention, compounding type, and frequency.

_r

The interest rate.

Type:

float

_daycounter

Day count convention for year fraction calculations.

Type:

DayCounter

_compounding

The compounding type (e.g., Simple, Compounded, Continuous).

Type:

CompoundingType

_frequency

Frequency of compounding per year.

Type:

int

__str__()[source]

Returns a string representation of the InterestRate object, including the rate and compounding type.

Returns:

A string representing the interest rate, its compounding type, and frequency (if applicable).

Return type:

str

Raises:

ValueError – If the compounding type is unknown.

compound_factor(*args)[source]

Calculates the compound factor over a given time period or between two dates.

Parameters:

*args – A single argument (t, float) representing the time in years, or four arguments (d1, d2, refStart, refEnd) where d1 and d2 are the start and end dates, and refStart and refEnd are optional reference dates.

Returns:

The compound factor.

Return type:

float

Raises:

ValueError – If the number of arguments is invalid or if negative time is provided.

property compounding: CompoundingType

Returns the compounding type.

Returns:

The compounding type (e.g., Simple, Compounded, Continuous).

Return type:

CompoundingType

property daycounter: DayCounter

Returns the day counter used.

Returns:

The day counter.

Return type:

DayCounter

discount_factor(t: float) float[source]

Calculates the discount factor for a given time period.

Parameters:

t (float) – The time period in years.

Returns:

The discount factor.

Return type:

float

property frequency: int

Returns the frequency of compounding.

Returns:

The frequency of compounding per year.

Return type:

int

static implied_rate(compound, daycounter, comp, freq, t)[source]

Calculates the implied interest rate from a given compound factor.

Parameters:
  • compound (float) – The compound factor.

  • daycounter (DayCounter) – The day count convention for time calculations.

  • comp (CompoundingType) – The compounding type (e.g., Simple, Compounded, Continuous).

  • freq (Frequency) – The compounding frequency per year.

  • t (float) – The time period in years.

Returns:

The implied InterestRate object.

Return type:

InterestRate

Raises:

ValueError – If the compound factor is non-positive, or if an invalid time period is provided.

property rate: float

Returns the interest rate.

Returns:

The interest rate.

Return type:

float

tensorquant.markethandles.ircurve module

class tensorquant.markethandles.ircurve.DefaultCurve(reference_date: date, pillars: list[date] | list[float], survival_probs: list[float], daycounter_convention: DayCounterConvention)[source]

Bases: object

Survival-probability / default-probability curve with piecewise-constant hazard rates.

Given a set of pillars and corresponding survival probabilities Q(t_i), the hazard rate over each interval [t_{i-1}, t_i] is kept constant:

h_i = -ln(Q(t_i) / Q(t_{i-1})) / (t_i - t_{i-1})

This allows cheap computation of Q(t) for any t via

Q(t) = Q(t_{i-1}) * exp(-h_i * (t - t_{i-1}))

A convenience constructor from_flat_hazard_rate() builds a flat (single-segment) curve from a constant hazard rate λ, i.e. Q(t) = exp(-λ·t).

classmethod from_flat_hazard_rate(reference_date: date, hazard_rate: float, daycounter_convention: DayCounterConvention, horizon: float = 100.0)[source]

Creates a flat DefaultCurve from a constant hazard rate λ.

Q(t) = exp(-λ·t).

Parameters:
  • reference_date (date) – Curve reference date.

  • hazard_rate (float) – Constant hazard rate λ (e.g. 0.03 for 3 %).

  • daycounter_convention (DayCounterConvention) – Day count convention.

  • horizon (float) – Far pillar in years (default 100). Determines the range over which the flat rate is valid.

Returns:

A flat default curve.

Return type:

DefaultCurve

property hazard_rates: list[float]

Piecewise-constant hazard rates for each interval between pillars.

marginal_pd(t1: date | float, t2: date | float) float[source]

Returns the marginal (conditional) probability of default in (t1, t2].

PD(t1, t2) = Q(t1) - Q(t2).

Parameters:
  • t1 (Union[date, float]) – Start of the period.

  • t2 (Union[date, float]) – End of the period.

Returns:

Probability of default in the interval (t1, t2].

Return type:

float

property pillars: list[float]

Year-fraction pillars (including the t = 0 anchor).

property reference_date: date

Reference date of the curve.

survival_prob(t: date | float) float[source]

Returns the survival probability Q(0, t).

Parameters:

t (Union[date, float]) – Evaluation time (date or year fraction).

Returns:

Survival probability Q(0, t).

Return type:

float

property survival_probs: list[float]

Survival probabilities at each pillar.

class tensorquant.markethandles.ircurve.FlatCurve(reference_date: date, rate: float, daycounter_convention: DayCounterConvention)[source]

Bases: RateCurve

A flat (constant-rate) curve implementation.

This curve assumes a single constant continuously-compounded rate for all maturities. It is implemented as a RateCurve with two very distant pillars carrying the same rate, so interpolation naturally keeps the curve flat.

property rate: float

Returns the flat rate of the curve.

class tensorquant.markethandles.ircurve.RateCurve(reference_date: date, pillars: list[date] | list[float], rates: list[float], interp: str, daycounter_convention: DayCounterConvention)[source]

Bases: object

Represents a financial rate curve used for discounting, zero rates, or forward rates.

_reference_date

The starting date of the curve.

Type:

date

_daycounter_convention

Convention for day counting.

Type:

DayCounterConvention

_daycounter

Day counter instance based on the convention.

Type:

DayCounter

_dates

Dates that represent the curve’s pillars.

Type:

list[date]

_pillars

Year fractions corresponding to the pillar dates.

Type:

list[float]

_pillar_days

Day counts between the reference date and the pillars.

Type:

list[int]

__rates

Interest rates associated with the pillars.

Type:

list[float]

_rates

Interest rates stored as TensorFlow variables.

Type:

list[Tensor]

interpolation_type

Type of interpolation used (e.g., ‘LINEAR’).

Type:

str

interp

Interpolation object used for rate calculations.

Type:

LinearInterp

_jacobian

Jacobian matrix of the curve, if applicable.

Type:

numpy.ndarray

property dates

Returns the pillar dates of the curve.

Returns:

A list of dates representing the curve’s pillars.

Return type:

list[date]

property daycounter

Returns the day counter used by the curve.

Returns:

The day counter object.

Return type:

DayCounter

property daycounter_convention

Returns the day count convention used by the curve.

Returns:

The day count convention.

Return type:

DayCounterConvention

discount(term: date | float) float[source]

Calculates the discount factor for a given term.

Parameters:

term (Union[date, float]) – The term for which to calculate the discount factor. Can be a date or year fraction.

Returns:

The discount factor.

Return type:

float

Raises:

TypeError – If the term is not a date or float.

forward_rate(d1: date | float, d2: date | float) float[source]

Calculates the forward rate between two dates or year fractions.

Parameters:
  • d1 (Union[date, float]) – The start of the period.

  • d2 (Union[date, float]) – The end of the period.

Returns:

The forward rate.

Return type:

float

Raises:

TypeError – If d1 and d2 are not both dates or both floats.

classmethod from_zcb(reference_date: date, pillars: list[date] | list[int], discount_factors: list[float], interp: str, daycounter_convention: DayCounterConvention)[source]

Creates a RateCurve instance from zero-coupon bond discount factors.

Parameters:
  • reference_date (date) – The reference date for the curve.

  • pillars (Union[list[date], list[int]]) – A list of dates or year fractions for the curve.

  • discount_factors (list[float]) – A list of discount factors (e.g., from zero-coupon bonds).

  • interp (str) – Interpolation method to use (e.g., ‘LINEAR’).

  • daycounter_convention (DayCounterConvention) – Day count convention for time calculations.

Returns:

A new RateCurve instance.

Return type:

RateCurve

inst_fwd(t: float)[source]

Calculates the instantaneous forward rate at a specific time.

Parameters:

t (float) – The time (in year fractions) to calculate the instantaneous forward rate.

Returns:

The instantaneous forward rate.

Return type:

float

property jacobian: ndarray

Returns the Jacobian matrix of the curve.

Returns:

The Jacobian matrix if set, otherwise None.

Return type:

numpy.ndarray

property name: str | None

Returns the name of the curve (market key identifier).

Returns:

The name/identifier of the curve, typically set by

MarketEnvironment when accessing the curve. None if not set.

Return type:

Optional[str]

property nodes

Returns the curve’s nodes (pillar dates and rates).

Returns:

A list of tuples, where each tuple contains a date and the corresponding rate.

Return type:

list[tuple]

property pillar_days

Returns the day counts for the pillars from the reference date.

Returns:

A list of day counts between the reference date and each pillar.

Return type:

list[int]

property pillars

Returns the year fractions (pillars) of the curve.

Returns:

A list of year fractions corresponding to the pillars.

Return type:

list[float]

property rates

Returns the list of interest rates for the curve.

Returns:

The rates corresponding to the pillars.

Return type:

list[float]

property reference_date

Returns the reference date of the curve.

Returns:

The reference date.

Return type:

date

zero_rate(term: date | float) float[source]

Calculates the zero rate for a given term.

Parameters:

term (Union[date, float]) – The term for which to calculate the zero rate. Can be a date or year fraction.

Returns:

The zero rate.

Return type:

float

Raises:

TypeError – If the term is not a date or float.

tensorquant.markethandles.utils module

class tensorquant.markethandles.utils.Currency(value)[source]

Bases: Enum

Enum to represent currency codes.

EUR

Euro currency code.

Type:

str

USD

US Dollar currency code.

Type:

str

GBP

British Pound currency code.

Type:

str

JPY

Japanese Yen currency code.

Type:

str

CAD

Canadian Dollar currency code.

Type:

str

CHF

Swiss Franc currency code.

Type:

str

AUD

Australian Dollar currency code.

Type:

str

AUD = 'AUD'
CAD = 'CAD'
CHF = 'CHF'
EUR = 'EUR'
GBP = 'GBP'
JPY = 'JPY'
USD = 'USD'
__str__()[source]

Return the string representation of the currency.

Returns:

The currency code (e.g., ‘EUR’, ‘USD’).

Return type:

str

class tensorquant.markethandles.utils.ExerciseType(value)[source]

Bases: Enum

An enumeration.

American = 0
Bermudan = 1
European = 2
class tensorquant.markethandles.utils.OptionType(value)[source]

Bases: Enum

An enumeration.

Call = 1
Put = -1
class tensorquant.markethandles.utils.PayoffType(value)[source]

Bases: Enum

An enumeration.

AssetOrNothing = 'AssetOrNothing'
CashOrNothing = 'CashOrNothing'
PlainVanilla = 'PlainVanilla'
class tensorquant.markethandles.utils.Position(value)[source]

Bases: Enum

Enum to represent trading position types.

Long

Represents a long position with a value of 1.

Type:

int

Short

Represents a short position with a value of -1.

Type:

int

Long = 1
Short = -1
class tensorquant.markethandles.utils.SwapType(value)[source]

Bases: Enum

Enum to represent types of swaps in a financial context.

Payer

Represents a payer swap with a value of 1.

Type:

int

Receiver

Represents a receiver swap with a value of -1.

Type:

int

Payer = 1
Receiver = -1
tensorquant.markethandles.utils.extract_value(x)[source]

Extracts the underlying value from a TensorFlow Tensor or returns the input if it is not a Tensor.

Parameters:

x (Union[Tensor, Any]) – Input value, which can be a TensorFlow Tensor or any other type.

Returns:

The extracted value from the Tensor if x is a Tensor, otherwise returns x as is.

Return type:

Any

tensorquant.markethandles.volatilitysurface module

class tensorquant.markethandles.volatilitysurface.BlackConstantVolatility(reference_date: date, volatility: float, calendar: Calendar | None = None, daycounter: DayCounter | None = None)[source]

Bases: VolatilitySurface

Constant volatility surface that returns the same volatility value regardless of strike and tenor.

This class provides the same interface as VolatilitySurface but always returns a constant volatility value, ignoring the strike and tenor parameters.

volatility(strike: float | None = None, tenor: float | None = None)[source]

Return the constant volatility value, ignoring strike and tenor.

This method has the same signature as VolatilitySurface.volatility() for interface compatibility, but always returns the constant volatility value regardless of the input parameters.

Parameters:
  • strike (float, optional) – Strike price (ignored for constant volatility).

  • tenor (float, optional) – Time to maturity in years (ignored for constant volatility).

Returns:

The constant volatility value as a TensorFlow variable.

Return type:

tf.Variable

class tensorquant.markethandles.volatilitysurface.VolatilitySurface(reference_date: date, calendar: Calendar, daycounter: DayCounter, strike: list[float], maturity: list[float], volatility_matrix)[source]

Bases: object

property calendar
property daycounter
property maturity
property reference_date
property strike
variance(strike, maturity: date)[source]
volatility(strike: float, tenor: float)[source]

bi-linear interpolation

property volatility_matrix

Module contents