33 return out <<
"OneSide";
35 return out <<
"Centered";
42 const vector<Real>& quant) {
43 Size n = instruments.size();
45 if (quant.empty() || (quant.size()==1 && quant[0]==1.0)) {
46 for (
Size k=0; k<
n; ++k)
47 npv += instruments[k]->NPV();
50 "dimension mismatch between instruments (" <<
n <<
51 ") and quantities (" << quant.size() <<
")");
52 for (
Size k=0; k<
n; ++k)
53 npv += quant[k] * instruments[k]->NPV();
60 const vector<ext::shared_ptr<Instrument> >& instruments,
61 const vector<Real>& quantities,
66 QL_REQUIRE(!quotes.empty(),
"empty SimpleQuote vector");
67 Size n = quotes.size();
69 QL_REQUIRE(shift!=0.0,
"zero shift not allowed");
71 pair<Real, Real> result(0.0, 0.0);
72 if (instruments.empty())
return result;
78 for (
Size i=0; i<
n; ++i)
79 if (quotes[i]->isValid())
80 quoteValues[i] = quotes[i]->value();
82 for (
Size i=0; i<
n; ++i)
83 if (quotes[i]->isValid())
84 quotes[i]->setValue(quoteValues[i]+shift);
88 result.first = (npv-referenceNpv)/shift;
93 for (
Size i=0; i<
n; ++i)
94 if (quotes[i]->isValid())
95 quotes[i]->setValue(quoteValues[i]-shift);
97 result.first = (npv-npv2)/(2.0*shift);
98 result.second = (npv-2.0*referenceNpv+npv2)/(shift*shift);
102 QL_FAIL(
"unknown SensitivityAnalysis (" <<
105 for (
Size i=0; i<
n; ++i)
106 if (quotes[i]->isValid())
107 quotes[i]->setValue(quoteValues[i]);
109 for (
Size i=0; i<
n; ++i)
111 quotes[i]->setValue(quoteValues[i]);
119 const vector<ext::shared_ptr<Instrument> >& instruments,
120 const vector<Real>& quantities,
124 QL_REQUIRE(shift!=0.0,
"zero shift not allowed");
126 pair<Real, Real> result(0.0, 0.0);
127 if (instruments.empty())
return result;
132 if (!quote->isValid())
return result;
133 Real quoteValue = quote->value();
136 quote->setValue(quoteValue+shift);
140 result.first = (npv-referenceNpv)/shift;
145 quote->setValue(quoteValue-shift);
147 result.first = (npv-npv2)/(2.0*shift);
148 result.second = (npv-2.0*referenceNpv+npv2)/(shift*shift);
152 QL_FAIL(
"unknown SensitivityAnalysis (" <<
155 quote->setValue(quoteValue);
157 quote->setValue(quoteValue);
166 vector<Real>& gammaVector,
167 vector<Real>& refVals,
172 QL_REQUIRE(shift!=0.0,
"zero shift not allowed");
174 QL_REQUIRE(!params.empty(),
"empty parameters vector");
175 Size m = params.size();
176 deltaVector.resize(m);
177 gammaVector.resize(m);
179 if (!quote->isValid()) {
180 for (
Size j=0; j<m; ++j) {
186 Real quoteValue = quote->value();
188 if (!refVals.empty()) {
190 "referenceValues has size " <<
191 refVals.size() <<
", instead of " << m);
195 for (
Size j=0; j<m; ++j) {
196 if (params[j]->isValid())
197 refVals[j] = params[j]->value();
205 quote->setValue(quoteValue+shift);
206 for (
Size j=0; j<m; ++j) {
209 deltaVector[j] = (params[j]->value()-refVals[j])/shift;
217 quote->setValue(quoteValue+shift);
218 vector<Real> plus(m);
219 for (
Size j=0; j<m; ++j) {
221 plus[j] = params[j]->value();
223 quote->setValue(quoteValue-shift);
224 for (
Size j=0; j<m; ++j) {
226 Real minus = params[j]->value();
227 deltaVector[j] = (plus[j]-minus)/(2.0*shift);
228 gammaVector[j] = (plus[j]-2.0*refVals[j]+minus)/(shift*shift);
237 QL_FAIL(
"unknown SensitivityAnalysis (" <<
242 quote->setValue(quoteValue);
247 quote->setValue(quoteValue);
257 pair<vector<Real>, vector<Real> >
259 const vector<ext::shared_ptr<Instrument> >& instr,
260 const vector<Real>& quant,
264 QL_REQUIRE(!quotes.empty(),
"empty SimpleQuote vector");
265 Size n = quotes.size();
266 pair<vector<Real>, vector<Real> > result(vector<Real>(
n, 0.0),
267 vector<Real>(
n, 0.0));
269 if (instr.empty())
return result;
273 pair<Real, Real> tmp;
274 for (
Size i=0; i<
n; ++i) {
276 result.first[i] = tmp.first;
277 result.second[i] = tmp.second;
285 std::vector<std::vector<Real> >& gammaMatrix,
291 QL_REQUIRE(!quotes.empty(),
"empty SimpleQuote vector");
292 QL_REQUIRE(!parameters.empty(),
"empty parameters vector");
294 Size n = quotes.size();
295 deltaMatrix.resize(
n);
296 gammaMatrix.resize(
n);
298 Size m = parameters.size();
299 vector<Real> referenceValues(m,
Null<Real>());
300 for (
Size i=0; i<m; ++i) {
301 if (parameters[i]->isValid())
302 referenceValues[i] = parameters[i]->value();
305 for (
Size i=0; i<
n; ++i) {
307 quotes[i], parameters, shift, type);
311 pair<vector<vector<Real> >, vector<vector<Real> > >
313 const vector<ext::shared_ptr<Instrument> >& instr,
314 const vector<Real>& quant,
318 QL_REQUIRE(!quotes.empty(),
"empty SimpleQuote range");
319 Size n = quotes.size();
320 vector<vector<Real> > first(
n);
321 vector<vector<Real> > second(
n);
322 for (
Size i=0; i<
n; ++i) {
323 Size tmp = quotes[i].size();
324 first[i] = vector<Real>(tmp, 0.0);
325 second[i] = vector<Real>(tmp, 0.0);
328 pair<vector<vector<Real> >, vector<vector<Real> > >
329 result(first, second);
331 if (instr.empty())
return result;
335 pair<Real, Real> tmp;
336 for (
Size i=0; i<
n; ++i) {
337 for (
Size j=0; j<quotes[i].size(); ++j) {
338 tmp =
bucketAnalysis(quotes[i][j], instr, quant, shift, type, npv);
339 result.first[i][j] = tmp.first;
340 result.second[i][j] = tmp.second;
Shared handle to an observable.
template class providing a null value for a given type.
#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)
QL_INTEGER Integer
integer number
std::size_t Size
size of a container
Abstract instrument class.
Real aggregateNPV(const vector< ext::shared_ptr< Instrument > > &instruments, const vector< Real > &quant)
utility fuction for weighted sum of NPVs
pair< Real, Real > parallelAnalysis(const vector< Handle< SimpleQuote > > "es, const vector< ext::shared_ptr< Instrument > > &instruments, const vector< Real > &quantities, Real shift, SensitivityAnalysis type, Real referenceNpv)
parallel shift PV01 sensitivity analysis for a SimpleQuote vector
std::ostream & operator<<(std::ostream &out, GFunctionFactory::YieldCurveModel type)
pair< Real, Real > bucketAnalysis(const Handle< SimpleQuote > "e, const vector< ext::shared_ptr< Instrument > > &instruments, const vector< Real > &quantities, Real shift, SensitivityAnalysis type, Real referenceNpv)
(bucket) PV01 sensitivity analysis for a (single) SimpleQuote
SensitivityAnalysis
Finite differences calculation.
sensitivity analysis function