Generating a CCDF plot for a list of data

71 Views Asked by At

I am trying to plot a CCDF graph using scipy and matplot but for some reason it is not graphing. I based my code from the following source: https://www.geeksforgeeks.org/how-to-calculate-and-plot-a-cumulative-distribution-function-with-matplotlib-in-python/. However, it does not seem to work. Take a look at my code and let me know if you see anything that I don't.

# compare the completion times and service times and check that they are different
# do this for multiple runs 
# 

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d

class Job:
    def __init__(self, arrivalTime, size):
        self.arrivalTime = arrivalTime
        self.size = size
        self.departureTime = None

departures = 0
maxDepartures = 100000

# Function that generates the job size
def generateJobSize():
    return np.random.exponential(1 / 3)

# Function that generates an interarrival time
def generateInterarrivalTime():
    return np.random.exponential(1 / 2)

jobQueue = [] 
serverEmpty = True
nextArrTime = generateInterarrivalTime()
nextDepTime = float('inf')
completionTimes = []
# initiate pointer for the job currently being serviced
servingJob = None
clock = 0.0
loop = 0

def handleArr():
  # declare these variables as global so that they can be edited outside the function
  global job, serverEmpty, servingJob, nextArrTime, nextDepTime, completionTimes
  # generate the size of the job
  size = generateJobSize()
  # create job object with those attriubtes
  job = Job(arrivalTime = clock, size = size)
  # if the server is empty upon this job's arrival into the system, immediately start servicing it 
  if serverEmpty == True:
    # create pointer that points to the job that is currently being serviced
    servingJob = job
    # determine the job's departure time based on the size and the current time
    job.departureTime = clock + job.size
    nextDepTime = job.departureTime
    # update the status of the server to busy
    serverEmpty = False
  else:
    # if a job is already being serviced, append the new arrival to the queue
    jobQueue.append(job)
  # generate the arrival time of the next job
  interArrTime = generateInterarrivalTime ()
  # set the next arrival time based upon that
  nextArrTime = clock + interArrTime
 
 

# fcn to handle departures
def handleDep():
  global job, departures, serverEmpty, servingJob, nextDepTime, completionTimes
  # determine the job's departure time based on the size and the current time
  job.departureTime = clock + job.size
  completionTimes.append(job.departureTime - job.arrivalTime)
  # update the time of the next departure based on this new information
  nextDepTime = clock + job.size
# when all the jobs have finished
# inc the counter by 1
  departures += 1
  # if the queue is not empty, pop the next job in line
  if len(jobQueue) != 0:
    job = jobQueue.pop(0)
    servingJob = job
  else: 
    serverEmpty = True
    nextDepTime = float("inf")
    servingJob = None

while departures < maxDepartures: 
  # determine the next event
  if nextArrTime <= nextDepTime:
    clock = nextArrTime
    handleArr()
  else: 
    loop += 1
    clock = nextDepTime
    handleDep()

print(len(completionTimes))

# --------- GRAPHS A CCDF PLOT -----------------

# # Sort completion times in ascending order
# completionTimes.sort()

# # Calculate probabilities for completion times
# probabilities_completion = np.arange(1, len(completionTimes) + 1) / len(completionTimes)

# # Calculate the CCDF for completion times
# ccdf_completion = 1 - probabilities_completion

# # Interpolate the data for a smooth line
# interp_func_completion = interp1d(completionTimes, ccdf_completion, kind='linear')

# # Generate interpolated data points for completion times
# completion_times_interp = np.linspace(min(completionTimes), max(completionTimes), num=1000)
# ccdf_completion_interp = interp_func_completion(completion_times_interp)

# # Plot completion times vs CCDF
# plt.plot(completion_times_interp, ccdf_completion_interp)
# plt.xlabel('Completion Time')
# plt.ylabel('CCDF')
# plt.title('Completion Time vs CCDF')
# plt.grid(True)
# plt.show()

x = np.sort(completionTimes)

#calculate CDF values
y = 1. * np.arange(len(completionTimes)) / (len(completionTimes) - 1)

#plot CDF
plt.plot(x, y)

I tried to plot the graph with the above commented code. It worked in the sense that the graph was generated, but I am not sure if it's actually correct.

0

There are 0 best solutions below