QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
catrisk.cpp
Go to the documentation of this file.
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
22#include <random>
23#include <utility>
24
25namespace QuantLib {
26
27
29 ext::shared_ptr<std::vector<std::pair<Date, Real> > > events,
30 Date eventsStart,
31 Date eventsEnd,
32 Date start,
33 Date end)
34 : CatSimulation(start, end), events_(std::move(events)), eventsStart_(eventsStart),
35 eventsEnd_(eventsEnd) {
41 } else {
43 }
45 while(i_<events_->size() && (*events_)[i_].first<periodStart_) ++i_; //i points to the first element after the start of the relevant period.
46 }
47
48 bool EventSetSimulation::nextPath(std::vector< std::pair< Date, Real > >& path) {
49 path.resize(0);
50 if(periodEnd_>eventsEnd_) //Ran out of event data
51 return false;
52
53 while(i_<events_->size() && (*events_)[i_].first<periodStart_) {
54 ++i_; //skip the elements between the previous period and this period
55 }
56 while(i_<events_->size() && (*events_)[i_].first<=periodEnd_){
57 std::pair<Date, Real> e(events_->at(i_).first+(start_.year() - periodStart_.year())*Years, events_->at(i_).second);
58 path.push_back(e);
59 ++i_; //i points to the first element after the start of the relevant period.
60 }
64 } else {
67 }
68 return true;
69 }
70
71 EventSet::EventSet(ext::shared_ptr<std::vector<std::pair<Date, Real> > > events,
72 Date eventsStart,
73 Date eventsEnd)
74 : events_(std::move(events)), eventsStart_(eventsStart), eventsEnd_(eventsEnd) {}
75
76 ext::shared_ptr<CatSimulation> EventSet::newSimulation(const Date& start, const Date& end) const{
77 return ext::make_shared<EventSetSimulation>(events_, eventsStart_, eventsEnd_, start, end);
78 }
79
81 : CatSimulation(start, end),
82 maxLoss_(maxLoss),
83 exponential_(lambda),
84 gammaAlpha_(alpha),
85 gammaBeta_(beta)
86 {
88 dayCount_ = dayCounter.dayCount(start, end);
89 yearFraction_ = dayCounter.yearFraction(start, end);
90 }
91
93 {
95 Real Y = gammaBeta_(rng_);
96 return X*maxLoss_/(X+Y);
97 }
98
99 bool BetaRiskSimulation::nextPath(std::vector<std::pair<Date, Real> > &path)
100 {
101 path.resize(0);
102 Real eventFraction = exponential_(rng_);
103 while(eventFraction<=yearFraction_)
104 {
105 auto days =
106 static_cast<Integer>(std::lround(eventFraction * dayCount_ / yearFraction_));
107 Date eventDate = start_ + days*Days;
108 if(eventDate<=end_)
109 {
110 path.emplace_back(eventDate, generateBeta());
111 }
112 else break;
113 eventFraction = exponential_(rng_);
114 }
115 return true;
116 }
117
119 Real years,
120 Real mean,
121 Real stdDev)
122 : maxLoss_(maxLoss), lambda_(1.0/years) {
123 QL_REQUIRE(mean<maxLoss, "Mean "<<mean<<"of the loss distribution must be less than the maximum loss "<<maxLoss);
124 Real normalizedMean = mean/maxLoss;
125 Real normalizedVar = stdDev*stdDev/(maxLoss*maxLoss);
126 QL_REQUIRE(normalizedVar<normalizedMean*(1.0-normalizedMean), "Standard deviation of "<<stdDev<<" is impossible to achieve in gamma distribution with mean "<<mean);
127 Real nu = normalizedMean*(1.0-normalizedMean)/normalizedVar - 1.0;
128 alpha_=normalizedMean*nu;
129 beta_=(1.0-normalizedMean)*nu;
130 }
131
132 ext::shared_ptr<CatSimulation> BetaRisk::newSimulation(const Date& start, const Date& end) const {
133 return ext::make_shared<BetaRiskSimulation>(start, end, maxLoss_, lambda_, alpha_, beta_);
134 }
135}
act/act day counters
classes that encapsulate catastrophe risk
Actual/Actual day count.
BetaRisk(Real maxLoss, Real years, Real mean, Real stdDev)
Definition: catrisk.cpp:118
ext::shared_ptr< CatSimulation > newSimulation(const Date &start, const Date &end) const override
Definition: catrisk.cpp:132
std::gamma_distribution< Real > gammaAlpha_
Definition: catrisk.hpp:110
std::exponential_distribution< Real > exponential_
Definition: catrisk.hpp:109
bool nextPath(std::vector< std::pair< Date, Real > > &path) override
Definition: catrisk.cpp:99
BetaRiskSimulation(Date start, Date end, Real maxLoss, Real lambda, Real alpha, Real beta)
Definition: catrisk.cpp:80
std::gamma_distribution< Real > gammaBeta_
Definition: catrisk.hpp:111
Concrete date class.
Definition: date.hpp:125
Month month() const
Definition: date.cpp:82
Year year() const
Definition: date.cpp:93
Day dayOfMonth() const
Definition: date.hpp:400
day counter class
Definition: daycounter.hpp:44
Date::serial_type dayCount(const Date &, const Date &) const
Returns the number of days between two dates.
Definition: daycounter.hpp:122
Time yearFraction(const Date &, const Date &, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date()) const
Returns the period between two dates as a fraction of year.
Definition: daycounter.hpp:128
ext::shared_ptr< std::vector< std::pair< Date, Real > > > events_
Definition: catrisk.hpp:85
EventSet(ext::shared_ptr< std::vector< std::pair< Date, Real > > > events, Date eventsStart, Date eventsEnd)
Definition: catrisk.cpp:71
ext::shared_ptr< CatSimulation > newSimulation(const Date &start, const Date &end) const override
Definition: catrisk.cpp:76
EventSetSimulation(ext::shared_ptr< std::vector< std::pair< Date, Real > > > events, Date eventsStart, Date eventsEnd, Date start, Date end)
Definition: catrisk.cpp:28
ext::shared_ptr< std::vector< std::pair< Date, Real > > > events_
Definition: catrisk.hpp:65
bool nextPath(std::vector< std::pair< Date, Real > > &path) override
Definition: catrisk.cpp:48
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
QL_REAL Real
real number
Definition: types.hpp:50
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Definition: any.hpp:35
Real years(const Period &p)
Definition: period.cpp:279
Real days(const Period &p)
Definition: period.cpp:330
STL namespace.
Real beta
Definition: sabr.cpp:200
Real nu
Definition: sabr.cpp:200
Real alpha
Definition: sabr.cpp:200