# Cost benefit analysis with varying discount rates

## Description

This code performs a cost-benefit analysis by plotting the Net Present Value (NPV) over time for different discount rates. It uses matplotlib to generate the visualizations. Here's a breakdown of what the code does: It imports the necessary libraries, matplotlib.pyplot and numpy, which are commonly used for plotting and numerical operations, respectively. It defines a time range T using numpy's arange function, which generates an array of numbers from 0 to 10 with a step size of 0.1, representing time in years. It defines two functions cost_function(t) and benefit_function(t) that represent the cost and benefit functions over time. These functions are defined using mathematical expressions. It creates a list discount_rates containing different discount rates to explore. It sets up a figure with a specific size using plt.figure(figsize=(10, 6)) to adjust the dimensions of the plot. It iterates over each discount rate in the discount_rates list. Inside the loop, it calculates the NPV for each discount rate at different time points using the provided cost and benefit functions and the formula for NPV. It plots the NPV over time using plt.plot(T, NPV, label=f'Discount Rate = {r}'), where T is the time range and NPV is the calculated Net Present Value for the current discount rate r. It also labels each plot with the corresponding discount rate. It adds labels to the x-axis (Time (years)) and y-axis (Net Present Value), sets the title of the plot (Cost-Benefit Analysis with Varying Discount Rates), adds a legend to distinguish between different discount rates, and enables grid lines for better readability. Finally, it displays the plot using plt.show(). In summary, this code allows for the comparison of the Net Present Value over time under different discount rates, providing insights into the financial viability of a project or investment.

## Files

## Steps to reproduce

import matplotlib.pyplot as plt import numpy as np # Time range T = np.arange(0, 10, 0.1) # Cost and benefit functions def cost_function(t): return 0.5 * t ** 2 def benefit_function(t): return 0.3 * t ** 3 # Discount rates to explore discount_rates = [0.03, 0.05, 0.07] # Plotting NPV for each discount rate plt.figure(figsize=(10, 6)) for r in discount_rates: NPV = [(benefit_function(t) - cost_function(t)) / ((1 + r) ** t) for t in T] plt.plot(T, NPV, label=f'Discount Rate = {r}') plt.xlabel('Time (years)') plt.ylabel('Net Present Value') plt.title('Cost-Benefit Analysis with Varying Discount Rates') plt.legend() plt.grid(True) plt.show()