-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
catrisk.hpp
147 lines (120 loc) · 4.48 KB
/
catrisk.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2012, 2013 Grzegorz Andruszkiewicz
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
<http://quantlib.org/license.shtml>.
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.
*/
/*! \file catrisk.hpp
\brief classes that encapsulate catastrophe risk
*/
#ifndef quantlib_catrisk_hpp
#define quantlib_catrisk_hpp
#include <ql/time/date.hpp>
#include <ql/errors.hpp>
#include <ql/shared_ptr.hpp>
#if defined(__GNUC__) && (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)) || (__GNUC__ > 4))
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#endif
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wtautological-overlap-compare"
#endif
#include <boost/random.hpp>
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
#if defined(__GNUC__) && (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)) || (__GNUC__ > 4))
#pragma GCC diagnostic pop
#endif
#include <vector>
namespace QuantLib {
class CatSimulation {
public:
CatSimulation(Date start,
Date end)
: start_(start), end_(end)
{}
virtual ~CatSimulation() = default;
virtual bool nextPath(std::vector<std::pair<Date, Real> > &path) = 0;
protected:
Date start_;
Date end_;
};
class CatRisk {
public:
virtual ~CatRisk() = default;
virtual ext::shared_ptr<CatSimulation> newSimulation(const Date& start, const Date& end) const = 0;
};
class EventSetSimulation : public CatSimulation {
public:
EventSetSimulation(const ext::shared_ptr<std::vector<std::pair<Date, Real> > >& events,
Date eventsStart,
Date eventsEnd,
Date start,
Date end);
bool nextPath(std::vector<std::pair<Date, Real> >& path) override;
private:
ext::shared_ptr<std::vector<std::pair<Date, Real> > > events_;
Date eventsStart_;
Date eventsEnd_;
Year years_;
Date periodStart_;
Date periodEnd_;
unsigned int i_;
};
class EventSet : public CatRisk {
public:
EventSet(const ext::shared_ptr<std::vector<std::pair<Date, Real> > >& events,
Date eventsStart,
Date eventsEnd);
ext::shared_ptr<CatSimulation> newSimulation(const Date& start,
const Date& end) const override;
private:
ext::shared_ptr<std::vector<std::pair<Date, Real> > > events_;
Date eventsStart_;
Date eventsEnd_;
};
class BetaRiskSimulation : public CatSimulation {
public:
BetaRiskSimulation(Date start,
Date end,
Real maxLoss,
Real lambda,
Real alpha,
Real beta) ;
bool nextPath(std::vector<std::pair<Date, Real> >& path) override;
Real generateBeta();
private:
Real maxLoss_;
Integer dayCount_;
Real yearFraction_;
boost::mt19937 rng_;
boost::variate_generator<boost::mt19937&, boost::exponential_distribution<> > exponential_;
boost::variate_generator<boost::mt19937&, boost::gamma_distribution<> > gammaAlpha_;
boost::variate_generator<boost::mt19937&, boost::gamma_distribution<> > gammaBeta_;
};
class BetaRisk : public CatRisk {
public:
BetaRisk(Real maxLoss,
Real years,
Real mean,
Real stdDev);
ext::shared_ptr<CatSimulation> newSimulation(const Date& start,
const Date& end) const override;
private:
Real maxLoss_;
Real lambda_;
Real alpha_;
Real beta_;
};
}
#endif