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
TCopulaThe copula policy.
TInitTraitsThe 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
factorWeightsIReadOnlyList<IReadOnlyList<double>>The weights,
factorWeights[iVariable][iFactor].initTraitsTInitTraitsThe policy's initialization parameters.
Exceptions
- ArgumentNullException
factorWeightsor 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
factorsWeightIReadOnlyList<double>Each variable's weight on the factor.
initTraitsTInitTraitsThe policy's initialization parameters.
Exceptions
- ArgumentNullException
factorsWeightis 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
correlSqrdoubleThe weight, the same for every variable: the square root of the variables' correlation.
nVariablesintThe number of variables.
initTraitsTInitTraitsThe 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
nVariablesintThe number of variables.
correlationdoubleThe correlation.
initTraitsTInitTraitsThe 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
IdiosyncFctrs
Gets the idiosyncratic factors' weights, √(1 − Σₖ aᵢₖ²).
public ImmutableArray<double> IdiosyncFctrs { get; }
Property Value
Integration
Gets the policy's integration, which an integrable model provides.
protected virtual LMIntegration Integration { get; }
Property Value
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
NumTotalFactors
Gets the number of independent random factors, systemic and idiosyncratic.
public int NumTotalFactors { get; }
Property Value
Size
Gets the number of latent variables.
public int Size { get; }
Property Value
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
probsReadOnlySpan<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
Returns
- double
The probability.
CumulativeZ(double)
The cumulative distribution of the idiosyncratic factors.
public double CumulativeZ(double z)
Parameters
zdoubleThe value.
Returns
- double
The probability.
Density(ReadOnlySpan<double>)
The density of the systemic factors.
public double Density(ReadOnlySpan<double> m)
Parameters
mReadOnlySpan<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
correlationdoubleThe correlation, whose square root is the factor's weight.
nVariablesintThe number of variables.
initTraitsTInitTraitsThe 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
fMultidimIntegrandThe function.
Returns
- double
The expected value.
Exceptions
- ArgumentNullException
fis 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
fMultidimVectorIntegrandThe function.
Returns
- double[]
The expected value.
Exceptions
- ArgumentNullException
fis 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
Returns
- double
The value.
InverseCumulativeY(double, int)
The inverse cumulative distribution of a latent variable.
public double InverseCumulativeY(double p, int iVariable)
Parameters
Returns
- double
The value.
InverseCumulativeZ(double)
The inverse cumulative distribution of the idiosyncratic factors.
public double InverseCumulativeZ(double p)
Parameters
pdoubleThe 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
allFactorsReadOnlySpan<double>The factors' values, a full sample.
iVarintThe variable.
Returns
- double
The variable's value.
Exceptions
- ArgumentOutOfRangeException
iVarisn't one of the model's variables, where QuantLib reads past its weights.- ArgumentException
allFactorsdoesn'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
Returns
- double
The correlation.
Exceptions
- ArgumentOutOfRangeException
iVar1oriVar2isn't one of the variables, where QuantLib reads past its weights.