Latest / Talk Python To Me / #423: Solving 10 different simulation problems with Python
Transcript
- 0:00Python is used for a wide variety of software projects.
- 0:03One area that's really gained a lot of momentum is in the computational space, including data science.
- 0:07On this episode, we welcome back Allen Downey to dive into a particular slice of this space,
- 0:13simulation problems in Python and physics and engineering in general.
- 0:17This is Talk Python To Me, episode 423, recorded June 6, 2023.
- 0:23Welcome to Talk Python To Me, a weekly podcast on Python.
- 0:39This is your host, Michael Kennedy.
- 0:41Follow me on Mastodon, where I'm @mkennedy, and follow the podcast using @talkpython,
- 0:46both on fosstodon.org.
- 0:49Be careful with impersonating accounts on other instances.
- 0:51There are many.
- 0:53Keep up with the show and listen to over seven years of past episodes at talkpython.fm.
- 0:57We've started streaming most of our episodes live on YouTube.
- 1:01Subscribe to our YouTube channel over at talkpython.fm/youtube to get notified about upcoming
- 1:07shows and be part of that episode.
- 1:09This episode of Talk Python To Me is brought to you by InfluxDB.
- 1:13InfluxDB is the database purpose built for handling time series data at a massive scale
- 1:19for real-time analytics.
- 1:20Try them for free at talkpython.fm/InfluxDB.
- 1:25And it's brought to you by the PyBites Developer Mindset Program.
- 1:28PyBytes' core mission is to help you break the vicious cycle of tutorial paralysis through
- 1:33developing real-world applications.
- 1:35The PyBites Developer Mindset Program will help you build the confidence you need to become
- 1:39a highly effective developer.
- 1:41Check it out at talkpython.fm/PDM.
- 1:44Hey, Alan.
- 1:45Welcome to Talk Python To Me.
- 1:47Welcome back.
- 1:48Thank you.
- 1:48It's a pleasure to be here.
- 1:49Pleasure to have you back.
- 1:50We were talking just a little bit before the show, realizing it's only been six years since
- 1:55you were on the podcast.
- 1:56How about that?
- 1:56Last time we talked about Python and engineering way back in 2017, which was a lot of fun and
- 2:03has some interesting lead-ins to some of the things that are actually created now.
- 2:08You were hinting I'm working on this project about simulating things with Python.
- 2:12Here we are talking about it.
- 2:13Yeah.
- 2:13I was just getting started then and the book has come out very recently.
- 2:18So we've kind of bookended the project.
- 2:20Absolutely.
- 2:21Well, it's quite a project and it's a very interesting one.
- 2:25I think a lot of people out there, they hear maybe they should use Python instead of, I don't
- 2:31know, MATLAB or some other language for modeling their domain, right?
- 2:36And with your book that you put together, which is available for purchase, or you can
- 2:42read it online for free.
- 2:43That works for you.
- 2:44It has a bunch of different examples.
- 2:45How many examples?
- 2:46Maybe 30 or so?
- 2:47I think that's right.
- 2:48There are examples in the chapter and then case studies that get into more detail.
- 2:53Yeah.
- 2:53So there's a lot of different areas of specialization where you're bringing Python to answer problems.
- 2:59And we're going to have a lot of fun talking about that.
- 3:02But let's just do a quick catch up.
- 3:04It's been six years since we last spoke.
- 3:06What have you been up to?
- 3:07Well, I've changed jobs a couple of times.
- 3:09I was a professor at Olin College for 19 years.
- 3:13I left at the end of last May.
- 3:16And then I was at Driven Data for a little while, working on data competitions and machine
- 3:23learning.
- 3:23And then recently I've joined Brilliant, where I'm doing curriculum design.
- 3:28That sounds very fun.
- 3:29You know, first of all, leaving college you were at for 19 years.
- 3:33That's a big deal.
- 3:34Yeah.
- 3:35I've been an academic pretty much all the way through.
- 3:37You know, I graduated from college.
- 3:39I went to grad school.
- 3:40I had my first job.
- 3:41I was a faculty member at Colby College and then Wellesley College.
- 3:46And then, as I said, Olin for 19 years.
- 3:48So it's a real change of career.
- 3:50How's it going?
- 3:52It's good.
- 3:54I'm learning a ton.
- 3:55You just, you know, whenever you start something new, you're always, you're on this, you know,
- 3:59the steep part of the learning curve, which is challenging, but it's also just exciting.
- 4:03You're getting exposed to different things, learning a ton.
- 4:06Yeah, absolutely.
- 4:07And working at Brilliant now, you know, Brilliant's a good company.
- 4:12They've been past sponsors of the show, which is great.
- 4:15And, you know, maybe just talk a bit about how you think about curriculum design, since
- 4:20that's what you're focused on there.
- 4:21How do you think about putting together small class, not semester-wide classes for people
- 4:27to learn?
- 4:28Yeah, that's a big part of it.
- 4:29It's a different format.
- 4:31It's smaller, as you said, it's interactive in the sense that the learner is interacting
- 4:37with these, you know, graphical elements and other ways of communicating ideas visually.
- 4:43We use a lot of animation and the text is deliberately very concise.
- 4:49We're trying to, you know, show, not tell.
- 4:52And that's a really different kind of curriculum design for me.
- 4:55I'm used to, you know, I have a semester-long course.
- 4:57I've got several hours a week.
- 5:00It's very different.
- 5:01And that's part of the challenge that was appealing to me.
- 5:04Because I saw some of the courses and I was very impressed with the pedagogy and the opportunities
- 5:10that you have there with animation and sound and interactives.
- 5:14It's a very different kind of teaching.
- 5:15But it's not, there's no, I don't have any interaction with students.
- 5:20So that's a challenge as well.
- 5:21It's a big challenge.
- 5:22I went from teaching in person to teaching online as well.
- 5:26Not through Brilliant.
- 5:27But I think there's a couple of really big differences.
- 5:30One is when you're either, you have a student for a semester, Monday, Wednesday, Friday at 11 to 12.
- 5:38Or if you're doing a week-long training class in person.
- 5:40Those groups are captive, right?
- 5:43And so the constant need to keep their attention for every single minute is really different, right?
- 5:50Because it's not impossible, but it's rare that students just get up and walk out because, you know, there's a five-minute section they weren't engaged with.
- 5:57But they could easily leave a website.
- 5:59Yeah.
- 5:59It's particularly in a format like Brilliant, you know, there's not a certificate at the end.
- 6:04The motivation to keep going is you're having a good time and you're intrinsically interested in the topic.
- 6:12So from my point of view as an instructor, the challenge there is to maintain that.
- 6:16It has to be interesting, engaging all the time.
- 6:21As you said, if you've got, you know, a five-minute lull, you might lose your audience.
- 6:25Yeah.
- 6:25The other part that is a big difference is when you have those folks in class, even in a Zoom meeting to a degree, you get immediate feedback of how does an idea land?
- 6:35If I present it this way, are they engaged?
- 6:38If I present it, or are they clearly not getting it?
- 6:41Whereas you put it out into the world at Brilliant or Talk Python or wherever, and they've got to, you take a guess and you put it out there, right?
- 6:49You don't get that feedback.
- 6:50Yeah.
- 6:50Not directly.
- 6:51That's definitely challenging.
- 6:52We do have some signals where we can see if a learner is getting stuck at a particular place or if we see a lot of drop-off on a particular lesson, we can go in and try to figure out what's happening.
- 7:03We get a little bit of explicit feedback.
- 7:05Sometimes the learners will tell us what's wrong.
- 7:07But most of the time, you're kind of, it's on you to make sure that that lesson works.
- 7:12And your colleagues were constantly reviewing each other's lessons and looking for those places where we think we might lose learners.
- 7:19Excellent.
- 7:20Well, I'm sure it's a lot of fun.
- 7:21Some nice comments in the live chat out here.
- 7:24People appreciate that as well.
- 7:26So one thing that you said you learned, which I haven't really learned before, is I don't even really know what it looks like, is Elm.
- 7:35Tell us about Elm.
- 7:36What is this?
- 7:36This is a programming language.
- 7:38As it says, it's primarily for web applications.
- 7:41It is a functional programming language.
- 7:44And it's mostly a stateless computational model.
- 7:47So it's very different from Python.
- 7:50I've been learning it.
- 7:51This is what we use for a lot of the interactive elements.
- 7:53And it was new to me.
- 7:56Honestly, I had not even heard of it.
- 7:58But it's been great.
- 8:00I just, you know, it's been a little while since I learned a new language.
- 8:03It's been about 23 years since I learned a new language.
- 8:06And I've done functional programming, but I haven't used it, you know, really for applications.
- 8:12So this has been, you know, challenging but fun.
- 8:15It looks a little bit like Lisp meets HTML.
- 8:18That's not bad.
- 8:20Yeah.
- 8:20Okay.
- 8:21Certainly the semantics is very Lisp-like.
- 8:23Syntax can be, although not too many parentheses.
- 8:27Not as many.
- 8:29You get that blister on your pinky or whatever finger you use for the parentheses.
- 8:32Maybe your right index figure.
- 8:34Right.
- 8:35Or ring figure.
- 8:35Yeah.
- 8:36Yeah.
- 8:37And then the syntax, it has a couple of nice features like pipes.
- 8:40So, you know, taking a value and piping it through a sequence of functions.
- 8:44And of course, you know, functional features like passing around functions as first class
- 8:48objects and partial function evaluation.
- 8:50Is this a front-end language, like a JavaScript?
- 8:52Or is it a back-end language, more like Python?
- 8:54Yeah.
- 8:54No, front-end, it's getting compiled into JavaScript.
- 8:56Okay.
- 8:57But it is strongly typed, like extremely strongly typed.
- 9:00Like integer and float are incompatible types.
- 9:03There we go.
- 9:05So very strongly typed.
- 9:06But that, along with a lot of error checking, gives you some more confidence that if you put
- 9:12this on a web page and a large number of people interact with it, there are maybe fewer errors
- 9:17that can get past a compiler.
- 9:19You know, a really dynamically typed language like Python, you're rolling the dice.
- 9:24You deploy that code.
- 9:25You know, some of those lines of code have never run.
- 9:28And the first time they do, you might be in trouble.
- 9:31The type hints are helping.
- 9:32mypy is helping, but that's not a runtime thing.
- 9:34Pydantic, for example, is a runtime version that option that helps a lot.
- 9:39Before we move off this front-end stuff, then we'll get to the modeling.
- 9:41Have you been tracking PyScript and the front-end Python stuff?
- 9:46Is that interesting to you?
- 9:46It definitely is, and especially from a teaching point of view, getting people into Python.
- 9:51One of the challenges, you know, from way back has always been, how do you get a new programmer
- 9:57to the point of executing their first line of code?
- 10:00That is definitely not as big a barrier as it used to be.
- 10:04It used to be.
- 10:05You know, you had to install things and download things and all that.
- 10:08But yeah, the lower that barrier, the better.
- 10:11And yeah, I think PyScript is one of the things that can help to lower it.
- 10:14Yeah, I do too. I'm very excited about PyScript.
- 10:16It had been interesting in the realm of kind of different ways to execute notebook-style code
- 10:22and so on when it was based on Pyodide.
- 10:25And now that they're talking about moving to MicroPython, it starts to get into the realm of it.
- 10:30It could download the same size as Vue or Angular almost.
- 10:34And then it becomes, maybe you can make real apps with this thing.
- 10:38So I'm looking forward to see where that goes.
- 10:39Oh, interesting. I had not heard about that. So that's exciting.
- 10:43Yeah, yeah. One of the big things that they're trying out, I had a couple of the folks on from various projects four or five months ago,
- 10:49is they're trying to, instead of using full CPython as the foundation, to use MicroPython,
- 10:54which means the download goes from like 10 megs to 100K.
- 10:58Yeah.
- 10:59When you're talking 100K download, you're like, you know, that's kind of like a big JavaScript.
- 11:03And if it's over a CDN and it's cached, you know, maybe you could just make that part of your app in general, not as a specialized thing.
- 11:09Yeah, that's interesting, particularly because I think one of the other challenges for Python right now is it takes a while before you're doing really interesting stuff.
- 11:20And, you know, JavaScript, for example, you can immediately start doing front end kinds of interactions.
- 11:27And a beginner can start to write programs where it resembles the kind of apps that they're using.
- 11:34And I think that's a pretty important hook.
- 11:36For Python, that's a long path.
- 11:37And so it sounds like this could shorten that path quite a bit.
- 11:40It sure could. That would be very interesting.
- 11:42And I think your insight there comes in as a bit of a teacher as well, right?
- 11:48Like it's one thing as an engineer, like I'm going to build this thing, so I'm going to choose this language for such and such.
- 11:53But as a teacher, you realize like if you don't have a win early enough in the learning cycle, then you might just bail.
- 11:59And for a lot of people, a win is I have a UI.
- 12:02I have a thing that looks like an app that I could see on my phone or I could see in my web browser or something like that, right?
- 12:09Yeah, definitely.
- 12:10Or a robot or some interaction with the real world.
- 12:13But yeah, converting Fahrenheit to Celsius is not going to do it.
- 12:18No, but Celsius to Fahrenheit, that one is the one.
- 12:20That's the one.
- 12:21Okay.
- 12:21But speaking of converting numbers and so on.
- 12:26This portion of Talk Python To Me is brought to you by Influx Data, the makers of InfluxDB.
- 12:33InfluxDB is a database purpose built for handling time series data at a massive scale for real-time analytics.
- 12:41Developers can ingest, store, and analyze all types of time series data, metrics, events, and traces in a single platform.
- 12:48So, dear listener, let me ask you a question.
- 12:50How would boundless cardinality and lightning-fast SQL queries impact the way that you develop real-time applications?
- 12:57InfluxDB processes large time series data sets and provides low-latency SQL queries, making it the go-to choice for developers building real-time applications and seeking crucial insights.
- 13:08For developer efficiency, InfluxDB helps you create IoT, analytics, and cloud applications using timestamped data rapidly and at scale.
- 13:18It's designed to ingest billions of data points in real-time with unlimited cardinality.
- 13:23InfluxDB streamlines building once and deploying across various products and environments from the edge, on-premise, and to the cloud.
- 13:31Try it for free at talkpython.fm/influxDB.
- 13:35The link is in your podcast player show notes.
- 13:38Thanks to Influx Data for supporting the show.
- 13:42Let's talk about your latest book.
- 13:44You have a lot of books over at Green Tea Press.
- 13:48This is your home for where your books go, right?
- 13:51Yeah.
- 13:52As well as links to other ones you've written at places like O'Reilly.
- 13:54Right.
- 13:55Yeah.
- 13:55So, everything starts here and there's a free version of all of the books and you can get to it from here.
- 14:01You can see kind of the evolution of my process because some of the earlier books, you know, some of them look better than others.
- 14:06Let's put it that way.
- 14:08And then links there for the ones that have been published, there are hard copies available and electronic versions in convenient format.
- 14:16I think we might have spoken about this six years ago, but it was six years ago and I'm not sure and I imagine a lot of people don't remember it.
- 14:22Did we talk about your textbook manifesto?
- 14:24What is this?
- 14:25That was what really got me started and came from my frustration with existing programming books, but also just textbooks in general.
- 14:35The way they're used in a lot of classes, they are expensive, they are way too big.
- 14:40You know, if there are 700 pages or a thousand pages, nobody's going to read that in a semester.
- 14:46And if you're assigning 50 pages a day, I think, you know, you're kidding yourself and the students are not getting much out of that.
- 14:55So this is the idea that you can write a book that your students can read and understand and that is at the size where you can do something like a flipped classroom where they are actually going to do some reading before class.
- 15:10Not 50 pages, but 10 maybe.
- 15:13Come to class.
- 15:13Now, as an instructor, I can help to figure out, you know, what are the problem spots?
- 15:18What should we cover in class?
- 15:19What can we do to practice those ideas in class to take advantage of the time when we're actually in a room together?
- 15:25So, you know, read when you're on your own.
- 15:28Come to class.
- 15:29Let's do things that are more useful than me standing at the front of the room talking to you.
- 15:33Yeah, sure.
- 15:34Okay.
- 15:35That makes a lot of sense.
- 15:36So let's dive into your book, Modeling and Simulation in Python.
- 15:40Now, as I'm going through this, I'm not sure what I would classify it as.
- 15:45Is this a science book?
- 15:47Is this an engineering book?
- 15:48Is it a programming book?
- 15:49It kind of feels like it's a little bit of all of those.
- 15:52How would you, what's your elevator pitch here?
- 15:54Yeah, it's that.
- 15:55It is the solution to a chicken and egg problem, which is that in an engineering curriculum, a science curriculum,
- 16:02and maybe even more broadly, I think, for teaching physics and math to almost anyone, a computational approach is really appealing.
- 16:12There are things that you can do with computation that are very difficult to do analytically.
- 16:17You can do more interesting things faster.
- 16:21So that's the appeal.
- 16:22The hard part is most people are not coming into college with programming experience.
- 16:28Some do, most don't.
- 16:30So whatever your curriculum is, but let me focus on engineering because that's where I was.
- 16:34You've got a cohort coming in who have programmed, a cohort who have never programmed, and then kind of maybe a middle.
- 16:41Maybe they've done like HTML or something like that.
- 16:43And now I want to teach them physical modeling, but I have to do kind of three things at the same time.
- 16:49The modeling piece, the physical models, the domain knowledge, you know, if it's a scientific system or an engineering system, whatever it is, and programming.
- 16:59And there's no good sequence.
- 17:01Like I can't send people out to take a computer science class, and then they come back and we do computational science.
- 17:10It's just there are a number of reasons that that doesn't work.
- 17:13And same thing if they go, let's say they go take calculus or linear algebra, and then they come back.
- 17:18Collecting that knowledge and then trying to synthesize it is really hard.
- 17:22And it means it's a sequence of courses instead of starting in right from the beginning.
- 17:27So those are some of the problems that we were working with.
- 17:30And the best solution, at least we think, is put it all together.
- 17:35We're going to teach people how to program using these physical models as examples.
- 17:40And so they're getting some mathematics, not a ton, kind of a gentle introduction to differential equations.
- 17:47Some physics, but it's a bit more broad because it's not just mechanics.
- 17:52And then some programming, but it's really a subset of programming that's focused on those domains.
- 17:59So that was my best explanation.
- 18:01Oh, it's good.
- 18:03It's good.
- 18:04I can see how there's not a real great solution for that in a college curriculum, right?
- 18:08You send them off to take a CS class, and they might come back of having done an introduction to programming course,
- 18:14but still not really be able to do too much, or an algorithms course.
- 18:17And like, I can really tell you about a doubly linked list.
- 18:21You're like, all right, well, you're still not ready.
- 18:22I'm sorry.
- 18:22You know, right?
- 18:23It's a challenge to bring these things together.
- 18:25I think that's generally a challenge in education a lot is to cross these things over and why a lot of people feel like certain things are, that's useless.
- 18:33I'll never use that.
- 18:34That doesn't have any value to me because it's taught in such an abstract, isolated silo.
- 18:40Yeah, and you never close the loop because we're making a promise.
- 18:43If we send a student to take a prerequisite class, the contract is, you go do this thing, and then when you come back, we're going to make it pay off.
- 18:53You're going to see the benefit, and we never do.
- 18:56It never pays off.
- 18:57It's tough.
- 18:59It's tough.
- 19:00So, like we said, there's about 30 different problems in here, like population growth.
- 19:05How long does it take for your coffee to cool?
- 19:08What's the optimal way to transfer it to work?
- 19:10Glucose, disease modeling, as well as vaccines.
- 19:14People can check out.
- 19:15So, I thought it might be fun for us to just pick out however many we get through.
- 19:19Let's say we take a shot at 10.
- 19:21We'll see if we get there.
- 19:2210 of these and just kind of talk through what is the problem?
- 19:25What are some of the Python ideas at play here?
- 19:28Maybe what are some of the math or science ideas as well?
- 19:30So, you want to start with world population?
- 19:33Sure.
- 19:34Yeah, this one was interesting for me.
- 19:36It's something I didn't know very much about, so I got to learn.
- 19:39I think it's a sweet spot of you don't need a lot of computational tools because this one comes very early in the book.
- 19:47But you can study a real system that's happening right now and get to understand it.
- 19:52So, it's a discrete system.
- 19:54So, we're just counting the number of people and modeling population growth.
- 19:59And so, the number of new people in any year is going to be some function of the current population.
- 20:05And so, we can introduce a couple of different models.
- 20:08You know, starting very simple.
- 20:10One of them says, what if we have the same number of births and deaths every year?
- 20:15Well, okay, the population would grow linearly or maybe shrink linearly, but it would be linear growth, constant slope.
- 20:21All right?
- 20:22What if it's proportional?
- 20:23What if we have a birth rate that's proportional to the current population?
- 20:28Some people might know.
- 20:29You're going to get exponential growth.
- 20:31And I think if you ask people what's going on in the world right now, they probably believe that we are experiencing exponential growth.
- 20:40That was my thought when I read this.
- 20:41I thought, oh, yeah, okay.
- 20:42So, we'll probably talk about geometric growth and those types of things.
- 20:45Yeah.
- 20:45Right.
- 20:46Well, that's the model.
- 20:47And we try to fit that model to the data, and it doesn't.
- 20:50And so, that's where the first lesson comes from, which is you can do all the theory you want, and eventually you will collide with data.
- 20:57And your theory may or may not survive the impact.
- 21:00And that's what happened here.
- 21:02The population is currently not growing exponentially.
- 21:06Going back to about 1960, it's pretty close to linear.
- 21:10And that one's kind of weird because, okay, wait a minute.
- 21:13If it's linear, that means we have roughly the same number of births and deaths every year.
- 21:18But how?
- 21:19You know, how do you get 7 billion people to coordinate and make that happen?
- 21:23So, there are a couple of ways to think about what's happening.
- 21:26One of them is the logistic growth model, which starts out exponential.
- 21:31And then, as you approach a carrying capacity, you approach this limit.
- 21:35It approaches that limit asymptotically at the high end.
- 21:39And in the middle, there's a piece that looks linear.
- 21:42So, you could look at that model and you could, okay, maybe that's what's going on.
- 21:45Except it turns out that's not quite right either.
- 21:48And that's where the domain knowledge comes in.
- 21:50Because so far, it's just been programming and looking at data.
- 21:55At some point, you have to talk to a demographer because they actually know what's going on.
- 21:59And what's going on turns out to be very interesting.
- 22:01It's the demographic transition.
- 22:04And this is a social and economic phenomenon, which is as societies, countries become more economically
- 22:12developed and have more personal freedom, fertility rates tend to go down.
- 22:16And they very often, it's often dramatic.
- 22:18It would be a shift from lifetime fertility of five, six, seven, or eight children down to very close to two.
- 22:26And 2.1 is the replacement level where population levels off.
- 22:30A lot of developed countries right now are at or below replacement in terms of birth rate.
- 22:36And so, the populations are leveling off or starting to decline.
- 22:40Yeah.
- 22:40There's going to be some knock-on economic effects.
- 22:42Although, I don't know that those were modeled in your book.
- 22:44But, you know, like, how does things like social security and support nets happen when the thing being supported is heavier or larger than the people doing the supporting, right?
- 22:55Right.
- 22:56Speaking of economies like Japan, for example, and so on.
- 22:59Yes.
- 22:59Yeah.
- 23:00They've been below replacement level for quite a while.
- 23:02So, in some sense, they are the predictor of what a lot of other countries are looking like now.
- 23:07And as you said, you know, vast economic consequences.
- 23:10Now, that's where I stopped.
- 23:12It's not an economics book.
- 23:14That's where it was, I think, fun for us to, you know, get to that point and say, look, okay, we have found some interesting things.
- 23:21And now, you know, go explore because there's a lot more to learn about this.
- 23:26Yeah.
- 23:26I doubt it would surprise the listeners that the standard data science pieces are at play here.
- 23:32Jupyter Notebooks, Pandas, Matplotlib, those kinds of things, right?
- 23:37Yeah, there it is.
- 23:38From Pandas, read HTML.
- 23:40Yes.
- 23:41Whenever we can, we try to get real data.
- 23:43One of the goals of this class is to connect students to the real world.
- 23:47And that's one of the places where data science is a real opportunity because the availability of data is great.
- 23:54And the computational tools for working with data are very good.
- 23:58Now, obviously, in Python, you know, Pandas is one of the popular libraries for data.
- 24:03There are others, you know, more generally, you know, there's R, you know, Matlab.
- 24:06This is a real, I think, teaching opportunity.
- 24:09One of the things I thought was interesting from a lot of these was the...
- 24:13The acquisition of the starter data for a lot of these.
- 24:16So, for example, in this one, you have...
- 24:19You're getting the world population estimates from a table.
- 24:22And I think this table originally comes from Wikipedia, but you've saved it locally because you're smart.
- 24:28And in case they change the format of that page, you don't have to rewrite your book.
- 24:32But, you know, it really highlights certain things that are just so easy if you know the way, but are not necessarily obvious.
- 24:39So, for example, here you have from Pandas, read, import, read, HTML.
- 24:43You can just go read HTML and you give it a...
- 24:46Either a URL or just a HTML file in this case.
- 24:50And then you can say, well, we just want the second table that was in that HTML.
- 24:54And it literally grabs out of Wikipedia.
- 24:57It grabs the table and turns it into a Pandas data frame, which is pretty remarkable, honestly.
- 25:02Yeah.
- 25:02Well, you know, I have mixed feelings about this because it is...
- 25:05Okay.
- 25:06It's great that we can get the data and it's easy with these tools.
- 25:10It's a little bit sketchy in the sense that, you know, as you said, we have to...
- 25:15What if Wikipedia changes the format?
- 25:16This is not exactly data that anybody intended to export.
- 25:22So, I think I would prefer to get this data from the original source in a format that was really intended.
- 25:30An HTML table is not exactly a data transport format.
- 25:33No, an Elm table.
- 25:35No, just kidding.
- 25:35I mean, it's fragile in a lot of ways.
- 25:38Somebody could insert a column, delete a column, more likely the insert one, right?
- 25:42To add, oh, we found a new data source.
- 25:43We added it.
- 25:44And all of a sudden, all of your stuff breaks.
- 25:45But it could even be as simple as we added more information to the Wikipedia page.
- 25:50And now there's a table that precedes this table in HTML.
- 25:52So, when you say, get me the second table, you actually get some unrelated thing, right?
- 25:56So, obviously, I mean, it's effectively web scraping behind the scenes, which is always fraught with instability.
- 26:03But you're taking advantage of what Wikipedia did, which is to collate all of these sources.
- 26:09People have gathered this and curated this collection of data.
- 26:13So, that's why I say mixed feelings, because it is a great opportunity to take advantage of that.
- 26:19But web scraping might not be the best way to do it.
- 26:21Yeah.
- 26:22Quick question from Marwan.
- 26:23Does the book include attempts to visualize the simulations, like modeling a bouncing ball becomes more rewarding if you can see it in action?
- 26:31Definitely.
- 26:31And that comes up in the third part of the book, which is about mechanical systems.
- 26:36And those are the things where, yeah, we've got objects moving in space.
- 26:39And that begs to be an animation.
- 26:42There's not a ton of it because it's a little bit.
- 26:45The tools for animation in Jupyter Notebooks using Matplotlib are a little bit clumsy.
- 26:52So, I didn't get deep into it.
- 26:54But definitely the point where you get to see the simulation and you see the objects moving in space, you, I think, appreciate it, for one thing.
- 27:03You also, I think, get to debug it because your eye is really good at seeing physical errors.
- 27:10If your physical model is doing something incorrect, you're going to see it instantly.
- 27:15It's going to bother you.
- 27:16Yeah.
- 27:16That does look right.
- 27:18And, you know, it's probably not necessarily a problem with the animation, with the modeling.
- 27:22But even things like acceleration and deceleration, if a thing just goes over and stops, you're like, oh, it didn't.
- 27:27That's not right.
- 27:27It shouldn't do that.
- 27:28It should slow down and come to a stop.
- 27:30It didn't hit an imaginary wall.
- 27:32This portion of Talk Python To Me is brought to you by the PyBites Developer Mindset Program.
- 27:40It's run by my two friends and frequent guests, Bob Belderbos and Julian Sequira.
- 27:45And instead of me telling you about it, let's hear them describe their program.
- 27:49Are you trying to code your way to a more flexible and impactful career but aren't getting anywhere?
- 27:55If so, the PyBytes Developer Mindset Coaching Program, PDM, can help get you unblocked.
- 28:02Imagine this.
- 28:03Building and deploying your own applications, mastering Python, and getting personalized one-on-one coaching every step of the way.
- 28:10We know the struggle of tutorial paralysis all too well.
- 28:15And that's why PDM focuses on real-world applications.
- 28:20Our mission is to help you build confidence as an effective developer.
- 28:24And remember, this isn't a bootcamp.
- 28:27It's a highly personalized, invite-only program.
- 28:30You won't be just another face in the crowd in some classroom, but a valued part of our inclusive community.
- 28:36And are you ready to give back to the open source world?
- 28:40With PDM, you'll gain the skills and the confidence to contribute meaningful code to the projects that matter to you.
- 28:49And it's not just about the coding.
- 28:51It's about joining a community of like-minded developers, fueling your intellectual curiosity and making a difference with your skills.
- 29:00So, are you ready to take your Python skills to new heights?
- 29:03We hope you are.
- 29:05So, please, apply now for the PDM program.
- 29:09With PyBytes, you don't just learn to code.
- 29:11You code to learn, to grow, and to give back.
- 29:16See you on the inside.
- 29:17Apply for the Python developer mindset today at talkpython.fm/PDM.
- 29:23It's quick and free to apply.
- 29:26The link is in your podcast player show notes.
- 29:28Thanks to PyBytes for sponsoring the show.
- 29:30All right.
- 29:33Next one to talk about is epidemiology.
- 29:37And in this one, you can talk about the freshman plague.
- 29:41People maybe with regard to college have heard of the freshman 15, like a certain amount of weight.
- 29:46A lot of people seem to put on from the stress of going to college.
- 29:49But the freshman plague is more about coming together in a big group from all over the place more than maybe people are used to coming from their hometown.
- 29:58Yeah.
- 29:59Well, we started this example.
- 30:01Well, let's see.
- 30:02I think we were working on this in 2017.
- 30:05But we were using this example previously.
- 30:08So before COVID is the point.
- 30:10And I guess at the time, it was sort of a fun example.
- 30:13Like, hey, everybody gets a cold during the first few weeks of the semester.
- 30:16Ha ha.
- 30:17And it's, you know, it's less funny now.
- 30:20But it's also definitely more relevant.
- 30:22You know, we're thinking about, you know, modeling an epidemic and modeling interventions.
- 30:28And in particular, understanding a concept like herd immunity.
- 30:31So if you're talking about, you know, public health and what, you know, what's our civic obligation, it's not just looking after yourself.
- 30:39There is a public consequence to your individual decisions.
- 30:44Yeah, absolutely.
- 30:45There sure is.
- 30:46And, yeah, this used to be somewhat of an interesting example.
- 30:50And then it became one of the biggest stories, you know, maybe of our lifetimes.
- 30:57Who knows?
- 30:58Anyway, so this is the Kermak-McKendrick model.
- 31:01And there's a couple of interesting things that I thought were interesting models here.
- 31:05So in this model, you have the susceptible folks who are not yet infected, but potentially could be.
- 31:11You have the recovered people.
- 31:12And you assume they have some kind of immunity, at least for their freshman year, I guess.
- 31:16And then the people who are infected.
- 31:17And you talk about this idea of having, what's it called?
- 31:21Stalk.
- 31:22Yeah.
- 31:23Stalk and flow diagrams.
- 31:26Those are pretty interesting because you think of the freshman class as kind of a fixed set, right?
- 31:33Mm-hmm.
- 31:34There are some different versions of the model.
- 31:36And right, Kermak-McKendrick.
- 31:37Nobody calls it that.
- 31:38Everybody just calls it an SIR model because those are your three.
- 31:42And you can extend it.
- 31:43There are additional groups.
- 31:45And what you're modeling is the flow of, in this case, it's people, from one stock to another.
- 31:52So someone who is susceptible, if they become infected, they move into the infectious stock.
- 31:59And when they recover, they move into the recovered.
- 32:01So this is the simplest model.
- 32:03And usually, it's a fixed population.
- 32:06The reason that this is where we start is that this lends itself to a differential equation that we can do some work with.
- 32:14We can't solve it analytically, but there are some useful analytic methods.
- 32:19And we can also work with it computationally.
- 32:21So this is one of several examples in the book where we do both.
- 32:24And that gives us a chance to show the strengths and weaknesses of analysis and computation,
- 32:31and also to do some validation by comparing the results from one and the other.
- 32:37So yeah, that's why this is where it is in the book.
- 32:39It's kind of the transition from the discrete systems in part one to the first order continuous systems.
- 32:47Yeah.
- 32:47Okay.
- 32:48One of the things that, I don't know, maybe I should have just known this or not,
- 32:51but what was interesting to me was thinking about applying differential equations to a discrete set, right?
- 32:58Like I think of differential equations of being either applied to like maybe an interval,
- 33:03all of the real numbers or the complex numbers or, but not to 90 things, you know, which that was surprising, I thought.
- 33:12And then your ability to model that in Python was pretty cool.
- 33:15Yeah.
- 33:16So I'm glad you mentioned that because it's a really useful modeling method.
- 33:21And it's a good example of the kind of modeling decisions that you're making all the time.
- 33:25And it can go in both directions.
- 33:27You can take something that you know is actually discrete, like people, and treat it as if it were continuous.
- 33:33And that works pretty well if you have a large population.
- 33:36Obviously, if you get down to very small numbers of people, it starts to break down.
- 33:39And that's where we can have a conversation about modeling and say, look, this is an approximation, right?
- 33:46This isn't how the world really is.
- 33:48But I can pretend, I can run the model, and those results can be useful even if the model isn't perfectly realistic.
- 33:56Because, you know, that's how models work.
- 33:58None of them are perfectly realistic.
- 34:00So that's, I think, one of the goals of the book is to engage the learner in those kinds of discussions about modeling decisions.
- 34:10Because I think that's missing in a lot of science classes.
- 34:14If you think about the traditional freshman physics class, you are told what the model is.
- 34:20You say, we're going to do a block on a frictionless plane.
- 34:23We're going to do a projectile moving in a vacuum.
- 34:26Well, the frictionless, that's a modeling decision.
- 34:29It says, we're going to temporarily ignore friction in order to solve this problem.
- 34:33But you as the learner didn't get to make that decision.
- 34:36You don't, you're not required to figure out the consequences of it or think about when it doesn't apply.
- 34:42And that's what we really wanted to get at here.
- 34:44Yeah, that's a really interesting point.
- 34:47And that's why a lot of people think math is just a bunch of formulas or, you know, science, hard sciences are often just a bunch of formulas.
- 34:55Then you just got to remember to use the formula.
- 34:57Because it just goes from this sterilized version, final version, without understanding, going from observation to making your way through it until you end up with something like this, right?
- 35:10Yeah, yeah.
- 35:11Yeah.
- 35:11Now, there's a scary version of this, which is that it's not objective.
- 35:15Because if there's one answer and your job is to figure out, you know, what's the right formula, apply the formula, get the answer at the back of the book, that's nice and objective.
- 35:26And, you know, we want our science to be objective.
- 35:29But regardless of what we want, it isn't.
- 35:32Because it's based on modeling decisions.
- 35:34All science is based on modeling decisions.
- 35:37And those are subjective decisions.
- 35:40It's deciding whether a particular model is suitable for purpose.
- 35:45It depends on what you're trying to do.
- 35:48If you want to make a general qualitative description of a system, maybe you can use a simple model.
- 35:53Maybe you can leave friction out of it.
- 35:55If you want to land an orbiter on Mars or a rover on Mars, you might need a more detailed model.
- 36:04Yeah, if you're off by 1% of 1 degree, it's going to matter by the time you get there.
- 36:10Yeah.
- 36:10Where's that playing in again?
- 36:11Yeah.
- 36:12Yeah.
- 36:12So in this one, you talked to using this, a couple of types and ideas in here with states and systems.
- 36:19And it sounds like in the book, it looks like in the book, you provided some foundational building blocks for modeling the mod sim module, right?
- 36:28So in addition to just having the books, you've got a bunch of Jupyter Notebooks that people can check out and as well as some code to kind of help this modeling here.
- 36:38You want to talk a bit about that?
- 36:39Sure.
- 36:39So this was my attempt to use some programming ideas to communicate modeling ideas by creating these objects.
- 36:48So one of them is a state object and it represents the state of a system.
- 36:52So if you think about an object moving through space, it has position and velocity.
- 36:58And at any point in time, if I write down the position and velocity, that's the state of that object.
- 37:04So in object-oriented programming, that begs to be an object.
- 37:08It is a state object that contains position and velocity.
- 37:12Now, under the hood, it is a pandas series that is mapping from names to values.
- 37:19Similarly with a system object, that's actually a Python namespace.
- 37:22So there are a couple of different things, but this one's a namespace.
- 37:26And it represents everything that you need to know about a system in order to model it.
- 37:32So it'll be the parameters of the system.
- 37:34If it's, let's take that projectile moving in space again, it might be mass, cross-sectional area, coefficient of drag, density of air, temperature of air, all the things to model.
- 37:45One of the examples is like a baseball in flight.
- 37:47Excellent.
- 37:48So in this one, you go through, you model, you run the simulation over time and eventually end up with a graph that shows, you know, a little over half of the students got lucky and didn't get the plague.
- 37:59But about half of them did at some point throughout the year or the semester or whatever it was.
- 38:03So that's, you can do a single run in order to simulate a hypothetical course of the disease.
- 38:10And now you can start to vary the parameters and say, you know, what if the disease is more infectious?
- 38:15What if the recovery time is longer or shorter?
- 38:18What if people start washing their hands?
- 38:20And what if we start vaccinating them?
- 38:23And so by varying the parameters now, you can see how the output of the model depends on the system parameters.
- 38:30And that's where another one of these objects comes in.
- 38:33We have a thing that's called a sweep series.
- 38:35It's a mapping from a hypothetical parameter to the output of the model using that parameter.
- 38:42Yeah, when you hear people talking about COVID, I remember them talking about the infection rate.
- 38:48You know, is it bigger than one?
- 38:49Is it less than one?
- 38:50And all of those things were very relevant here, right?
- 38:54Right.
- 38:54Yes.
- 38:55The R0 and R, the contact rate.
- 38:59And that term comes straight out of this model.
- 39:03That is the ratio of the two parameters here.
- 39:06So beta is the rate of infection, depending on how many people are susceptible and how many are infected.
- 39:12And gamma is the rate of recovery, which is the fraction of infected people who recover in a unit of time.
- 39:20And the ratio of those two things is the contact rate, the R0.
- 39:24Yep.
- 39:25All right.
- 39:26Well, way more relevant these days than it was a few years ago.
- 39:28Oh, yeah.
- 39:30We all know a lot more about epidemiology than we used to.
- 39:33We sure do.
- 39:35So my daughter was just enjoying going back and watching some of her favorite YouTubers videos from February and March of 2020.
- 39:43Just laughing.
- 39:44They were saying, oh, it looks like we're going to have to stay home for a week.
- 39:47And she's just like, they have no idea.
- 39:48They have no idea.
- 39:49Okay.
- 39:49All right.
- 39:51Let's go on to something more fun.
- 39:52One of my favorite things, coffee.
- 39:54Come on.
- 39:55Yeah.
- 39:56Awesome.
- 39:56And so tell us about the coffee cooling problem that you solve here.
- 40:00This one's a classic.
- 40:02The source that I'm aware of is a Martin Gardner column in Scientific American.
- 40:07And it posed this question, which is, if I get my coffee and I also have some milk and I'm planning to add the milk to the coffee, should I add it now?
- 40:17And so that they cool down together.
- 40:18Or should I drive to my destination?
- 40:21Let's suppose you're commuting.
- 40:22Should I get to my destination and then mix them?
- 40:25With the goal of getting the coffee down to a pleasantly drinkable temperature as quickly as possible.
- 40:31Okay.
- 40:32Yeah.
- 40:32You pick it up.
- 40:33Maybe you hit one of those drive-through coffee shack things that just stands on its own.
- 40:37And you're like, oh, it's always too hot here.
- 40:39How should I do this so that I can basically have a perfect coffee when I arrive where I'm going, right?
- 40:45Okay.
- 40:45Excellent.
- 40:46So this one involves Newton's law of cooling, right?
- 40:50Did I get that right?
- 40:51You got it.
- 40:52And the heat transfer.
- 40:53There we go.
- 40:53Newton's law of cooling.
- 40:54Another differential equation here.
- 40:56As they want to be in simulation.
- 40:59Yes.
- 41:00Right.
- 41:00So this is another one, which is a first order differential equation.
- 41:04You can do it two ways, which is you can break it up into discrete steps in time.
- 41:09And so your differential equation becomes a difference equation.
- 41:12And that's a natural thing for a programming language because now it's a loop.
- 41:18And during each time step, you figure out the difference between the coffee and the environment.
- 41:23That tells you the rate of change.
- 41:25And then that lets you make a prediction for the next time step.
- 41:29And so if you repeat that process, that is Euler's method for solving differential equations.
- 41:35So there's kind of a natural transition there.
- 41:37And then I think for this problem, we don't yet use the ODE solver.
- 41:42But then later in the book, we do.
- 41:44And that's trying to motivate, you know, why are we using this library?
- 41:48Well, you know, there are limits to Euler's method.
- 41:51At some point, it's not very accurate.
- 41:53Yeah, it might not be accurate enough for sure.
- 41:55And ODE, ordinary differential equations for people out there listening.
- 41:59You got it right.
- 42:00Thank you.
- 42:00Ordinary differential equation.
- 42:02And so, you know, Python has, you know, SciPy.
- 42:05SciPy has several functions that will solve differential equations for you.
- 42:10And so we're kind of, we're sneaking up on that.
- 42:12Yeah.
- 42:12Okay.
- 42:13It is interesting that you can say, yeah, sure.
- 42:15It's a differential equation.
- 42:16And those might sound scary and hard to solve and all those kinds of things.
- 42:20But you can subtract two numbers.
- 42:22You can do a simple thing.
- 42:24And looping is good.
- 42:25And so if you just make your loop have small enough increments, you're kind of doing the
- 42:29same thing that, you know, taking the limit to get to calculus does anyway, right?
- 42:33Yeah.
- 42:33We're kind of, we're short circuiting what I think is a really unnecessary loop, which
- 42:38is there's a lot of physical modeling where you take a system, you write a differential
- 42:44equation, but then you can't actually do anything with that differential equation.
- 42:49You can't solve it.
- 42:50And so you're going to have to use numerical methods anyway.
- 42:53And if you look at what the numerical method is doing, it's basically Euler's method.
- 42:58It's, you know, they're more complicated versions, but it's the same idea.
- 43:02We're going to take time.
- 43:03We're going to break it up into discrete steps.
- 43:05And we're basically simulating the system.
- 43:09So what we do in the book is cut out the middle and go straight to simulation.
- 43:14There's almost no reason to write a differential equation if you can't solve it.
- 43:19You might as well just start with a simulation.
- 43:21So what's the story with the coffee?
- 43:23How should I approach this?
- 43:24I'm not going to give it away.
- 43:25People got to earn it.
- 43:28They got to come figure it out, huh?
- 43:30You do have at the end.
- 43:31So one of the big challenges here is not like, how soon in my trip should I get the coffee?
- 43:36How long should I let it sit or any those kinds of things?
- 43:38It's one of the important parts is you have milk or cream you want to put into the coffee.
- 43:43You should decide if you put it into the hot coffee, the cold cream right away, that starts
- 43:49at somewhere other than if you maybe wait till you get to the office and then mix the cream.
- 43:53So people can come back and solve that about the milk.
- 43:56Right.
- 43:57Here's a question.
- 43:58This came up.
- 43:59One of the people I chat with on Twitter posed this question.
- 44:03It's a variation of this.
- 44:04He said he was disagreeing with his wife about using an electric kettle.
- 44:08The question is, let's say that you heat up a kettle full of water.
- 44:13You use half of it.
- 44:15And now the other half is there.
- 44:17You have a stay warm feature where you can set the kettle so that it keeps the water at,
- 44:23let's say, tea making temperature for you.
- 44:25Or you can shut it off, let it cool down.
- 44:28And then later on, when you want to make tea, heat it up again.
- 44:31Now, let's leave out of it for now that the time that it would take to heat up again.
- 44:36Let's say you don't care about that.
- 44:37You just want to know about energy consumption.
- 44:39Is it better to keep the water hot or let it cool down and then you have to use all that
- 44:45energy to heat it up again?
- 44:46Do you want to get in on this?
- 44:48Yeah.
- 44:48My guess is it's better to let it cool down because the wider temperature gradient you
- 44:54keep between the ambient air and the water, the higher rate of loss of temperature, which
- 45:00you actually had a really interesting piece, tidbit of information here.
- 45:05You said, okay, if the coffee cools from, you know, cooking temperature down to drinking temperature,
- 45:10that's a ton of energy.
- 45:11It says to give you a sense, if you're able to harness all of that heat to do work, which
- 45:16you can't because of inefficiencies of engines.
- 45:18But if somehow you had a magic levitating engine that converted a heat energy to potential
- 45:24energy, you could use this lost energy to lift the cup of coffee from sea level to 8,500
- 45:31meters just under the height of Mount Everest.
- 45:33That's an insane amount of energy when you think about it.
- 45:36That is.
- 45:36And honestly, I did this calculation 10 times and I'm still not sure.
- 45:41It's got to be wrong.
- 45:42No, it's awesome.
- 45:43I love it.
- 45:44I believe that this is true.
- 45:47But if someone contradicts me, I would be grateful because I just want to know.
- 45:51But at least according to the calculation I did, the difference between mechanical energy
- 45:56and heat energy is just, it defies your intuition.
- 46:00And it's really, it's thermodynamics that is the difference because to take something from
- 46:06sea level up to the top of Mount Everest is organized energy and heat is disorganized energy.
- 46:13And what that means is, you know, you can have, you can have quite a lot of heat and
- 46:17not be able to do much work with it because it's disorganized.
- 46:20Yeah.
- 46:20Okay.
- 46:21Excellent.
- 46:21Before we move on this one, I remember you were, I can't remember where I said, I think
- 46:25it might've also been Scientific American, but there was a debate about coffee of whether
- 46:29blowing on it, if it was the cool air hitting the water that would cool it, if it was just
- 46:35the evaporation.
- 46:36And someone did an experiment where they, they put a fan on it with cool background air
- 46:41and they put a hairdryer with hot air blowing on it.
- 46:45And it's still, it's the evaporation.
- 46:46It's like these state changes and the, this energy of this heat loss.
- 46:50It's, it's pretty powerful.
- 46:51Yeah.
- 46:51The latent heat of vaporization is, is a huge number, which is good because that's what
- 46:56keeps us cool.
- 46:56That's why sweat works.
- 46:58That's right.
- 46:58That's right.
- 46:58It's good.
- 46:59We're coming up on summer.
- 47:00So we're going to appreciate that.
- 47:01I'm sure.
- 47:01All right.
- 47:02Onto the next one limits to growth.
- 47:05Tell me about this one.
- 47:06Yeah.
- 47:06So this is related to the population growth that we were talking about earlier.
- 47:11And this is developing the logistic growth model, but then trying to figure out, is that
- 47:17really what's going on?
- 47:19Is, is human population leveling off because we are hitting resource limits?
- 47:24Like, are we running out of food or, or other resources?
- 47:28The answer for now is no.
- 47:30I mean, we can certainly look at places in the world where resources are scarce, but
- 47:35that's not what's limiting growth.
- 47:36Interestingly, it's almost the opposite.
- 47:38It is the places that are most economically developed, where resources are most available,
- 47:44where fertility rates are the fastest.
- 47:47Okay.
- 47:47And, and it's not being driven by death rate.
- 47:49That's the other thing.
- 47:50I think the other reason that we know that what we're seeing is not a resource limit because
- 47:56that would drive up the death rate.
- 47:58Yeah.
- 47:59And that's not the case.
- 48:00The death rate is dropping at all ages in all places.
- 48:03Well, with a few exceptions.
- 48:05Yeah.
- 48:06Yeah.
- 48:06Of course.
- 48:07So this one's kind of about finding that equilibrium.
- 48:09Yeah.
- 48:10This is taking the model to its limit and saying, okay, if we use the logistic model, even though
- 48:17we think physically, this isn't exactly what's happening.
- 48:19Can we make a prediction about where we think world population will level off and possibly
- 48:25even start to fall?
- 48:26And one thing that surprised me is that even though this model is very simple and not realistic,
- 48:32the predictions that come out of it are not wildly different from what we're getting from
- 48:38real demographers.
- 48:39So the UN and the US census both have projections for world population out to about 2100.
- 48:47And they predict world population will probably level off around 2100 at somewhere between 9 and 11 billion people.
- 48:56And this very simple model is not too far off from that.
- 49:00Yeah.
- 49:00Okay.
- 49:01That's a pretty wide range.
- 49:02You know, a lot of things can happen.
- 49:04Yeah, sure.
- 49:05A lot of assumptions and current equilibrium might change, right?
- 49:10Like, for example, climate change.
- 49:11Who knows what that does?
- 49:12Yep.
- 49:12That's big.
- 49:13But right now, the biggest drivers are the indices of world development.
- 49:19If you look at things like health and especially child mortality, when child mortality is low,
- 49:25fertility goes down.
- 49:27And it's counterintuitive.
- 49:29But to some extent, people are having, let's say, enough children.
- 49:34If your children have a very high chance of survival, enough is a smaller number.
- 49:40It's a funny way I'm looking at it.
- 49:41But that's, so health, economic opportunity, human rights is one of the other big driving
- 49:47forces.
- 49:48Right.
- 49:48Autonomy.
- 49:48Yeah.
- 49:49Freedom to make reproductive decisions and education.
- 49:52That's the other big one.
- 49:54Yep.
- 49:54Yep.
- 49:55Okay.
- 49:55Another one from the health area, glucose and insulin.
- 49:59Yeah.
- 50:00What's the problem we're solving here?
- 50:01So this is a model of the system in our bodies and actually, I think all mammals to regulate
- 50:08blood sugar.
- 50:09And it's the interaction of glucose or blood sugar and the hormones that cause blood sugar
- 50:14to go up and down.
- 50:15The primary one being insulin.
- 50:17And there's a model that's called the minimal model.
- 50:20And it was proposed by researchers who were trying to describe, you know, what's the minimum
- 50:26set of interactions that we need in order to have a model that fits the data pretty well.
- 50:32This is an example that I liked partly because the discussion of modeling was so explicit that
- 50:38there are some quotes from these papers where they are more open about their thinking than
- 50:45usual.
- 50:46It seems like people kind of hide the models.
- 50:48And these guys were really thinking about what do we need to include and what can we leave
- 50:54out?
- 50:55And how do we evaluate whether the model is good enough?
- 50:58And the way they did good enough is they actually had data from studies of blood sugar and diabetes
- 51:05where people have a challenge test.
- 51:09They are actually injected with glucose solution and then they monitor their insulin levels and blood sugar levels over three hours with measurements every two minutes.
- 51:22And so you can see blood sugar goes up and then insulin goes up and then glucose comes down and then insulin comes down.
- 51:29And then you eventually get back, if everything works, to your steady state base levels where you started.
- 51:36I see.
- 51:36So if you don't have diabetes, your body should react that way.
- 51:40But maybe if you do, like it can't produce the insulin to counterbalance that or something like this, right?
- 51:46So I think I've heard of that as a test for seeing if you have either have diabetes or maybe are pre-diabetic, right?
- 51:53Yep, exactly.
- 51:54And there are two things.
- 51:55Okay.
- 51:55You might not be producing enough insulin or your cells might be insensitive to it.
- 52:01So there are kind of two ways that that can go wrong.
- 52:03Okay.
- 52:03So I think, I don't remember which one that you did this on here.
- 52:07There's all, all these come with great plots over time and, you know, running the simulation and seeing how it evolves over time.
- 52:15And those kinds of things.
- 52:16But at least one of these you used SimPy, right?
- 52:20Yes.
- 52:21SimPy is pretty interesting.
- 52:23Yeah.
- 52:23So this is symbolic computation and it allows you to solve systems equations, nonlinear equations, differential equations, at least for the ones that have an analytic solution.
- 52:35SimPy will often find it for you.
- 52:37And when I say find a solution, it's not a numerical approximation.
- 52:42It's a symbolic representation of the function.
- 52:45Right.
- 52:45That's why I thought it was so interesting as a thing.
- 52:47Because so everything we've been talking about so far are like these Euler type solutions.
- 52:52We're like, okay, well, if we just approximate, if the gap is small enough, it's like calculus, but it's a loop, you know?
- 52:59Whereas this says, actually, the integral of this differential equation is this other formula, right?
- 53:06Like you would learn in, you know, calculus or something.
- 53:08Yeah.
- 53:09And so what you get is this object that represents the function and you can render it as LaTeX.
- 53:15So you can kind of see it, you know, with the nice integral symbols and infinity symbols and all that.
- 53:21And you can also, from that, generate Python code.
- 53:24So you could have, you know, solve your differential equation and then evaluate the result as a numerical computation.
- 53:32Right.
- 53:32And the answer, instead of being 3.14, whatever, is just the letter pi.
- 53:37Right.
- 53:37Right.
- 53:37It's like, here, you gave me a formula.
- 53:39Here's the actual symbolic solution to it as a number, right?
- 53:43Yep.
- 53:43Yeah.
- 53:43Pretty cool.
- 53:44So you did, I believe you used that in some of your simulations or some of your models, right?
- 53:49There were a couple of examples where we did both.
- 53:51We would do the numerical solver and then the symbolic computation and then compare them.
- 53:58And then a couple of places where there's an analysis that you can do, like the SIR model,
- 54:04where the, you could do the analysis by hand and certainly in lots of science and math classes,
- 54:09that's what you would do.
- 54:10But I really wanted to show people how to do this symbolically because I think it separates two parts of the activity.
- 54:17There's the low level activity of actually, you know, writing all the symbols on paper
- 54:22and being very careful not to leave off the minus signs and the two, you know, factor of two and all that.
- 54:28And then there's the high level, keeping track, what am I doing?
- 54:31What's my goal?
- 54:32What are the steps I'm following?
- 54:33To get there.
- 54:34And I think symbolic computation has a lot of potential to separate those two,
- 54:39where I think people could come away with a much better understanding of that high level stuff
- 54:44if they are freed from so much of their attention being on the low level stuff.
- 54:50Yeah.
- 54:50Yeah.
- 54:51Just the grind of factoring polynomials or taking the derivative or whatever, right?
- 54:57Yep.
- 54:57Yeah.
- 54:58Or you see, you know, mechanical systems, if you're working with physical vectors,
- 55:02you just, you have a page that's just full of signs and cosines.
- 55:06And it's, in some sense, what that, what that is, is a failure of notation because that page full of
- 55:13signs and cosines is really telling you that you have not represented the computation in an effective
- 55:19way.
- 55:20That the signs and cosines are a consequence of not using vector arithmetic.
- 55:25Right.
- 55:26Which you do make a big use of with pandas and things like that, right?
- 55:30Right.
- 55:31We get to it.
- 55:32And this is, I think, where I started to reach the limits of the current tools.
- 55:37Because ideally, if you have vector quantities, like position and velocity and acceleration,
- 55:44you should represent them as vectors.
- 55:46And so when you add two vectors, you're not converting them to their coordinates, adding the coordinates,
- 55:53and then putting the result back into a vector.
- 55:55You just think of vector addition as a fundamental operation.
- 56:00But in the same way, we would like to be able to do differentiation and numerical solution to these methods,
- 56:08also using vectors as primitive objects.
- 56:11And we're not quite there.
- 56:13The SciPy libraries don't really do that.
- 56:16So I end up doing a lot of packing and unpacking, where I start with vectors, but then to do the computation,
- 56:23I have to break them down into coordinates.
- 56:25And then when I get the result, I have to stuff it back into vectors.
- 56:28I think the next generation of scientific computing, we need vectors as basic types,
- 56:35and we need computation with units.
- 56:37Yeah, right, which is something that's also often missing.
- 56:41To my knowledge, this does not exist in Python or any other programming language that's not.
- 56:46At least it is mainstream.
- 56:47There are probably some engineering ones.
- 56:48But we have things like Pint, right?
- 56:50Yeah.
- 56:51Which is a pretty neat library.
- 56:52It's very neat, and it's very close to what I want.
- 56:56AstroPy is the other library I'm aware of that does a really good job with units.
- 57:01And I've played around with each of them enough to be excited for the potential, but also to have hit a wall of, I still can't quite do what I want.
- 57:11Ideally, it should be end-to-end.
- 57:13I should be able to start with the parameters of my system and the initial state described using vectors with units,
- 57:21And then run the whole computation so that at the end, my result is in the form of vectors with units.
- 57:29Yeah.
- 57:30And that's still, that's not easy yet.
- 57:32Yeah, not yet.
- 57:33Okay.
- 57:33You know, the SimPy stuff is a little like that in the sense that the thing you get back is like kind of a function in the mathematical sense,
- 57:42like f of x equals, you know, e to the x.
- 57:45And you can compose them and pass them around, right?
- 57:48And sort of use them a little bit more than just, well, I got an answer out of this, and then I feed the answer there, right, numerically.
- 57:54Yes.
- 57:55So SimPy is end-to-end in this way, that you start with symbols and you end with symbols.
- 58:01There's no like in-between where you have to break it down into numerical methods and then build it up again.
- 58:07So yeah, I think SimPy has achieved this goal and we're still, I think, working on vectors and units and scientific computation.
- 58:16Yeah, absolutely.
- 58:17All right.
- 58:17Well, we're pretty close to out of time, but I think maybe let's just touch on one more kind of fun.
- 58:23And that is the, people can go and play with this.
- 58:26That's the bungee dunk.
- 58:29Do you want to tell people what kind of insanity a bungee dunk is?
- 58:32And then you can just talk about this modeling problem.
- 58:34As a way to say, some human being put their body in jeopardy to test this theory, right?
- 58:40Tell us about this.
- 58:41Yes.
- 58:42Okay.
- 58:42So this was partly prompted by this video, which shows a person doing a bungee jump.
- 58:47So they're jumping off a crane.
- 58:49They have a bungee cord attached to their ankles.
- 58:52And when the cord is stretched out and fully extended, they are about four feet from the ground and they can reach out.
- 59:01And the person jumping has a cookie or a biscuit because they're in the UK.
- 59:05And they dunk the cookie into a cup of tea that is on a dock at the bottom of this.
- 59:11And I forget the number.
- 59:13Was it 30 meters?
- 59:14But it was a roll round.
- 59:15Yeah.
- 59:15Something like that.
- 59:17Yeah.
- 59:17It was way too far for a person to do that with their body.
- 59:20Now, part of this is that the bungee-
- 59:21The video shows them literally they come to a complete stop because the way the springs work.
- 59:27And they are right at where they literally do dunk this biscuit into the tea and then shoot back up for the back and forth oscillation by Hook's Law of the bungee, right?
- 59:37You got it.
- 59:38Right.
- 59:39Now, the margin of error here is low.
- 59:41Most people want to do a bungee jump to not do this because if it just turns out that he had extended about two meters more than he expected to, he would have at least hit the plank.
- 59:54And he was over a body of water.
- 59:56So maybe it's not a catastrophe, but it was a risky thing to do.
- 1:00:00At a minimum, it's going to hurt.
- 1:00:01Yes.
- 1:00:03Yeah.
- 1:00:03So basically, you go through the idea of like, well, if you wanted to try, the record was 70 meters.
- 1:00:10Oh, okay.
- 1:00:11Oh, that is way up there.
- 1:00:13So that's way up there.
- 1:00:16So if you wanted to say, you know what, guy with a cool beard that currently holds the Guinness Book of World Records, we have pandas, we have notebooks, we're going to beat this.
- 1:00:25Right.
- 1:00:26You could go through and model all the details out.
- 1:00:29And you talk through some of the assumptions like the bungee will not apply a slowing force to you until it fully extends the slack length of the bungee.
- 1:00:38And then it is a perfect spring and on and on, right?
- 1:00:41Right.
- 1:00:41But here's where there is some interesting physics.
- 1:00:44So here is the other motivation for this problem.
- 1:00:47Remember, thinking about modeling decisions.
- 1:00:49What can you leave out and what do you have to include in order to get a precise answer?
- 1:00:55So in this scenario, like air resistance is definitely a factor.
- 1:00:59And the stretch of the spring and its restoring force is definitely a factor.
- 1:01:04But here's the interesting part.
- 1:01:05During the first phase of the jump, while the bungee cord is not yet stretching, but it is uncoiling because it starts out in a great big U shape.
- 1:01:15It's dangling down and it has to unravel or, you know, unwrap itself as you are falling.
- 1:01:22The first model that you probably think of is that it's just free fall, that you are being pulled by the force of gravity and the bungee cord is being pulled by the force of gravity.
- 1:01:32And so there's no net interaction between the two.
- 1:01:35And so you would think it would be just your acceleration would be equal to G, you know, 9.8 meters per second square.
- 1:01:41And that's what turns out not to be true.
- 1:01:43And it's not at all obvious, but there is an interaction with the cord.
- 1:01:47It is transferring momentum from the cord to you.
- 1:01:50It's analogous to a whip action.
- 1:01:53And the net effect is that you accelerate faster than gravity because the cord is actually pulling you down.
- 1:01:59It turns out that the difference you would be off by, if you ignore this effect, you would be off by about two meters.
- 1:02:06And that's going to be a problem.
- 1:02:09That was your margin of error.
- 1:02:11Right.
- 1:02:12So there's a nice physics paper that explains this.
- 1:02:15And there's a really nice Veritasium video that explains the phenomenon and does a really compelling demonstration that shows that it's true.
- 1:02:25Veritasium is fantastic for this kind of stuff.
- 1:02:28It's a YouTube channel that does these kinds of experiments.
- 1:02:31A little bit like the Discovery Show, but way better.
- 1:02:34They're really good.
- 1:02:35Yeah.
- 1:02:35Did you see the downwind sort of sailboat, but a sail car?
- 1:02:39No, I don't.
- 1:02:40Oh, maybe I did actually see that.
- 1:02:42Yeah.
- 1:02:42I think I did.
- 1:02:43That one's very good.
- 1:02:44It's been a while.
- 1:02:44Yeah.
- 1:02:45I'll see if I can find the channel and put it up in the show notes for people.
- 1:02:49Yeah.
- 1:02:49All right, Al.
- 1:02:49I think with that, we probably should call it for time, but very interesting.
- 1:02:54People don't actually recommend you go try to break the bungee dunk record.
- 1:02:59Yeah, no.
- 1:03:00Do something else.
- 1:03:01But if you do, it is fun to watch.
- 1:03:03Yeah.
- 1:03:03Make a good model.
- 1:03:04Exactly.
- 1:03:05Make a good model and let us know.
- 1:03:08We'll share it, but we don't recommend it.
- 1:03:10All right.
- 1:03:11Final two questions before we get out of here, Alan.
- 1:03:14You're going to write some Python code.
- 1:03:15What editor do you use?
- 1:03:16I'm using mostly Jupyter Notebooks, but also a little bit of VS Code.
- 1:03:22Okay.
- 1:03:22For Notebooks, is it straight Jupyter?
- 1:03:24Is it JupyterLab?
- 1:03:25I just had JupyterLab 4.0 come out, which is a big update.
- 1:03:28I know.
- 1:03:28And every time JupyterLab gets better, I think now's the time for me to make the switch, but I have
- 1:03:34not.
- 1:03:34And partly it's familiarity and partly as a teaching environment.
- 1:03:38I prefer a simple environment with the minimum distraction.
- 1:03:41Sure.
- 1:03:42That's certainly valid.
- 1:03:43All right.
- 1:03:44Then notable PyPI package, or Conda package, if you prefer.
- 1:03:48Right.
- 1:03:49Well, I have to say the SciPy, NumPy, Pandas, those were the primary things I was using
- 1:03:55for this book.
- 1:03:57And then maybe a shout out to Pint, which I, you know, as I said, I think it's not quite
- 1:04:01doing everything I want, but I think it's a really great tool.
- 1:04:05Excellent.
- 1:04:05Yeah, I agree.
- 1:04:06All right.
- 1:04:06People are interested.
- 1:04:07They want to check out your book.
- 1:04:08I'll put the link in the show notes.
- 1:04:11There's Jupyter Notebooks up on Google CoLab.
- 1:04:14If you're feeling brave, you can create your own virtual environment, install them locally
- 1:04:18and run them there as well.
- 1:04:19That works too.
- 1:04:20Anything else you want to say?
- 1:04:21How people get started with this book?
- 1:04:23The idea is to just get you into it.
- 1:04:25There are some case studies and a lot of chances to take whatever system you're interested in
- 1:04:30and apply these tools.
- 1:04:32So I hope people enjoy it.
- 1:04:34And if you do anything interesting with it, let me know.
- 1:04:37Sounds good.
- 1:04:37Well, thank you for being here.
- 1:04:39And thanks to everyone for listening.
- 1:04:40Thank you.
- 1:04:41This has been another episode of Talk Python To Me.
- 1:04:44Thank you to our sponsors.
- 1:04:46Be sure to check out what they're offering.
- 1:04:48It really helps support the show.
- 1:04:50InfluxData encourages you to try InfluxDB.
- 1:04:53InfluxDB is a database purpose-built for handling time series data at a massive scale for real-time
- 1:05:00analytics.
- 1:05:00Try it for free at talkpython.fm/InfluxDB.
- 1:05:04Are you ready to level up your Python career?
- 1:05:08And could you use a little bit of personal and individualized guidance to do so?
- 1:05:13Check out the PyBytes Python Developer Mindset Program at talkpython.fm/PDM.
- 1:05:20Want to level up your Python?
- 1:05:22We have one of the largest catalogs of Python video courses over at Talk Python.
- 1:05:26Our content ranges from true beginners to deeply advanced topics like memory and async.
- 1:05:31And best of all, there's not a subscription in sight.
- 1:05:34Check it out for yourself at training.talkpython.fm.
- 1:05:37Be sure to subscribe to the show.
- 1:05:38Open your favorite podcast app and search for Python.
- 1:05:41We should be right at the top.
- 1:05:43You can also find the iTunes feed at /itunes, the Google Play feed at /play,
- 1:05:48and the direct RSS feed at /rss on talkpython.fm.
- 1:05:52We're live streaming most of our recordings these days.
- 1:05:55If you want to be part of the show and have your comments featured on the air,
- 1:05:59be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
- 1:06:03This is your host, Michael Kennedy.
- 1:06:05Thanks so much for listening.
- 1:06:06I really appreciate it.
- 1:06:07Now get out there and write some Python code.
- 1:06:09I really appreciate it.
- 1:06:30Thank you.