Ex. (195) - Average, standard deviation and coefficient of variation, is presented here in three programming languages: Python, MATLAB, and JavaScript. Although the implementations differ in syntax, the underlying concept remains identical across all three versions. Each code sample is reproduced from its respective volume of the series Coding Examples from Simple to Complex (Springer, 2024).
The above example performs statistical calculations on an array a and then calls the stat function with the array. The code starts by defining an array a with a list of numerical values. Then, the code initializes a variable b to 0 and another variable e to 0. Additionally, it creates an array r with three elements for storing statistical results, namely for the average (AV; mathematically denoted as x̄), standard deviation (SD; mathematically denoted as σ), and the coefficient of variation (CV; mathematically denoted as Cv).
For the expressions shown above, please observe the progressive replacement and correlation with the variables from the code. The for-loop iterates through each element in the array a to calculate the sum of all values, which is stored in variable b. Next, the average (AV) is calculated by dividing the sum b by the total number of elements in the array (n), and the result is stored in r[0]. The code then proceeds to calculate the sum of squared differences from the average (e) for each element in the array. This step is essential for calculating the standard deviation (SD). The standard deviation (SD) is calculated as the square root of the sum of squared differences from the average, divided by (n − 1). The result is stored in r[1]. The coefficient of variation (CV) is calculated as the ratio of the standard deviation to the average (r[1]/r[0]). The r array, which now contains the calculated statistical values, is returned by the stat function. Thus, the code prints the result, which is the r array returned by the stat function, to the console using the print function.
Note that the stat function is essentially used to compute and return statistical information about the input array a, including the average, standard deviation, and coefficient of variation. Although clearly described in Ex. 156, here, in order to avoid an unnecessary import for the math library and the sqrt function, we use pure mathematical knowledge to take the square root. Thus, note that variance: e / (n − 1). The standard deviation is the square root of the variance. The expression (e / (n − 1)) ** 0.5 calculates this square root. The use of “** 0.5” is equivalent to using the math.sqrt() function of the math library. An experimentation with both approaches yields the same results. Raising a number to the power of 0.5 is equivalent to taking its square root. The square root of a number is the inverse operation of squaring that number. Squaring a number means raising it to the power of 2, thus, the inverse operation is to raise it to the power of ½ or 0.5. One can notice further that q0.5 × q0.5 = q0.5+0.5 = q1 = q, where q is a number. Thus, q0.5 is indeed the square root of q, because when it is multiplied by itself it gives back q.
def stat(a):
n = len(a)
b = 0
e = 0
r = [0, 0, 0] # AV, SD, CV
for j in range(n):
b += a[j]
r[0] = b / n
for j in range(n):
e += (a[j] - r[0]) ** 2
r[1] = (e / (n - 1)) ** 0.5
r[2] = r[1] / r[0]
return r
a = [5, 1, 8, 4, 6, 2, 8, 9]
b = stat(a)
print(b)Python output:
[5.375, 2.9246489410818914, 0.5441207332245379]
let a = [5, 1, 8, 4, 6, 2, 8, 9];
let b = stat(a)
print(b)
function stat(a){
let n = a.length;
let b = 0;
let e = 0;
let r = [];
r[0] = 0; // AV
r[1] = 0; // SD
r[2] = 0; // CV
for(var j=0; j<n; j++){
b += a[j];
}
r[0] = b/n;
for(var j=0; j<n; j++){
e += Math.pow((a[j] - r[0]), 2);
}
r[1] = Math.sqrt(e/(n-1));
r[2] = r[1]/r[0];
return r;
}Javascript output:
[5.375, 2.9246489410818914, 0.5441207332245379]
a = [5, 1, 8, 4, 6, 2, 8, 9];
b = stat(a);
disp(b);
function r = stat(a)
n = length(a);
b = 0;
e = 0;
% Initialize the results array
r = zeros(1, 3); % AV, SD, CV
% Calculate the sum of
% the array elements.
for j=1:n
b = b + a(j);
end
% Calculate the arithmetic
% mean (AV).
r(1) = b/n;
% Calculate the standard
% deviation (SD).
for j=1:n
e = e + (a(j) - r(1))^2;
end
r(2) = sqrt(e/(n-1));
% Calculate the coefficient
% of variation (CV).
r(3) = r(2)/r(1);
endMatlab output:
[5.375, 2.9246489410818914, 0.5441207332245379]
- Paul A. Gagniuc. Coding Examples from Simple to Complex - Applications in Python, Springer, 2024, pp. 1-245.
- Paul A. Gagniuc. Coding Examples from Simple to Complex - Applications in MATLAB, Springer, 2024, pp. 1-255.
- Paul A. Gagniuc. Coding Examples from Simple to Complex - Applications in Javascript, Springer, 2024, pp. 1-240.