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.
Diese Übung ist Teil des Kurses
Monte Carlo Simulations in Python
Anleitung zur Übung
- 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 thebmiandtccolumns of the historical datasetdiaand the simulatedbmiandtcresults fromdf_resultsto assess whether they are similar. - Similarly, use
.cov()from pandas to calculate the covariance matrix of thebmiandtccolumns ofdiaand the simulatedbmiandtcresults fromdf_resultsto assess whether they are similar.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
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(____)