Logo Search packages:      
Sourcecode: quantlib version File versions


/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

 Copyright (C) 2001, 2002, 2003 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
 <quantlib-dev@lists.sf.net>. The license is also available online at

 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.

#include <ql/models/model.hpp>
#include <ql/math/optimization/problem.hpp>

namespace QuantLib {

    namespace {
        void no_deletion(CalibratedModel*) {}

    CalibratedModel::CalibratedModel(Size nArguments)
    : arguments_(nArguments),
      constraint_(new PrivateConstraint(arguments_)) {}

    class CalibratedModel::CalibrationFunction : public CostFunction {
                  CalibratedModel* model,
                  const std::vector<boost::shared_ptr<CalibrationHelper> >&
                  const std::vector<Real>& weights)
        : model_(model, no_deletion), instruments_(instruments),
          weights_(weights) {}

        virtual ~CalibrationFunction() {}

        virtual Real value(const Array& params) const {

            Real value = 0.0;
            for (Size i=0; i<instruments_.size(); i++) {
                Real diff = instruments_[i]->calibrationError();
                value += diff*diff*weights_[i];

            return std::sqrt(value);

        virtual Disposable<Array> values(const Array& params) const {

            Array values(instruments_.size());
            for (Size i=0; i<instruments_.size(); i++) {
                values[i] = instruments_[i]->calibrationError()

            return values;

        virtual Real finiteDifferenceEpsilon() const { return 1e-6; }
        boost::shared_ptr<CalibratedModel> model_;
        const std::vector<boost::shared_ptr<CalibrationHelper> >& instruments_;
        std::vector<Real> weights_;

00076     void CalibratedModel::calibrate(
        const std::vector<boost::shared_ptr<CalibrationHelper> >& instruments,
        OptimizationMethod& method,
        const EndCriteria& endCriteria,
        const Constraint& additionalConstraint,
        const std::vector<Real>& weights) {

        QL_REQUIRE(weights.empty() ||
                   weights.size() == instruments.size(),
                   "mismatch between number of instruments and weights");

        Constraint c;
        if (additionalConstraint.empty())
            c = *constraint_;
            c = CompositeConstraint(*constraint_,additionalConstraint);
        std::vector<Real> w = weights.empty() ?
                              std::vector<Real>(instruments.size(), 1.0):
        CalibrationFunction f(this, instruments, w);

        Problem prob(f, c, params());
        method.minimize(prob, endCriteria);

        Array result(prob.currentValue());

00104     Disposable<Array> CalibratedModel::params() const {
        Size size = 0, i;
        for (i=0; i<arguments_.size(); i++)
            size += arguments_[i].size();
        Array params(size);
        Size k = 0;
        for (i=0; i<arguments_.size(); i++) {
            for (Size j=0; j<arguments_[i].size(); j++, k++) {
                params[k] = arguments_[i].params()[j];
        return params;

    void CalibratedModel::setParams(const Array& params) {
        Array::const_iterator p = params.begin();
        for (Size i=0; i<arguments_.size(); i++) {
            for (Size j=0; j<arguments_[i].size(); j++, p++) {
                QL_REQUIRE(p!=params.end(),"parameter array too small");
                arguments_[i].setParam(j, *p);
        QL_REQUIRE(p==params.end(),"parameter array too big!");

    ShortRateModel::ShortRateModel(Size nArguments)
    : CalibratedModel(nArguments) {}


Generated by  Doxygen 1.6.0   Back to index