# template class xf::fintech::internal::HJMPcaEngine¶

#include "hjm_engine.hpp"


## Overview¶

Class for calculating the inputs to the MonteCarlo simulation of Heath-Jarrow-Morton framework. It deals with analysing historical data, calculating the risk neutral drift and the polynomial fitting of the discrete volatility vectors.

Parameters:

 DT DataType for the internal calculations MAX_TENORS Maximum synthetisable tenors supported by the framework. MAX_CURVES Maximum number of days analyzable from historical data NCU Number of compute units on the historical data matrix.
template <
typename DT,
unsigned int MAX_TENORS,
unsigned MAX_CURVES,
unsigned NCU
>
class HJMPcaEngine: public hjmModelParams


## Methods¶

### riskNeutralDrift¶

void riskNeutralDrift (
DT vol_c1] [PD[0],
DT vol_c2] [PD[1],
DT vol_c3] [PD[2],
DT rnd [MAX_TENORS]
)


Risk Neutral drift calculation. tau assumed to be 0.5 years.

RnD calculated as :math: vol(t) * integral(vol(t))  for all volatilities and for :math: t=1,…,n

### volatility_polyfit¶

void volatility_polyfit (
DT discreteVols [MAX_TENORS][N],
DT vol1] [VOL_POLYFIT_FANOUT][PD[0],
DT vol2] [VOL_POLYFIT_FANOUT][PD[1],
DT vol3] [VOL_POLYFIT_FANOUT][PD[2]
)


Transforms each discrete volatility vector into a continuous least-squares fitting of a polynomial. The chosen degrees for the polynomial is a model specific parameter selected for better convergence. Each set of coefficients is also split into several copies for its use in different parts of the model. The outputs is a set of coefficients, ‘degree + 1’ elements wide that represent the function :math: c_0 + c_1*x + c_2*x^2 + … + c_n*x^n  .

Parameters:

 discreteVols Matrix with discrete volatilities arranged in sets of N vectors. vol1 Polynomial coefficients for the first volatility vector vol2 Polynomial coefficients for the second volatility vector vol3 Polynomial coefficients for the third volatility vector

### matdiffRows¶

template <
unsigned R,
unsigned C
>
void matdiffRows (
DT input [R][C],
DT output [R-1][C],
unsigned r,
unsigned c
)


matdiff function that computes the difference between rows in a matrix.

Parameters:

 input Input matrix data. output Output containing the diff of the matrix data. It will contain 1 fewer row than the original.

### mtransp¶

template <
unsigned R,
unsigned C
>
void mtransp (
unsigned r,
unsigned c,
DT input [R][C],
DT output [C][R]
)


Calculates the transpose of a matrix.

### calculateDiscreteVolatility¶

void calculateDiscreteVolatility (
DT ratesIn [MAX_CURVES][MAX_TENORS],
DT discreteVols [MAX_TENORS][N]
)


Extracts N discrete volatility vectors from a matrix of historical interest rates. A Principal Component Analysis will be performed on the differences of the data, and a dimensionality reduction will be performed to extract the noTenors datapoints for each vector.

Parameters:

 ratesIn Matrix with all the raw historical data, with size noTenors x noObservations discreteVols Output vectors of the discrete volatilities arranged in sets of N. It will contain noTenors elements.