Tuesday, 28 February 2023

Is there any better way to use ratios?

I am looking to predict the number of cars that will be parked in my 100-car capacity lot over the next seven days. To price the rental accordingly, I am analyzing the reservations from the past seven days and how they ultimately played out for each day. By using this data, I can determine the expected demand for each day. For instance, if I had 15 reservations on the fifth day seven days ago and final reservation for that day was 60, and now have 25 reservations for the 5th day in future, I can assume that the fifth day has increased in demand by a fraction of 25/15 and my future demand is 25*60/15 = 100.

The problem in this case is sometime my model predict more than 100 reservations despite the fact that my capacity is 100 for each day. Is there better way to scale and project the demand, I don’t wish to use a hard limit of 100 in the code?

Code below:

from datetime import datetime, timedelta, date

# Historical data
historical_data = {
    '18-Feb-23': (35, 38),
    '19-Feb-23': (40, 50),
    '20-Feb-23': (35, 50),
    '21-Feb-23': (28, 50),
    '22-Feb-23': (15, 65),
    '23-Feb-23': (10, 54),
    '24-Feb-23': (3, 70)
}

# Future data
future_data = {
    '25-Feb-23': (40, None),
    '26-Feb-23': (35, None),
    '27-Feb-23': (28, None),
    '28-Feb-23': (20, None),
    '1-Mar-23': (25, None),
    '2-Mar-23': (3, None),
    '3-Mar-23': (1, None)
}

# Calculate expected demand for each day
for date in future_data:
    # Get the corresponding date seven days ago
    past_date = (datetime.strptime(date, '%d-%b-%y') - timedelta(days=7)).strftime('%d-%b-%y')
    
    # Calculate the fraction of increase in demand from past to future
    if past_date in historical_data:
        past_reservations = historical_data[past_date][0]
        past_final_reservations = historical_data[past_date][1]
        future_reservations = future_data[date][0]
        fraction_increase = future_reservations / past_reservations
    else:
        # If past data is not available, assume no change in demand
        fraction_increase = 1.0
    
    # Calculate the expected final reservations for the future date
    if fraction_increase > 0:
        expected_final_reservations = int(round(past_final_reservations * fraction_increase))
    else:
        expected_final_reservations = past_final_reservations
    
    future_data[date] = (future_data[date][0], expected_final_reservations)

# Print the predictions
for date in future_data:
    print(f"{date}: Expected final reservations = {future_data[date][1]}")


from Is there any better way to use ratios?

No comments:

Post a Comment