/// <summary> /// Converts the input vector into a matrix. /// </summary> /// <param name="x"></param> /// <returns></returns> public Matrix VecToMat(Vector <double> x) { var tempMatrix = new Matrix(_timeGrid.ExpiryCount, _timeGrid.TenorCount); if (_parameters.ExponentialForm) { for (int i = 0; i < _timeGrid.ExpiryCount; i++) { for (int j = 0; j < _timeGrid.TenorCount; j++) { tempMatrix[i, j] = Math.Exp(x[i * _timeGrid.TenorCount + j] * 20); } } } else { for (int i = 0; i < _timeGrid.ExpiryCount; i++) { for (int j = 0; j < _timeGrid.TenorCount; j++) { tempMatrix[i, j] = x[i * _timeGrid.TenorCount + j]; } } } return(tempMatrix); }
/// <summary> /// Vertical smoothing. /// </summary> /// <param name="inputVolMat"></param> /// <returns></returns> private double SmoothV(Matrix inputVolMat) { double total = 0; for (int i = 0; i < _timeGrid.ExpiryCount - 1; i++) { for (int j = 0; j < _timeGrid.TenorCount; j++) { double temp = Math.Log(inputVolMat[i, j] / inputVolMat[i + 1, j]); total += temp * temp; } } total = total * _vFactor; return(total); }
/// <summary> /// Quality of fit for caplets, used when the volatility is perturbed during gradient calculations. /// </summary> /// <param name="newRow"></param> /// <param name="replacedRow"></param> /// <returns></returns> private double QOFCaplet(Matrix newRow, int replacedRow) { double total = 0; for (int i = 0; i < _targets.CapletCount; i++) { if (_targets.CapletImpliedVol[i] > 0) { double temp = Math.Log(_volatilities.FindImpliedVolatility(_targets.CapletExpiry[i], 1, newRow, replacedRow) / _targets.CapletImpliedVol[i]); total += temp * temp; } } total = total * _cFactor; return(total); }
/// <summary> /// Quality of fit for swaptions, used when the volatility is perturbed during gradient calculations. /// </summary> /// <param name="newRow"></param> /// <param name="replacedRow"></param> /// <returns></returns> private double QOFSwaption(Matrix newRow, int replacedRow) { double total = 0; for (int i = 0; i < _targets.SwaptionCount; i++) { if (_targets.SwaptionImpliedVol[i] > 0) { double temp = Math.Log(_volatilities.FindImpliedVolatility(_targets.SwaptionExpiry[i], _targets.SwaptionTenor[i], newRow, replacedRow) / _targets.SwaptionImpliedVol[i]); total += temp * temp; } } total = total * _sFactor; return(total); }