Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Take our 85+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

Publication

Exploration of CDFs: Their Applications in Data Science
Data Analysis   Data Science   Latest   Machine Learning

Exploration of CDFs: Their Applications in Data Science

Last Updated on April 30, 2024 by Editorial Team

Author(s): Ghadah AlHabib

Originally published on Towards AI.

Image generated by ChatGPT

Introduction to CDFs

Cumulative Distribution Functions (CDF) are functions that represent the probability of a random variable X that takes on a value less than or equal to a specific value x. It’s the cumulative probability associated with a distribution.

Use Case of using CDFs: Analyzing Customer Wait Times to Optimize Service Efficiency

The use of CDFs in this example is to understand the overall wait time experience of customers. It allows business owners to specify certain thresholds like the 80th percentile and set measurable goals based on the distribution of their data. For instance, if a business aims to increase customer satisfaction by reducing wait times, the CDF can help in providing a clear target and pinpoint specific wait times to aim for. So, β€œWe want X% of our customers to be served within Y minutes”.

Let’s dive into the code:

Step 1: Dataset

First, we will generate a synthetic dataset to simulate realistic data attributes. CustomerID gives each observation a unique identifier, and timestamps give a specific time point to each observation.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Seed for reproducibility
np.random.seed(42)

# Generate positive wait times using an absolute value of a normal distribution
wait_times = np.abs(np.random.normal(8, 3, 1000))
customer_ids = range(1, 1001)
timestamps = pd.date_range(start='2024-01-01 08:00', periods=1000, freq='T') # One-minute intervals

df = pd.DataFrame({
'CustomerID': customer_ids,
'Timestamp': timestamps,
'WaitTime': wait_times
})
Screenshot by author

Why did we use a normal distribution to simulate the generation? Primarily for simplicity and illustrative purposes. We could use other distributions such as the exponential distribution. So, depending on your data, whether it’s skewed or bounded, you’ll be able to choose the most suitable distribution.

Step 2: Construct the CDF

sorted_df = df.sort_values('WaitTime').reset_index(drop=True)
sorted_df['CDF'] = (sorted_df.index + 1) / len(sorted_df)

The values are sorted by the wait time to calculate the cumulative sum of probabilities up to a certain wait time. Then, the CDF for each wait time is calculated by taking the index (which, after sorting, represents the number of values less than or equal to the current wait time) and dividing it by the total number of observations (rows). This computes the proportion of customers who have waited for less than or equal to each specific wait time.

Below we can see the sorted wait time along with the CDF value:

Screenshot by author

Step 3: Let’s plot the CDF

plt.figure(figsize=(10, 6))
plt.step(sorted_df['WaitTime'], sorted_df['CDF'], where='post')
plt.xlabel('Wait Time (minutes)')
plt.ylabel('CDF')
plt.title('CDF of Customer Wait Times')
plt.grid(True)
plt.show()
Graph generated by author

Step 4: Let’s extract insights! What kind of questions does this graph answer?

Question 1: What percentage of customers experience a wait time of less than 10 minutes?

wait_time_threshold = 10
sorted_df[sorted_df['WaitTime'] < wait_time_threshold]['CDF'].max() * 100

# Output: 75.70% of customers wait less than 10 minutes.
Graph generated by author

Question 2: How long do 50% of the customers wait?

Graph generated by author
percentage = 0.50
sorted_df[sorted_df['CDF'] <= percentage]['WaitTime'].max()

# Output: 50% of the customers wait less than or equal to 8.07 minutes.

Question 3: Give me more insights about resource allocation. Does the timing of the day affect the wait time distribution?

  • If the CDF varies significantly at different times: need for differential resource allocation to improve customer wait times during peak hours. Longer waiting times; peak hours.
  • If the CDF remains stable and no dramatic difference in customer wait times: balanced resource allocation.

Let’s explore the implementation:

sorted_df['Hour'] = sorted_df['Timestamp'].dt.hour

def assign_time_slot(hour):
if 8 <= hour < 12:
return 'Morning'
elif 12 <= hour < 16:
return 'Afternoon'
elif 16 <= hour < 20:
return 'Evening'
else:
return 'Other Timings'

sorted_df['TimeSlot'] = sorted_df['Hour'].apply(assign_time_slot)

grouped = sorted_df.groupby('TimeSlot')

for name, group in grouped:
sorted_group = group.sort_values('WaitTime').reset_index(drop=True)
sorted_group['CDF'] = (sorted_group.index + 1) / len(sorted_group)
plt.step(sorted_group['WaitTime'], sorted_group['CDF'], where='post', label=name)

plt.xlabel('Wait Time (minutes)')
plt.ylabel('CDF')
plt.title('CDF of Customer Wait Times by Time Slot')
plt.legend()
plt.grid(True)
plt.show()
Graph generated by author

Interpreting the results:

The curves for each time slot are close to each other, indicating that the distribution of wait times does not change significantly throughout the day and there is no significant peak time. Therefore, it’s a good indicator that resource allocation is balanced and that there’s a consistent service experience throughout the day.

Let’s use a real dataset from Kaggle!

Queue Waiting Time Prediction
Screenshot by author
Graph generated by the author

Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.

Published via Towards AI

Feedback ↓