RandLib 2.0.0
Probability distributions, fitting, and heavy-tail tools for C++20
Loading...
Searching...
No Matches
cauchy.hpp
Go to the documentation of this file.
1#pragma once
2
7
8namespace randlib
9{
10
24template <typename RealType>
25class CauchyRand final : public StableDistribution<RealType>
26{
27 RealType variate(detail::EngineRef g) const override { return (*this)(g); }
28
29public:
30 CauchyRand(double location = 0, double scale = 1);
31 [[nodiscard]] std::string Name() const override;
32 [[nodiscard]] Support SupportType() const override { return Support::Infinite; }
33 [[nodiscard]] RealType MinValue() const override { return -INFINITY; }
34 [[nodiscard]] RealType MaxValue() const override { return INFINITY; }
35
36 [[nodiscard]] double f(const RealType &x) const override;
37 [[nodiscard]] double F(const RealType &x) const override;
38 [[nodiscard]] double S(const RealType &x) const override;
39 template <std::uniform_random_bit_generator G>
40 [[nodiscard]] RealType operator()(G &g) const;
41
42 template <std::uniform_random_bit_generator G>
43 [[nodiscard]] static RealType StandardVariate(G &g);
44
45private:
46 RealType quantileImpl(double p) const override;
47 RealType quantileImpl1m(double p) const override;
48
49 std::complex<double> CFImpl(double t) const override;
50
51public:
52 [[nodiscard]] long double Entropy() const;
53};
54
55} // namespace randlib
56
58
59namespace randlib
60{
61
62template <typename RealType>
63CauchyRand<RealType>::CauchyRand(double location, double scale) : StableDistribution<RealType>(1, 0, scale, location)
64{
65}
66
67template <typename RealType>
68std::string CauchyRand<RealType>::Name() const
69{
70 return "Cauchy(" + this->toStringWithPrecision(this->GetLocation()) + ", " +
71 this->toStringWithPrecision(this->GetScale()) + ")";
72}
73
74template <typename RealType>
75double CauchyRand<RealType>::f(const RealType &x) const
76{
77 return this->pdfCauchy(x);
78}
79
80template <typename RealType>
81double CauchyRand<RealType>::F(const RealType &x) const
82{
83 return this->cdfCauchy(x);
84}
85
86template <typename RealType>
87double CauchyRand<RealType>::S(const RealType &x) const
88{
89 return this->cdfCauchyCompl(x);
90}
91
92template <typename RealType>
93template <std::uniform_random_bit_generator G>
95{
96 return this->mu + this->gamma * StandardVariate(g);
97}
98
99template <typename RealType>
100template <std::uniform_random_bit_generator G>
102{
103 double x, y;
104 do {
105 x = 2 * detail::uniform01<RealType>(g) - 1;
106 y = 2 * detail::uniform01<RealType>(g) - 1;
107 } while (y == 0.0 || x * x + y * y > 1.0);
108 return x / y;
109}
110
111template <typename RealType>
112std::complex<double> CauchyRand<RealType>::CFImpl(double t) const
113{
114 return this->cfCauchy(t);
115}
116
117template <typename RealType>
118RealType CauchyRand<RealType>::quantileImpl(double p) const
119{
120 return this->quantileCauchy(p);
121}
122
123template <typename RealType>
124RealType CauchyRand<RealType>::quantileImpl1m(double p) const
125{
126 return this->quantileCauchy1m(p);
127}
128
129template <typename RealType>
131{
132 return 2 * randlib::consts::ln2 + this->logGamma + randlib::consts::lnpi;
133}
134
135} // namespace randlib
The CauchyRand class Cauchy distribution.
Definition cauchy.hpp:26
double S(const RealType &x) const override
Definition cauchy.hpp:87
Support SupportType() const override
Definition cauchy.hpp:32
static RealType StandardVariate(G &g)
Definition cauchy.hpp:101
CauchyRand(double location=0, double scale=1)
Definition cauchy.hpp:63
RealType MaxValue() const override
Definition cauchy.hpp:34
RealType MinValue() const override
Definition cauchy.hpp:33
long double Entropy() const
Definition cauchy.hpp:130
std::string Name() const override
Definition cauchy.hpp:68
double f(const RealType &x) const override
Definition cauchy.hpp:75
RealType operator()(G &g) const
Definition cauchy.hpp:94
double F(const RealType &x) const override
Definition cauchy.hpp:81
The StableDistribution class Abstract class for Stable distribution.
Definition stable.hpp:74
The EngineRef class Non-owning, type-erased reference to any uniform random bit generator.
Definition engine/uniform.hpp:116
constexpr long double ln2
Definition constants.hpp:18
constexpr long double lnpi
Definition constants.hpp:21
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
@ Infinite
(-∞, ∞)