Table of Contents

Class LatentModel<TCopula, TInitTraits>

Namespace
NanoQL.Experimental
Assembly
NanoQL.dll

The generic multifactor latent variable model: latent variables Yᵢ = Σₖ aᵢₖMₖ + √(1 − Σₖ aᵢₖ²) Zᵢ over independent zero-mean, unit-variance systemic factors Mₖ and idiosyncratic factors Zᵢ, the Zᵢ all of one law, their distributions given by a copula policy.

[Experimental("NQL0001")]
public class LatentModel<TCopula, TInitTraits> where TCopula : struct, ICopulaPolicy<TCopula, TInitTraits> where TInitTraits : struct

Type Parameters

TCopula

The copula policy.

TInitTraits

The policy's initialization parameters.

Inheritance
LatentModel<TCopula, TInitTraits>
Derived
Inherited Members

Remarks

An unsealed, immutable class: its derived models give the latent variables a meaning (a default time, a recovery), and an integrable one its integration. The correlation of Yᵢ and Yⱼ is Σₖ aᵢₖaⱼₖ. Its factor samplers are working types of their own (LatentModelFactorSampler<TCopula, TInitTraits, TUsng>, BoxMullerFactorSampler<TUrng>, PolarStudentTFactorSampler<TUrng>).

QuantLib's four constructors are three and a factory: its quote constructor takes a correlation and uses its square root as the factor (latentmodel.hpp:696-710), where its (correlSqr, nVariables) constructor takes the factor itself, so the two can't merge once a quote is a double; the correlation form is FromCorrelation(double, int, TInitTraits), and a new correlation is a new model, where QuantLib's observes its quote. A model of no variables or no factors is refused, where QuantLib's reads the first row of an empty matrix (latentmodel.hpp:653). The constructor on one factor's weights builds the policy from the weights, where QuantLib's default-constructs it first, which the Gaussian policy can't do: QuantLib crashes (latentmodel.hpp:667-680; Divergences/LatentModelVectorConstructorTests.cs).

Constructors

LatentModel(IReadOnlyList<IReadOnlyList<double>>, TInitTraits)

Initializes a new instance of the LatentModel<TCopula, TInitTraits> class on any number of variables and factors.

public LatentModel(IReadOnlyList<IReadOnlyList<double>> factorWeights, TInitTraits initTraits = default)

Parameters

factorWeights IReadOnlyList<IReadOnlyList<double>>

The weights, factorWeights[iVariable][iFactor].

initTraits TInitTraits

The policy's initialization parameters.

Exceptions

ArgumentNullException

factorWeights or a row is null.

ArgumentException

There's no variable or no factor; the policy refuses the weights; or a row has another number of factors than the first ("Name i provides a different number of factors").

LatentModel(IReadOnlyList<double>, TInitTraits)

Initializes a new instance of the LatentModel<TCopula, TInitTraits> class on one factor, each variable with its own weight.

public LatentModel(IReadOnlyList<double> factorsWeight, TInitTraits initTraits = default)

Parameters

factorsWeight IReadOnlyList<double>

Each variable's weight on the factor.

initTraits TInitTraits

The policy's initialization parameters.

Exceptions

ArgumentNullException

factorsWeight is null.

ArgumentException

There's no variable, or the policy refuses the weights.

LatentModel(double, int, TInitTraits)

Initializes a new instance of the LatentModel<TCopula, TInitTraits> class on one factor, every variable with the same weight.

public LatentModel(double correlSqr, int nVariables, TInitTraits initTraits = default)

Parameters

correlSqr double

The weight, the same for every variable: the square root of the variables' correlation.

nVariables int

The number of variables.

initTraits TInitTraits

The policy's initialization parameters.

Exceptions

ArgumentException

There's no variable, or the policy refuses the weights.

LatentModel(int, double, TInitTraits)

Initializes a new instance of the LatentModel<TCopula, TInitTraits> class on one factor of a correlation, QuantLib's quote constructor, for a derived model's: every variable's weight the correlation's square root, its idiosyncratic factor √(1 − correlation), as QuantLib computes them.

protected LatentModel(int nVariables, double correlation, TInitTraits initTraits)

Parameters

nVariables int

The number of variables.

correlation double

The correlation.

initTraits TInitTraits

The policy's initialization parameters.

Exceptions

ArgumentException

There's no variable, or the policy refuses the weights.

Properties

Copula

Gets the copula policy.

public TCopula Copula { get; }

Property Value

TCopula

FactorWeights

Gets the factor weights, FactorWeights[iVariable][iFactor].

public ImmutableArray<ImmutableArray<double>> FactorWeights { get; }

Property Value

ImmutableArray<ImmutableArray<double>>

IdiosyncFctrs

Gets the idiosyncratic factors' weights, √(1 − Σₖ aᵢₖ²).

public ImmutableArray<double> IdiosyncFctrs { get; }

Property Value

ImmutableArray<double>

Integration

Gets the policy's integration, which an integrable model provides.

protected virtual LMIntegration Integration { get; }

Property Value

LMIntegration

Exceptions

NanoQLException

The model provides none ("Integration non implemented in Latent model.").

NumFactors

Gets the number of systemic factors.

public int NumFactors { get; }

Property Value

int

NumTotalFactors

Gets the number of independent random factors, systemic and idiosyncratic.

public int NumTotalFactors { get; }

Property Value

int

Size

Gets the number of latent variables.

public int Size { get; }

Property Value

int

Methods

AllFactorCumulInverter(ReadOnlySpan<double>)

The inverse cumulative distributions of all the factors, the systemic ones and then one idiosyncratic factor per variable, as integrations and samplers use them.

public double[] AllFactorCumulInverter(ReadOnlySpan<double> probs)

Parameters

probs ReadOnlySpan<double>

The probabilities.

Returns

double[]

The factors' values.

CumulativeY(double, int)

The cumulative probability of a latent variable taking a value.

public double CumulativeY(double val, int iVariable)

Parameters

val double

The value.

iVariable int

The variable.

Returns

double

The probability.

CumulativeZ(double)

The cumulative distribution of the idiosyncratic factors.

public double CumulativeZ(double z)

Parameters

z double

The value.

Returns

double

The probability.

Density(ReadOnlySpan<double>)

The density of the systemic factors.

public double Density(ReadOnlySpan<double> m)

Parameters

m ReadOnlySpan<double>

The factors' values.

Returns

double

The density.

FromCorrelation(double, int, TInitTraits)

The one-factor model of a correlation, every variable with the same: QuantLib's quote constructor.

public static LatentModel<TCopula, TInitTraits> FromCorrelation(double correlation, int nVariables, TInitTraits initTraits = default)

Parameters

correlation double

The correlation, whose square root is the factor's weight.

nVariables int

The number of variables.

initTraits TInitTraits

The policy's initialization parameters.

Returns

LatentModel<TCopula, TInitTraits>

The model.

Exceptions

ArgumentException

There's no variable, or the policy refuses the weights.

IntegratedExpectedValue(MultidimIntegrand)

The expected value of a scalar function of the systemic factors, integrated over their density.

public double IntegratedExpectedValue(MultidimIntegrand f)

Parameters

f MultidimIntegrand

The function.

Returns

double

The expected value.

Exceptions

ArgumentNullException

f is null.

NanoQLException

The model has no integration.

IntegratedExpectedValueV(MultidimVectorIntegrand)

The expected value of a vector function of the systemic factors, integrated over their density.

public double[] IntegratedExpectedValueV(MultidimVectorIntegrand f)

Parameters

f MultidimVectorIntegrand

The function.

Returns

double[]

The expected value.

Exceptions

ArgumentNullException

f is null.

NanoQLException

The model has no integration, or its integration no vector form.

InverseCumulativeDensity(double, int)

The inverse cumulative distribution of a systemic factor.

public double InverseCumulativeDensity(double p, int iFactor)

Parameters

p double

The probability.

iFactor int

The factor.

Returns

double

The value.

InverseCumulativeY(double, int)

The inverse cumulative distribution of a latent variable.

public double InverseCumulativeY(double p, int iVariable)

Parameters

p double

The probability.

iVariable int

The variable.

Returns

double

The value.

InverseCumulativeZ(double)

The inverse cumulative distribution of the idiosyncratic factors.

public double InverseCumulativeZ(double p)

Parameters

p double

The probability.

Returns

double

The value.

LatentVarValue(ReadOnlySpan<double>, int)

A latent variable's value given every factor's: the systemic factors' and then the idiosyncratic ones', of which only the variable's own is read.

public double LatentVarValue(ReadOnlySpan<double> allFactors, int iVar)

Parameters

allFactors ReadOnlySpan<double>

The factors' values, a full sample.

iVar int

The variable.

Returns

double

The variable's value.

Exceptions

ArgumentOutOfRangeException

iVar isn't one of the model's variables, where QuantLib reads past its weights.

ArgumentException

allFactors doesn't reach the variable's idiosyncratic factor, where QuantLib reads past its end.

LatentVariableCorrel(int, int)

The correlation of two latent variables.

public double LatentVariableCorrel(int iVar1, int iVar2)

Parameters

iVar1 int

The first variable.

iVar2 int

The second variable.

Returns

double

The correlation.

Exceptions

ArgumentOutOfRangeException

iVar1 or iVar2 isn't one of the variables, where QuantLib reads past its weights.