hasquant
Safe HaskellNone
LanguageHaskell2010

QuantLib.Math

Description

Numeric helpers and matrix/vector value types. RealMatrix stores dense numeric grids; boxed Matrix supports small or object-valued data.

Synopsis

Types

Matrices, vectors and grids

data Matrix a Source #

Instances

Instances details
Show a => Show (Matrix a) Source # 
Instance details

Defined in QuantLib.Internal

Methods

showsPrec :: Int -> Matrix a -> ShowS #

show :: Matrix a -> String #

showList :: [Matrix a] -> ShowS #

Eq a => Eq (Matrix a) Source # 
Instance details

Defined in QuantLib.Internal

Methods

(==) :: Matrix a -> Matrix a -> Bool #

(/=) :: Matrix a -> Matrix a -> Bool #

data RealMatrix Source #

Row-major numeric matrix backed by contiguous storage. Use this for large dense grids such as regression and volatility-surface data; small process/correlation matrices remain boxed Matrix values; object matrices retain Matrix because their elements need continuation-based FFI marshalling rather than a raw contiguous pointer.

This representation interoperates directly with hmatrix without making hasquant depend on it: Numeric.LinearAlgebra.reshape cols realMatrixData makes a row-major hmatrix matrix view with no element copy. The reverse conversion through flatten is zero-copy only for a contiguous row-major hmatrix matrix; BLAS-produced or sliced matrices can require a reorder/copy.

Instances

Instances details
Show RealMatrix Source # 
Instance details

Defined in QuantLib.Internal

Eq RealMatrix Source # 
Instance details

Defined in QuantLib.Internal

type RealVector = Vector Double Source #

Contiguous numeric data for APIs whose natural size is hundreds or thousands of values. The public element type stays Double; C's double representation is used only at the FFI boundary.

data NonEmptyVector a Source #

A storable vector known not to be empty. The constructor is deliberately hidden; build one with singletonNonEmptyVector, consNonEmptyVector, or nonEmptyVector.

Rounding and optimization

data Rounding Source #

Constructors

NoRounding 
Rounding 

Fields

Instances

Instances details
Show Rounding Source # 
Instance details

Defined in QuantLib.Internal.Common

Eq Rounding Source # 
Instance details

Defined in QuantLib.Internal.Common

data EndCriteria Source #

Constructors

EndCriteria 

Fields

data OptimizationMethod Source #

Constructors

LevenbergMarquardt 

Fields

Simplex !Double

lambda, characteristic length

Interpolation and numerical traits

data SalvagingAlgorithm Source #

Algorithm used to salvage a matrix that is not positive semi-definite before taking its pseudo square root. Higham only works for correlation matrices.

data CmsMarketCalibrationType Source #

Random sequences and statistics

data SobolDirectionIntegers Source #

Finite-difference schemes

Constructors

Matrices and vectors

boxedRealMatrix :: Word -> Word -> [Double] -> Either String (Matrix Double) Source #

Construct a list-backed numeric matrix for small-dimensional APIs such as process correlation and diffusion matrices. Returns a boxed Matrix Double, not a RealMatrix -- despite the shared RealMatrix spelling this is not the pair of realMatrixFromVector, which is the constructor for the contiguous RealMatrix type. Reach for this one when the dimensions are small and fixed (a correlation or diffusion matrix), and for realMatrixFromVector for large dense numerical grids such as volatility surfaces and multi-asset LSM data.

realMatrixFromVector :: Word -> Word -> RealVector -> Either String RealMatrix Source #

Construct a row-major numeric matrix backed by contiguous storage. This is the constructor for RealMatrix; for a small correlation/diffusion matrix use boxedRealMatrix, which yields a boxed Matrix Double instead.

Time grids

timeGrid Source #

Arguments

:: Double

end

-> Word

steps

-> IO TimeGrid 

Regularly spaced time-grid.

timeGridFromVector Source #

Arguments

:: NonEmptyVector Double

mandatoryTimes

-> IO TimeGrid 

Time grid with mandatory time points. Mandatory points are guaranteed to belong to the grid. No additional points are added.

timeGridFromVectorWithSteps Source #

Arguments

:: NonEmptyVector Double

mandatoryTimes

-> Word

steps

-> IO TimeGrid 

Time grid with mandatory time points. Mandatory points are guaranteed to belong to the grid. Additional points are then added with regular spacing between pairs of mandatory times in order to reach the desired number of steps.

Inspectors

Rounding and optimization

applyRounding Source #

Arguments

:: Rounding

rounding

-> Double

value

-> Double 

rounds a value to the precision and rule carried by the given Rounding

optimize Source #

Arguments

:: (RealVector -> Double)

cost function

-> RealVector

initial guess

-> Maybe Constraint 
-> OptimizationMethod 
-> EndCriteria 
-> IO (RealVector, Double, EndCriteriaType) 

Minimizes an arbitrary Haskell-defined cost function via QuantLib's general-purpose Problem/OptimizationMethod machinery -- unlike calibrate, which drives a CalibratedModel's own built-in calibration error against bound CalibrationHelpers, this takes any 'RealVector -> Double' objective. The cost function crosses back into Haskell once per outer optimizer iteration over the whole parameter vector; see withCostFunction.

Matrix decompositions

symmetricSchurDecomposition Source #

Arguments

:: Matrix Double

symmetric matrix

-> IO ([Double], Matrix Double)

eigenvalues, eigenvectors as columns

Eigenvalues and eigenvectors of a real symmetric matrix, computed by the symmetric threshold Jacobi algorithm. The eigenvalues come back in decreasing order, and the eigenvectors are the columns of the returned matrix: column i belongs to the i-th eigenvalue.

pseudoSqrt Source #

Arguments

:: Matrix Double

symmetric matrix

-> SalvagingAlgorithm 
-> IO (Matrix Double) 

Pseudo square root S of a real symmetric matrix M, i.e. the matrix with S*transpose S == M. When M is not positive semi-definite the given SalvagingAlgorithm approximates it; with SalvagingNone a non-positive-semi-definite input throws instead.

rankReducedSqrt Source #

Arguments

:: Matrix Double

symmetric matrix

-> Word

maxRank

-> Double

componentRetainedPercentage

-> SalvagingAlgorithm 
-> IO (Matrix Double) 

Rank-reduced pseudo square root of a real symmetric matrix: the result has rank at most maxRank. If maxRank reaches the matrix size, the given percentage of the eigenvalues' sum is retained instead.

choleskyDecomposition Source #

Arguments

:: Matrix Double

symmetric matrix

-> Bool

flexible

-> IO (Matrix Double) 

Cholesky factor L of a symmetric positive-definite matrix M, i.e. the lower-triangular matrix with L*transpose L == M. Pass True for flexible to accept a merely positive semi-definite (rank-deficient) input, whose factor is completed with zeroes rather than producing nan.

choleskySolveFor Source #

Arguments

:: Matrix Double

Cholesky factor L

-> [Double]

b

-> IO [Double] 

Solves M*x == b given the Cholesky factor L of M -- the first argument is the factor returned by choleskyDecomposition, not M itself.

Risk statistics

riskStatisticsMean Source #

Arguments

:: RealVector

sample

-> IO Double 

Mean of a caller-supplied sample. Each riskStatistics* call evaluates a fresh sample.

riskStatisticsStandardDeviation Source #

Arguments

:: RealVector

sample

-> IO Double 

Standard deviation of the sample (square root of riskStatisticsVariance).

riskStatisticsVariance Source #

Arguments

:: RealVector

sample

-> IO Double 

Variance of the sample, N/(N-1)-corrected.

riskStatisticsSkewness Source #

Arguments

:: RealVector

sample

-> IO Double 

Skewness of the sample; 0 for a gaussian distribution.

riskStatisticsKurtosis Source #

Arguments

:: RealVector

sample

-> IO Double 

Excess kurtosis of the sample; 0 for a gaussian distribution.

riskStatisticsMin Source #

Arguments

:: RealVector

sample

-> IO Double 

Minimum sample value. Throws if the sample is empty.

riskStatisticsMax Source #

Arguments

:: RealVector

sample

-> IO Double 

Maximum sample value. Throws if the sample is empty.

riskStatisticsSemiVariance Source #

Arguments

:: RealVector

sample

-> IO Double 

Variance of the sample values falling below the sample mean (Markowitz semi-variance). Throws if fewer than two sample values fall below the mean.

riskStatisticsDownsideVariance Source #

Arguments

:: RealVector

sample

-> IO Double 

Variance of the sample values falling below zero. Throws if fewer than two sample values fall below zero.

riskStatisticsPercentile Source #

Arguments

:: RealVector

sample

-> Double

y

-> IO Double 

Empirical y-th percentile of the sample; y must lie in (0.0, 1.0].

riskStatisticsGaussianPercentile Source #

Arguments

:: RealVector

sample

-> Double

y

-> IO Double 

y-th percentile assuming the sample is gaussian (mean/standard deviation matched); y must lie in (0.0, 1.0).

riskStatisticsValueAtRisk Source #

Arguments

:: RealVector

sample

-> Double

centile

-> IO Double 

Empirical value-at-risk at the given centile, which must lie in [0.9, 1.0) — a loss (non-negative), the negative of the empirical (1-centile)-th percentile floored at zero.

riskStatisticsGaussianValueAtRisk Source #

Arguments

:: RealVector

sample

-> Double

centile

-> IO Double 

Value-at-risk at the given centile assuming the sample is gaussian; centile must lie in [0.9, 1.0).

riskStatisticsExpectedShortfall Source #

Arguments

:: RealVector

sample

-> Double

centile

-> IO Double 

Empirical expected shortfall (conditional value-at-risk) at the given centile, which must lie in [0.9, 1.0): the average of the sample values below the value-at-risk threshold. Throws if the sample is empty, or if no sample value falls below the threshold.

riskStatisticsGaussianExpectedShortfall Source #

Arguments

:: RealVector

sample

-> Double

centile

-> IO Double 

Expected shortfall at the given centile assuming the sample is gaussian; centile must lie in [0.9, 1.0).

riskStatisticsPotentialUpside Source #

Arguments

:: RealVector

sample

-> Double

centile

-> IO Double 

Empirical potential upside (the reciprocal notion of value-at-risk, floored at zero) at the given centile, which must lie in [0.9, 1.0).

riskStatisticsGaussianPotentialUpside Source #

Arguments

:: RealVector

sample

-> Double

centile

-> IO Double 

Potential upside at the given centile assuming the sample is gaussian; centile must lie in [0.9, 1.0).

riskStatisticsRegret Source #

Arguments

:: RealVector

sample

-> Double

target

-> IO Double 

Variance of the sample values falling below target (Dembo/Freeman regret). Throws if fewer than two sample values fall below target.

riskStatisticsShortfall Source #

Arguments

:: RealVector

sample

-> Double

target

-> IO Double 

Probability (fraction of the sample, by count) of falling below target. Throws if the sample is empty.

riskStatisticsAverageShortfall Source #

Arguments

:: RealVector

sample

-> Double

target

-> IO Double 

Average shortfall below target, i.e. the mean of target - x over sample values x below target. Throws if no sample value falls below target.

Matrices and vectors

Time grids

timeAt Source #

Arguments

:: TimeGrid

grid

-> Word

index

-> IO Double 

returns the time at the given index of the grid

size :: TimeGrid -> Word Source #

returns the number of times on the grid

points :: TimeGrid -> IO RealVector Source #

Returns all times on the grid in contiguous storage.