QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
levyflightdistribution.hpp
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) 2015 Andres Hernandez
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 levyflightdistribution.hpp
21 \brief Levy Flight, aka Pareto Type I, distribution
22*/
23
24#ifndef quantlib_levy_flight_distribution_hpp
25#define quantlib_levy_flight_distribution_hpp
26
27#include <ql/types.hpp>
28#include <ql/errors.hpp>
29#include <random>
30
31namespace QuantLib {
32
33 //! Levy Flight distribution
34 /*! The levy flight distribution is a random distribution with
35 the following form:
36 \f[
37 p(x) = \frac{\alpha x_m^{\alpha}}{x^{\alpha+1}}
38 \f]
39 with support over \f$ x \in [x_m, \infty) \f$
40 and the parameter \f$ \alpha > 0 \f$.
41
42 Levy Flight is normally defined as \f$ x_m = 1 \f$ and \f$ 0 <
43 \alpha < 2 \f$, which is where \f$ p(x) \f$ has an infinite
44 variance. However, the more general version, known as Pareto
45 Type I, is well defined for \f$ \alpha > 2 \f$, so the current
46 implementation does not restrict \f$ \alpha \f$ to be smaller
47 than 2.
48 */
50 {
51 public:
53 {
54 public:
55 /*! Constructs parameters with a given xm and alpha
56 Requires: alpha > 0
57 */
58 param_type(Real xm = 1.0, Real alpha = 1.0)
59 : xm_(xm), alpha_(alpha) { QL_REQUIRE(alpha_ > 0.0, "alpha must be larger than 0"); }
60
61 //! Returns the xm parameter of the distribution
62 Real xm() const { return xm_; }
63
64 //! Returns the alpha parameter of the distribution
65 Real alpha() const { return alpha_; }
66
67 private:
70 };
71
72 //! \name Constructors
73 //@{
74 /*! Constructs a LevyFlightDistribution with a given xm and alpha
75 Requires: alpha > 0
76 */
77 explicit LevyFlightDistribution(Real xm = 1.0, Real alpha = 1.0)
78 : xm_(xm), alpha_(alpha) { QL_REQUIRE(alpha_ > 0.0, "alpha must be larger than 0"); }
79
80 //!Constructs a LevyFlightDistribution from its parameters
81 explicit LevyFlightDistribution(const param_type& parm)
82 : xm_(parm.xm()), alpha_(parm.alpha()) {}
83
84 // compiler-generated copy ctor and assignment operator are fine
85 //@}
86
87 //! \name Inspectors
88 //@{
89 //! Returns the xm parameter of the distribution
90 Real xm() const { return xm_; }
91
92 //! Returns the alpha parameter of the distribution
93 Real alpha() const { return alpha_; }
94
95 //! Returns the smallest value that the distribution can produce
97 { return xm_; }
98 //! Returns the largest value that the distribution can produce
100 { return QL_MAX_REAL; }
101
102 //! Returns the parameters of the distribution
103 param_type param() const { return {xm_, alpha_}; }
104 //@}
105
106 //! Sets the parameters of the distribution
107 void param(const param_type& parm) {
108 xm_ = parm.xm();
109 alpha_ = parm.alpha();
110 }
111
112 /*! Effects: Subsequent uses of the distribution do not depend
113 on values produced by any engine prior to invoking reset.
114 */
115 void reset() { }
116
117 //! Returns the value of the pdf for x
119 using std::pow;
120 if(x < xm_) return 0.0;
121 return alpha_*pow(xm_/x, alpha_)/x;
122 }
123
124 /*! Returns a random variate distributed according to the
125 levy flight distribution.
126 */
127 template<class Engine>
128 Real operator()(Engine& eng) const {
129 using std::pow;
130 return xm_*pow(std::uniform_real_distribution<Real>(0.0, 1.0)(eng), -1.0/alpha_);
131 }
132
133 /*! Returns a random variate distributed according to the
134 levy flight with parameters specified by parm
135 */
136 template<class Engine>
137 Real operator()(Engine& eng, const param_type& parm) const {
138 return LevyFlightDistribution (parm)(eng);
139 }
140
141 private:
144 };
145
146}
147
148#endif
Real xm() const
Returns the xm parameter of the distribution.
Real alpha() const
Returns the alpha parameter of the distribution.
param_type param() const
Returns the parameters of the distribution.
Real operator()(Real x) const
Returns the value of the pdf for x.
void param(const param_type &parm)
Sets the parameters of the distribution.
Real operator()(Engine &eng, const param_type &parm) const
LevyFlightDistribution(const param_type &parm)
Constructs a LevyFlightDistribution from its parameters.
Real xm() const
Returns the xm parameter of the distribution.
LevyFlightDistribution(Real xm=1.0, Real alpha=1.0)
Real max BOOST_PREVENT_MACRO_SUBSTITUTION() const
Returns the largest value that the distribution can produce.
Real alpha() const
Returns the alpha parameter of the distribution.
Real min BOOST_PREVENT_MACRO_SUBSTITUTION() const
Returns the smallest value that the distribution can produce.
Classes and functions for error handling.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
#define QL_MAX_REAL
Definition: qldefines.hpp:176
QL_REAL Real
real number
Definition: types.hpp:50
Definition: any.hpp:35
Custom types.