Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Member Functions | List of all members
OptionInterpolator2d< InterpolatorStrike, InterpolatorExpiry > Class Template Reference

#include <qle/interpolators/optioninterpolator2d.hpp>

+ Inheritance diagram for OptionInterpolator2d< InterpolatorStrike, InterpolatorExpiry >:
+ Collaboration diagram for OptionInterpolator2d< InterpolatorStrike, InterpolatorExpiry >:

Public Member Functions

 OptionInterpolator2d (const QuantLib::Date &referenceDate, const QuantLib::DayCounter &dayCounter, bool lowerStrikeConstExtrap=true, bool upperStrikeConstExtrap=true, const InterpolatorStrike &interpolatorStrike=InterpolatorStrike(), const InterpolatorExpiry &interpolatorExpiry=InterpolatorExpiry(), const QuantLib::Date &baseDate=QuantLib::Date())
 OptionInterpolator2d default Constructor. More...
 
 OptionInterpolator2d (const QuantLib::Date &referenceDate, const QuantLib::DayCounter &dayCounter, const std::vector< QuantLib::Date > &dates, const std::vector< QuantLib::Real > &strikes, const std::vector< QuantLib::Real > &values, bool lowerStrikeConstExtrap=true, bool upperStrikeConstExtrap=true, const InterpolatorStrike &interpolatorStrike=InterpolatorStrike(), const InterpolatorExpiry &interpolatorExpiry=InterpolatorExpiry(), const QuantLib::Date &baseDate=QuantLib::Date())
 OptionInterpolator2d Constructor with dates. More...
 
 OptionInterpolator2d (const QuantLib::Date &referenceDate, const QuantLib::Calendar &calendar, const QuantLib::BusinessDayConvention &bdc, const QuantLib::DayCounter &dayCounter, const std::vector< QuantLib::Period > &tenors, const std::vector< QuantLib::Real > &strikes, const std::vector< QuantLib::Real > &values, bool lowerStrikeConstExtrap=true, bool upperStrikeConstExtrap=true, const InterpolatorStrike &interpolatorStrike=InterpolatorStrike(), const InterpolatorExpiry &interpolatorExpiry=InterpolatorExpiry(), const QuantLib::Date &baseDate=QuantLib::Date())
 OptionInterpolator2d Constructor with Tenors. More...
 
 OptionInterpolator2d (const OptionInterpolator2d &)=delete
 
OptionInterpolator2doperator= (const OptionInterpolator2d &)=delete
 
void initialise (const std::vector< QuantLib::Date > &dates, const std::vector< QuantLib::Real > &strikes, const std::vector< QuantLib::Real > &values)
 Initialise. More...
 
void initialise (const std::vector< QuantLib::Period > &tenors, const std::vector< QuantLib::Real > &strikes, const std::vector< QuantLib::Real > &values, const QuantLib::Calendar &calendar, const QuantLib::BusinessDayConvention &bdc)
 
- Public Member Functions inherited from OptionInterpolatorBase
virtual ~OptionInterpolatorBase ()
 
 OptionInterpolatorBase (const QuantLib::Date &referenceDate)
 
virtual QuantLib::Real getValue (QuantLib::Time t, QuantLib::Real strike) const =0
 virtual access methods More...
 
virtual QuantLib::Real getValue (QuantLib::Date d, QuantLib::Real strike) const =0
 
const QuantLib::Date & referenceDate () const
 
std::vector< QuantLib::Time > times () const
 
std::vector< QuantLib::Date > expiries () const
 
std::vector< std::vector< QuantLib::Real > > strikes () const
 
std::vector< std::vector< QuantLib::Real > > values () const
 

Getters

std::vector< QuantLib::Interpolation > interpolations_
 
QuantLib::DayCounter dayCounter_
 
bool lowerStrikeConstExtrap_
 
bool upperStrikeConstExtrap_
 
InterpolatorStrike interpolatorStrike_
 
InterpolatorExpiry interpolatorExpiry_
 
bool initialised_
 
QuantLib::Date baseDate_
 
std::vector< QuantLib::Time > times () const
 
std::vector< QuantLib::Date > expiries () const
 
std::vector< std::vector< QuantLib::Real > > strikes () const
 
std::vector< std::vector< QuantLib::Real > > values () const
 
QuantLib::DayCounter dayCounter () const
 
QuantLib::Real getValue (QuantLib::Time t, QuantLib::Real strike) const override
 virtual access methods More...
 
QuantLib::Real getValue (QuantLib::Date d, QuantLib::Real strike) const override
 
QuantLib::Real getValueForStrike (QuantLib::Real strike, const std::vector< QuantLib::Real > &strks, const std::vector< QuantLib::Real > &vars, const QuantLib::Interpolation &intrp) const
 

Additional Inherited Members

- Protected Attributes inherited from OptionInterpolatorBase
std::vector< QuantLib::Date > expiries_
 
std::vector< QuantLib::Time > times_
 
std::vector< std::vector< QuantLib::Real > > strikes_
 
std::vector< std::vector< QuantLib::Real > > values_
 
QuantLib::Date referenceDate_
 

Detailed Description

template<class InterpolatorStrike, class InterpolatorExpiry>
class QuantExt::OptionInterpolator2d< InterpolatorStrike, InterpolatorExpiry >

Option surface interpolator

Definition at line 71 of file optioninterpolator2d.hpp.

Constructor & Destructor Documentation

◆ OptionInterpolator2d() [1/4]

OptionInterpolator2d ( const QuantLib::Date &  referenceDate,
const QuantLib::DayCounter &  dayCounter,
bool  lowerStrikeConstExtrap = true,
bool  upperStrikeConstExtrap = true,
const InterpolatorStrike &  interpolatorStrike = InterpolatorStrike(),
const InterpolatorExpiry &  interpolatorExpiry = InterpolatorExpiry(),
const QuantLib::Date &  baseDate = QuantLib::Date() 
)

OptionInterpolator2d default Constructor.

Definition at line 75 of file optioninterpolator2d.hpp.

81 lowerStrikeConstExtrap_(lowerStrikeConstExtrap), upperStrikeConstExtrap_(upperStrikeConstExtrap),
82 interpolatorStrike_(interpolatorStrike), interpolatorExpiry_(interpolatorExpiry), initialised_(false),
83 baseDate_(baseDate == QuantLib::Date() ? referenceDate : baseDate){};
QuantLib::DayCounter dayCounter() const
OptionInterpolatorBase(const QuantLib::Date &referenceDate)
const QuantLib::Date & referenceDate() const

◆ OptionInterpolator2d() [2/4]

OptionInterpolator2d ( const QuantLib::Date &  referenceDate,
const QuantLib::DayCounter &  dayCounter,
const std::vector< QuantLib::Date > &  dates,
const std::vector< QuantLib::Real > &  strikes,
const std::vector< QuantLib::Real > &  values,
bool  lowerStrikeConstExtrap = true,
bool  upperStrikeConstExtrap = true,
const InterpolatorStrike &  interpolatorStrike = InterpolatorStrike(),
const InterpolatorExpiry &  interpolatorExpiry = InterpolatorExpiry(),
const QuantLib::Date &  baseDate = QuantLib::Date() 
)

OptionInterpolator2d Constructor with dates.

Definition at line 146 of file optioninterpolator2d.hpp.

154 upperStrikeConstExtrap_(upperStrikeConstExtrap), interpolatorStrike_(interpolatorStrike),
155 interpolatorExpiry_(interpolatorExpiry), initialised_(false),
156 baseDate_(baseDate == QuantLib::Date() ? referenceDate : baseDate) {
157
158 initialise(dates, strikes, values);
159};
std::vector< std::vector< QuantLib::Real > > strikes() const
std::vector< std::vector< QuantLib::Real > > values() const
void initialise(const std::vector< QuantLib::Date > &dates, const std::vector< QuantLib::Real > &strikes, const std::vector< QuantLib::Real > &values)
Initialise.
+ Here is the call graph for this function:

◆ OptionInterpolator2d() [3/4]

OptionInterpolator2d ( const QuantLib::Date &  referenceDate,
const QuantLib::Calendar &  calendar,
const QuantLib::BusinessDayConvention &  bdc,
const QuantLib::DayCounter &  dayCounter,
const std::vector< QuantLib::Period > &  tenors,
const std::vector< QuantLib::Real > &  strikes,
const std::vector< QuantLib::Real > &  values,
bool  lowerStrikeConstExtrap = true,
bool  upperStrikeConstExtrap = true,
const InterpolatorStrike &  interpolatorStrike = InterpolatorStrike(),
const InterpolatorExpiry &  interpolatorExpiry = InterpolatorExpiry(),
const QuantLib::Date &  baseDate = QuantLib::Date() 
)

OptionInterpolator2d Constructor with Tenors.

Definition at line 162 of file optioninterpolator2d.hpp.

169 upperStrikeConstExtrap_(upperStrikeConstExtrap), interpolatorStrike_(interpolatorStrike),
170 interpolatorExpiry_(interpolatorExpiry), initialised_(false),
171 baseDate_(baseDate == QuantLib::Date() ? referenceDate : baseDate) {
172
173 initialise(tenors, strikes, values, calendar, bdc);
174}
+ Here is the call graph for this function:

◆ OptionInterpolator2d() [4/4]

OptionInterpolator2d ( const OptionInterpolator2d< InterpolatorStrike, InterpolatorExpiry > &  )
delete

Member Function Documentation

◆ operator=()

OptionInterpolator2d & operator= ( const OptionInterpolator2d< InterpolatorStrike, InterpolatorExpiry > &  )
delete

◆ initialise() [1/2]

void initialise ( const std::vector< QuantLib::Date > &  dates,
const std::vector< QuantLib::Real > &  strikes,
const std::vector< QuantLib::Real > &  values 
)

Initialise.

Definition at line 177 of file optioninterpolator2d.hpp.

179 {
180 using QuantLib::Date;
181 using QuantLib::Interpolation;
182 using QuantLib::Real;
183 using QuantLib::Size;
184 using QuantLib::Time;
185 using std::pair;
186 using std::set;
187 using std::vector;
188 QL_REQUIRE((strikes.size() == dates.size()) && (dates.size() == values.size()),
189 "dates, strikes and values vectors not of equal size.");
190
191 // first get uniques dates and sort
192 set<Date> datesSet(dates.begin(), dates.end());
193 expiries_ = vector<Date>(datesSet.begin(), datesSet.end());
194 vector<bool> dateDone(expiries_.size(), false);
195 times_ = vector<Time>(expiries_.size());
196 interpolations_ = vector<Interpolation>(expiries_.size());
197 values_ = vector<vector<Real> >(expiries_.size());
198 strikes_ = vector<vector<Real> >(expiries_.size());
199
200 // Populate expiry info. (Except interpolation obj members)
201 for (Size i = 0; i < dates.size(); i++) {
202 vector<Date>::iterator found = find(expiries_.begin(), expiries_.end(), dates[i]);
203 QL_REQUIRE(found != expiries_.end(), "Date should already be loaded" << dates[i]);
204 Size ii;
205 ii = distance(expiries_.begin(), found); // index of expiry
206
207 if (!dateDone[ii]) {
208 // add expiry data if not found
209 QL_REQUIRE(dates[i] >= referenceDate_,
210 "Expiry date:" << dates[i] << " before asof date: " << referenceDate_);
211 times_[ii] = dayCounter_.yearFraction(referenceDate_, dates[i]);
212 strikes_[ii].push_back(strikes[i]);
213 values_[ii].push_back(values[i]);
214 dateDone[ii] = true;
215 } else {
216 // expiry found => add if strike not found
217 Real tmpStrike = strikes[i];
218 vector<Real>::iterator fnd =
219 find_if(strikes_[ii].begin(), strikes_[ii].end(), CloseEnoughComparator(tmpStrike));
220 if (fnd == strikes_[ii].end()) {
221 // add strike/var pairs if strike not found for this expiry
222 strikes_[ii].push_back(strikes[i]);
223 values_[ii].push_back(values[i]);
224 }
225 }
226 }
227
228 // check on strikes and values size
229 for (Size i = 0; i < expiries_.size(); i++)
230 QL_REQUIRE(strikes_[i].size() == values_[i].size(),
231 "different number of variances and strikes for date: " << expiries_[i]);
232
233 // set expiries' interpolations.
234 for (Size i = 0; i < expiries_.size(); i++) {
235 // sort strikes within this expiry
236 vector<pair<Real, Real> > tmpPairs(strikes_[i].size());
237 vector<Real> sortedStrikes;
238 vector<Real> sortedVals;
239 for (Size j = 0; j < strikes_[i].size(); j++) {
240 tmpPairs[j] = pair<Real, Real>(strikes_[i][j], values_[i][j]);
241 }
242 sort(tmpPairs.begin(), tmpPairs.end()); // sorts according to first. (strikes)
243 for (vector<pair<Real, Real> >::iterator it = tmpPairs.begin(); it != tmpPairs.end(); it++) {
244 sortedStrikes.push_back(it->first);
245 sortedVals.push_back(it->second);
246 }
247 strikes_[i] = sortedStrikes;
248 values_[i] = sortedVals;
249
250 // set interpolation
251 if (strikes_[i].size() == 1) {
252 interpolations_[i] = Constant().interpolate(values_[i].front());
253 } else {
254 interpolations_[i] =
255 interpolatorStrike_.interpolate(strikes_[i].begin(), strikes_[i].end(), values_[i].begin());
256 }
257 interpolations_[i].enableExtrapolation();
258 }
259 initialised_ = true;
260}
std::vector< QuantLib::Interpolation > interpolations_
std::vector< std::vector< QuantLib::Real > > strikes_
std::vector< QuantLib::Time > times_
std::vector< QuantLib::Date > expiries_
std::vector< std::vector< QuantLib::Real > > values_
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ initialise() [2/2]

void initialise ( const std::vector< QuantLib::Period > &  tenors,
const std::vector< QuantLib::Real > &  strikes,
const std::vector< QuantLib::Real > &  values,
const QuantLib::Calendar &  calendar,
const QuantLib::BusinessDayConvention &  bdc 
)

Definition at line 263 of file optioninterpolator2d.hpp.

267 {
268
269 std::vector<QuantLib::Date> dates;
270 for (auto t : tenors)
271 dates.push_back(calendar.advance(referenceDate(), t, bdc));
272
273 initialise(dates, strikes, values);
274}

◆ times()

std::vector< QuantLib::Time > times

Definition at line 364 of file optioninterpolator2d.hpp.

364 {
365 QL_REQUIRE(initialised_, "No data provided to OptionInterpolator2d");
367}
std::vector< QuantLib::Time > times() const
+ Here is the call graph for this function:

◆ expiries()

std::vector< QuantLib::Date > expiries

Definition at line 369 of file optioninterpolator2d.hpp.

369 {
370 QL_REQUIRE(initialised_, "No data provided to OptionInterpolator2d");
372}
std::vector< QuantLib::Date > expiries() const
+ Here is the call graph for this function:

◆ strikes()

std::vector< std::vector< QuantLib::Real > > strikes

Definition at line 374 of file optioninterpolator2d.hpp.

374 {
375 QL_REQUIRE(initialised_, "No data provided to OptionInterpolator2d");
377}
std::vector< std::vector< QuantLib::Real > > strikes() const
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ values()

std::vector< std::vector< QuantLib::Real > > values

Definition at line 379 of file optioninterpolator2d.hpp.

379 {
380 QL_REQUIRE(initialised_, "No data provided to OptionInterpolator2d");
382}
std::vector< std::vector< QuantLib::Real > > values() const
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dayCounter()

QuantLib::DayCounter dayCounter ( ) const

Definition at line 122 of file optioninterpolator2d.hpp.

122{ return dayCounter_; }
+ Here is the caller graph for this function:

◆ getValue() [1/2]

QuantLib::Real getValue ( QuantLib::Time  t,
QuantLib::Real  strike 
) const
overridevirtual

virtual access methods

Implements OptionInterpolatorBase.

Definition at line 297 of file optioninterpolator2d.hpp.

297 {
298 using QuantLib::Interpolation;
299 using QuantLib::Real;
300 using QuantLib::Size;
301 using QuantLib::Time;
302 using std::vector;
303 Time baseTime = dayCounter_.yearFraction(referenceDate_, baseDate_);
304 QL_REQUIRE(initialised_, "No data provided to OptionInterpolator2d");
305 QL_REQUIRE(t >= baseTime, "Variance requested for date before base date: " << baseDate_);
306 if (QuantLib::close_enough(t,baseTime)) {
307 // requested at reference date
308 QL_REQUIRE(!values_.empty(), "OptionInterpolator2d: no expiries given");
309 QL_REQUIRE(!values_.front().empty(), "OptionInterpolator2d: no value for first expiry given");
310 return values_[0][0];
311 } else {
312 QL_REQUIRE(!expiries_.empty(), "OptionInterpolator2d: no expiry given");
313 if (expiries_.size() == 1) {
314 return getValueForStrike(strike, strikes_[0], values_[0], interpolations_[0]);
315 }
316 // ind1 and ind2 two expiries on either side of requested time.
317 Size ind1, ind2;
318 if (t <= times_.front()) {
319 // near end of expiries, use first 2 strikes
320 ind1 = 0;
321 ind2 = 1;
322 } else if (t > times_.back()) {
323 // far end of expiries, use last 2
324 ind1 = times_.size() - 2;
325 ind2 = times_.size() - 1;
326 } else {
327 // requested between existing expiries (interpolate between expiries)
328 ind2 = distance(times_.begin(), lower_bound(times_.begin(), times_.end(), t));
329 ind1 = (ind2 != 0) ? ind2 - 1 : 0;
330 }
331 // interpolate between expiries
332 vector<Real> tmpVars(2);
333 vector<Time> xAxis;
334 xAxis.push_back(times_[ind1]);
335 xAxis.push_back(times_[ind2]);
336
337 tmpVars[0] = getValueForStrike(strike, strikes_[ind1], values_[ind1], interpolations_[ind1]);
338 tmpVars[1] = getValueForStrike(strike, strikes_[ind2], values_[ind2], interpolations_[ind2]);
339 Interpolation interp = interpolatorExpiry_.interpolate(xAxis.begin(), xAxis.end(), tmpVars.begin());
340 // linear extrapolation of expiries in case t > time_.back() above.
341 interp.enableExtrapolation(true);
342 return interp(t);
343 }
344}
QuantLib::Real getValueForStrike(QuantLib::Real strike, const std::vector< QuantLib::Real > &strks, const std::vector< QuantLib::Real > &vars, const QuantLib::Interpolation &intrp) const

◆ getValue() [2/2]

QuantLib::Real getValue ( QuantLib::Date  d,
QuantLib::Real  strike 
) const
overridevirtual

Implements OptionInterpolatorBase.

Definition at line 347 of file optioninterpolator2d.hpp.

347 {
348 QL_REQUIRE(initialised_, "No data provided to OptionInterpolator2d");
349 QL_REQUIRE(d >= baseDate_, "Variance requested for date before reference date: " << baseDate_);
350 QuantLib::Real valueReturn;
351
352 std::vector<QuantLib::Date>::const_iterator it = find(expiries_.begin(), expiries_.end(), d);
353 // if the date matches one of the expiries get the value on that day, otherwise use time interpolation
354 if (it != expiries_.end()) {
355 QuantLib::Size dis = distance(expiries_.begin(), it);
356 valueReturn = getValueForStrike(strike, strikes_[dis], values_[dis], interpolations_[dis]);
357 } else {
358 QuantLib::Time t = dayCounter_.yearFraction(referenceDate_, d);
359 valueReturn = getValue(t, strike);
360 }
361 return valueReturn;
362}
QuantLib::Real getValue(QuantLib::Time t, QuantLib::Real strike) const override
virtual access methods

◆ getValueForStrike()

QuantLib::Real getValueForStrike ( QuantLib::Real  strike,
const std::vector< QuantLib::Real > &  strks,
const std::vector< QuantLib::Real > &  vars,
const QuantLib::Interpolation &  intrp 
) const
private

Definition at line 277 of file optioninterpolator2d.hpp.

280 {
281 QL_REQUIRE(!strks.empty(), "OptionInterpolator2d: no strikes given");
282 QL_REQUIRE(strks.size() == vars.size(), "OptionInterpolator2d: strikes size ("
283 << strks.size() << ") does not match vars size (" << vars.size()
284 << ")");
285 QuantLib::Real retVar;
286 if (strike > strks.back() && upperStrikeConstExtrap_) {
287 retVar = vars.back(); // force flat extrapolate far strike if requested
288 } else if (strike < strks.front() && lowerStrikeConstExtrap_) {
289 retVar = vars.front(); // force flat extrapolate near strike if requested
290 } else {
291 retVar = intrp(strike); // interpolate between strikes or extrap with interpolator default
292 }
293 return retVar;
294}

Member Data Documentation

◆ interpolations_

std::vector<QuantLib::Interpolation> interpolations_
protected

Definition at line 128 of file optioninterpolator2d.hpp.

◆ dayCounter_

QuantLib::DayCounter dayCounter_
private

Definition at line 131 of file optioninterpolator2d.hpp.

◆ lowerStrikeConstExtrap_

bool lowerStrikeConstExtrap_
private

Definition at line 135 of file optioninterpolator2d.hpp.

◆ upperStrikeConstExtrap_

bool upperStrikeConstExtrap_
private

Definition at line 136 of file optioninterpolator2d.hpp.

◆ interpolatorStrike_

InterpolatorStrike interpolatorStrike_
private

Definition at line 137 of file optioninterpolator2d.hpp.

◆ interpolatorExpiry_

InterpolatorExpiry interpolatorExpiry_
private

Definition at line 138 of file optioninterpolator2d.hpp.

◆ initialised_

bool initialised_
private

Definition at line 139 of file optioninterpolator2d.hpp.

◆ baseDate_

QuantLib::Date baseDate_
private

Definition at line 140 of file optioninterpolator2d.hpp.