Monte Carlo Simulation and Python 4 - Plotting with Matplotlib
Key Takeaways
This video covers the basics of using Matplotlib to visualize Monte Carlo simulation results in Python, specifically plotting the trajectory of a simple betting strategy.
Full Transcript
hello everybody and welcome to the fourth mon Carlo uh within Python tutorial video in this video what we're going to be working on is visualizing our um our betters and seeing more than just the end result we actually want to see the trajectory uh as well uh so this will be especially useful when we start comparing different better types and assessing the volatility of their strategy as well uh since this kind of volatility in a strategy or Draw down in a strategy uh often denotes some form of risk or added risk the more volatile the strategy is um so at this point even though we've got a better some betters that just really they're all using the same strategy we do know that they end up at different points but we haven't really seen um how they got there so for this we're going to actually plot the full uh the full path and you'll need matplot lib if you don't already have it if you've watched many of my tutorials you probably already have matplot lib if you don't to acquire it go to matplot li. org and I don't know why they don't have download like right up at the top but for some reason they don't and so scroll down a little bit um okay and here you can download the latest version uh either get uh you've got Mac OS you've got 64bit here and you've got your 32bit here uh so once you have map plot lib downloaded you should be be able to open up a console type import map plot lib and it should work if it doesn't work you something went wrong so anyway luckily it comes with aexe so just download it and install it easy enough once you've done that um you can begin with the video all right so once you have that uh let's go to the top and underneath import random we're going to need uh map plot lib of course and we're also going to import matplot li. pyplot uh as PLT next we need to generate the list of plots that we're going to plot so under a simple better what we want to add here is just under wager we'll just say WX so we'll say wager and that's the X variable and then we've got value and that's the Y variable so that's how I'm just going to denote them lowercase w uppercase x lowercase V uppercase y uh the next thing I want to go ahead and do is we're going to actually be plotting the current wager so since we're doing 100 Wagers it just makes sense to make the current wager one and then wager count or while current wager is less than or equal to wager count so this will start at one and end at 100 just uh so when we're looking at things it just ends at 100 and makes sense to our eyeballs so uh once we've done that the next thing that we want to do is if let's see well under both of these we need to do some work here so we'll do WX do aend and so again this was the uh wager count so we just want to append current wager and then W or I'm sorry V uppercase y do append and this is where we append the value um so value so we do that after we've uh rolled the dice and found out our odds and then again down here we just do the same thing or found out where we left off so those and then finally at the end of all of this we just want to instead of print funds we don't need to do that anymore instead what we're going to go ahead and do is um PLT do plot and what we want to plot is wxvy okay so that's all we want to do and then everything else runs the same so while X is less so it's going to do this and it's going to keep plotting but we don't see the plot yet so the next thing we want to do is go ahead and actually plot this so we'll come down here and we'll just say We'll add some labels to it Just for kicks plot. y label so our y label is whoops is account value and then plot. X label our X label is wager count and finally uh we'll just run a plus show okay so we should be able to save this and what this is going to do is basically with a sample size of 100 betters it's going to run through with a simple better strategy um and it's going to plot up each step of the way so the whole trajectory for uh what we've got is a starting amount of 10,000 wager size of 100 and it's GNA is that right yeah wager size of 100 and it's going to do 10,000 Wagers let's just start it uh at a smaller 100 Wagers uh so we'll save that and let's go ahead and run it see if we run into any problems nope we didn't so here's our plot each line is a different better and so as you can see uh so far we have kind of a hard time recognizing with our eyeballs that there is any sort of uh house Edge so with a short number of Wagers like a 100 we don't really see those odds too like black and white we do obviously see a few of these betters lost some money but no one went broke and some people made money and all of that so let's go ahead and close this one and let add a few more Wagers so let's say that we do a th Wagers and we'll run that one and now we can see that there's significantly more obviously wager uh not significantly more Wagers but the same number of actual people doing the Wagers so we've got 100 you know people that are gambling here and maybe now you can start to see that it's got a little bit of a downward slope but what if we uh increase this again and now now we're doing 10,000 Wagers this one might take it there we go and now it's even more obvious what's happening but again we have some people here like this guy this blue guy that at one point was even over $20,000 and it looks like he ended at about $50,000 so obviously you've got some successful people but we can see after about the 4,000 Mark quite a few people actually went broke in this one um and then all the way down here where we've got people that lost way more money than they even started with so that should all have been fairly um expected there should be no surprises with this one uh we'll even do let's see we can make it uh let's get a th betters instead and say have 10,000 we'll do 100,000 I'll run this and I'll just pause it while we run it let's see if anybody made profit all right so here's our example it took a little bit to come up but anyway as you can see uh literally no one made money with this strategy so I think this brings the point uh brings the point home that literally without some sort of strategy on a long enough timeline everyone loses there wasn't a single person who actually uh made a profit so if you're in some sort of gambling scenario uh that has an edge obviously you will lose over time there's no one that's lucky enough to to actually have made money here initially some people did make money but eventually whatever risk you're taking on even if that's a 1% risk on on a long-term scale it always catches up it just always does um so this is just a good example of that now again it shouldn't be too surprising to you guys but also you should be able to see here uh such a wide range of outcome after you know 100,000 so even though um Everybody did lose was still quite the uh difference in how heavily some of these people lost so that's going to conclude this video in the next video we're going to begin creating our new better and this better is going to do what a lot of people decide to do and that is to double up on every loss so say you're trading with $100 uh each time you lose you just double up so you're you're doing or you're you're betting $100 if you lose now you bet 200 if you lose again now you bet 400 if you win you go back to betting 100 and so on uh so we're going to do that some people uh believe that gives them some sort of better odds or it's a way to kind of scheme the system which again is kind of a bit of the gamblers fallacy since people think the odds somehow get higher you still only have 50/50 odds but it is a form of a strategy so we will indeed see how good that strategy actually is so anyways uh hopefully that sounds exciting to you guys stay tuned to the next video as always thanks for watching
Original Description
Monte Carlo Simulation with Python Playlist: http://www.youtube.com/watch?v=9M_KPXwnrlE&feature=share&list=PLQVvvaa0QuDdhOnp-FnVStDsALpYk2hk0
In this video, we cover some of the basics of using Matplotlib to chart our Monte Carlo generation results.
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
More on: AI Pair Programming
View skill →Related Reads
📰
📰
📰
📰
Can AI Badger Reduce Local Coding Agent Token Usage?
Dev.to AI
Devin, the "First AI Software Engineer," Failed 86% of Its Benchmark Tasks, and Then What
Dev.to · Alex @ Vibe Agent Making
How I Built Captrix AI with Codex and TestSprite
Dev.to · ayush002jha
The Collapse of ‘Who Understands, Who Maintains’ — A Software Landscape in the AI Age
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI