# Term Structures Functions

In this section we document term structures.

## Term Structure Types and Methods

DiffFusion.TermstructureResultSizeType
@enum(
TermstructureResultSize,
TermstructureVector,
TermstructureScalar
)

Specify the dimensions/shape of the values modelled by a term structure.

For some term structures (e.g. ParameterTermstructure and VolatilityTermstructure) the result may be either a vector or a scalar.

### CorrelationTermstructure

DiffFusion.CorrelationTermstructureType
abstract type CorrelationTermstructure <: Termstructure end

An abstract correlation term structure that provides methods to calculate instantaneous correlations.

Call operator for CorrelationTermstructure is defined as

(ts::CorrelationTermstructure)(args...) = correlation(ts, args...)
DiffFusion.correlationMethod
correlation(ts::CorrelationTermstructure, alias1::String, alias2::String)

Return a scalar instantaneous correlation.

DiffFusion.correlationMethod
correlation(ts::CorrelationTermstructure, aliases::AbstractVector{String})

Return a symmetric matrix of instantaneous correlations.

DiffFusion.correlationMethod
correlation(ts::CorrelationTermstructure, aliases1::AbstractVector{String}, aliases2::AbstractVector{String})

Return a matrix of instantaneous correlations, each element of aliases1 versus each element of aliases2. The size of the resulting matrix is (length(aliases1), length(aliases2)).

DiffFusion.correlationMethod
correlation(ts::CorrelationTermstructure, alias1::String, aliases2::AbstractVector{String})

Return an (1,N) matrix of instantaneous correlations.

DiffFusion.correlationMethod
correlation(ts::CorrelationTermstructure, aliases1::AbstractVector{String}, alias2::String)

Return an (N, 1) matrix of instantaneous correlations.

### CreditDefaultTermstructure

Call operator for CreditDefaultTermstructure is defined as

(ts::CreditDefaultTermstructure)(args...) = survival(ts, args...)
DiffFusion.survivalMethod
survival(ts::CreditDefaultTermstructure, t::ModelTime)

Return the survival probability with observation time t.

### FuturesTermstructure

DiffFusion.FuturesTermstructureType
abstract type FuturesTermstructure <: Termstructure end

An abstract futures term structure that provides methods to calculate prices of futures. Such prices represent risk-neutral expectations of spot prices.

Call operator for FuturesTermstructure is defined as

(ts::FuturesTermstructure)(args...) = future_price(ts, args...)
DiffFusion.future_priceMethod
future_price(ts::FuturesTermstructure, t::ModelTime)

Return the price of a future with settlement time t.

### InflationTermstructure

DiffFusion.InflationTermstructureType
abstract type InflationTermstructure <: Termstructure end

An abstract inflation term structure that provides methods to calculate forward inflation index. Forward inflation index is a T-forward measure expectation of (spot) inflation index values.

Call operator for InflationTermstructure is defined as

(ts::InflationTermstructure)(args...) = inflation_index(ts, args...)
DiffFusion.inflation_indexMethod
inflation_index(ts::InflationTermstructure, t::ModelTime)

Return the forward inflation index with observation time t.

### ParameterTermstructure

DiffFusion.ParameterTermstructureType
abstract type ParameterTermstructure <: Termstructure end

An abstract generic parameter term structure that provides methods to retrieve parameter values for various incarnations of signatures.

Call operator for ParameterTermstructure is defined as

(ts::ParameterTermstructure)(args...) = value(ts, args...)
DiffFusion.valueFunction
value(ts::ParameterTermstructure, result_size::TermstructureResultSize = TermstructureVector)

Return a value for constant/time-homogeneous parameters.

DiffFusion.valueFunction
value(ts::ParameterTermstructure, t::ModelTime, result_size::TermstructureResultSize = TermstructureVector)

Return a value for a given observation time t.

### YieldTermstructure

DiffFusion.YieldTermstructureType
abstract type YieldTermstructure <: Termstructure end

An abstract yield term structure that provides methods to calculate discount factors zero rates and forward rates.

Call operator for YieldTermstructure is defined as

(ts::YieldTermstructure)(args...) = discount(ts, args...)
DiffFusion.discountMethod
discount(ts::YieldTermstructure, t::ModelTime)

Return the discount factor with observation time t.

DiffFusion.zero_rateMethod
zero_rate(ts::YieldTermstructure, t0::ModelTime, t1::ModelTime)

Return the continuous compounded zero rate over a period t0 to t1.

DiffFusion.zero_rateMethod
zero_rate(ts::YieldTermstructure, t::ModelTime)

Return the continuous compounded zero rate as of today with observation time t.

DiffFusion.forward_rateFunction
forward_rate(ts::YieldTermstructure, t::ModelTime, dt=1.0e-6)

Return the instantaneous forward rate with observation time t.

### VolatilityTermstructure

DiffFusion.VolatilityTermstructureType
abstract type VolatilityTermstructure <: Termstructure end

An abstract volatility term structure that provides methods to calculate volatility values for various incarnations of signatures.

Call operator for VolatilityTermstructure is defined as

(ts::VolatilityTermstructure)(args...) = volatility(ts, args...)
DiffFusion.volatilityFunction
volatility(ts::VolatilityTermstructure, t::ModelTime, result_size::TermstructureResultSize = TermstructureVector)

Return a volatility for a given observation time t.

DiffFusion.volatilityMethod
volatility(ts::VolatilityTermstructure, t::ModelTime, x::ModelValue)

Return a scalar volatility for a given observation time t and underlying or strike value x.

## Correlation Term Structures

DiffFusion.CorrelationHolderType
struct CorrelationHolder <: CorrelationTermstructure
alias::String
correlations::Dict{String, ModelValue}
sep::String
value_type::DataType
end

A container holding correlation values.

A CorrelationHolder allows to calculate correlation matrices based on String alias keys (identifiers).

value_type specifies the type of correlation entries. This ensures that all values are of consistent type. This feature is required for correlation sensitivity calculation.

DiffFusion.correlation_holderFunction
correlation_holder(
alias::String,
correlations::Dict,
sep::String = "<>",
value_type::DataType = ModelValue,
)

Create a CorrelationHolder object from dictionary.

correlation_holder(
alias::String,
sep::String = "<>",
value_type::DataType = ModelValue,
)

Create an empty CorrelationHolder object.

correlation_holder(m::AssetModel)

Return the correlation holder term structure.

Return the model's CorrelationHolder.

DiffFusion.correlation_keyFunction
correlation_key(ch::CorrelationHolder, alias1::String, alias2::String)

Derive the key for correlation dictionary from two aliases.

DiffFusion.set_correlation!Function
set_correlation!(
ch::CorrelationHolder,
alias1::String,
alias2::String,
value::ModelValue
)

Insert a new correlation value into CorrelationHolder. If a correlation already exists it is overwritten.

DiffFusion.getFunction
get(ch::CorrelationHolder, alias1::String, alias2::String)

Implement methodology to obtain a scalar correlation from a CorrelationHolder.

## Credit Default Term Structures

DiffFusion.FlatSpreadCurveType
struct FlatSpreadCurve <: CreditDefaultTermstructure
alias::String
end

DiffFusion.flat_spread_curveFunction
flat_spread_curve(alias::String, spread::ModelValue)

flat_spread_curve(spread::ModelValue)

DiffFusion.LogSurvivalCurveType
struct LogSurvivalCurve <: CreditDefaultTermstructure
alias::String
times::AbstractVector
values::AbstractVector
end

Log-interpolated survival probabilities.

DiffFusion.survival_curveFunction
survival_curve(
alias::String,
times::AbstractVector,
survival_probs::AbstractVector,
interp_method = (x,y) -> linear_interpolation(x, y, extrapolation_bc = Line()),
)

Create a LogSurvivalCurve.

survival_curve(
alias::String,
times::AbstractVector,
survival_probs::AbstractVector,
method_alias::String,
)

Create a LogSurvivalCurve for pre-defined interpolation methods.

## Parameter Term Structures

DiffFusion.PiecewiseFlatParameterType
abstract type PiecewiseFlatParameter <: ParameterTermstructure end

A generic vector-valued model parameter term structure with piece-wise constant interpolation and constant extrapolation.

DiffFusion.BackwardFlatParameterType
struct BackwardFlatParameter <: PiecewiseFlatParameter
alias::String
times::AbstractVector
values::AbstractMatrix
end

A generic vector-valued model parameter term structure with piece-wise constant backward-flat interpolation and constant extrapolation.

DiffFusion.backward_flat_parameterFunction
backward_flat_parameter(
alias::String,
times::AbstractVector,
values::AbstractMatrix,
)

Create a BackwardFlatParameter object for vector-valued parameters.

backward_flat_parameter(
alias::String,
times::AbstractVector,
values::AbstractVector,
)

Create a BackwardFlatParameter object for scalar parameters.

DiffFusion.flat_parameterFunction
flat_parameter(value::ModelValue)

Create a constant BackwardFlatParameter object.

flat_parameter(alias::String, value::ModelValue)

Create a constant BackwardFlatParameter object.

flat_parameter(value::AbstractVector)

Create a constant BackwardFlatParameter object.

flat_parameter(alias::String, value::AbstractVector)

Create a constant BackwardFlatParameter object.

DiffFusion.ForwardFlatParameterType
struct ForwardFlatParameter <: PiecewiseFlatParameter
alias::String
times::AbstractVector
values::AbstractMatrix
end

A generic vector-valued model parameter term structure with piece-wise constant forward-flat interpolation and constant extrapolation.

DiffFusion.forward_flat_parameterFunction
forward_flat_parameter(
alias::String,
times::AbstractVector,
values::AbstractMatrix,
)

Create a ForwardFlatParameter object for vector-valued parameters.

forward_flat_parameter(
alias::String,
times::AbstractVector,
values::AbstractVector,
)

Create a ForwardFlatParameter object for scalar parameters.

DiffFusion.time_idxMethod
time_idx(ts::BackwardFlatParameter, t)

Find the index such that T[idx-1] < t <= T[idx]. If t is larger than the last (or all) times T then return length(T)+1.

DiffFusion.time_idxMethod
time_idx(ts::ForwardFlatParameter, t)

Find the index such that T[idx] >= t > T[idx+1]. If t is smaller than the first (or all) times T then return 0.

## Yield Term Structures

DiffFusion.flat_forwardFunction
flat_forward(alias::String, rate)

Create a FlatForward yield curve.

flat_forward(rate)

Create a FlatForward yield curve without alias.

DiffFusion.ZeroCurveType
struct ZeroCurve <: YieldTermstructure
alias::String
times::AbstractVector
values::AbstractVector
interpolation
end

A yield term structure based on interpolated continuous compounded zero rates.

DiffFusion.zero_curveFunction
zero_curve(
alias::String,
times::AbstractVector,
values::AbstractVector,
interp_method = (x,y) -> linear_interpolation(x, y, extrapolation_bc = Line()),
)

Create a ZeroCurve object.

zero_curve(
times::AbstractVector,
values::AbstractVector,
interp_method = (x,y) -> linear_interpolation(x, y, extrapolation_bc = Line()),
)

Create a ZeroCurve object without alias.

zero_curve(
alias::String,
times::AbstractVector,
values::AbstractVector,
method_alias::String,
)

Create a ZeroCurve object using interpolation string.

zero_curve(
times::AbstractVector,
values::AbstractVector,
method_alias::String,
)

Create a ZeroCurve object using interpolation string.

DiffFusion.LinearZeroCurveType
struct LinearZeroCurve <: YieldTermstructure
alias::String
times::AbstractVector
values::AbstractVector
end

A yield term structure based on continuous compounded zero rates with linear interpolation and flat extrapolation.

This curve aims at mitigating limitations of Zygote and ZeroCurve.

DiffFusion.linear_zero_curveFunction
linear_zero_curve(
alias::String,
times::AbstractVector,
values::AbstractVector,
)

Create a LinearZeroCurve.

linear_zero_curve(
times::AbstractVector,
values::AbstractVector,
)

Create a LinearZeroCurve with empty alias.

## Volatility Term Structures

DiffFusion.BackwardFlatVolatilityType
struct BackwardFlatVolatility <: VolatilityTermstructure
alias::String
times::AbstractVector
values::AbstractMatrix
end

A vector-valued volatility term structure with piece-wise constant (backward-flat) interpolation and constant extrapolation.

DiffFusion.backward_flat_volatilityFunction
backward_flat_volatility(
alias::String,
times::AbstractVector,
values::AbstractMatrix,
)

Create a BackwardFlatVolatility object for vector-valued volatility.

Volatility values are of size (nvols, ntimes).

backward_flat_volatility(
alias::String,
times::AbstractVector,
values::AbstractVector,
)

Create a BackwardFlatVolatility object for scalar volatility.

DiffFusion.flat_volatilityFunction
flat_volatility(alias::String, value)

Create a BackwardFlatVolatility object for a flat volatility.

flat_volatility(alias::String, value)

Create a BackwardFlatVolatility object for a flat volatility.

DiffFusion.time_idxMethod
time_idx(ts::BackwardFlatVolatility, t)

Find the index such that T[idx-1] < t <= T[idx]. If t is larger than the last (or all) times T then return length(T)+1.

## Common Methods Overview

DiffFusion.correlationFunction
correlation(ts::CorrelationTermstructure, alias1::String, alias2::String)

Return a scalar instantaneous correlation.

correlation(ts::CorrelationTermstructure, aliases::AbstractVector{String})

Return a symmetric matrix of instantaneous correlations.

correlation(ts::CorrelationTermstructure, aliases1::AbstractVector{String}, aliases2::AbstractVector{String})

Return a matrix of instantaneous correlations, each element of aliases1 versus each element of aliases2. The size of the resulting matrix is (length(aliases1), length(aliases2)).

correlation(ts::CorrelationTermstructure, alias1::String, aliases2::AbstractVector{String})

Return an (1,N) matrix of instantaneous correlations.

correlation(ts::CorrelationTermstructure, aliases1::AbstractVector{String}, alias2::String)

Return an (N, 1) matrix of instantaneous correlations.

correlation(ch::CorrelationHolder, alias1::String, alias2::String)

Return a scalar instantaneous correlation.

correlation(ch::CorrelationHolder, aliases1::AbstractVector{String}, aliases2::AbstractVector{String})

Return a matrix of instantaneous correlations, each element of aliases1 versus each element of aliases2. The size of the resulting matrix is (length(aliases1), length(aliases2)).

correlation(ch::CorrelationHolder, aliases::AbstractVector{String})

Return a symmetric matrix of instantaneous correlations.

DiffFusion.discountFunction
discount(ts::YieldTermstructure, t::ModelTime)

Return the discount factor with observation time t.

discount(ts::FlatForward, t::ModelTime)

Calculate discount factor.

discount(ts::ZeroCurve, t::ModelTime)

Calculate discount factor.

discount(ts::LinearZeroCurve, t::ModelTime)

Calculate discount factor.

discount(
t::ModelTime,
ts_dict::Dict{String,Termstructure},
first_alias::String,
second_alias::Union{String,Nothing} = nothing,
operation::Union{String,Nothing} = nothing,
)

Derive the discount factor for one or two of curve alias and a curve operation.

DiffFusion.valueFunction
value(ts::ParameterTermstructure, result_size::TermstructureResultSize = TermstructureVector)

Return a value for constant/time-homogeneous parameters.

value(ts::ParameterTermstructure, t::ModelTime, result_size::TermstructureResultSize = TermstructureVector)

Return a value for a given observation time t.

value(ts::PiecewiseFlatParameter, result_size::TermstructureResultSize = TermstructureVector)

Return a value for constant/time-homogeneous parameters.

value(ts::PiecewiseFlatParameter, t::ModelTime, result_size::TermstructureResultSize = TermstructureVector)

Return a value for a given observation time t.

DiffFusion.volatilityFunction
volatility(ts::VolatilityTermstructure, t::ModelTime, result_size::TermstructureResultSize = TermstructureVector)

Return a volatility for a given observation time t.

volatility(ts::VolatilityTermstructure, t::ModelTime, x::ModelValue)

Return a scalar volatility for a given observation time t and underlying or strike value x.

volatility(ts::BackwardFlatVolatility, t::ModelTime, result_size::TermstructureResultSize = TermstructureVector)

Return a vector of volatilities for a given observation time t.

DiffFusion.survivalFunction
survival(ts::CreditDefaultTermstructure, t::ModelTime)

Return the survival probability with observation time t.

survival(ts::LogSurvivalCurve, t::ModelTime)