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