Case 2: $CO_2$ emissions from traffic¶
What's the propagated uncertainty? *How large will be the $CO_2$ emissions?*
In this project, you have chosen to work on the uncertainty of the number of cars and heavy vehicles to estimate their $CO_2$ emissions. You have observations every five minutes of the number of cars, $C$, and the number of heavy vehicles, $H$ (more on the dataset here). As you know, traffic is an important source of $CO_2$ emissions that contribute to the greenhouse effect. Here, the emitted $CO_2$ will be estimated for a trip of 1km using the emissions intensities from Nishimura and Haga (2023), assuming that the cars use gasolite as fuel and the heavy vehicles, diesel. Emissions can be computed as
$$ CO2 = 143 C + 469 H $$The goal of this project is:
- Choose a reasonable distribution function for $H$ and $C$.
- Fit the chosen distributions to the observations of $H$ and $C$.
- Assuming $H$ and $C$ are independent, propagate their distributions to obtain the distribution of emissions of $CO_2$.
- Analyze the distribution of emissions of $CO_2$.
Importing packages¶
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
from math import ceil, trunc
plt.rcParams.update({'font.size': 14})
1. Explore the data¶
First step in the analysis is exploring the data, visually and through its statistics.
# Import
C, H = np.genfromtxt('dataset_traffic.csv', delimiter=",", unpack=True, skip_header=True)
# plot time series
fig, ax = plt.subplots(2, 1, figsize=(10, 7), layout = 'constrained')
ax[0].plot(H,'k')
ax[0].set_xlabel('Time')
ax[0].set_ylabel('Number of heavy vehicles, H')
ax[0].grid()
ax[1].plot(C,'k')
ax[1].set_xlabel('Time')
ax[1].set_ylabel('Number of cars, C')
ax[1].grid()
# Statistics for H
print(stats.describe(H))
# Statistics for d
print(stats.describe(C))
Task 1:
Describe the data based on the previous statistics:
- Which variable presents a higher variability?
- What does the skewness coefficient means? Which kind of distribution functions should we consider to fit them?
2. Empirical distribution functions¶
Now, we are going to compute and plot the empirical PDF and CDF for each variable. Note that you have the pseudo-code for the empirical CDF in the reader.
Task 2:
Define a function to compute the empirical CDF.
def ecdf(YOUR_INPUT):
#Your code
return YOUR_OUTPUT
#Your plot here
Task 3:
Based on the results of Task 1 and the empirical PDF and CDF, select one distribution to fit to each variable. For $H$, select between Gumbel or Gaussian distribution, while for $C$ choose between Uniform or Lognormal.
3. Fitting a distribution¶
Task 4:
Fit the selected distributions to the observations using MLE.
Hint: Use Scipy built in functions (watch out with the parameters definition!).
#your code here
4. Assessing goodness of fit¶
Task 5:
Assess the goodness of fit of the selected distribution using:
- One graphical method: QQplot or Logscale. Choose one.
- Kolmogorov-Smirnov test.
Hint: You have Kolmogorov-Smirnov test implemented in Scipy.
#Your code here
Task 6:
Interpret the results of the GOF techniques. How does the selected parametric distribution perform?
5. Propagating the uncertainty¶
Using the fitted distributions, we are going to propagate the uncertainty from $H$ and $C$ to the emissions of $CO_2$ assuming that $H$ and $C$ are independent.
Task 7:
Draw 10,000 random samples from the fitted distribution functions for $H$ and $C$.
Compute emissions of $CO_2$ for each pair of samples.
Compute emissions of $CO_2$ for the observations.
Plot the PDF and exceedance curve in logscale of the emissions of $CO_2$ computed using both the simulations and the observations.
# Here, the solution is shown for the Lognormal distribution
# Draw random samples
rs_H = #your code here
rs_C = #your code here
#Compute Fh
rs_CO2 = #your code here
#repeat for observations
CO2 = #your code here
#plot the PDF and the CDF
Task 8:
Interpret the figures above, answering the following questions:
If you run the code in the cell below, you will obtain a scatter plot of both variables. Explore the relationship between both variables and answer the following questions:
Task 9:
Observe the plot below. What differences do you observe between the generated samples and the observations?
Compute the correlation between $H$ and $C$ for the samples and for the observartions. Are there differences?
What can you improve into the previous analysis? Do you have any ideas/suggestions on how to implement those suggestions?
fig, axes = plt.subplots(1, 1, figsize=(7, 7))
axes.scatter(rs_H, rs_C, 40, 'k', label = 'Simulations')
axes.scatter(H, C, 40, 'r','x', label = 'Observations')
axes.set_xlabel('Number of heavy vehicles, H ')
axes.set_ylabel('Number of cars, C')
axes.legend()
axes.grid()
#Correlation coefficient calculation here
End of notebook.