| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2008 Jose Aparicio |
| 5 | Copyright (C) 2008 Chris Kenyon |
| 6 | Copyright (C) 2008 Roland Lichters |
| 7 | Copyright (C) 2008 StatPro Italia srl |
| 8 | Copyright (C) 2009 Ferdinando Ametrano |
| 9 | |
| 10 | This file is part of QuantLib, a free-software/open-source library |
| 11 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 12 | |
| 13 | QuantLib is free software: you can redistribute it and/or modify it |
| 14 | under the terms of the QuantLib license. You should have received a |
| 15 | copy of the license along with this program; if not, please email |
| 16 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 17 | <http://quantlib.org/license.shtml>. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 20 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 21 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 22 | */ |
| 23 | |
| 24 | #include <ql/math/integrals/gaussianquadratures.hpp> |
| 25 | #include <ql/termstructures/credit/hazardratestructure.hpp> |
| 26 | #include <utility> |
| 27 | |
| 28 | namespace QuantLib { |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | template <class F> |
| 33 | struct remapper { |
| 34 | F f; |
| 35 | Time T; |
| 36 | remapper(F f, Time T) : f(std::move(f)), T(T) {} |
| 37 | // This remaps [-1,1] to [0,T]. No differential included. |
| 38 | Real operator()(Real x) const { |
| 39 | const Real arg = (x+1.0)*T/2.0; |
| 40 | return f(arg); |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | template <class F> |
| 45 | remapper<F> remap(const F& f, Time T) { |
| 46 | return remapper<F>(f,T); |
| 47 | } |
| 48 | |
| 49 | } |
| 50 | |
| 51 | HazardRateStructure::HazardRateStructure( |
| 52 | const DayCounter& dc, |
| 53 | const std::vector<Handle<Quote> >& jumps, |
| 54 | const std::vector<Date>& jumpDates) |
| 55 | : DefaultProbabilityTermStructure(dc, jumps, jumpDates) {} |
| 56 | |
| 57 | HazardRateStructure::HazardRateStructure( |
| 58 | const Date& refDate, |
| 59 | const Calendar& cal, |
| 60 | const DayCounter& dc, |
| 61 | const std::vector<Handle<Quote> >& jumps, |
| 62 | const std::vector<Date>& jumpDates) |
| 63 | : DefaultProbabilityTermStructure(refDate, cal, dc, jumps, jumpDates) {} |
| 64 | |
| 65 | HazardRateStructure::HazardRateStructure( |
| 66 | Natural settlDays, |
| 67 | const Calendar& cal, |
| 68 | const DayCounter& dc, |
| 69 | const std::vector<Handle<Quote> >& jumps, |
| 70 | const std::vector<Date>& jumpDates) |
| 71 | : DefaultProbabilityTermStructure(settlDays, cal, dc, jumps, jumpDates) {} |
| 72 | |
| 73 | Real HazardRateStructure::hazardRateImpl(Time) const { |
| 74 | QL_FAIL("hazardRateImpl() must be implemented by a class derived from HazardRateStructure" ); |
| 75 | } |
| 76 | |
| 77 | Probability HazardRateStructure::survivalProbabilityImpl(Time t) const { |
| 78 | static GaussChebyshevIntegration integral(48); |
| 79 | // the Gauss-Chebyshev quadratures integrate over [-1,1], |
| 80 | // hence the remapping (and the Jacobian term t/2) |
| 81 | return std::exp(x: -integral(remap(f: [&](Time tau){ return hazardRateImpl(tau); }, T: t)) * t/2.0); |
| 82 | } |
| 83 | |
| 84 | } |
| 85 | |