22template <
typename RealType>
36 [[nodiscard]] std::string
Name()
const override;
38 [[nodiscard]] RealType
MinValue()
const override {
return sigma; }
39 [[nodiscard]] RealType
MaxValue()
const override {
return INFINITY; }
43 [[nodiscard]]
inline double GetShape() const noexcept {
return alpha; }
44 [[nodiscard]]
inline double GetScale() const noexcept {
return sigma; }
45 [[nodiscard]]
inline double GetLogShape() const noexcept {
return logAlpha; }
46 [[nodiscard]]
inline double GetLogScale() const noexcept {
return logSigma; }
48 [[nodiscard]]
double f(
const RealType &x)
const override;
49 [[nodiscard]]
double logf(
const RealType &x)
const override;
50 [[nodiscard]]
double F(
const RealType &x)
const override;
51 [[nodiscard]]
double S(
const RealType &x)
const override;
54 template <std::uniform_random_bit_generator G>
55 static RealType variateForAlphaEqualOne(G &g);
56 template <std::uniform_random_bit_generator G>
57 static RealType variateForAlphaEqualTwo(G &g);
58 template <std::uniform_random_bit_generator G>
59 static RealType variateForGeneralAlpha(
double shape, G &g);
62 template <std::uniform_random_bit_generator G>
65 template <std::uniform_random_bit_generator G>
68 [[nodiscard]]
long double Mean()
const override;
69 [[nodiscard]]
long double Variance()
const override;
70 [[nodiscard]] RealType
Median()
const override;
71 [[nodiscard]] RealType
Mode()
const override;
72 [[nodiscard]]
long double Skewness()
const override;
76 RealType quantileImpl(
double p)
const override;
77 RealType quantileImpl1m(
double p)
const override;
78 std::complex<double> CFImpl(
double t)
const override;
81 [[nodiscard]]
long double Entropy()
const;
127template <
typename RealType>
134template <
typename RealType>
137 return "Pareto(" + this->toStringWithPrecision(GetShape()) +
", " + this->toStringWithPrecision(GetScale()) +
")";
140template <
typename RealType>
144 throw std::invalid_argument(
"Pareto distribution: shape should be positive");
146 logAlpha = std::log(alpha);
149template <
typename RealType>
153 throw std::invalid_argument(
"Pareto distribution: scale should be positive");
155 logSigma = std::log(sigma);
158template <
typename RealType>
161 return (x < sigma) ? 0.0 : std::exp(logf(x));
164template <
typename RealType>
169 double logX = std::log(x);
170 double y = logSigma - logX;
177template <
typename RealType>
180 return (x > sigma) ? -std::expm1l(alpha * std::log(sigma / x)) : 0.0;
183template <
typename RealType>
186 return (x > sigma) ? std::pow(sigma / x, alpha) : 1.0;
189template <
typename RealType>
190template <std::uniform_random_bit_generator G>
196template <
typename RealType>
197template <std::uniform_random_bit_generator G>
198RealType ParetoRand<RealType>::variateForAlphaEqualTwo(G &g)
203template <
typename RealType>
204template <std::uniform_random_bit_generator G>
205RealType ParetoRand<RealType>::variateForGeneralAlpha(
double shape, G &g)
210template <
typename RealType>
211template <std::uniform_random_bit_generator G>
214 return sigma * StandardVariate(alpha, g);
217template <
typename RealType>
218template <std::uniform_random_bit_generator G>
222 return variateForAlphaEqualOne(g);
224 return variateForAlphaEqualTwo(g);
225 return variateForGeneralAlpha(shape, g);
228template <
typename RealType>
232 for (RealType &var : outputData)
233 var = sigma * variateForAlphaEqualOne(g);
235 for (RealType &var : outputData)
236 var = sigma * variateForAlphaEqualTwo(g);
238 for (RealType &var : outputData)
239 var = sigma * variateForGeneralAlpha(alpha, g);
243template <
typename RealType>
246 return (alpha > 1) ? alpha * sigma / (alpha - 1) : INFINITY;
249template <
typename RealType>
253 long double var = sigma / (alpha - 1);
255 return alpha * var / (alpha - 2);
257 return (alpha > 1) ? INFINITY : NAN;
260template <
typename RealType>
266template <
typename RealType>
272template <
typename RealType>
277 double skewness = (alpha - 2.0) / alpha;
278 skewness = std::sqrt(skewness);
279 skewness *= (1 + alpha) / (alpha - 3);
280 return skewness + skewness;
283template <
typename RealType>
288 double numerator = alpha + 1;
293 double denominator = alpha * (alpha - 3) * (alpha - 4);
294 return 6.0 * numerator / denominator;
297template <
typename RealType>
300 double y = logSigma - std::log1pl(-p) / alpha;
304template <
typename RealType>
305RealType ParetoRand<RealType>::quantileImpl1m(
double p)
const
307 double y = logSigma - std::log(p) / alpha;
311template <
typename RealType>
312std::complex<double> ParetoRand<RealType>::CFImpl(
double t)
const
314 return this->integrateCharacteristicFunction(t);
317template <
typename RealType>
320 return logSigma - logAlpha + 1.0 / alpha + 1;
323template <
typename RealType>
326 if (!this->allElementsAreNotSmallerThan(sigma, sample))
328 this->LOWER_LIMIT_VIOLATION + this->toStringWithPrecision(sigma));
329 double invShape = this->GetSampleLogMean(sample) - logSigma;
332 "Possibly all the elements of the sample coincide with the lower boundary σ.");
333 double shape = 1.0 / invShape;
335 shape *= (1.0 - 1.0 / sample.size());
339template <
typename RealType>
342 RealType minVar = *std::min_element(sample.begin(), sample.end());
345 double scale = unbiased ? (1.0 - 1.0 / (sample.size() * alpha)) * minVar : minVar;
349template <
typename RealType>
352 RealType minVar = *std::min_element(sample.begin(), sample.end());
356 double logBiasedSigma = std::log(minVar);
357 double invShape = this->GetSampleLogMean(sample) - logBiasedSigma;
360 double shape = 1.0 / invShape;
362 double scale = minVar;
364 int n = sample.size();
365 shape *= 1.0 - 2.0 / n;
366 scale *= 1.0 - invShape / (n - 1);
372template <
typename RealType>
376 if (!this->allElementsAreNotSmallerThan(sigma, sample))
378 this->LOWER_LIMIT_VIOLATION + this->toStringWithPrecision(sigma));
379 int n = sample.size();
380 double newShape = priorDistribution.
GetShape() + n;
381 double newRate = priorDistribution.
GetRate() + n * (this->GetSampleLogMean(sample) - logSigma);
383 SetShape(MAP ? posteriorDistribution.Mode() : posteriorDistribution.Mean());
384 return posteriorDistribution;
The ContinuousDistribution class Abstract class for all continuous distributions.
Definition continuous.hpp:22
The GammaDistribution class Abstract class for Gamma distribution.
Definition continuous/gamma.hpp:31
double GetShape() const noexcept
Definition continuous/gamma.hpp:78
double GetRate() const noexcept
Definition continuous/gamma.hpp:86
The GammaRand class Gamma distribution.
Definition continuous/gamma.hpp:295
The ParetoRand class Pareto distribution.
Definition pareto.hpp:24
long double Variance() const override
Definition pareto.hpp:250
double S(const RealType &x) const override
Definition pareto.hpp:184
Support SupportType() const override
Definition pareto.hpp:37
double f(const RealType &x) const override
Definition pareto.hpp:159
void Fit(std::span< const RealType > sample, bool unbiased=false)
Fit parameters to maximum-likelihood estimators or to UMVU if unbiased == true.
Definition pareto.hpp:350
double GetScale() const noexcept
Definition pareto.hpp:44
void FitShape(std::span< const RealType > sample, bool unbiased=false)
Fit α to maximum-likelihood estimator or to UMVU if unbiased == true.
Definition pareto.hpp:324
long double Skewness() const override
Definition pareto.hpp:273
RealType MinValue() const override
Definition pareto.hpp:38
double GetShape() const noexcept
Definition pareto.hpp:43
GammaRand< RealType > FitShapeBayes(std::span< const RealType > sample, const GammaDistribution< RealType > &priorDistribution, bool MAP=false)
Fit α, using bayesian inference.
Definition pareto.hpp:373
double GetLogScale() const noexcept
Definition pareto.hpp:46
long double Mean() const override
Definition pareto.hpp:244
RealType operator()(G &g) const
Definition pareto.hpp:212
RealType Mode() const override
Mode is the value, which has the largest probability to happen, or, in the case of continuous distrib...
Definition pareto.hpp:267
std::string Name() const override
Definition pareto.hpp:135
ParetoRand(double shape=1, double scale=1)
Definition pareto.hpp:128
double logf(const RealType &x) const override
Definition pareto.hpp:165
void FitScale(std::span< const RealType > sample, bool unbiased=false)
Fit σ to maximum-likelihood estimator or to UMVU if unbiased == true.
Definition pareto.hpp:340
long double Entropy() const
Definition pareto.hpp:318
RealType Median() const override
Definition pareto.hpp:261
void SetShape(double shape)
Definition pareto.hpp:141
void SetScale(double scale)
Definition pareto.hpp:150
RealType MaxValue() const override
Definition pareto.hpp:39
double F(const RealType &x) const override
Definition pareto.hpp:178
static RealType StandardVariate(double shape, G &g)
Definition pareto.hpp:219
double GetLogShape() const noexcept
Definition pareto.hpp:45
long double ExcessKurtosis() const override
Definition pareto.hpp:284
The EngineRef class Non-owning, type-erased reference to any uniform random bit generator.
Definition engine/uniform.hpp:116
#define RANDLIB_DEPRECATED_FIT_MEMBER
Definition fwd.hpp:9
constexpr long double ln2
Definition constants.hpp:18
Real standardNormal(G &g)
Ziggurat algorithm of Marsaglia and Tsang with 256 layers and an explicit sign bit.
Definition variates.hpp:45
bool areClose(RealType a, RealType b, RealType eps=1e-6)
Definition functions.hpp:38
Definition bivariate_normal.hpp:10
@ WrongSample
the sample could not have been drawn from this distribution
Support
Shape of the support of a univariate distribution.
Definition univariate.hpp:15