Skip to content

Instantly share code, notes, and snippets.

@Dartv
Created September 21, 2015 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dartv/4e77ffa81efc17f5e80a to your computer and use it in GitHub Desktop.
Save Dartv/4e77ffa81efc17f5e80a to your computer and use it in GitHub Desktop.
Calculating frequency
let nums = [30, 36, 37, 41, 42, 41, 40, 41, 46, 47, 46, 46, 47, 45, 46, 45, 50, 51, 50, 52, 52, 51, 51, 52, 50, 50, 56, 55, 56, 57, 57, 57, 57, 58, 58, 55, 61, 62, 67, 67];
let unique = _.unique(nums);
let absoluteFrequency = _.countBy(nums, num => num);
let relativeFrequency = () => {
let obj = {};
let values = _.values(absoluteFrequency);
let keys = _.keys(absoluteFrequency);
for (var i = 0; i < keys.length; i++) {
obj[keys[i]] = values[i] / nums.length;
}
return obj;
}
let cummulativeFrequency = _.values(absoluteFrequency).reduce((sum, val) => sum += val, 0);
unique.sort();
console.log(`Уникальные числа: ${unique}`);
console.log(`Относительная частота: `, relativeFrequency());
console.log(`Абсолютная частота: `, absoluteFrequency);
console.log(`Накопительная частота: ${cummulativeFrequency}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment