16 Aug 2023

Utility function

What is a utility function?

A utility function, in the context of machine learning and optimization, is a mathematical function that assigns a numerical value to a particular outcome or decision. It is used to represent the preference or desirability of different outcomes or decisions.

In the case of model evaluation and hyperparameter tuning, a utility function is used as a scoring metric to measure the performance of a model or a combination of hyperparameters. The utility function provides a way to quantify how well the model is performing or how desirable a set of hyperparameters is.

One common approach in AI decision-making is to maximize the expected utility, which considers the probability of different outcomes occurring. The expected utility is calculated by multiplying the utility of each outcome by its corresponding probability and summing up the results. The AI system chooses the action with the highest expected utility as the optimal choice.

Examples:

  1. Self-Driving Cars: In the self-driving cars application, the utility function may consider factors such as time takenfuel consumptionsafety, and comfort, and assign utility values to different routes based on these factors. The self-driving car can then use the utility values to calculate the expected utility of each route, taking into account the probabilities of different traffic conditions or road obstacles, and choose the route with the highest expected utility to reach the destination.
  2. Recommendation Systems: Consider a recommendation system that suggests movies to users based on their preferences. The utility function of the recommendation system may assign higher utility values to movies that match the user's preferred genre, actors, or directors and lower utility values to films that do not match these preferences. The recommendation system can then use the utility values to rank and recommend movies to the user based on their utility values, with higher utility movies being recommended more prominently.

Code example

# Define the utility function
def calculate_utility(salary, commute_time):
    # Utility is a linear combination of salary and negative commute time
    return salary - 2 * commute_time

# Job offer 1 details
salary_job1 = 60000  # in dollars per year
commute_time_job1 = 30  # in minutes

# Job offer 2 details
salary_job2 = 55000  # in dollars per year
commute_time_job2 = 20  # in minutes

# Calculate the utility for each job offer
utility_job1 = calculate_utility(salary_job1, commute_time_job1)
utility_job2 = calculate_utility(salary_job2, commute_time_job2)

# Compare the utilities to make a decision
if utility_job1 > utility_job2:
    print("Job offer 1 is the better choice.")
elif utility_job2 > utility_job1:
    print("Job offer 2 is the better choice.")
else:
    print("Both job offers are equally desirable.")