LoslegenKostenlos loslegen

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

Kurs anzeigen

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 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.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

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(____)
Code bearbeiten und ausführen