37 Real x=0.5*(low+high);
38 Real y=(theObject.*Value)(x);
41 if (
y < target) low = x;
42 else if (
y > target) high = x;
45 y = (theObject.*Value)(x);
46 }
while ((std::fabs(high-low) > tolerance));
56 bool (
T::*Value)(
Real)) {
58 Real x=0.5*(low+high);
59 bool ok=(theObject.*Value)(x);
66 ok = (theObject.*Value)(x);
67 }
while ((std::fabs(high-low) > tolerance));
77 bool (
T::*Value)(
Real)) {
79 Real x=0.5*(low+high);
80 bool ok=(theObject.*Value)(x);
87 ok = (theObject.*Value)(x);
88 }
while ( (std::fabs(high-low) > tolerance) );
100 bool (
T::*Condition)(
Real),
103 Real leftValue = (theObject.*Value)(low);
104 Real rightValue = (theObject.*Value)(high);
105 Real W = 0.5*(3.0-std::sqrt(5.0));
106 Real x=W*low+(1-W)*high;
107 Real midValue = (theObject.*Value)(x);
111 while(high - low > tolerance) {
113 if (x - low > high -x)
115 Real tentativeNewMid = W*low+(1-W)*x;
116 Real tentativeNewMidValue = (theObject.*Value)(tentativeNewMid);
117 bool conditioner = (theObject.*Condition)(tentativeNewMidValue);
119 if ((theObject.*Condition)(x))
122 if (leftValue < rightValue)
128 if (tentativeNewMidValue < midValue)
131 rightValue = midValue;
133 midValue = tentativeNewMidValue;
137 low = tentativeNewMid;
138 leftValue = tentativeNewMidValue;
143 Real tentativeNewMid = W*x+(1-W)*high;
144 Real tentativeNewMidValue = (theObject.*Value)(tentativeNewMid);
145 bool conditioner = (theObject.*Condition)(tentativeNewMidValue);
147 if ((theObject.*Condition)(x))
150 if (leftValue < rightValue)
156 if (tentativeNewMidValue < midValue)
159 leftValue = midValue;
161 midValue = tentativeNewMidValue;
165 high = tentativeNewMid;
166 rightValue = tentativeNewMidValue;
180: parametricform_(
std::move(parametricform)) {}
211 Real dum1, dum2, dum3;
234 const std::vector<Volatility>& ratetwohomogeneousvols,
241 std::vector<Volatility>& ratetwovols) {
249 for (
Integer i =0; i < stepindex+1; ++i) {
250 ratetwovols[i] = ratetwohomogeneousvols[i] *
251 (*parametricform_)(i) * a;
252 varSoFar += ratetwovols[i]* ratetwovols[i];
258 Real requiredSd = std::sqrt(VarToFind);
259 b = requiredSd / (ratetwohomogeneousvols[stepindex+1] *
260 (*parametricform_)(stepindex));
261 ratetwovols[stepindex+1] = requiredSd;
271 Real valueAtTP =
q.valueAtTurningPoint();
284 Real dum1, dum2, dum3;
299 const std::vector<Volatility>& rateonevols,
300 const std::vector<Volatility>& ratetwohomogeneousvols,
301 const std::vector<Real>& correlations,
312 std::vector<Volatility>& ratetwovols) {
320 for (
Size i=0; i <=static_cast<Size>(stepindex)+1; ++i)
321 totalVar_+=ratetwohomogeneousvols[i]*ratetwohomogeneousvols[i];
327 for (
Integer i=0; i < stepindex+1; ++i)
335 if (valueAtTP <= targetVariance) {
338 ratetwohomogeneousvols,
351 Real bottomAlpha = alphaMin;
353 Real topAlpha = alphaMax;
354 Real bilimit = alpha0;
356 if (bottomValue > targetVariance && topValue > targetVariance) {
359 while ( i < steps && topValue> targetVariance) {
360 topAlpha = alpha0 + (alphaMax-alpha0)*(i+0.0)/(steps+0.0);
365 if (topValue <= targetVariance)
366 bilimit = alpha0 + (topAlpha-alpha0)*(i-2.0)/(steps+0.0);
369 if (bottomValue > targetVariance && topValue > targetVariance) {
372 while ( i < steps && topValue> targetVariance) {
373 bottomAlpha = alpha0 + (alphaMin-alpha0)*(i+0.0)/(steps+0.0);
378 if (bottomValue <= targetVariance)
379 bilimit = alpha0 +(bottomAlpha-alpha0)*(i-2.0)/(steps+0.0);
382 if (bottomValue > targetVariance && topValue > targetVariance)
385 if (bottomValue <= targetVariance) {
407 ratetwohomogeneousvols,
421 const std::vector<Volatility>& rateonevols,
422 const std::vector<Volatility>& ratetwohomogeneousvols,
423 const std::vector<Real>& correlations,
434 std::vector<Volatility>& ratetwovols) {
444 for (
Size i=0; i <=static_cast<Size>(stepindex)+1; ++i)
445 totalVar_+=ratetwohomogeneousvols[i]*ratetwohomogeneousvols[i];
451 for (
Integer i=0; i < stepindex+1; ++i)
456 Real alpha1 = alphaMin;
457 Real alpha2 = alphaMax;
464 bool foundOKPoint = alpha0OK || alphaMaxOK || alphaMinOK;
471 alpha1 = FindLowestOK<AlphaFinder>(
479 alpha1 = FindLowestOK<AlphaFinder>(
491 alpha2 = FindHighestOK<AlphaFinder>(
502 bool foundUpOK =
false;
503 bool foundDownOK =
false;
504 Real alphaUp = alpha0, alphaDown = alpha0;
505 Real stepSize = (alphaMax-alpha0)/steps;
508 j<static_cast<Size>(steps) && !foundUpOK && !foundDownOK;
510 alphaUp = alpha0+j*stepSize;
512 alphaDown = alpha0-j*stepSize;
515 foundOKPoint = foundUpOK || foundDownOK;
521 alpha2 = FindHighestOK<AlphaFinder>(
529 alpha1 = FindLowestOK<AlphaFinder>(
541 alpha = Minimize<AlphaFinder>(
552 ratetwohomogeneousvols,
Real minusValueAtTurningPoint(Real alpha)
Real homogeneityfailure(Real alpha)
std::vector< Volatility > ratetwohomogeneousvols_
bool testIfSolutionExists(Real alpha)
Real valueAtTurningPoint(Real alpha)
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 finalPart(Real alphaFound, Integer stepindex, const std::vector< Volatility > &ratetwohomogeneousvols, Real quadraticPart, Real linearPart, Real constantPart, 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< AlphaForm > parametricform_
Real computeQuadraticPart(Real alpha)
std::vector< Volatility > rateonevols_
std::vector< Real > correlations_
Real computeLinearPart(Real alpha)
std::vector< Volatility > putativevols_
AlphaFinder(ext::shared_ptr< AlphaForm > parametricform)
bool roots(Real &x, Real &y) const
ext::function< Real(Real)> b
QL_INTEGER Integer
integer number
std::size_t Size
size of a container
ext::shared_ptr< YieldTermStructure > q