33 const ext::shared_ptr<PiecewiseConstantCorrelation>& corr,
34 const std::vector<ext::shared_ptr<PiecewiseConstantVariance> >& displacedSwapVariances,
35 const std::vector<Volatility>& mktCapletVols,
36 const ext::shared_ptr<CurveState>& cs,
38 const std::vector<Real>& alphaInitial,
39 const std::vector<Real>& alphaMax,
40 const std::vector<Real>& alphaMin,
41 bool maximizeHomogeneity,
42 ext::shared_ptr<AlphaForm> parametricForm)
44 evolution, corr, displacedSwapVariances, mktCapletVols, cs, displacement),
45 alphaInitial_(alphaInitial), alphaMax_(alphaMax), alphaMin_(alphaMin),
46 maximizeHomogeneity_(maximizeHomogeneity), parametricForm_(
std::move(parametricForm)),
47 alpha_(numberOfRates_),
a_(numberOfRates_),
b_(numberOfRates_) {
54 ") and alphaInitial (" << alphaInitial.size() <<
")");
58 ") and alphaMax (" << alphaMax.size() <<
")");
62 ") and alphaMin (" << alphaMin.size() <<
")");
68 const std::vector<ext::shared_ptr<PiecewiseConstantVariance> >& displacedSwapVariances,
69 const std::vector<Volatility>& capletVols,
73 const std::vector<Real>& alphaInitial,
74 const std::vector<Real>& alphaMax,
75 const std::vector<Real>& alphaMin,
76 bool maximizeHomogeneity,
77 const ext::shared_ptr<AlphaForm>& parametricForm,
79 const Size numberOfFactors,
83 std::vector<Real>&
alpha,
87 std::vector<Matrix>& swapCovariancePseudoRoots) {
90 displacedSwapVariances, capletVols, cs);
94 const std::vector<Time>& rateTimes = evolution.
rateTimes();
97 "number of factors (" << numberOfFactors <<
98 ") cannot be greater than numberOfRates (" <<
99 numberOfRates <<
")");
101 "number of factors (" << numberOfFactors <<
102 ") must be greater than zero");
106 alpha.resize(numberOfRates);
107 a.resize(numberOfRates);
108 b.resize(numberOfRates);
111 std::vector<Matrix> corrPseudo(corr.
times().size());
112 for (
Size i=0; i<corrPseudo.size(); ++i)
114 numberOfFactors, 1.0,
123 std::vector<std::vector<Volatility> > newVols;
124 std::vector<Volatility> theseNewVols(numberOfRates);
125 std::vector<Volatility> firstRateVols(numberOfRates);
126 firstRateVols[0] = std::sqrt(displacedSwapVariances[0]->variances()[0]);
127 std::vector<Volatility> secondRateVols(numberOfRates);
128 std::vector<Real> correlations(numberOfRates);
129 newVols.push_back(firstRateVols);
132 alpha[0] = alphaInitial[0];
138 for (
Size i=0; i<numberOfRates-1; ++i) {
141 const std::vector<Real>& var =
142 displacedSwapVariances[i+1]->variances();
143 for (
Size j =0; j < i+2; ++j)
144 secondRateVols[j] = std::sqrt(var[j]);
146 for (
Size k=0; k < i+1; k++) {
147 Real correlation=0.0;
148 for (
Size l=0; l < numberOfFactors; ++l) {
149 Real term1 = corrPseudo[k][i][l];
150 Real term2 = corrPseudo[k][i+1][l];
151 correlation += term1*term2;
153 correlations[k] = correlation;
156 Real w0 = invertedZedMatrix[i][i];
157 Real w1 = invertedZedMatrix[i][i+1];
159 for (
Size k = i+2; k <invertedZedMatrix.
columns(); ++k)
160 w0+= invertedZedMatrix[i][k];
162 Real targetVariance= capletVols[i]*capletVols[i]*rateTimes[i];
165 if (maximizeHomogeneity)
184 success = solver.
solve(alphaInitial[i+1] ,
206 newVols.push_back(theseNewVols);
207 firstRateVols = theseNewVols;
210 swapCovariancePseudoRoots.resize(numberOfSteps);
211 for (
Size k=0; k<numberOfSteps; ++k) {
212 swapCovariancePseudoRoots[k] = corrPseudo[k];
213 for (
Size j=0; j<numberOfRates; ++j) {
214 Real coeff =newVols[j][k];
215 for (
Size i=0; i<numberOfFactors; ++i)
216 swapCovariancePseudoRoots[k][j][i]*=coeff;
218 QL_ENSURE(swapCovariancePseudoRoots[k].rows()==numberOfRates,
220 <<
" abcd vol wrong number of rows: "
221 << swapCovariancePseudoRoots[k].rows()
222 <<
" instead of " << numberOfRates);
223 QL_ENSURE(swapCovariancePseudoRoots[k].columns()==numberOfFactors,
225 <<
" abcd vol wrong number of columns: "
226 << swapCovariancePseudoRoots[k].columns()
227 <<
" instead of " << numberOfFactors);
bool solveWithMaxHomogeneity(Real alpha0, Integer stepindex, const std::vector< Volatility > &rateonevols, const std::vector< Volatility > &ratetwohomogeneousvols, const std::vector< Real > &correlations, Real w0, Real w1, Real targetVariance, Real tolerance, Real alphaMax, Real alphaMin, Integer steps, Real &alpha, Real &a, Real &b, std::vector< Volatility > &ratetwovols)
bool solve(Real alpha0, Integer stepindex, const std::vector< Volatility > &rateonevols, const std::vector< Volatility > &ratetwohomogeneousvols, const std::vector< Real > &correlations, Real w0, Real w1, Real targetVariance, Real tolerance, Real alphaMax, Real alphaMin, Integer steps, Real &alpha, Real &a, Real &b, std::vector< Volatility > &ratetwovols)
ext::shared_ptr< PiecewiseConstantCorrelation > corr_
ext::shared_ptr< CurveState > cs_
static void performChecks(const EvolutionDescription &evolution, const PiecewiseConstantCorrelation &corr, const std::vector< ext::shared_ptr< PiecewiseConstantVariance > > &displacedSwapVariances, const std::vector< Volatility > &mktCapletVols, const CurveState &cs)
std::vector< ext::shared_ptr< PiecewiseConstantVariance > > displacedSwapVariances_
std::vector< Volatility > usedCapletVols_
EvolutionDescription evolution_
std::vector< Matrix > swapCovariancePseudoRoots_
Curve state for market-model simulations
Market-model evolution description.
Size numberOfRates() const
const std::vector< Time > & rateTimes() const
Size numberOfSteps() const
Matrix used in linear algebra.
virtual const Matrix & correlation(Size i) const
virtual const std::vector< Time > & times() const =0
static Matrix coterminalSwapZedMatrix(const CurveState &cs, Spread displacement)
#define QL_ENSURE(condition, message)
throw an error if the given post-condition is not verified
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
#define QL_FAIL(message)
throw an error (possibly with file and line information)
ext::function< Real(Real)> b
unsigned QL_INTEGER Natural
positive integer
QL_INTEGER Integer
integer number
Real Spread
spreads on interest rates
std::size_t Size
size of a container
Matrix rankReducedSqrt(const Matrix &matrix, Size maxRank, Real componentRetainedPercentage, SalvagingAlgorithm::Type sa)
Matrix inverse(const Matrix &m)
pseudo square root of a real symmetric matrix
Utility functions for mapping between swap rate and forward rate.