4

Statistics for Programmers - Frequency Distributions

 6 months ago
source link: https://nishtahir.com/1-statistics-for-programmers-frequency-distributions/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

By Nish Tahir in Statistics For Programmers — Feb 11, 2024

Statistics for Programmers - Frequency Distributions

A Frequency Distribution is a common way to understand a trend in a dataset. It's a tabular representation of the number of times a value appears in a dataset. If we denote values in a dataset as x1,x2,…,xn, their corresponding frequencies can be denoted as f1,f2,…,fn. This relationship can be expressed as a table.

Value (x)Frequency (f)x1f1x2f2⋮⋮xnfn

Applying this practically, let's consider a dataset of 10 users who were asked to review a product on a scale of 1 to 5. The dataset can be represented as an array of reviews.

[3, 1, 5, 5, 2, 4, 5, 3, 1, 5]

We can construct a frequency distribution table for this dataset by counting the number of times each unique element appears in the array.

Value (x) | Frequency (f)
-------------------------
1         | 2
2         | 1
3         | 2
4         | 1
5         | 4

This can be expressed in code using a Map (or Dictionary depending on your language of choice) of unique values and how many times they appear in a given dataset.

Once again considering our array of reviews,

const arr = [3, 1, 5, 5, 2, 4, 5, 3, 1, 5];

We can construct a function that counts the number of times each unique element appears in the array.

function frequencyDistribution(arr) {
  const map = {};
  for(let i = 0; i < arr.length; i++) {
      const item = arr[i];
      if (map[item]) {
          map[item] += 1;
      } else {
          map[item] = 1;
      }
  }
  return map;
}

Applying this function to our dataset gives us the following output,

This post is for subscribers only

Already have an account? Sign in.

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK