skewness.js (2096B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var isnan = require( '@stdlib/math/base/assert/is-nan' ); 24 var variance = require( './../../../../../base/dists/weibull/variance' ); 25 var gamma = require( '@stdlib/math/base/special/gamma' ); 26 var sqrt = require( '@stdlib/math/base/special/sqrt' ); 27 var mean = require( './../../../../../base/dists/weibull/mean' ); 28 var pow = require( '@stdlib/math/base/special/pow' ); 29 30 31 // MAIN // 32 33 /** 34 * Returns the skewness of a Weibull distribution. 35 * 36 * @param {PositiveNumber} k - shape parameter 37 * @param {PositiveNumber} lambda - scale parameter 38 * @returns {number} skewness 39 * 40 * @example 41 * var v = skewness( 1.0, 1.0 ); 42 * // returns 2.0 43 * 44 * @example 45 * var v = skewness( 4.0, 12.0 ); 46 * // returns ~-0.087 47 * 48 * @example 49 * var v = skewness( 8.0, 2.0 ); 50 * // returns ~-0.534 51 * 52 * @example 53 * var v = skewness( 1.0, -0.1 ); 54 * // returns NaN 55 * 56 * @example 57 * var v = skewness( -0.1, 1.0 ); 58 * // returns NaN 59 * 60 * @example 61 * var v = skewness( 2.0, NaN ); 62 * // returns NaN 63 * 64 * @example 65 * var v = skewness( NaN, 2.0 ); 66 * // returns NaN 67 */ 68 function skewness( k, lambda ) { 69 var sigma2; 70 var sigma; 71 var out; 72 var mu; 73 if ( 74 isnan( k ) || 75 isnan( lambda ) || 76 k <= 0.0 || 77 lambda <= 0.0 78 ) { 79 return NaN; 80 } 81 mu = mean( k, lambda ); 82 sigma2 = variance( k, lambda); 83 sigma = sqrt( sigma2 ); 84 out = gamma( 1.0 + ( 3.0/k ) ) * pow( lambda, 3.0 ); 85 out -= ( 3.0*mu*sigma2 ) + pow( mu, 3.0 ); 86 out /= pow( sigma, 3.0 ); 87 return out; 88 } 89 90 91 // EXPORTS // 92 93 module.exports = skewness;