/usr/include/ql/models/model.hpp is in libquantlib0-dev 1.12-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2001, 2002, 2003 Sadruddin Rejeb
Copyright (C) 2005, 2007 StatPro Italia srl
Copyright (C) 2013, 2015 Peter Caspers
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
/*! \file model.hpp
\brief Abstract interest rate model class
*/
#ifndef quantlib_interest_rate_model_hpp
#define quantlib_interest_rate_model_hpp
#include <ql/option.hpp>
#include <ql/methods/lattices/lattice.hpp>
#include <ql/models/parameter.hpp>
#include <ql/models/calibrationhelper.hpp>
#include <ql/math/optimization/endcriteria.hpp>
namespace QuantLib {
class OptimizationMethod;
//! Affine model class
/*! Base class for analytically tractable models.
\ingroup shortrate
*/
class AffineModel : public virtual Observable {
public:
//! Implied discount curve
virtual DiscountFactor discount(Time t) const = 0;
virtual Real discountBond(Time now,
Time maturity,
Array factors) const = 0;
virtual Real discountBondOption(Option::Type type,
Real strike,
Time maturity,
Time bondMaturity) const = 0;
virtual Real discountBondOption(Option::Type type,
Real strike,
Time maturity,
Time bondStart,
Time bondMaturity) const;
};
//! Term-structure consistent model class
/*! This is a base class for models that can reprice exactly
any discount bond.
\ingroup shortrate
*/
class TermStructureConsistentModel : public virtual Observable {
public:
TermStructureConsistentModel(
const Handle<YieldTermStructure>& termStructure)
: termStructure_(termStructure) {}
const Handle<YieldTermStructure>& termStructure() const {
return termStructure_;
}
private:
Handle<YieldTermStructure> termStructure_;
};
//! Calibrated model class
class CalibratedModel : public virtual Observer, public virtual Observable {
public:
CalibratedModel(Size nArguments);
void update() {
generateArguments();
notifyObservers();
}
//! Calibrate to a set of market instruments (usually caps/swaptions)
/*! An additional constraint can be passed which must be
satisfied in addition to the constraints of the model.
*/
virtual void calibrate(
const std::vector<boost::shared_ptr<CalibrationHelper> >&,
OptimizationMethod& method,
const EndCriteria& endCriteria,
const Constraint& constraint = Constraint(),
const std::vector<Real>& weights = std::vector<Real>(),
const std::vector<bool>& fixParameters = std::vector<bool>());
Real value(const Array& params,
const std::vector<boost::shared_ptr<CalibrationHelper> >&);
const boost::shared_ptr<Constraint>& constraint() const;
//! Returns end criteria result
EndCriteria::Type endCriteria() const { return shortRateEndCriteria_; }
//! Returns the problem values
const Array& problemValues() const { return problemValues_; }
//! Returns array of arguments on which calibration is done
Disposable<Array> params() const;
virtual void setParams(const Array& params);
Integer functionEvaluation() const { return functionEvaluation_; }
protected:
virtual void generateArguments() {}
std::vector<Parameter> arguments_;
boost::shared_ptr<Constraint> constraint_;
EndCriteria::Type shortRateEndCriteria_;
Array problemValues_;
Integer functionEvaluation_;
private:
//! Constraint imposed on arguments
class PrivateConstraint;
//! Calibration cost function class
class CalibrationFunction;
friend class CalibrationFunction;
};
//! Abstract short-rate model class
/*! \ingroup shortrate */
class ShortRateModel : public CalibratedModel {
public:
explicit ShortRateModel(Size nArguments);
virtual boost::shared_ptr<Lattice> tree(const TimeGrid&) const = 0;
};
// inline definitions
inline Real AffineModel::discountBondOption(Option::Type type,
Real strike,
Time maturity,
Time,
Time bondMaturity) const {
return discountBondOption(type, strike, maturity, bondMaturity);
}
inline const boost::shared_ptr<Constraint>&
CalibratedModel::constraint() const {
return constraint_;
}
class CalibratedModel::PrivateConstraint : public Constraint {
private:
class Impl : public Constraint::Impl {
public:
explicit Impl(const std::vector<Parameter>& arguments)
: arguments_(arguments) {}
bool test(const Array& params) const {
Size k=0;
for (Size i=0; i<arguments_.size(); i++) {
Size size = arguments_[i].size();
Array testParams(size);
for (Size j=0; j<size; j++, k++)
testParams[j] = params[k];
if (!arguments_[i].testParams(testParams))
return false;
}
return true;
}
Array upperBound(const Array ¶ms) const {
Size k = 0, k2 = 0;
Size totalSize = 0;
for (Size i = 0; i < arguments_.size(); i++) {
totalSize += arguments_[i].size();
}
Array result(totalSize);
for (Size i = 0; i < arguments_.size(); i++) {
Size size = arguments_[i].size();
Array partialParams(size);
for (Size j = 0; j < size; j++, k++)
partialParams[j] = params[k];
Array tmpBound =
arguments_[i].constraint().upperBound(partialParams);
for (Size j = 0; j < size; j++, k2++)
result[k2] = tmpBound[j];
}
return result;
}
Array lowerBound(const Array ¶ms) const {
Size k = 0, k2 = 0;
Size totalSize = 0;
for (Size i = 0; i < arguments_.size(); i++) {
totalSize += arguments_[i].size();
}
Array result(totalSize);
for (Size i = 0; i < arguments_.size(); i++) {
Size size = arguments_[i].size();
Array partialParams(size);
for (Size j = 0; j < size; j++, k++)
partialParams[j] = params[k];
Array tmpBound =
arguments_[i].constraint().lowerBound(partialParams);
for (Size j = 0; j < size; j++, k2++)
result[k2] = tmpBound[j];
}
return result;
}
private:
const std::vector<Parameter>& arguments_;
};
public:
explicit PrivateConstraint(const std::vector<Parameter>& arguments)
: Constraint(boost::shared_ptr<Constraint::Impl>(
new PrivateConstraint::Impl(arguments))) {}
};
}
#endif
|