/src/quantlib/ql/processes/gsrprocess.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2013, 2015 Peter Caspers |
5 | | |
6 | | This file is part of QuantLib, a free-software/open-source library |
7 | | for financial quantitative analysts and developers - http://quantlib.org/ |
8 | | |
9 | | QuantLib is free software: you can redistribute it and/or modify it |
10 | | under the terms of the QuantLib license. You should have received a |
11 | | copy of the license along with this program; if not, please email |
12 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
13 | | <https://www.quantlib.org/license.shtml>. |
14 | | |
15 | | This program is distributed in the hope that it will be useful, but WITHOUT |
16 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
17 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
18 | | */ |
19 | | |
20 | | #include <ql/processes/gsrprocess.hpp> |
21 | | #include <cmath> |
22 | | #include <utility> |
23 | | |
24 | | namespace QuantLib { |
25 | | |
26 | | GsrProcess::GsrProcess(Array times, |
27 | | Array vols, |
28 | | Array reversions, |
29 | | const Real T, |
30 | | const Date& referenceDate, |
31 | | DayCounter dc) |
32 | 0 | : ForwardMeasureProcess1D(T), core_(std::move(times), std::move(vols), std::move(reversions), T), referenceDate_(referenceDate), |
33 | 0 | dc_(std::move(dc)) { |
34 | 0 | flushCache(); |
35 | 0 | } |
36 | | |
37 | 0 | void GsrProcess::checkT(const Time t) const { |
38 | 0 | QL_REQUIRE(t <= getForwardMeasureTime() && t >= 0.0, |
39 | 0 | "t (" << t |
40 | 0 | << ") must not be greater than forward measure time (" |
41 | 0 | << getForwardMeasureTime() << ") and non-negative"); |
42 | 0 | } |
43 | | |
44 | 0 | Real GsrProcess::time(const Date &d) const { |
45 | 0 | QL_REQUIRE( |
46 | 0 | referenceDate_ != Date() && dc_ != DayCounter(), |
47 | 0 | "time can not be computed without reference date and day counter"); |
48 | 0 | return dc_.yearFraction(referenceDate_, d); |
49 | 0 | } |
50 | | |
51 | 0 | Real GsrProcess::x0() const { return 0.0; } |
52 | | |
53 | 0 | Real GsrProcess::drift(Time t, Real x) const { |
54 | 0 | return core_.y(t) - |
55 | 0 | core_.G(t, getForwardMeasureTime()) * sigma(t) * sigma(t) - |
56 | 0 | reversion(t) * x; |
57 | 0 | } |
58 | | |
59 | 0 | Real GsrProcess::diffusion(Time t, Real) const { |
60 | 0 | checkT(t); |
61 | 0 | return sigma(t); |
62 | 0 | } |
63 | | |
64 | 0 | Real GsrProcess::expectation(Time w, Real xw, Time dt) const { |
65 | 0 | checkT(w + dt); |
66 | 0 | return core_.expectation_x0dep_part(w, xw, dt) + |
67 | 0 | core_.expectation_rn_part(w, dt) + |
68 | 0 | core_.expectation_tf_part(w, dt); |
69 | 0 | } |
70 | | |
71 | | |
72 | | |
73 | 0 | Real GsrProcess::stdDeviation(Time t0, Real x0, Time dt) const { |
74 | 0 | return std::sqrt(variance(t0, x0, dt)); |
75 | 0 | } |
76 | | |
77 | 0 | Real GsrProcess::variance(Time w, Real, Time dt) const { |
78 | 0 | checkT(w + dt); |
79 | 0 | return core_.variance(w,dt); |
80 | 0 | } |
81 | | |
82 | 0 | Real GsrProcess::sigma(Time t) const { return core_.sigma(t); } |
83 | | |
84 | 0 | Real GsrProcess::reversion(Time t) const { return core_.reversion(t); } |
85 | | |
86 | 0 | Real GsrProcess::y(Time t) const { |
87 | 0 | checkT(t); |
88 | 0 | return core_.y(t); |
89 | 0 | } |
90 | | |
91 | 0 | Real GsrProcess::G(Time t, Time w, Real) const { |
92 | 0 | QL_REQUIRE(w >= t, "G(t,w) should be called with w (" |
93 | 0 | << w << ") not lesser than t (" << t << ")"); |
94 | 0 | QL_REQUIRE(t >= 0.0 && w <= getForwardMeasureTime(), |
95 | 0 | "G(t,w) should be called with (t,w)=(" |
96 | 0 | << t << "," << w << ") in Range [0," |
97 | 0 | << getForwardMeasureTime() << "]."); |
98 | | |
99 | 0 | return core_.G(t,w); |
100 | 0 | } |
101 | | |
102 | | |
103 | | } |