math-utils-simple 2.1.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.js +16 -1
- package/src/statistics/average.js +13 -0
- package/src/statistics/count.js +7 -0
- package/src/statistics/frequency.js +13 -0
- package/src/statistics/interQuartileRange.js +7 -0
- package/src/statistics/max.js +19 -0
- package/src/statistics/median.js +18 -0
- package/src/statistics/min.js +19 -0
- package/src/statistics/mode.js +38 -0
- package/src/statistics/quartiles.js +37 -0
- package/src/statistics/range.js +6 -0
- package/src/statistics/standardDeviation.js +5 -0
- package/src/statistics/sum.js +7 -0
- package/src/statistics/variance.js +21 -0
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -69,4 +69,19 @@ export { default as markup } from "./percentages/markup.js";
|
|
|
69
69
|
export { default as profitPercentage } from "./percentages/profitPercentage.js";
|
|
70
70
|
export { default as lossPercentage } from "./percentages/lossPercentage.js";
|
|
71
71
|
export { default as margin } from "./percentages/margin.js";
|
|
72
|
-
export { default as relativeChange } from "./percentages/relativeChange.js";
|
|
72
|
+
export { default as relativeChange } from "./percentages/relativeChange.js";
|
|
73
|
+
|
|
74
|
+
// STATISTICS
|
|
75
|
+
export { default as sum } from "./statistics/sum.js";
|
|
76
|
+
export { default as average } from "./statistics/average.js";
|
|
77
|
+
export { default as median } from "./statistics/median.js";
|
|
78
|
+
export { default as mode } from "./statistics/mode.js";
|
|
79
|
+
export { default as range } from "./statistics/range.js";
|
|
80
|
+
export { default as variance } from "./statistics/variance.js";
|
|
81
|
+
export { default as standardDeviation } from "./statistics/standardDeviation.js";
|
|
82
|
+
export { default as max } from "./statistics/max.js";
|
|
83
|
+
export { default as min } from "./statistics/min.js";
|
|
84
|
+
export { default as count } from "./statistics/count.js";
|
|
85
|
+
export { default as frequency } from "./statistics/frequency.js";
|
|
86
|
+
export { default as quartiles } from "./statistics/quartiles.js";
|
|
87
|
+
export { default as interQuartileRange } from "./statistics/interQuartileRange.js";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import sum from "./sum.js";
|
|
2
|
+
|
|
3
|
+
export default function average(array) {
|
|
4
|
+
if (!Array.isArray(array)) {
|
|
5
|
+
throw new TypeError("Expected an array.");
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
if (array.length === 0) {
|
|
9
|
+
throw new Error("Cannot calculate average of an empty array.");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return sum(array) / array.length;
|
|
13
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export default function max(array) {
|
|
2
|
+
if (!Array.isArray(array)) {
|
|
3
|
+
throw new TypeError("Expected an array.");
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
if (array.length === 0) {
|
|
7
|
+
throw new Error("Cannot find maximum of an empty array.");
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
let largest = array[0];
|
|
11
|
+
|
|
12
|
+
for (const value of array) {
|
|
13
|
+
if (value > largest) {
|
|
14
|
+
largest = value;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return largest;
|
|
19
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export default function median(array) {
|
|
2
|
+
if (!Array.isArray(array)) {
|
|
3
|
+
throw new TypeError("Expected an array.");
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
if (array.length === 0) {
|
|
7
|
+
throw new Error("Cannot calculate median of an empty array.");
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const sorted = [...array].sort((a, b) => a - b);
|
|
11
|
+
const middle = Math.floor(sorted.length / 2);
|
|
12
|
+
|
|
13
|
+
if (sorted.length % 2 === 0) {
|
|
14
|
+
return (sorted[middle - 1] + sorted[middle]) / 2;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return sorted[middle];
|
|
18
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export default function min(array) {
|
|
2
|
+
if (!Array.isArray(array)) {
|
|
3
|
+
throw new TypeError("Expected an array.");
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
if (array.length === 0) {
|
|
7
|
+
throw new Error("Cannot find minimum of an empty array.");
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
let smallest = array[0];
|
|
11
|
+
|
|
12
|
+
for (const value of array) {
|
|
13
|
+
if (value < smallest) {
|
|
14
|
+
smallest = value;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return smallest;
|
|
19
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export default function mode(array) {
|
|
2
|
+
if (!Array.isArray(array)) {
|
|
3
|
+
throw new TypeError("Expected an array.");
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
if (array.length === 0) {
|
|
7
|
+
throw new Error("Cannot calculate mode of an empty array.");
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const counts = new Map();
|
|
11
|
+
|
|
12
|
+
for (const value of array) {
|
|
13
|
+
counts.set(value, (counts.get(value) || 0) + 1);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
let maxFrequency = 0;
|
|
17
|
+
|
|
18
|
+
for (const frequency of counts.values()) {
|
|
19
|
+
if (frequency > maxFrequency) {
|
|
20
|
+
maxFrequency = frequency;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// No mode if every value occurs equally often.
|
|
25
|
+
if (maxFrequency === 1) {
|
|
26
|
+
return [];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const modes = [];
|
|
30
|
+
|
|
31
|
+
for (const [value, frequency] of counts) {
|
|
32
|
+
if (frequency === maxFrequency) {
|
|
33
|
+
modes.push(value);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return modes.sort((a, b) => a - b);
|
|
38
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import median from "./median.js";
|
|
2
|
+
|
|
3
|
+
export default function quartiles(array) {
|
|
4
|
+
if (!Array.isArray(array)) {
|
|
5
|
+
throw new TypeError("Expected an array.");
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
if (array.length === 0) {
|
|
9
|
+
throw new Error("Cannot calculate quartiles of an empty array.");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const sorted = [...array].sort((a, b) => a - b);
|
|
13
|
+
|
|
14
|
+
const q2 = median(sorted);
|
|
15
|
+
|
|
16
|
+
const middle = Math.floor(sorted.length / 2);
|
|
17
|
+
|
|
18
|
+
let lowerHalf;
|
|
19
|
+
let upperHalf;
|
|
20
|
+
|
|
21
|
+
if (sorted.length % 2 === 0) {
|
|
22
|
+
lowerHalf = sorted.slice(0, middle);
|
|
23
|
+
upperHalf = sorted.slice(middle);
|
|
24
|
+
} else {
|
|
25
|
+
lowerHalf = sorted.slice(0, middle);
|
|
26
|
+
upperHalf = sorted.slice(middle + 1);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const q1 = median(lowerHalf);
|
|
30
|
+
const q3 = median(upperHalf);
|
|
31
|
+
|
|
32
|
+
return {
|
|
33
|
+
q1,
|
|
34
|
+
q2,
|
|
35
|
+
q3,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import average from "./average.js";
|
|
2
|
+
|
|
3
|
+
export default function variance(array) {
|
|
4
|
+
if (!Array.isArray(array)) {
|
|
5
|
+
throw new TypeError("Expected an array.");
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
if (array.length === 0) {
|
|
9
|
+
throw new Error("Cannot calculate variance of an empty array.");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const mean = average(array);
|
|
13
|
+
|
|
14
|
+
let sum = 0;
|
|
15
|
+
|
|
16
|
+
for (const value of array) {
|
|
17
|
+
sum += (value - mean) ** 2;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return sum / array.length;
|
|
21
|
+
}
|