35 return out <<
"OneSide";
37 return out <<
"Centered";
44 const vector<Real>& quant) {
45 Size n = instruments.size();
47 if (quant.empty() || (quant.size()==1 && quant[0]==1.0)) {
48 for (
Size k=0; k<
n; ++k)
49 npv += instruments[k]->NPV();
52 "dimension mismatch between instruments (" <<
n <<
53 ") and quantities (" << quant.size() <<
")");
54 for (
Size k=0; k<
n; ++k)
55 npv += quant[k] * instruments[k]->NPV();
62 const vector<ext::shared_ptr<Instrument> >& instruments,
63 const vector<Real>& quantities,
68 QL_REQUIRE(!quotes.empty(),
"empty SimpleQuote vector");
69 Size n = quotes.size();
71 QL_REQUIRE(shift!=0.0,
"zero shift not allowed");
73 pair<Real, Real> result(0.0, 0.0);
74 if (instruments.empty())
return result;
80 for (
Size i=0; i<
n; ++i)
81 if (quotes[i]->isValid())
82 quoteValues[i] = quotes[i]->value();
84 for (
Size i=0; i<
n; ++i)
85 if (quotes[i]->isValid())
86 quotes[i]->setValue(quoteValues[i]+shift);
90 result.first = (npv-referenceNpv)/shift;
95 for (
Size i=0; i<
n; ++i)
96 if (quotes[i]->isValid())
97 quotes[i]->setValue(quoteValues[i]-shift);
99 result.first = (npv-npv2)/(2.0*shift);
100 result.second = (npv-2.0*referenceNpv+npv2)/(shift*shift);
104 QL_FAIL(
"unknown SensitivityAnalysis (" <<
107 for (
Size i=0; i<
n; ++i)
108 if (quotes[i]->isValid())
109 quotes[i]->setValue(quoteValues[i]);
111 for (
Size i=0; i<
n; ++i)
113 quotes[i]->setValue(quoteValues[i]);
121 const vector<ext::shared_ptr<Instrument> >& instruments,
122 const vector<Real>& quantities,
126 QL_REQUIRE(shift!=0.0,
"zero shift not allowed");
128 pair<Real, Real> result(0.0, 0.0);
129 if (instruments.empty())
return result;
134 if (!quote->isValid())
return result;
135 Real quoteValue = quote->value();
138 quote->setValue(quoteValue+shift);
142 result.first = (npv-referenceNpv)/shift;
147 quote->setValue(quoteValue-shift);
149 result.first = (npv-npv2)/(2.0*shift);
150 result.second = (npv-2.0*referenceNpv+npv2)/(shift*shift);
154 QL_FAIL(
"unknown SensitivityAnalysis (" <<
157 quote->setValue(quoteValue);
159 quote->setValue(quoteValue);
168 vector<Real>& gammaVector,
169 vector<Real>& refVals,
174 QL_REQUIRE(shift!=0.0,
"zero shift not allowed");
176 QL_REQUIRE(!params.empty(),
"empty parameters vector");
177 Size m = params.size();
178 deltaVector.resize(m);
179 gammaVector.resize(m);
181 if (!quote->isValid()) {
182 for (
Size j=0; j<m; ++j) {
188 Real quoteValue = quote->value();
190 if (!refVals.empty()) {
192 "referenceValues has size " <<
193 refVals.size() <<
", instead of " << m);
197 for (
Size j=0; j<m; ++j) {
198 if (params[j]->isValid())
199 refVals[j] = params[j]->value();
207 quote->setValue(quoteValue+shift);
208 for (
Size j=0; j<m; ++j) {
211 deltaVector[j] = (params[j]->value()-refVals[j])/shift;
219 quote->setValue(quoteValue+shift);
220 vector<Real> plus(m);
221 for (
Size j=0; j<m; ++j) {
223 plus[j] = params[j]->value();
225 quote->setValue(quoteValue-shift);
226 for (
Size j=0; j<m; ++j) {
228 Real minus = params[j]->value();
229 deltaVector[j] = (plus[j]-minus)/(2.0*shift);
230 gammaVector[j] = (plus[j]-2.0*refVals[j]+minus)/(shift*shift);
239 QL_FAIL(
"unknown SensitivityAnalysis (" <<
244 quote->setValue(quoteValue);
249 quote->setValue(quoteValue);
259 pair<vector<Real>, vector<Real> >
261 const vector<ext::shared_ptr<Instrument> >& instr,
262 const vector<Real>& quant,
266 QL_REQUIRE(!quotes.empty(),
"empty SimpleQuote vector");
267 Size n = quotes.size();
268 pair<vector<Real>, vector<Real> > result(vector<Real>(
n, 0.0),
269 vector<Real>(
n, 0.0));
271 if (instr.empty())
return result;
275 pair<Real, Real> tmp;
276 for (
Size i=0; i<
n; ++i) {
278 result.first[i] = tmp.first;
279 result.second[i] = tmp.second;
287 std::vector<std::vector<Real> >& gammaMatrix,
293 QL_REQUIRE(!quotes.empty(),
"empty SimpleQuote vector");
294 QL_REQUIRE(!parameters.empty(),
"empty parameters vector");
296 Size n = quotes.size();
297 deltaMatrix.resize(
n);
298 gammaMatrix.resize(
n);
300 Size m = parameters.size();
301 vector<Real> referenceValues(m,
Null<Real>());
302 for (
Size i=0; i<m; ++i) {
303 if (parameters[i]->isValid())
304 referenceValues[i] = parameters[i]->value();
307 for (
Size i=0; i<
n; ++i) {
309 quotes[i], parameters, shift, type);
313 pair<vector<vector<Real> >, vector<vector<Real> > >
315 const vector<ext::shared_ptr<Instrument> >& instr,
316 const vector<Real>& quant,
320 QL_REQUIRE(!quotes.empty(),
"empty SimpleQuote range");
321 Size n = quotes.size();
322 vector<vector<Real> > first(
n);
323 vector<vector<Real> > second(
n);
324 for (
Size i=0; i<
n; ++i) {
325 Size tmp = quotes[i].size();
326 first[i] = vector<Real>(tmp, 0.0);
327 second[i] = vector<Real>(tmp, 0.0);
330 pair<vector<vector<Real> >, vector<vector<Real> > >
331 result(first, second);
333 if (instr.empty())
return result;
337 pair<Real, Real> tmp;
338 for (
Size i=0; i<
n; ++i) {
339 for (
Size j=0; j<quotes[i].size(); ++j) {
340 tmp =
bucketAnalysis(quotes[i][j], instr, quant, shift, type, npv);
341 result.first[i][j] = tmp.first;
342 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.
#define QL_DEPRECATED_DISABLE_WARNING
#define QL_DEPRECATED_ENABLE_WARNING
sensitivity analysis function