Skip to content

Instantly share code, notes, and snippets.

@alairock
Created October 26, 2023 23:06
Show Gist options
  • Save alairock/c7ac7b41e3cba8e0b79c9802b6998f7d to your computer and use it in GitHub Desktop.
Save alairock/c7ac7b41e3cba8e0b79c9802b6998f7d to your computer and use it in GitHub Desktop.
Calculation of clocking in, minute-by-minute, vs rounding.
import random
import numpy as np
import matplotlib.pyplot as plt
# Initialize variables for multiple tests
num_tests = 10000
differences = []
# Loop for each test
for test in range(num_tests):
num_days = 365 # 2 years
total_minutes_exact = 0
total_minutes_rounded = 0
# Loop through each day
for day in range(num_days):
# Generate a random start time for the day
start_time = random.randint(0, 540) # Anytime from 00:00 to 08:00
# Generate a random end time for the day
end_time = start_time + random.randint(1, 480) # Max of 8 hours (480 minutes)
# Calculate the actual minutes worked for the day
minutes_worked_exact = end_time - start_time
# Round to nearest half-hour
# if minutes_worked_exact % 30 >= 15:
# minutes_worked_rounded = (minutes_worked_exact // 30 + 1) * 30
# else:
# minutes_worked_rounded = (minutes_worked_exact // 30) * 30
# round to the nearest hour
if minutes_worked_exact % 60 >= 30:
minutes_worked_rounded = (minutes_worked_exact // 60 + 1) * 60
else:
minutes_worked_rounded = (minutes_worked_exact // 60) * 60
# Add to the tallies
total_minutes_exact += minutes_worked_exact
total_minutes_rounded += minutes_worked_rounded
# Convert total minutes to hours for easy comparison
total_hours_exact = total_minutes_exact / 60
total_hours_rounded = total_minutes_rounded / 60
# Calculate the difference
difference = total_hours_rounded - total_hours_exact
# Append to list for later analysis
differences.append(difference)
# Calculate statistics
median_difference = np.median(differences)
average_difference = np.mean(differences)
std_deviation = np.std(differences)
# Print statistics
print(f"Median Difference: {median_difference}")
print(f"Average Difference: {average_difference}")
print(f"Standard Deviation: {std_deviation}")
# Plotting
plt.hist(differences, bins=50, alpha=0.75, label='Difference Distribution')
plt.axvline(median_difference, color='r', linestyle='dashed', linewidth=1, label=f"Median: {median_difference}")
plt.axvline(average_difference, color='g', linestyle='dashed', linewidth=1, label=f"Mean: {average_difference}")
plt.axvline(average_difference + std_deviation, color='y', linestyle='dashed', linewidth=1, label=f"Mean+StdDev: {average_difference + std_deviation}")
plt.axvline(average_difference - std_deviation, color='y', linestyle='dashed', linewidth=1, label=f"Mean-StdDev: {average_difference - std_deviation}")
plt.legend(loc='upper right')
plt.xlabel('Hours')
plt.ylabel('Frequency')
plt.title('Difference in Hours (Rounded - Exact)')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment