EME 210
Data Analytics for Energy Systems

Introducing Monte Carlo

PrintPrint

Read It: Introducing Monte Carlo

[00:02:09.31] OK, so then, we'll talk about Monte Carlo today. We'll see how long it takes. Probably will take the whole time. Kind of a strange phrase, Monte Carlo, right? What does this mean? What are we getting at here? What is he talking about? Where is Monte Carlo? Monte Carlo in Italy. Why is he talking about some place in Italy? Well, it's--  

[00:02:37.30] Monte Carlo refers to a way of simulating things. And since we've done quite a lot of simulation in this course, bootstrapping for confidence intervals and randomization, for hypothesis testing-- Monte Carlo is a really fitting subject here. It's another way to do the sort of simulation that we've been talking about in class. But it goes along with this concept of probability.  

[00:03:03.07] Now that's a really a useful way to do simulation when you have no data or even just a little bit of data. You are relying on your expert knowledge of a system to come up with an answer or a range of answers. So that's the setting. Let's get into some of the details, OK?  

Enter image and alt text here. No sizes!
INSERT IMAGE: L29:Slide 2

[00:03:32.92] So, we talked about bootstrapping before. We call bootstrapping the confidence intervals. We randomly selected some values from a sample data. We have some collection of numbers. That's our sample of data. And we randomly select some values with replacement. With Monte Carlo, we're going to randomly set values, but from probability distributions, so these theoretical representations of how our data varies, OK? Talk more about probability distributions in these slides.  

[00:04:11.95] We did this already. With the bootstrapping, recall this is introduced in the context of dice rolls, rolling two dice, summing them up. And in order to perform this random selection of data, we use the random choice function. And we used it a lot after that, too where we're randomly sampling from, in this case, the sample being called rolls. Number of samples we select, size n, and then replaceable is true, saying we're going to sample with replacement. Take one, read the value, record it, put back. And this is what we got, graphically speaking.  

[00:04:51.61] With Monte Carlo, we've done this, too, in the same context. We used Monte Carlo, actually, to come up with the theoretical distribution of what we should see there. We simulated two dice rolls using this random randint function, if you recall that. What this is doing is it's just simulating an integer between 1 and 6. You put in 7 because it's going to be one more than the end number that you want. And so, since a single dice roll is just a random selection of 1, 2, 3, 4, 5, or 6, this works in the exact same way. We do that for the second dice, too, and we add them up.  

[00:05:41.44] And so, this randint, this is basically our probability distribution. We're saying that we've got an even probability of getting a 1 as a 2, as a 3, as a 4, as a 5, as a 6. With all that equal probability of occurring, let's see. We'll randomly select one based upon that even weight. And this is based upon our expert knowledge of the physics of the system. We are expert dice-rollers. We know how they work. We know that we have an equal chance of getting 1, 2, 3, 4, 5, or 6. We roll the dice.  

[00:06:22.96] And then we got-- this is what we got with that, the distribution of the sum of two dice. That was our output. And so, this is a form of Monte Carlo, simulating two inputs, D1 and D2, performing some operation on them, and then looking at how the outcomes vary, what the distribution of the outcomes are. So, we've done this already. We've already done Monte Carlo. Did you guys know that we did Monte Carlo already?  


Try It: OPTION 1 GOOGLE COLAB

  1. Click the Google Colab file used in the video here.
  2. Go to the Colab file and click "File" then "Save a copy in Drive", this will create a new Colab file that you can edit in your own Google Drive account.
  3. Once you have it saved in your Drive, try to implement the following code to import a file of your choice by mounting your Google Drive:

Note: You must be logged into your PSU Google Workspace in order to access the file.

from google.colab import drive

drive.mount('/content/drive')

import pandas as pd

df = pd.read_csv('yourfilename.csv')

df # print the dataframe

Once you have implemented this code on your own, come back to this page to test your knowledge.


OPTION 2 : DATACAMP

 Try It: OPTION 2 DataCamp - Apply Your Coding Skills

Dictionaries are a quick way to create a variable from scratch. However, their functionality is limited, so we will often want to convert those dictionaries into DataFrames. Try to code this conversion in the cell below. Hint: Make sure to import the Pandas library.

# This will get executed each time the exercise gets initialized. # Create a Simple Dictionary mydict = {'Name':['Amy', 'Bob', 'Clair', 'Daisy'], 'Birthday':['9/3/1991', '4/21/1988', '4/21/1990', '11/11/1989'], 'Age':[31, 34, 32, 33]} # convert the dictionary to a DataFrame # print the values in the 'Birthday' column # Create a Simple Dictionary mydict = {'Name':['Amy', 'Bob', 'Clair', 'Daisy'], 'Birthday':['9/3/1991', '4/21/1988', '4/21/1990', '11/11/1989'], 'Age':[31, 34, 32, 33]} # convert the dictionary to a DataFrame import pandas as pd mydataframe = pd.DataFrame(mydict) # print the values in the 'Birthday' column mydataframe['Birthday']


Assess It: Check Your Knowledge

Knowledge Check (replace question)