|
1 | 1 | /**
|
2 | 2 | * @license Apache-2.0
|
3 | 3 | *
|
4 |
| -* Copyright (c) 2024 The Stdlib Authors. |
| 4 | +* Copyright (c) 2025 The Stdlib Authors. |
5 | 5 | *
|
6 | 6 | * Licensed under the Apache License, Version 2.0 (the "License");
|
7 | 7 | * you may not use this file except in compliance with the License.
|
|
19 | 19 | #include "stdlib/stats/base/dists/weibull/skewness.h"
|
20 | 20 | #include "stdlib/math/base/assert/is_nan.h"
|
21 | 21 | #include "stdlib/math/base/special/gamma.h"
|
22 |
| -#include <math.h> |
| 22 | +#include "stdlib/math/base/special/pow.h" |
| 23 | +#include "stdlib/math/base/special/sqrt.h" |
23 | 24 |
|
24 | 25 | /**
|
25 |
| -* Evaluates the skewness for a Weibull distribution with shape parameter `k`. |
| 26 | +* Evaluates the skewness for a Weibull distribution with shape parameter `k` and scale parameter `lambda`. |
26 | 27 | *
|
27 |
| -* @param k shape parameter (k > 0) |
28 |
| -* @return skewness |
| 28 | +* @param k shape parameter |
| 29 | +* @param lambda scale parameter |
| 30 | +* @return skewness of the distribution |
29 | 31 | *
|
30 | 32 | * @example
|
31 |
| -* double y = stdlib_base_dists_weibull_skewness( 1.5 ); |
32 |
| -* // returns ~1.14 |
| 33 | +* double y = stdlib_base_dists_weibull_skewness( 4.0, 12.0 ); |
| 34 | +* // returns ~-0.087 |
33 | 35 | */
|
34 |
| -double stdlib_base_dists_weibull_skewness( const double k ) { |
35 |
| - if ( stdlib_base_is_nan( k ) || k <= 0.0 ) { |
36 |
| - return 0.0/0.0; // NaN |
37 |
| - } |
| 36 | +double stdlib_base_dists_weibull_skewness( const double k, const double lambda ) { |
| 37 | + if ( |
| 38 | + stdlib_base_is_nan( k ) || |
| 39 | + stdlib_base_is_nan( lambda ) || |
| 40 | + k <= 0.0 || |
| 41 | + lambda <= 0.0 |
| 42 | + ) { |
| 43 | + return 0.0/0.0; // NaN |
| 44 | + } |
38 | 45 |
|
39 |
| - // Compute gamma-related terms: |
40 |
| - double gamma1 = stdlib_base_gamma( 1.0 + ( 1.0 / k ) ); |
41 |
| - double gamma2 = stdlib_base_gamma( 1.0 + ( 2.0 / k ) ); |
42 |
| - double gamma3 = stdlib_base_gamma( 1.0 + ( 3.0 / k ) ); |
| 46 | + double gamma1 = stdlib_base_gamma( 1.0 + (3.0 / k) ); |
| 47 | + double gamma2 = stdlib_base_gamma( 1.0 + (2.0 / k) ); |
| 48 | + double gamma3 = stdlib_base_gamma( 1.0 + (1.0 / k) ); |
43 | 49 |
|
44 |
| - // Compute variance and mean terms: |
45 |
| - double variance = gamma2 - ( gamma1 * gamma1 ); |
46 |
| - double stddev = sqrt( variance ); |
| 50 | + double mu = gamma3 * lambda; |
| 51 | + double sigma2 = (gamma2 * lambda * lambda) - (mu * mu); |
| 52 | + double sigma = stdlib_base_sqrt( sigma2 ); |
47 | 53 |
|
48 |
| - // Compute skewness: |
49 |
| - return ( gamma3 - ( 3.0 * gamma1 * gamma2 ) + ( 2.0 * pow( gamma1, 3 ) ) ) / pow( stddev, 3 ); |
| 54 | + double numerator = (gamma1 * lambda * lambda * lambda) - (3.0 * mu * sigma2) - (mu * mu * mu); |
| 55 | + double denominator = stdlib_base_pow( sigma, 3.0 ); |
| 56 | + |
| 57 | + return numerator / denominator; |
50 | 58 | }
|
0 commit comments