Monte Carlo Simulation and Python 18 - 2D charting monte carlo variables

sentdex · Beginner ·💻 AI-Assisted Coding ·12y ago

Key Takeaways

This video demonstrates how to use Matplotlib to create a 2D scatter plot of Monte Carlo simulation data, specifically to visualize the relationship between variables and their impact on profit. The code reads data from a CSV file and uses the csv.reader module to parse the data, then plots the data using Matplotlib's scatter plot function.

Full Transcript

what is going on everybody welcome to another monte carlo tutorial video with python uh where we left off we were generating some data and we were just saving it to a csv and basically the data in that csv is if the data is better than uh one percent or worse than negative one percent then we want to know what variables contributed to that data and then we save that to the csv so now what we want to do is we want to chart it up and visualize the data because what we're looking for is if there's any clumps of data either in positive or negative that can kind of give us a bit of an idea of variables that correspond to either the positive or negative changes so with that what we're going to do is i'm going to get rid of this i'm just going to literally delete everything in here i suggest you save yours just in case you want to revisit the code later on or you want to run your own tests so take a moment and save the data maybe just go file new and open yourself up a blank script anyway i'm going to clear my now now and start anew so what we're doing here is we're going to start plotting this stuff so of course if you want to plot you need matplot lib and then we want to import matte matte plot lib.pi plot as plt i assume everybody at this point has matplotlib since we needed it earlier in the video so anyway and then we also want to import csv because we saved this data to a csv now we want to graph this stuff so we're going to have a define graph empty parameter function and now we're going to say with open uh file name here we have money carlo liberal.csv with the intention to read and we're going to open this as monte carlo data is going to equal csv dot reader and what do we want to read well we want to read monte carlo with a delimiter delimiter of a comma because that was our delimiter so again we're using csv.reader which is we imported csv up here and basically this is just python's csv reading app or module basically so just a good way to easily read the contents of a csv file so when you use it you just have to tell it what is it reading and then what's the delimiter and it automatically understands new line and all that so we've got data's next what we want to do is for each line in datas we want to do something with this data just so happens we want to plot it um so we're going to need to explain what's what here so first we're going to say percent roi that is equal to the float of each line zero uh wager size percent equals flow each line one and what we've got going on here is it's automatically being divided up by the comma delimiter so each line and datas is spit out as a list and you've got chunks in that list and it's just so happens it's split up in the uh in the way that we set it up so anyway moving on um so the next part in that list is wager count and that is equal to the flow of each line two and then finally p color for plot color equals each line three just as a quick aside in typical debugging and all this stuff for um python and charting and matplotlib obviously the word color where it doesn't really correspond like before in this tutorial i wanted to use results oh and anyway or like return that was what i wanted to use um and that you can't do that because return is a you know built-in function same thing with color though color is a built-in parameter for you guessed it color of plots so you can't just use color you have to use something else like p color just keep that in mind when you name variables sometimes you might name a variable that runs over the name of a very important variable so each time we find this data what do we want to do with it well we're going to scatter plot it so plot dot scatter and for now we'll just do a simple 2d scatter plot so wager size percent that's what we care about and wager count finally color equals p color so we add those to the plots and then when everything's all said and done after the width uh we're going to plot show now we'll just have it automatically call graph save it run it and the file was actually i'm trying to think how big my file was but it was actually a pretty large file so anyways we have our plot now at least with my eyeballs um i don't see any correlation uh at all here this looks like a pretty random plotting um i mean there's some places where you've got groups but i mean you really have to grasp it at straws here if you think you're seeing any sort of pattern so i don't see any pattern but there is an unsaid variable so far here and that is how much money um so we know that this is more than one percent uh return on investment but we know nothing else we just know it's more than one percent um so we definitely want to dig further into this and the way we would dig further is by how much money did we make um you know what is that percent uh so that's the next thing that we want to look at so luckily we obviously have percent roi that's one of our variables so we can plot that but we also kind of want to plot it alongside this stuff so what we're going to do now is we're going to plot with matplotlib's 3d capabilities so that's what we're going to do in the next video so stay tuned for that also the 3d plotting is very intense for python so what i figured i would do is because this is random um it's also pretty hard to see anything of significance just right now anyways and i bet in the 3d we won't see too much so i went ahead and i've been building a file that is that requires five percent so it has to be a return on investment greater than five percent or less than negative five percent so i've been building that at the moment and so in my video i've got a couple of different things i'll show you the one percent file and i'll also show you the five percent and all of that so anyway that's what we're going to be doing the next video is plotting this on a 3d plot so we can actually see all of the variables here so that should be pretty interesting uh as always thanks for watching thanks for all the support and subscriptions and until next time

Original Description

Monte Carlo Simulation with Python Playlist: http://www.youtube.com/watch?v=9M_KPXwnrlE&feature=share&list=PLQVvvaa0QuDdhOnp-FnVStDsALpYk2hk0 Here we use Matplotlib to chart a 2D representation of our variables and their relationship to profit. In the monte carlo simulation with Python series, we test various betting strategies. A simple 50/50 strategy, a martingale strategy, and the d'alembert strategy. We use the monte carlo simulator to calculate possible paths, as well as to calculate preferred variables to use including wager size, how many wagers, and more. There are many purposes for a monte carlo simulator. Some people use them as a form of brute force to solve complex mathematical equations. A popular example used is to have a monte carlo simulator solve for pi. In our case, we are using the Monte Carlo simulator to account for randomness and the degree of risk associated with a betting strategy. In the world of stock trading and investing, people can use the Monte Carlo simulator to test a given strategy's risk. It used to be very much the case that only performance was considered, for the most part, to decide on a trader's value. Only until recently has the paradigm shifted to consider a strategy's risk more closely. Through this series, you will be able to see just how much random variability can affect the outcome, regardless of how "good" or "bad" a strategy might have been. http://seaofbtc.com http://sentdex.com http://hkinsley.com https://twitter.com/sentdex Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from sentdex · sentdex · 0 of 60

← Previous Next →
1 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

This video teaches how to create a 2D scatter plot of Monte Carlo simulation data using Matplotlib, to visualize variable relationships and their impact on profit. The code reads data from a CSV file and uses csv.reader to parse the data, then plots the data using Matplotlib's scatter plot function.

Key Takeaways
  1. Import necessary libraries (Matplotlib and csv)
  2. Read data from a CSV file using csv.reader
  3. Parse the data and extract relevant variables
  4. Create a 2D scatter plot using Matplotlib's scatter plot function
  5. Customize the plot as needed (e.g. add labels, titles)
💡 Using Matplotlib's 2D scatter plot function can help visualize complex relationships between variables in Monte Carlo simulation data, making it easier to identify patterns and trends.

Related Reads

📰
How technical interviews should change in the age of AI-assisted coding
Learn how to adapt technical interviews to the age of AI-assisted coding and improve the hiring process for software engineering roles
Medium · Programming
📰
Why I’m Building an AI Website Builder That Verifies Its Own Work
Learn how an AI website builder can verify its own work, increasing efficiency and accuracy in web development
Medium · Startup
📰
I made a Claude Code plugin that turns your project into a demo video
Create a demo video for your project automatically with a Claude Code plugin
Medium · Programming
📰
Poolside releases Laguna S 2.1, the open-weight coding model pitched as the West’s answer to DeepSeek and Qwen
Learn about Laguna S 2.1, a 118-billion-parameter open-weight coding model for agentic coding, and its potential to match or exceed larger models
The Next Web AI
Up next
Building In Public | Vibe Coding Tribe | Get real testers without paying
Tech Friend AJ
Watch →