30template <
typename RealType>
42 [[nodiscard]] std::string
Name()
const override;
43 void SetShapes(
double shape1,
double shape2);
44 [[nodiscard]]
inline double GetAlpha() const noexcept {
return alpha; }
45 [[nodiscard]]
inline double GetBeta() const noexcept {
return beta; }
48 [[nodiscard]] RealType
MinValue()
const override {
return 0; }
49 [[nodiscard]] RealType
MaxValue()
const override {
return INFINITY; }
51 [[nodiscard]]
double f(
const RealType &x)
const override;
52 [[nodiscard]]
double logf(
const RealType &x)
const override;
53 [[nodiscard]]
double F(
const RealType &x)
const override;
54 [[nodiscard]]
double S(
const RealType &x)
const override;
57 RealType fromBetaVariate(
const RealType &betaVar)
const;
60 template <std::uniform_random_bit_generator G>
63 [[nodiscard]]
long double Mean()
const override;
64 [[nodiscard]]
long double Variance()
const override;
65 [[nodiscard]] RealType
Median()
const override;
66 [[nodiscard]] RealType
Mode()
const override;
67 [[nodiscard]]
long double Skewness()
const override;
71 RealType quantileImpl(
double p)
const override;
72 RealType quantileImpl1m(
double p)
const override;
74 std::complex<double> CFImpl(
double t)
const override;
80 [[nodiscard]]
inline double GetBetaFunction()
const {
return B.GetBetaFunction(); }
111template <
typename RealType>
114 SetShapes(shape1, shape2);
117template <
typename RealType>
120 return "Beta Prime(" + this->toStringWithPrecision(GetAlpha()) +
", " + this->toStringWithPrecision(GetBeta()) +
124template <
typename RealType>
127 if (!(shape1 > 0 && shape2 > 0))
128 throw std::invalid_argument(
"Beta-prime distribution: shapes should be positive");
129 B.SetShapes(shape1, shape2);
130 alpha = B.GetAlpha();
134template <
typename RealType>
141 return 1.0 / GetBetaFunction();
142 return (alpha > 1) ? 0.0 : INFINITY;
144 return std::exp(logf(x));
147template <
typename RealType>
154 return -GetLogBetaFunction();
155 return (alpha > 1) ? -INFINITY : INFINITY;
157 double y = (alpha - 1) * std::log(x);
158 y -= (alpha + beta) * std::log1pl(x);
159 return y - GetLogBetaFunction();
162template <
typename RealType>
170 return math::ibeta(x / (1.0 + x), alpha, beta, GetLogBetaFunction(), std::log(x) - std::log1pl(x), -std::log1pl(x));
173template <
typename RealType>
181 double y = 1.0 / (1.0 + x);
182 return math::ibeta(y, beta, alpha, GetLogBetaFunction(), -std::log1pl(x), std::log(x) - std::log1pl(x));
185template <
typename RealType>
189 return betaVar / (1.0 - betaVar);
190 RealType logVar = std::log(betaVar), log1mVar = std::log1p(-betaVar);
191 return std::exp(logVar - log1mVar);
194template <
typename RealType>
195template <std::uniform_random_bit_generator G>
199 return fromBetaVariate(x);
202template <
typename RealType>
206 for (RealType &var : outputData)
207 var = fromBetaVariate(var);
210template <
typename RealType>
213 return (beta > 1) ? alpha / (beta - 1) : INFINITY;
216template <
typename RealType>
221 double betam1 = beta - 1;
222 double numerator = alpha * (alpha + betam1);
223 double denominator = (betam1 - 1) * betam1 * betam1;
224 return numerator / denominator;
227template <
typename RealType>
230 return (alpha == beta) ? 1.0 : quantileImpl(0.5);
233template <
typename RealType>
236 return (alpha < 1) ? 0 : (alpha - 1) / (beta + 1);
239template <
typename RealType>
244 long double aux = alpha + beta - 1;
245 long double skewness = (beta - 2) / (alpha * aux);
246 skewness = std::sqrt(skewness);
249 return aux * skewness / (beta - 3);
252template <
typename RealType>
257 long double betam1 = beta - 1;
258 long double numerator = betam1 * betam1 * (beta - 2) / (alpha * (alpha + betam1));
259 numerator += 5 * beta - 11;
260 long double denominator = (beta - 3) * (beta - 4);
261 return 6 * numerator / denominator;
264template <
typename RealType>
272template <
typename RealType>
273RealType BetaPrimeRand<RealType>::quantileImpl1m(
double p)
const
275 double y = B.Quantile1m(p);
279template <
typename RealType>
280std::complex<double> BetaPrimeRand<RealType>::CFImpl(
double t)
const
282 return this->integrateCharacteristicFunction(t);
285template <
typename RealType>
288 if (!this->allElementsArePositive(sample))
290 long double lnG = this->GetSampleLogMean(sample) - B.GetSampleLog1pMeanNorm(sample);
291 long double mean = 0.5;
294 for (
const RealType &var : sample)
295 mean += var / (1.0 + var);
296 mean /= sample.size();
298 B.FitAlpha(lnG, mean);
299 SetShapes(B.GetAlpha(), beta);
302template <
typename RealType>
305 if (!this->allElementsArePositive(sample))
307 long double lnG1m = -B.GetSampleLog1pMeanNorm(sample);
308 long double mean = 0.5;
311 for (
const RealType &var : sample)
312 mean += var / (1.0 + var);
313 mean /= sample.size();
315 B.FitBeta(lnG1m, mean);
316 SetShapes(alpha, B.GetBeta());
319template <
typename RealType>
322 if (!this->allElementsArePositive(sample))
324 long double lnG1m = -B.GetSampleLog1pMeanNorm(sample);
325 long double lnG = this->GetSampleLogMean(sample) + lnG1m;
326 long double m = 0.0, v = 0.0;
327 int n = sample.size();
328 for (
int i = 0; i < n; ++i) {
329 double x = sample[i] / (1.0 + sample[i]);
334 B.FitShapes(lnG, lnG1m, m, v / n);
335 SetShapes(B.GetAlpha(), B.GetBeta());
The BetaPrimeRand class Beta-prime distribution.
Definition beta_prime.hpp:32
RealType MinValue() const override
Definition beta_prime.hpp:48
void FitBeta(std::span< const RealType > sample)
fit β by maximum-likelihood
Definition beta_prime.hpp:303
long double Skewness() const override
Definition beta_prime.hpp:240
double GetAlpha() const noexcept
Definition beta_prime.hpp:44
void SetShapes(double shape1, double shape2)
Definition beta_prime.hpp:125
double GetBetaFunction() const
Definition beta_prime.hpp:80
RealType operator()(G &g) const
Definition beta_prime.hpp:196
void FitAlpha(std::span< const RealType > sample)
fit α by maximum-likelihood
Definition beta_prime.hpp:286
long double ExcessKurtosis() const override
Definition beta_prime.hpp:253
void Fit(std::span< const RealType > sample)
fit shapes by maximum-likelihood
Definition beta_prime.hpp:320
RealType Mode() const override
Mode is the value, which has the largest probability to happen, or, in the case of continuous distrib...
Definition beta_prime.hpp:234
std::string Name() const override
Definition beta_prime.hpp:118
RealType Median() const override
Definition beta_prime.hpp:228
double f(const RealType &x) const override
Definition beta_prime.hpp:135
BetaPrimeRand(double shape1=1, double shape2=1)
Definition beta_prime.hpp:112
long double Mean() const override
Definition beta_prime.hpp:211
Support SupportType() const override
Definition beta_prime.hpp:47
double S(const RealType &x) const override
Definition beta_prime.hpp:174
double F(const RealType &x) const override
Definition beta_prime.hpp:163
double GetBeta() const noexcept
Definition beta_prime.hpp:45
long double Variance() const override
Definition beta_prime.hpp:217
RealType MaxValue() const override
Definition beta_prime.hpp:49
double GetLogBetaFunction() const
Definition beta_prime.hpp:85
double logf(const RealType &x) const override
Definition beta_prime.hpp:148
The BetaRand class Beta distribution.
Definition continuous/beta.hpp:246
The ContinuousDistribution class Abstract class for all continuous distributions.
Definition continuous.hpp:22
RealType quantileImpl(double p, RealType initValue) const override
Definition continuous.hpp:140
RealType quantileImpl1m(double p, RealType initValue) const override
Definition continuous.hpp:152
void Sample(std::span< T > outputData) const
Fills the output with variates drawn with the engine of this object.
Definition distribution.hpp:139
T Quantile(double p) const
Definition univariate.hpp:307
The EngineRef class Non-owning, type-erased reference to any uniform random bit generator.
Definition engine/uniform.hpp:116
#define RANDLIB_DETAIL_SUPPRESS_DEPRECATED_END
Definition fwd.hpp:26
#define RANDLIB_DETAIL_SUPPRESS_DEPRECATED_BEGIN
Definition fwd.hpp:25
#define RANDLIB_DEPRECATED_FIT_MEMBER
Definition fwd.hpp:9
Real ibeta(Real x, std::type_identity_t< Real > a, std::type_identity_t< Real > b, std::type_identity_t< Real > logBetaFun, std::type_identity_t< Real > logX, std::type_identity_t< Real > log1mX)
Fast calculation of regularized beta function, using precalculated values.
Definition math/beta.hpp:250
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