14template <
typename RealType>
24 [[nodiscard]]
double supportLog(
double z)
const
26 const double product = shape * z;
27 return std::fabs(product) < 0.5 ? std::log1p(product) : std::log(std::fma(shape, z, 1.0));
33 SetParameters(shapeParameter, scaleParameter, locationParameter);
36 void SetParameters(
double newShape,
double newScale,
double newLocation)
38 if (!std::isfinite(newShape))
39 throw std::invalid_argument(
"Generalized Pareto distribution: shape should be finite");
40 if (!(newScale > 0) || !std::isfinite(newScale))
41 throw std::invalid_argument(
"Generalized Pareto distribution: scale should be positive and finite");
42 if (!std::isfinite(newLocation))
43 throw std::invalid_argument(
"Generalized Pareto distribution: location should be finite");
46 location = newLocation;
47 logScale = std::log(scale);
50 [[nodiscard]]
double GetShape() const noexcept {
return shape; }
51 [[nodiscard]]
double GetScale() const noexcept {
return scale; }
52 [[nodiscard]]
double GetLocation() const noexcept {
return location; }
54 [[nodiscard]] std::string
Name()
const override
64 [[nodiscard]] RealType
MinValue()
const override {
return static_cast<RealType
>(location); }
65 [[nodiscard]] RealType
MaxValue()
const override
67 return shape < 0 ? static_cast<RealType>(location - scale / shape) : std::numeric_limits<RealType>::infinity();
70 [[nodiscard]]
double logf(
const RealType &x)
const override
72 if (x < location || (shape < 0 && x >
MaxValue()))
74 const double z = (x - location) / scale;
77 const double support = std::fma(shape, z, 1.0);
87 return -logScale - (1 / shape + 1) * supportLog(z);
90 [[nodiscard]]
double f(
const RealType &x)
const override {
return std::exp(
logf(x)); }
92 [[nodiscard]]
double S(
const RealType &x)
const override
98 const double z = (x - location) / scale;
99 return shape == 0 ? std::exp(-z) : std::exp(-supportLog(z) / shape);
102 [[nodiscard]]
double F(
const RealType &x)
const override
108 const double z = (x - location) / scale;
109 const double logSurvival = shape == 0 ? -z : -supportLog(z) / shape;
110 return -std::expm1(logSurvival);
113 template <std::uniform_random_bit_generator G>
118 return static_cast<RealType
>(location + scale * exponential);
119 return static_cast<RealType
>(location + scale * std::expm1(shape * exponential) / shape);
122 [[nodiscard]]
long double Mean()
const override {
return shape < 1 ? location + scale / (1 - shape) : INFINITY; }
124 [[nodiscard]]
long double Variance()
const override
128 const long double denominator = (1 - shape) * (1 - shape) * (1 - 2 * shape);
129 return scale * scale / denominator;
132 [[nodiscard]] RealType
Median()
const override {
return quantileImpl(0.5); }
134 [[nodiscard]] RealType
Mode()
const override {
return shape < -1 ?
MaxValue() :
static_cast<RealType
>(location); }
136 [[nodiscard]]
long double Skewness()
const override
138 if (shape >= 1.0 / 3)
140 return 2 * (1 + shape) * std::sqrt(1 - 2 * shape) / (1 - 3 * shape);
147 const long double numerator = 3 * (1 - 2 * shape) * (2 * shape * shape + shape + 3);
148 return numerator / ((1 - 3 * shape) * (1 - 4 * shape)) - 3;
151 [[nodiscard]]
long double Entropy()
const {
return logScale + shape + 1; }
154 RealType quantileImpl(
double p)
const override
156 const double exponential = -std::log1p(-p);
158 return static_cast<RealType
>(location + scale * exponential);
159 return static_cast<RealType
>(location + scale * std::expm1(shape * exponential) / shape);
162 RealType quantileImpl1m(
double p)
const override
164 const double exponential = -std::log(p);
166 return static_cast<RealType
>(location + scale * exponential);
167 return static_cast<RealType
>(location + scale * std::expm1(shape * exponential) / shape);
The ContinuousDistribution class Abstract class for all continuous distributions.
Definition continuous.hpp:22
Definition generalized_pareto.hpp:16
long double Entropy() const
Definition generalized_pareto.hpp:151
RealType MinValue() const override
Definition generalized_pareto.hpp:64
double GetScale() const noexcept
Definition generalized_pareto.hpp:51
double logf(const RealType &x) const override
Definition generalized_pareto.hpp:70
double F(const RealType &x) const override
Definition generalized_pareto.hpp:102
RealType Median() const override
Definition generalized_pareto.hpp:132
double f(const RealType &x) const override
Definition generalized_pareto.hpp:90
double GetLocation() const noexcept
Definition generalized_pareto.hpp:52
RealType operator()(G &g) const
Definition generalized_pareto.hpp:114
double S(const RealType &x) const override
Definition generalized_pareto.hpp:92
long double Mean() const override
Definition generalized_pareto.hpp:122
long double Variance() const override
Definition generalized_pareto.hpp:124
Support SupportType() const override
Definition generalized_pareto.hpp:60
long double ExcessKurtosis() const override
Definition generalized_pareto.hpp:143
RealType MaxValue() const override
Definition generalized_pareto.hpp:65
void SetParameters(double newShape, double newScale, double newLocation)
Definition generalized_pareto.hpp:36
double GetShape() const noexcept
Definition generalized_pareto.hpp:50
std::string Name() const override
Definition generalized_pareto.hpp:54
long double Skewness() const override
Definition generalized_pareto.hpp:136
GeneralizedParetoRand(double shapeParameter=0, double scaleParameter=1, double locationParameter=0)
Definition generalized_pareto.hpp:31
RealType Mode() const override
Mode is the value, which has the largest probability to happen, or, in the case of continuous distrib...
Definition generalized_pareto.hpp:134
std::string toStringWithPrecision(const double a_value, const int n=6) const
Definition distribution.hpp:181
The EngineRef class Non-owning, type-erased reference to any uniform random bit generator.
Definition engine/uniform.hpp:116
Real standardNormal(G &g)
Ziggurat algorithm of Marsaglia and Tsang with 256 layers and an explicit sign bit.
Definition variates.hpp:45
Definition bivariate_normal.hpp:10
Support
Shape of the support of a univariate distribution.
Definition univariate.hpp:15