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
|
/*
Copyright (C) 2001, 2002 Sadruddin Rejeb
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 ferdinando@ametrano.net
The license is also available online at http://quantlib.org/html/license.html
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 parameter.hpp
\brief Model parameter classes
\fullpath
ql/InterestRateModelling/%parameter.hpp
*/
// $Id: parameter.hpp,v 1.3 2002/02/22 12:55:18 sadrejeb Exp $
#ifndef quantlib_interest_rate_modelling_parameter_h
#define quantlib_interest_rate_modelling_parameter_h
#include <ql/qldefines.hpp>
#include <ql/array.hpp>
#include <ql/termstructure.hpp>
namespace QuantLib {
namespace InterestRateModelling {
class Parameter {
public:
class ParameterImpl {
public:
virtual double value(const Array& params, Time t) const = 0;
};
Parameter() {}
Parameter(Size size, const Handle<ParameterImpl>& impl)
: params_(size), impl_(impl) {}
virtual ~Parameter() {}
const Array& params() const { return params_; }
void setParam(Size i, double x) { params_[i] = x; }
virtual Size size() const { return params_.size(); }
double operator()(Time t) const { return impl_->value(params_, t); }
const Handle<ParameterImpl>& implementation() const {return impl_;}
protected:
Array params_;
Handle<ParameterImpl> impl_;
};
class ConstantParameter : public Parameter {
public:
class ConstantParameterImpl : public Parameter::ParameterImpl {
public:
double value(const Array& params, Time t) const {
return params[0];
}
};
ConstantParameter()
: Parameter(1, Handle<ParameterImpl>(new ConstantParameterImpl))
{}
ConstantParameter(double value)
: Parameter(1, Handle<ParameterImpl>(new ConstantParameterImpl)) {
params_[0] = value;
}
};
class NullParameter : public Parameter {
public:
class NullParameterImpl : public Parameter::ParameterImpl {
public:
double value(const Array& params, Time t) const {
return 0.0;
}
};
NullParameter()
: Parameter(0, Handle<ParameterImpl>(new NullParameterImpl()))
{}
};
class PiecewiseConstantParameter : public Parameter {
public:
class PiecewiseConstantParameterImpl : public Parameter::ParameterImpl {
public:
PiecewiseConstantParameterImpl(const std::vector<Time>& times)
: times_(times) {}
virtual ~PiecewiseConstantParameterImpl() {}
double value(const Array& params, Time t) const {
Size size = times_.size();
for (Size i=0; i<size; i++) {
if (t<times_[i])
return params[i];
}
return params[size];
}
private:
std::vector<Time> times_;
};
PiecewiseConstantParameter(const std::vector<Time>& times)
: Parameter(times.size()+1,
Handle<ParameterImpl>(new PiecewiseConstantParameterImpl(times)))
{}
};
class TermStructureFittingParameter : public Parameter {
public:
class NumericalImpl : public Parameter::ParameterImpl {
public:
NumericalImpl(
const RelinkableHandle<TermStructure>& termStructure)
: times_(0), values_(0), termStructure_(termStructure) {}
virtual ~NumericalImpl() {}
void set(Time t, double x) {
times_.push_back(t);
values_.push_back(x);
}
void change(double x) {
values_.back() = x;
}
void reset() {
times_.clear();
values_.clear();
}
double value(const Array& params, Time t) const {
std::vector<Time>::const_iterator result =
std::find(times_.begin(), times_.end(), t);
QL_REQUIRE(result!=times_.end(),"Fitting parameter unset!");
return values_[result - times_.begin()];
}
const RelinkableHandle<TermStructure>& termStructure() const {
return termStructure_;
}
private:
std::vector<Time> times_;
std::vector<double> values_;
RelinkableHandle<TermStructure> termStructure_;
};
TermStructureFittingParameter(const Handle<ParameterImpl>& impl)
: Parameter(0, impl) {}
TermStructureFittingParameter(
const RelinkableHandle<TermStructure>& term)
: Parameter(0, Handle<ParameterImpl>(new NumericalImpl(term)))
{}
};
}
}
#endif
|