CommencerCommencer gratuitement

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.

Cet exercice fait partie du cours

Monte Carlo Simulations in Python

Afficher le cours

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 the bmi and tc columns of the historical dataset dia and the simulated bmi and tc results from df_results to assess whether they are similar.
  • Similarly, use .cov() from pandas to calculate the covariance matrix of the bmi and tc columns of dia and the simulated bmi and tc results from df_results to assess whether they are similar.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de 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(____)
Modifier et exécuter le code