Comparing simulated and historical data
A good simulation should have similar results to the historical data. Was that true for the simulation in the video? In this exercise, you'll explore one way to examine the simulation results and find out!
First, you'll perform a simulation using the multivariate normal distribution and the mean and covariance matrix of dia
. Then, you'll check the means of both the historical and simulated data. Are they similar?
The diabetes dataset has been loaded as a DataFrame, dia
, and the following libraries have been imported for you: pandas
as pd
, numpy
as np
, and scipy.stats
as st
.
This exercise is part of the course
Monte Carlo Simulations in Python
Exercise instructions
- Perform the simulation 10,000 times using the multivariate normal distribution and the mean and covariance matrix of
dia
. - Use the
.mean()
function in pandas to calculate the mean values of thebmi
andtc
columns of the historical datasetdia
and the simulatedbmi
andtc
results fromdf_results
to assess whether they are similar. - Similarly, use
.cov()
from pandas to calculate the covariance matrix of thebmi
andtc
columns ofdia
and the simulatedbmi
andtc
results fromdf_results
to assess whether they are similar.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
cov_dia = dia[["age", "bmi", "bp", "tc", "ldl", "hdl", "tch", "ltg", "glu"]].cov()
mean_dia = dia[["age", "bmi", "bp", "tc", "ldl", "hdl", "tch", "ltg", "glu"]].mean()
# Complete the code to perform the simulation
simulation_results = st.multivariate_normal.rvs(____)
df_results = pd.DataFrame(simulation_results,columns=["age", "bmi", "bp", "tc", "ldl", "hdl", "tch", "ltg", "glu"])
# Calculate bmi and tc means for the historical and simulated results
print(dia[["bmi","tc"]].____)
print(____)
# Calculate bmi and tc covariances for the historical and simulated results
print(____)
print(____)