| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2012, 2013 Grzegorz Andruszkiewicz |
| 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 | <http://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 | /*! \file catrisk.hpp |
| 21 | \brief classes that encapsulate catastrophe risk |
| 22 | */ |
| 23 | |
| 24 | #ifndef quantlib_catrisk_hpp |
| 25 | #define quantlib_catrisk_hpp |
| 26 | |
| 27 | #include <ql/time/date.hpp> |
| 28 | #include <ql/errors.hpp> |
| 29 | #include <ql/shared_ptr.hpp> |
| 30 | #include <random> |
| 31 | #include <vector> |
| 32 | |
| 33 | namespace QuantLib { |
| 34 | |
| 35 | class CatSimulation { |
| 36 | public: |
| 37 | CatSimulation(Date start, |
| 38 | Date end) |
| 39 | : start_(start), end_(end) |
| 40 | {} |
| 41 | |
| 42 | virtual ~CatSimulation() = default; |
| 43 | virtual bool nextPath(std::vector<std::pair<Date, Real> > &path) = 0; |
| 44 | protected: |
| 45 | Date start_; |
| 46 | Date end_; |
| 47 | }; |
| 48 | |
| 49 | class CatRisk { |
| 50 | public: |
| 51 | virtual ~CatRisk() = default; |
| 52 | virtual ext::shared_ptr<CatSimulation> newSimulation(const Date& start, const Date& end) const = 0; |
| 53 | }; |
| 54 | |
| 55 | class EventSetSimulation : public CatSimulation { |
| 56 | public: |
| 57 | EventSetSimulation(ext::shared_ptr<std::vector<std::pair<Date, Real> > > events, |
| 58 | Date eventsStart, |
| 59 | Date eventsEnd, |
| 60 | Date start, |
| 61 | Date end); |
| 62 | bool nextPath(std::vector<std::pair<Date, Real> >& path) override; |
| 63 | |
| 64 | private: |
| 65 | ext::shared_ptr<std::vector<std::pair<Date, Real> > > events_; |
| 66 | Date eventsStart_; |
| 67 | Date eventsEnd_; |
| 68 | |
| 69 | Year years_; |
| 70 | Date periodStart_; |
| 71 | Date periodEnd_; |
| 72 | unsigned int i_ = 0; |
| 73 | }; |
| 74 | |
| 75 | class EventSet : public CatRisk { |
| 76 | public: |
| 77 | EventSet(ext::shared_ptr<std::vector<std::pair<Date, Real> > > events, |
| 78 | Date eventsStart, |
| 79 | Date eventsEnd); |
| 80 | |
| 81 | ext::shared_ptr<CatSimulation> newSimulation(const Date& start, |
| 82 | const Date& end) const override; |
| 83 | |
| 84 | private: |
| 85 | ext::shared_ptr<std::vector<std::pair<Date, Real> > > events_; |
| 86 | Date eventsStart_; |
| 87 | Date eventsEnd_; |
| 88 | }; |
| 89 | |
| 90 | class BetaRiskSimulation : public CatSimulation { |
| 91 | public: |
| 92 | BetaRiskSimulation(Date start, |
| 93 | Date end, |
| 94 | Real maxLoss, |
| 95 | Real lambda, |
| 96 | Real alpha, |
| 97 | Real beta) ; |
| 98 | |
| 99 | bool nextPath(std::vector<std::pair<Date, Real> >& path) override; |
| 100 | Real generateBeta(); |
| 101 | |
| 102 | private: |
| 103 | Real maxLoss_; |
| 104 | |
| 105 | Integer dayCount_; |
| 106 | Real yearFraction_; |
| 107 | |
| 108 | std::mt19937 rng_; |
| 109 | std::exponential_distribution<Real> exponential_; |
| 110 | std::gamma_distribution<Real> gammaAlpha_; |
| 111 | std::gamma_distribution<Real> gammaBeta_; |
| 112 | }; |
| 113 | |
| 114 | class BetaRisk : public CatRisk { |
| 115 | public: |
| 116 | BetaRisk(Real maxLoss, |
| 117 | Real years, |
| 118 | Real mean, |
| 119 | Real stdDev); |
| 120 | |
| 121 | ext::shared_ptr<CatSimulation> newSimulation(const Date& start, |
| 122 | const Date& end) const override; |
| 123 | |
| 124 | private: |
| 125 | Real maxLoss_; |
| 126 | Real lambda_; |
| 127 | Real alpha_; |
| 128 | Real beta_; |
| 129 | }; |
| 130 | |
| 131 | } |
| 132 | |
| 133 | #endif |
| 134 | |