Skewness is a measure of the asymmetry of a probability distribution. It is used in various fields including finance, economics, and social sciences. Skewness is a fundamental statistic that helps to provide a better understanding of the underlying distribution of data.
(* Calculate Skewness *)
let skew ?mean ?sd x =
let m = _get_mean mean x in
let s =
match sd with
| Some a -> a
| None -> std ~mean:m x
let t = ref 0. in
(fun a ->
let s = (a -. m) /. s in
t := !t +. (s *. s *. s))
let n = float_of_int (Array.length x) in
!t /. n
skew function is used to calculate the skewness of an array of values. It takes three optional input arguments:
mean is used to specify the mean value of the input array.
sd is used to specify the standard deviation of the input array, and
x is the input array.
skew function works in the following steps:
meanvalue of the input array is calculated using the
_get_meanfunction. If a
meanvalue is specified as an input argument, that value is used.
- If a standard deviation value is specified as an input argument
sd, it is used to calculate the skewness of the array. If no standard deviation value is specified, the
stdfunction is used to calculate the standard deviation of the input array. The
stdfunction also uses the
_get_meanfunction to calculate the mean value of the input array.
- An iteration loop is run for each value in the input array.
- For each value in the array, the skewness value is calculated as follows:
- Subtract the mean value of the input array from the current array value.
- Divide the result by the standard deviation value of the input array.
- Calculate the cube of the result obtained from the previous step.
- Add the result obtained to a running sum,
- Divide the final value of
tby the length of the input array to obtain the skewness value.
skew function iterates over every value in the input array once, and for each value, performs a constant number of operations. Therefore, the time complexity of this function is O(n), where n is the length of the input array. The time complexity of the
_get_mean function and the
std function is also O(n), as both of these functions iterate over the input array once to compute their respective values. Thus, the overall time complexity of the
skew function is dominated by the time complexity of the
std functions. The space complexity of this function is O(1), as it uses a constant amount of memory for storing the