Skip to content

Commit

Permalink
getArithmeticMeanFromFrequency()
Browse files Browse the repository at this point in the history
  • Loading branch information
mzusin committed Mar 9, 2024
1 parent 16387ef commit 762437b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/main/statistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ export const getArithmeticMean = (data: number[], decimalPlaces = Infinity) : nu
return setDecimalPlaces(sum / data.length, decimalPlaces);
};

/**
* Frequency map: number ---> it's frequency
*/
export const getArithmeticMeanFromFrequency = (frequencyMap: Map<number, number>, decimalPlaces = Infinity) => {

let mean = 0;

for(const [val, frequency] of frequencyMap) {
mean += val * frequency;
}

return setDecimalPlaces(mean, decimalPlaces);
};

/**
* Central tendency: What is the central number in the sorted array?
* Good for lists like [3, 3, 3, 3, 3, 3, 100] - 100 here is called "Outlier"
Expand Down
15 changes: 14 additions & 1 deletion test/statistics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
getMode,
getVariance,
getVariance1,
getStandardDeviation
getStandardDeviation,
getArithmeticMeanFromFrequency,
} from '../src/main/statistics';

describe('Statistics', () => {
Expand Down Expand Up @@ -34,6 +35,18 @@ describe('Statistics', () => {
});
});

describe('getArithmeticMeanFromFrequency()', () => {

test('map 1', () => {
const map = new Map();
map.set(3, 0.6); // 60% of number 3
map.set(4, 0.2); // 20% of number 4
map.set(5, 0.2); // 20% of number 5

expect(getArithmeticMeanFromFrequency(map, 2)).toStrictEqual(3.6);
});
});

describe('getMedian()', () => {

test('[]', () => {
Expand Down

0 comments on commit 762437b

Please sign in to comment.