19template <
typename RealType>
27 double logk_lambda = 0;
32 [[nodiscard]] std::string
Name()
const override;
34 [[nodiscard]] RealType
MinValue()
const override {
return 0; }
35 [[nodiscard]] RealType
MaxValue()
const override {
return INFINITY; }
38 [[nodiscard]]
inline double GetScale() const noexcept {
return lambda; }
39 [[nodiscard]]
inline double GetShape() const noexcept {
return k; }
41 [[nodiscard]]
double f(
const RealType &x)
const override;
42 [[nodiscard]]
double logf(
const RealType &x)
const override;
43 [[nodiscard]]
double F(
const RealType &x)
const override;
44 [[nodiscard]]
double S(
const RealType &x)
const override;
45 template <std::uniform_random_bit_generator G>
48 [[nodiscard]]
long double Mean()
const override;
49 [[nodiscard]]
long double Variance()
const override;
50 [[nodiscard]] RealType
Median()
const override;
51 [[nodiscard]] RealType
Mode()
const override;
52 [[nodiscard]]
long double Skewness()
const override;
56 RealType quantileImpl(
double p)
const override;
57 RealType quantileImpl1m(
double p)
const override;
59 std::complex<double> CFImpl(
double t)
const override;
62 [[nodiscard]]
long double Entropy()
const;
70 double getNorm(std::span<const RealType>
sample)
const;
99template <
typename RealType>
102 SetParameters(scale, shape);
105template <
typename RealType>
108 return "Weibull(" + this->toStringWithPrecision(GetScale()) +
", " + this->toStringWithPrecision(GetShape()) +
")";
111template <
typename RealType>
115 throw std::invalid_argument(
"Weibull distribution: scale should be positive");
117 throw std::invalid_argument(
"Weibull distribution: shape should be positive");
121 logk_lambda = std::log(k / lambda);
124template <
typename RealType>
132 return (k > 1) ? 0.0 : INFINITY;
134 return std::exp(this->logf(x));
137template <
typename RealType>
145 return (k > 1) ? -INFINITY : INFINITY;
147 double xAdj = x / lambda;
148 double xAdjPow = std::pow(xAdj, k);
149 return logk_lambda + (k - 1) * std::log(xAdj) - xAdjPow;
152template <
typename RealType>
155 return (x > 0.0) ? -std::expm1l(-std::pow(x / lambda, k)) : 0.0;
158template <
typename RealType>
161 return (x > 0.0) ? std::exp(-std::pow(x / lambda, k)) : 1.0;
164template <
typename RealType>
165template <std::uniform_random_bit_generator G>
171template <
typename RealType>
174 return lambda * std::tgammal(1 + kInv);
177template <
typename RealType>
180 double res = std::tgammal(1 + kInv);
182 res += std::tgammal(1 + kInv + kInv);
183 return lambda * lambda * res;
186template <
typename RealType>
192template <
typename RealType>
197 double y = std::log1pl(-kInv);
198 y = std::exp(kInv * y);
202template <
typename RealType>
205 long double mu = Mean();
206 long double var = Variance();
207 long double sigma = std::sqrt(var);
208 long double numerator = std::tgammal(1 + 3.0 * kInv);
209 numerator *= lambda * lambda * lambda;
210 numerator -= 3 * mu * var;
211 numerator -= mu * mu * mu;
212 long double denominator = var * sigma;
213 return numerator / denominator;
216template <
typename RealType>
219 long double mu = Mean();
220 long double var = Variance();
221 long double sigma = std::sqrt(var);
222 long double skewness = Skewness();
223 long double numerator = lambda * lambda;
224 numerator *= numerator;
225 numerator *= std::tgammal(1 + 4.0 * kInv);
226 numerator -= 4 * skewness * var * sigma * mu;
227 long double mu2 = mu * mu;
228 numerator -= 6 * mu2 * var;
229 numerator -= mu2 * mu2;
230 long double kurtosis = numerator / (var * var);
234template <
typename RealType>
237 double x = -std::log1pl(-p);
238 x = std::pow(x, kInv);
242template <
typename RealType>
243RealType WeibullRand<RealType>::quantileImpl1m(
double p)
const
245 double x = -std::log(p);
246 x = std::pow(x, kInv);
250template <
typename RealType>
251std::complex<double> WeibullRand<RealType>::CFImpl(
double t)
const
253 double lambdaT = lambda * t;
258 long double re = 0.0, im = 0.0;
259 long double addon = 0.0;
260 long double logLambdaT = std::log(lambdaT);
265 addon = n2 * logLambdaT;
266 addon += std::lgammal(1.0 + n2 / k);
267 addon -= std::lgammal(1.0 + n2);
268 addon = std::exp(addon);
269 re += (n & 1) ? -addon : addon;
275 int n2p1 = n + n + 1;
276 addon = n2p1 * logLambdaT;
277 addon += std::lgammal(1.0 + n2p1 / k);
278 addon -= std::lgammal(1.0 + n2p1);
279 addon = std::exp(addon);
280 im += (n & 1) ? -addon : addon;
283 return std::complex<double>(re, im);
288 [
this, t](
double x) {
289 if (x <= 0.0 || x > 1.0)
291 double xAdj = x / lambda;
292 double xAdjPow = std::pow(xAdj, k - 1);
293 double y = k / lambda * xAdjPow * std::expm1l(-xAdj * xAdjPow);
294 return std::cos(t * x) * y;
298 double re2 = this->ExpectedValue([t](
double x) {
return std::cos(t * x); }, 1.0, INFINITY);
301 [
this, t](
double x) {
304 return std::sin(t * x) * std::pow(x, k);
308 re3 /= std::pow(lambda, k);
310 double re = re1 + re2 + re3;
312 double im = this->ExpectedValue([t](
double x) {
return std::sin(t * x); }, 0.0, INFINITY);
314 return std::complex<double>(re, im);
317template <
typename RealType>
323template <
typename RealType>
327 RealType maxVar = *std::max_element(sample.begin(), sample.end());
328 for (
const RealType &var : sample) {
329 sum += std::pow(var / maxVar, k);
331 long double avg = sum / sample.size();
332 return maxVar * std::pow(avg, kInv);
335template <
typename RealType>
338 if (!this->allElementsArePositive(sample))
340 double knorm = getNorm(sample);
341 SetParameters(knorm, k);
344template <
typename RealType>
349 if (!this->allElementsArePositive(sample))
351 int n = sample.size();
352 double newShape = priorDistribution.
GetShape() + n;
353 double knorm = getNorm(sample);
354 double newRate = priorDistribution.
GetRate() + n * std::pow(knorm, k);
356 double powScale = MAP ? posteriorDistribution.
Mode() : posteriorDistribution.
Mean();
357 SetParameters(std::pow(powScale, kInv), k);
358 return posteriorDistribution;
The ContinuousDistribution class Abstract class for all continuous distributions.
Definition continuous.hpp:22
The InverseGammaRand class Inverse-Gamma distribution.
Definition inverse_gamma.hpp:23
double GetRate() const noexcept
Definition inverse_gamma.hpp:49
double GetShape() const noexcept
Definition inverse_gamma.hpp:48
long double Mean() const override
Definition inverse_gamma.hpp:146
RealType Mode() const override
Mode is the value, which has the largest probability to happen, or, in the case of continuous distrib...
Definition inverse_gamma.hpp:168
virtual void sample(detail::EngineRef g, std::span< T > outputData) const
Fills the output with variate(g); distributions with a faster bulk algorithm override it.
Definition distribution.hpp:66
virtual std::complex< double > CFImpl(double t) const
Definition univariate.hpp:365
The WeibullRand class Weibull distribution.
Definition weibull.hpp:21
double F(const RealType &x) const override
Definition weibull.hpp:153
long double ExcessKurtosis() const override
Definition weibull.hpp:217
void SetParameters(double scale, double shape)
Definition weibull.hpp:112
void FitScale(std::span< const RealType > sample)
Fit λ by maximum-likelihood.
Definition weibull.hpp:336
RealType operator()(G &g) const
Definition weibull.hpp:166
double logf(const RealType &x) const override
Definition weibull.hpp:138
double GetScale() const noexcept
Definition weibull.hpp:38
RealType Median() const override
Definition weibull.hpp:187
double f(const RealType &x) const override
Definition weibull.hpp:125
long double Variance() const override
Definition weibull.hpp:178
RealType MinValue() const override
Definition weibull.hpp:34
double GetShape() const noexcept
Definition weibull.hpp:39
long double Entropy() const
Definition weibull.hpp:318
RealType MaxValue() const override
Definition weibull.hpp:35
InverseGammaRand< RealType > FitScaleBayes(std::span< const RealType > sample, const InverseGammaRand< RealType > &priorDistribution, bool MAP=false)
Fit λ, using bayesian inference.
Definition weibull.hpp:345
long double Skewness() const override
Definition weibull.hpp:203
RealType Mode() const override
Mode is the value, which has the largest probability to happen, or, in the case of continuous distrib...
Definition weibull.hpp:193
long double Mean() const override
Definition weibull.hpp:172
std::string Name() const override
Definition weibull.hpp:106
WeibullRand(double scale=1, double shape=1)
log(k/λ)
Definition weibull.hpp:100
double S(const RealType &x) const override
Definition weibull.hpp:159
Support SupportType() const override
Definition weibull.hpp:33
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
constexpr long double egamma
Definition constants.hpp:36
Real standardNormal(G &g)
Ziggurat algorithm of Marsaglia and Tsang with 256 layers and an explicit sign bit.
Definition variates.hpp:45
constexpr long double minimumTolerance
Definition constants.hpp:44
Real integrate(const F &f, std::type_identity_t< Real > a, std::type_identity_t< Real > b, std::type_identity_t< Real > tolerance=1e-11, int maxSubdivisions=512)
Global adaptive Gauss-Kronrod quadrature (QUADPACK QAG).
Definition quadrature.hpp:226
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