- Everyone out there on YouTube, hello, Vince.
Hey, Garant.
- Hi.
- It's good to have you here on YouTube
and the podcast in just a moment.
Make sure you're patient as we're getting started.
I know some of you were waiting for the live stream to start
so very nice to have you here.
Now, let's kick this off, guys.
- Thanks to anyone who's here.
- Yeah, thanks for being here
and if you have any thoughts or questions,
please put them in the live chat
if you're here watching live.
Vince Garant, welcome to Talk Python to Me.
- Thanks, Michael.
Thank you very much for having us.
Thank you.
- Yeah, thanks.
Nice to be here.
- Yeah, it's great to have you.
Vince, I guess it's welcome back for you, right?
- Yeah, I was on the show a while ago
and I'm a big listener, love the show.
Yeah, I came here a long time ago
to discuss one of the game theoretic libraries I work on.
So yeah, thanks for having me again.
- Yeah, you bet.
I love game theory, it's amazing.
We'll touch on that a little bit.
We're gonna do kind of a survey
of a bunch of different areas of applied math
and how Python and compare that
with how R might solve those problems.
Some of the libraries involved,
some of the techniques involved.
So lots of fun things.
I'm looking forward to diving into them with you.
- Awesome, awesome.
- But before we do,
maybe we'll kick it off with Garen.
You're new, tell people quickly about how you got
into programming, Python, math.
How'd you end up here on this show?
- So I'm a lecturer at Cardiff University
in the School of Mathematics.
So I learned to program while I was doing my PhD.
And Vince here was actually my PhD supervisor.
So I essentially learned programming
from Vince during that time.
- Nice.
Yeah, and I think during my PhD, I sort of realized
or I learned that everything that we'd been able to learn
during my like masters and stuff,
it was all completely doable in open source software.
And that's kind of where I focused my PhD on then.
I sort of, one of my main projects during my PhD
was building a library to be able to do some of this stuff.
And then, yeah, I passed.
- That's what general area was your PhD in?
I know math, but what specifically?
- Discrete event simulation was where I specialized
in my PhD, but like in conjunction with some
of the other techniques, stuff like Markov chains
and queueing theory, which all sort of are different ways
of solving the same sort of problem.
And that's where I sort of specialized in.
- Fun, very, very cool.
- We have a running joke with Garrent
that one of his sub topics in his PhD
that he became hyper specialized in as PhDs go is deadlock.
And so there's a tweet came up about a question
about deadlock and all these jokes that come up.
We got very excited.
Well, I got very excited.
Like, "Garrent, someone's joking about your work."
- That's fantastic.
Vince, you've already told your story.
Maybe just a quick update.
What have you been up to in the last couple of years?
- Yes, in the last couple of years,
having the pleasure of working with Geraint now,
now that he's a colleague of mine,
but otherwise continuing my work on the Axtra library,
which is the library we were chatting about just now.
And yeah, I'm still a mathematician at Cardiff,
where the best part of my job is getting to teach people
how to program.
And that's, I think mathematics is such a cool subject,
but in a way you're only ever a theoretical mathematician
until you can program the stuff you're doing.
Even the applied stuff is quite theoretic.
And then it's once you can code these things
that are so powerful,
it really kind of releases the magic of mathematics
for want of a less cringy type of way of saying it.
So that's kind of what I really enjoy doing
and what I keep doing.
Kind of what we're going to be talking about today.
- Yeah, absolutely, we're gonna touch on a bunch
of different manifestations of that basically.
Cool, I wanna put out a quick disclaimer.
So we're gonna be talking about math things,
obviously Python things as well on the Python show here.
The disclaimer is, I have some degrees in math,
but I have not done anything with them for 20 years.
And therefore, I know just enough to ask bad questions
and make poor assumptions.
So you all are gonna have to keep me on track
and just tell me, no, no, no, Michael,
you are totally misremembering that
from your courses 20 years ago.
- The real problem is we might just be
on just the other side of that boundary
where we could confidently give you bad answers.
That's the--
(laughing)
- Look, if you say it confidently, I'm sure--
- Yeah, exactly, confidently giving bad answers
is the problem.
- Yeah, sure, all right, cool.
- Well, let's kick this off by just talking about
what is applied mathematics.
Because one of the final courses I took,
it very much confused me,
both in the content and just purely in its title.
This was a course at UCSD in the PhD program there.
The course was titled Applied Abstract Algebra.
- Right.
(laughing)
- I'm distracted.
(laughing)
- Why is it applied?
Because I really don't, I just, so let's just start out.
We're gonna be talking about solving applied math problems.
Like what the heck is applied math versus like,
what am I doing, a calculus class
where I'm solving a bunch of like formulaic type of problems
versus real analysis where I'm chaining theories
to derive new ideas or maybe it's a numerical analysis
or stats, like what, where are we in the math world?
- Yeah, I think applied mathematics
is an interesting kind of like term
because in a way it's become a,
it's come destroyed from its meaning
in a lot of cases in that there's a whole area
of applied mathematics as a subfield of mathematics.
That's not necessarily applied in any realistic sense.
I don't know what you think, Gav.
- Yeah, yeah, so I think traditionally
what applied mathematics was when I was doing my undergrad was these sort of physics models
where they assume no gravity and they assume no friction and stuff like that which doesn't seem
very realistic to me at all. Whereas when me and Vince have been talking about applied
mathematics sometimes that goes under another name called operational research or operations research
that we call it applied mathematics because we're applying various techniques in mathematics
to a situation, a real situation. Right. So that they are and I do think not necessarily by
definition but just in general different techniques that apply to the more physicsy problems to the
more management style problems maybe that is yeah but not by definition it doesn't have to be like
that. It's just that's what people find useful. Yeah, in a way a lot of these labels in terms of
the mathematical subfields and things are not terribly helpful because they create barriers
between these fields. There's this problem I studied in my PhD where it's in a pure enumerative
combinatorics trying to count these types of matrices. Really neat problem actually,
'cause a whole other series of conversations.
And the most elegant proof that came for this conjecture
came from statistical mechanics, whatever that means,
but it's a field of mathematical physics,
and it's where like all these boundaries from fields
not always are helpful,
whereas really the techniques are the helpful ones
and knowing when to apply them.
Yeah, so I think the definition that we would use
for the purpose of what we're talking about today
in terms of applied mathematics are just,
without necessarily going to an incredibly high level
of mathematics where you're talking about the stuff
that they do at CERN that is certainly applied,
you know, there's some very low hanging fruit.
I don't know if that's the right word.
Low hanging fruit of where mathematics can vary much.
- Like day to day trumps, yeah.
- Exactly, day to day.
And it's where you take these ideas of, you know,
in high school where you're told,
oh, matrices are important because computers
can do mathematics really quickly with them,
but you don't do it with a computer.
So, you know, who cares, right?
- Yeah, exactly.
- But I'm happy with the matrix.
- They tell you how to do it with computers
and then you, yeah, then you proceed to do it by hand
for the next two weeks, yeah.
- Exactly, two weeks if you're lucky.
- I wish I could tell you.
I wish I could tell you that, but yeah, exactly.
You're like, well, I could tell you
whether this matrix is singular or not,
but I have no idea why I would do that, right?
So this is sort of the next step, like,
well, what would you, if you had that knowledge,
that kind of knowledge, what would you do with it?
Like what kind of problems could you answer?
And some of the examples that you all cover in this book
that we're gonna highlight a little bit
are things like if you have two businesses
that are competing, what choices might they make?
Or if, no, I know this is extremely theoretical
and it's never gonna happen,
but what if there was like a pandemic
and there were people who were sick?
(laughing)
- Probably not that fun to be in.
(laughing)
We're using it to make weird trade-offs about society, like should we shut everybody in a basement?
Should we make them behave in different ways?
What's the cost versus benefit analysis?
And those kinds of things, right?
And all these things that you can, without needing too much high-level mathematics,
you can describe them mathematically relatively simply.
And then just with a little bit of code, you can really do cool stuff with it.
It doesn't take much code, you know,
and that's where Python really comes in.
- Yeah, for sure.
One of the things that we'll see throughout here
is there's a bunch of different examples
across these different areas of math
and these different types of problems
in different libraries that apply to solving them.
But the thing that's cool is every one of those solutions
fits within a couple of pages on the long ones, right?
For Jupyter Notebooks that have the solutions
plus have some of the output of the answer, right?
It's not a ton of code, is it?
- No, no, exactly.
And that's, this kind of touches on something else
that we were gonna talk about is the idea
that a lot of the time when these things are taught,
they're taught with specific pieces of software,
mostly commercial pieces of software.
And then the topic itself is kind of not separated
from the software used to attack the topic.
we often go into meetings with industrial partners for research products,
and they talk about the tool as opposed to the idea.
And that's a pity because that makes the idea much more complicated than it is,
because all of a sudden you have to take apart this really complex idea.
But often with open source software, or with Python and R,
which are the two examples that we've used,
it's like three lines of code to find out the effect of a pandemic or other things.
- I also think it demystifies things a little bit
because if what you do is you go to some expensive
polished piece of software like Maple or something
and you can say, you say, well, I could ask this
expensive, complex, huge piece of software a question
and it can magically give me the answer.
- Yeah.
- And it's great that you know you have this
very fancy tool, but it doesn't necessarily reveal
a ton of understanding because of it, right?
- Yeah.
- Yeah, cool.
- Yeah, and an idea that's beginning to form in my head,
I was just saying this to Gary,
I just haven't fully formed,
I think there's a blog post that no one's gonna read
that I wanna write one day about,
you know, the ethics of it,
about teaching these fancy commercial softwares,
why these great companies give out these educational software is that
they immediately have lots of users of the software that don't know how to use anything
else. And I wonder if ethically as educators if that's okay. I think we should make more of an
effort to separate the concept from the software. And I was kind of like saying like I don't think
I don't think we should teach any commercial software.
Geraint, you made a good point.
- Yeah, I don't really have a problem
with teaching a particular topic through commercial software
as long as we teach it in a separated way
so that you know, once you've formulated the concepts
and the ideas mathematically,
you can use whatever software you want.
But in the classroom, we happen to have
this expensive license, we're gonna use this.
But outside of the classroom,
there are other things you can use.
- Yeah, well, I think there's a couple aspects here.
One, Vince, you spoke about the ethics of it.
Is it the right thing to do
to basically reinforce this loop?
- Exactly, exactly.
- That entrenches some of these.
The other part is, if you're in the UK
or if you're in the US and you say to your student,
probably especially if you're in the US,
they're used to getting paid so much.
I just paid for both my daughters to go through college.
So I'm aware that here you pay a lot for the actual tuition, but many places you'll say
to students, what is it in the UK?
Is there, how is college, what's the affordability?
I know in Germany and other places, but I don't know what that was.
- So it's a touchy subject in that tuition fees have just come in.
And so cost of universities here are equivalent,
not far off to in-state tuition
in public universities in the United States.
I think they're not far off.
But then we get to charge, and I'm saying we,
very, very not talking about me,
but we get to charge whatever we want
to a certain extent to people from the European Union
and then even more to people from outside of that.
Actually, I don't even know if that's true
about the European Union because of recent events anymore.
But yeah.
So yeah, there is a cost.
- Sure, okay.
So going back though, my point about the ethics was,
it's one thing to be in the US or places in Europe
where we have a decent amount of money.
It's another to say to somebody,
you have to pay $500 for this software
to learn the subject, right?
where 500 US dollars might be the yearly income
for an employed person and their student.
It seems like there's a lot of good would be done
by building up maybe the missing gaps
that force or encourage people
to go to this commercial software,
building that up rather in the form of SciPy libraries
and the Python data science stack in general.
So that it's free, open source and all those things.
- I think that would apply for all subjects.
I think really close to what we're talking about here
is with mathematics, I think it's really frustrating
'cause maybe I'm thinking too purely now,
but I mean, pure of heart, not pure mathematics.
In terms of like, mathematics is just such a clean
and minimalistic subject.
You need a pen and a paper to do it.
And if all of a sudden we're teaching people
to use a pen and paper to do mathematics
and saying like, oh, but you can only do it
with a pen made by a BIC.
But don't worry, BIC is--
- Long pen is the only one that can do calculus.
- Exactly, right?
And then once you graduate,
if you want to write down this idea again,
you have to use a BIC pen,
but you have to pay for it yourself.
And I really do think mathematics,
evolution of mathematics and a lot of science, I would argue, is into computing, right? Is
into getting computers to do it. And that extension of using the pen now is to use code.
And yeah, it's...
I do think, I think, for me at least, I think one of the important things is our separation
of mathematical concepts to software. Because it's a lot less of a problem, but there's
still a problem going, the only way you can do this is by using free open software. There
are some places, unfortunately, that are not allowed to use open software because they
think there's security problems and stuff. So if you've only taught open software, then
certain, like the NHS, for example, I know they discourage using open software. So maybe
you think then, think, "I can't do this maths thing anymore." But if you've separated the
concept from the software, at least you know the concept and you know you've got choices.
Yeah, absolutely. I think that's very true. Separating the concept from the software is key
where possible and I think perhaps we don't always do a good job of that. And here I am speaking as
an industry, I think sometimes we often... I've been to conversations where people have said,
"I need to use MATLAB. Can you help me with MATLAB?" And it turned out they wanted to know
how to numerically solve a differential equation. They didn't want to do math lab. They just wanted
to solve a numeric equation. They just did not know that that's what they were doing.
I do want to move off of this so we can dive into the applied math aspects. But I think one other
final thing on this is I feel like professors, especially as they get older, they've taught
a certain way with a certain set of tools for 20 years.
And you know what?
That course they wrote, they already have the materials and the exercises and all of
that.
And the idea of saying, "Well, we're going to now throw out this and grab some new open
source library."
I mean, there's 350,000 libraries on PyPI and they're always changing and turning and
and something new is coming along.
And it's maybe a lot to ask,
but also maybe not too much to ask to say,
look, you should keep up with these things.
And if there's a new way to present it to your students,
not because even some of the ethics stuff,
but you might be doing a disservice to your student
to say, look, the only way that you can solve these problems
is with say Fortran,
or the only way you can do
is with a $2,000 toolbox extension to MATLAB.
That's the way that you have to go to solve this.
here's this super well-respected professor telling you that's how you do it.
Well, that's not actually how you do it.
That's how they've done it for 20 years.
And so there's a lot of inertia there, right?
I'm really new at being a lecturer, so maybe I'm being really naive here, but
it is an effort to keep up with all this stuff, but that's our job.
Our job is to keep up with the latest maths.
Anyway, if I was still using, if in 30 years time, I was still using the same
math that I'm using today, I wouldn't be doing my job properly.
So why is the same true with software?
Yeah, I think it actually, frankly, comes back to ethics in that it's unethical to not make the effort to update your training.
And if we were talking about doctors and surgeons, they do need to learn new things as the science evolves.
I think it could probably with lecturers, professors, higher education, it comes back
to that dichotomy between research and teaching and how teaching is viewed.
But yes, absolutely.
I think, yeah.
In a way, I would be disappointed if at the end of my career, I'm still teaching Python.
In a way, I also hope I have.
in a way.
Yeah, yeah.
Well, but not 2020 Python, but 2050 Python or whatever it is, right?
Absolutely.
Yeah, exactly.
Oh, Python's already on.
I started teaching Python 2, and then I had to figure out how to teach both.
Yeah, exactly.
Yeah, yeah.
Sorry to cut you off.
So you started teaching Python 2, and then now you've moved on.
And then I had to figure out how to deal with Python 2 and 3, and now I just teach Python
3, and yeah.
Yeah.
I can't believe that transition took so long,
but well, there it is.
All right, well, let's dive into some of the problems.
And we're gonna, more specifically,
I'm gonna take this book that you all created
and use it to sort of guide our exploration
of both some of these problems,
how we might solve them with Python,
and some of the libraries that make it possible.
I guess one of the things that struck me about that journey
is that there's not that many external packages
that are required.
It's not like, well, this one takes 10 to solve this problem.
This one takes these complex, these five.
If you put them together just right, you get the answer.
So that'll be fun.
But tell us a bit about this book,
Applied Mathematics with Open Source Software.
- Yeah, so publisher, this is an academic publisher,
which amongst other things means
the book is really too expensive.
But they often just send out emails to people saying,
hey, could you write a book?
It's not necessarily a source of flattery.
But we got one of these emails
and Geraint actually had just given a talk at PyCon UK.
I don't know if you want to say a little bit
about the talk you gave, Geraint.
- Yeah, so it was very much a precursor to this book
where I had given an introduction
to the field of operational research,
which is how we label this certain type
of applied mathematics
and all the different problems that could occur.
And I think the title of the talk was,
there's a library for that,
because every sort of problem that we could come up with,
you could pip install a library
and in a function or two solve the problem,
which was a neat little,
oh, there it is, it's a neat little.
- Is this it?
- Yeah, yeah.
- If I could just interrupt the call for proposals
for PyCon UK closes on Saturday.
So if you're thinking of talking, please submit a talk.
- So yeah, and it was just really neat
that we had these, like what we call standard problems.
It was essentially our postgraduate MSC course
that we teach, but instead of teaching the maths,
there was one or two Python functions that solved it for you
And then, yeah, that eventually evolved into this book
where instead of each little topic
being a couple of functions,
we filled out chapters on some of the theory,
and again, tried to separate the maths and the programming,
emphasizing that by introducing another language,
which was R, to show that you can do it
in two different ways,
and the software is independent of the problem.
- Yeah. - Yeah.
- And we kind of, I think you and I have described it,
'cause every now and then as we were writing the book,
we kind of stop and go, "Wait, who's the book for?"
And as we were making decisions about how exactly to do something, we're like, "Wait,
who's the before and after?"
I won't necessarily...
Well, maybe we should talk about that.
But I think we've written a book that's meant to be useful to people wanting to get started
either with the mathematical field problem or with how to actually do it with the code.
And so every chapter has got the same kind of structure
that describes a problem.
Then it describes the basics ideas
between behind the theory of the problem
or the mathematical tools,
the theory behind the mathematical tools.
But again, not going into too much depth,
kind of what I said before about,
you don't need to understand
all of Newton and Leibniz's calculus
to be able to kind of predict the outcome of a pandemic.
And then it just solves the problem.
original problem, it solves it in two separate ways, the R and the Python, using R and Python.
And the final kind of section, every chapter is pointing at a bunch of literature on it,
which should be useful.
Yeah, and more broadly, you might be able to, these types of problems appear and what
you might be able to do with them.
But the really cool stuff with the publisher is that we had an ongoing kind of conversation
before the start.
Garren and I were both like, well, the book has to be open.
And so they've agreed, like all--
there's not a PDF floating around online of the book.
But all the source files are there.
So all the LaTeX files are there.
All the Jupyter notebooks are there with the code.
We haven't quite gotten all our Markdown files up yet,
but they are theoretically all there.
And so the book is very open, which we're happy about.
Yeah, that's great.
- It is very shiny new.
It still has that new book smell.
It was released just almost exactly a month ago, right?
- That's right, that's right.
Yeah, yeah.
Keep on saying I've got to have Garent over to celebrate,
but we don't have enough doing that.
- I have to sign a copy for my nan.
(all laughing)
- All right, now before we dive into the various problems
on the Python side, while we're still kind of talking
a little bit high level, this is,
one of the interesting aspects here is the Python
and R aspect.
So when you go through each chapter,
which is, I think I agree with you
that it's pretty approachable, even if you don't have a,
you don't have to have like a master's in math
or specialize in that area for it to make sense.
It doesn't have, if you don't really want to see
a ton of formulas, yeah, it's not super intense.
- I think that's our first official review, Michael.
Pretty approachable, it's fantastic to hear.
(laughing)
- Well, like I said, my math,
while I study a ton of it is highly rusted
and it was pretty approachable for me, so there's that.
But maybe just talk about putting R and Python side by side
for solving a set of different problems.
What are some of the takeaways
either of you got from that?
- Yeah, so one thing I wanna say,
- Yeah, I'm sorry, I'm interrupting you there,
but one thing we wanted to do here,
'cause we would, well, I would certainly describe myself
as a Python person, not an R person.
I know R, I write R, I've written a book in R,
but I would certainly describe myself
as a Python person in that.
- If you're gonna sit down and write something,
you would pick Python first.
- I would pick Python, exactly.
But also, I'm embedded in the Python community,
I listen to some good Python podcasts,
I'm a Python person and I can look at some Python code
and the joyous, that looks very Pythonic, right?
I can tell you that.
And so we were kind of aware that we didn't want
to translate the Python to R when we were writing this.
We wanted to write this as close to R people as we could.
I don't know if we managed that,
but that was something as well that we were aware of.
It's not just translating languages.
Geraint, did you--
- In Python, yeah, in Python we have the word
Pythonic for idiomatic Python.
Do you all know, is there a word for that in R?
Or is it just idiomatic R?
- I don't know, I don't know, yeah.
- I don't either.
Geraint, you were gonna say something, jump in here.
- So I was gonna say, as we were writing the book,
we noticed that there were certain problems
which the R and the Python lined up exactly,
not from design, just from,
if we were gonna approach these independently,
it is pretty much exactly the same way to solve it.
There were some problems where
we had to reformulate the mathematics
because the way to do it in R
was so different to the way you do it in Python.
That doesn't mean the math's changed,
we just had to reformulate it, re-communicate it
so that we could write it down in R.
And then there were some problems again
where we have to use different mathematics
to solve it in Python and R
because of the availability of libraries and stuff.
So I thought that was really, really neat.
And I think that emphasizes again,
the separation of concept to software
because sometimes you did have to do very different things
in different softwares.
- My favorite chapter on this is the one
that is your specialty, Darren,
the discrete event simulation chapter
where I forget the name of the two
theoretic ideas about discrete event simulation,
process-based and what's the other one?
- Yeah, so there's process-based
and there's the event scheduling approach.
There's two different ways of approaching
discrete event simulation
and the standard library in R to do it is process-based
and I don't wanna call it the way to do it in Python,
The way we've said in Python to do it
is by using the Q libraries, CIW library.
- The only reason Gerard doesn't want to say
it's the way to do it in Python
is because it's using his library,
the library that he's the maintainer of.
(laughing)
- Yeah, nice.
- Both those libraries, I'm pretty sure that underneath,
well, I know how Q works underneath,
but the Simmer, which is the R version,
I'm pretty sure underneath they work very, very similarly.
There's not many ways you can do different ways
of discrete dimensionalization,
but the way you formulate the problem is very different.
And going through that in the book and going in Python,
you have to take like an overhead worldview
where you look at the system,
whereas in R you have to take a sort of,
you have to put yourself mind in the, sorry,
put yourself in the mind of the customer and go,
what is the customer doing right now
rather than from a systems point of view
in order to formulate the problem
before you could just then go and solve it.
I just thought it was very, very interesting
that the way to do it in each language was so different
and yet probably underneath
it's doing exactly the same thing.
You get the same answers out.
You just got to formulate it in a different way.
- I think that's such a nice example where diversity,
and I'm using diversity
from the way that languages do things.
I think that's the best chapter in the book, frankly.
I think that's the best chapter in the book
because you not only do things differently
from a programming point of view,
but because you do things differently
from a programming point of view,
the theoretic discussion allows you to kind of,
yeah, give a really good understanding
of these two ways of doing this.
- Yeah. - Yeah.
- Yeah. - Something else
that's kind of worth mentioning about the Python in R bit.
I don't know if this is a terrible address,
but I like tests.
I don't like trusting any code.
I like writing tests all the time.
And we came to this immediate thing
So we wanted the book to be tested.
We wanted to know the code was correct in the book,
but we wanted to have these two different languages.
And we didn't want to kind of use the standard
doc test notation for the Python.
And then we wanted to find something that would work
for the R, et cetera.
So we actually wrote a little language of Gnostic doc tester
that basically runs through and tests all the code
in the book for us.
And theoretically, when we write the second edition,
when we add Julia as well,
it will be able to handle that as well.
(laughing)
- Yeah, I guess Julia is another option
that could have possibly shown up in here.
Pretty interesting.
I guess it's worth pulling this up.
It's probably worth pulling up the Stack Overflow trends
and you're putting those in there.
- Yeah.
- Right, so we have R, we've got Julia.
I don't know where these all lie right now,
but I would imagine the graph,
the graph looks a little bit different across these.
Just for people listening, I would say,
you know, R is certainly growing.
It's actually growing pretty healthily
compared to many, many programming languages.
And it's not going crazy, but it's going across.
You've got Python, which if anyone's looked
in the last couple of years,
it's just done insane stuff in terms of popularity
- Over there, so there's that.
And then Julia is, it's, I don't know,
I can't really read it.
It's like 0.01% versus say like 2% for Julia,
for R, excuse me, and then for 16% for Python.
And there is something to be considered
about choosing not just a language,
'cause the language has a cool way of doing async
or its generators are beautiful,
but choosing a language for its ecosystem as well.
- Yeah.
- And I think that's where it touches on this here, right?
- But also for the community that's already there,
I think, at least in the area of mathematics
that me and Vincent are in,
I think R and Python are quite prevalent,
but they're also used-
- Maybe they're people there, right?
- Yeah.
- But I think that also used in different ways,
would you agree Vince?
I would say Python's very,
because of PyPy, things like out of the box,
there's one library to do this and it's their way to do it.
Whereas with R, people tend to write their own stuff
and then upload it.
- Yeah.
- So you might get lots and lots of different ways
of doing the same thing,
but each one might specialize slightly in a different way.
Whereas with Python, I feel like there's one library
that does it really, really well.
I think people just use the ecosystems in different ways.
- Yeah, both those two things are often talked as
strengths of either library, right?
The fact that Python has that one way to do things,
and then the seven different ways to do string formatting
conversation comes up.
Then the fact that R is kind of like
closer to the science.
I don't know if that's even accurate,
but that's the feeling I guess.
It's also a strength, right?
- Yeah, the reason there's lots and lots of libraries
to do the same thing is 'cause they've each specialized
in something which a new academic
has just written a paper on.
And that's why there's lots of different ways of doing it.
- Yeah.
- All right, sure.
And maybe, you know, like out in the audience,
the ninja says, "R is really powerful
"when it comes to things like analytics and so on."
So maybe there's certain areas where...
- Yeah.
- You know, there's the libraries you can pick,
there's definitely better.
Okay, now we talked about the book.
The book also, as you pointed out,
has an open version where you can find it on GitHub,
which obviously we'll link to.
And in here for each one of these,
you've got some examples which are basically the,
at least for the Python side,
the Jupyter Notebook examples here.
So I thought maybe we could sort of talk through a little bit
of what is the problem and then,
maybe really first quick,
quickly introduce what this area is.
And then we can talk about the problem,
the example problem you put out there
and some of the libraries and techniques there.
- Sure.
So the chapter two, which you're looking at right there,
uses is about a mathematical concept called Markov chain.
So it's this probability model that you loosely go that
as the system changes over time,
the system is in a particular state
and then you go with a given probability
of being in that state to another state
and you can go around like that.
That's not the best explanation I've ever given.
I've explained that many times, that was probably one of my worst explanations.
But Markov chains can be used to model queues. And there's a whole field of mathematics called
queuing theory, and that is often used as a very applied problem. And so the problem we got here
is, imagine you've got a barber, and the barber has kind of two decisions, how many chairs they
have to cut hair in and how many chairs they have to keep people waiting for a chair.
And obviously that has an impact on the queue, right, of how long people have to wait to
get their hair cut.
And you can use Markup Change to build up a model and find the probability of having
to wait relatively simply.
I don't know if you want to clean up what I just said.
(laughing)
- No, yeah, so I think,
the model then would be the set of states
and the probabilities of going between each states
from the state you're in.
And that can be written as a matrix.
And the neat thing then is there's loads and loads
of linear algebra techniques,
which at this point you don't really need to know about,
but there are linear algebra techniques,
which stuff like NumPy can just do for you,
which if you know which techniques to use,
you know how to interpret that
as a real life interpretation.
So for example, there's one certain set of equations
you can solve using our Markov chain,
sorry, using our matrix,
which will give you the long run probabilities
of being in each state.
There's another certain linear algebraic technique
which will give you something else.
And that's what this is.
- Nice.
- Yeah, the problem is essentially comes down
of solving a matrix equation,
which you can write down and understand
with basic mathematical knowledge.
And then that can be solved.
This goes back to what you told me in high school,
that matrices are great
'cause computers can do them fast,
but then you never actually get to do that.
You just write down equations.
But it's just a call to NumPy's,
the NALS library can just solve it immediately for you.
- It's amazing.
Yeah, so the problem here to make this applied
or more operational, I guess,
that you all put out there says you have a barber shop
and the shop notices that there's a lot of customers
who are, they come and the shop is full
and the waiting room is full and people just leave.
And I, you know, since COVID I've just decided
I'm cutting my own hair.
I can't, it took too long to find a barber
or anything that worked.
And then after that, I kind of figured it out.
But there used to be this place I would go to
and boy, did it need this problem.
It would be, you would go there
and it'd be like an hour and a half wait.
And you know, you just, all the time.
And you never knew, so they would solve it by giving away free beer to people while they
wait, which was nice.
But this one wants to solve it more in a more formal business way where they would have
people just wait less.
So it says you have two barber chairs, there's room for four people in the waiting room,
there's about 10 customers an hour, and it takes about 15 minutes to serve a customer.
How can we minimize or how do we change things
so that we don't have customers leaving
'cause they come in and see a full waiting room
and take off, right?
Yeah.
- So instead of knocking down a wall
and building a bigger waiting room
or spending a lot of money on an extra barber chair
and just seeing the effect
and maybe five years later trying the other way,
you can just mathematically model this.
And this is a good example of, I think,
what we've described as applied mathematics
so that you can answer these cool questions.
- So you could say,
well, maybe we need a bigger waiting room
or maybe we need more barber chairs
or something like that, right?
And then you can make that trade off.
Yeah, so let me find the,
this is the numpy, numpy.linalg magic.
And so basically you set it up
so that you set up all these scenarios in these constraints
And then you just say, "NumPy, solve it," right?
- That's pretty much it, yeah.
Mathematically, you write down this matrix A
and another vector B,
and you're solving an equation AX equals B.
Most of the code is just getting the matrix set up
and the B set up.
Actually, most of the code is the doc strings.
And then, yeah, you just use the linalg.leastSquares.
That's one of many ways you can do it.
least squares is actually getting an approximate solution
to the linear algebraic equation.
And there's various reasons why that's a better idea
than solving it directly.
And yeah, it's just basically one line.
- Well, I think one of the neat things about this then
is all that maths you sort of learn in school
or even first year university where you're doing
sort of row operations and stuff.
That's not the mathematics that matters here.
The mathematics of math is, what is the matrix you need to do that to?
And once you've done it, how do you interpret the result?
That is the mathematics.
It's not the actual operations that you spent so long doing in first year university.
That's such a good point, Garen.
That's such a good point, Garen, because I think what is mathematics is an interesting
question, right?
And is it being able to differentiate a whole bunch of quadratics really fast?
Or is it knowing which quadratic
you need to differentiate, right?
And I would argue that it is that, you know,
it's knowing what, really knowing what the problem is
so that, okay, then the actual specific
mathematical technique, well, who cares, right?
- Sure.
Well, Vince, people so often say,
oh, I'm never gonna use that.
I'm never gonna do these things.
I can do that with a calculator, or I can do that,
you know, and I think a lot of that might be
slightly misplaced.
I'm not gonna say misplaced,
a somewhat misplaced focus on some of these things, right?
Like if you could say, well,
spend a little time showing you how to differentiate,
then we'll show you how to actually solve problems
with that instead of just getting, you know,
more and more complicated techniques that you can apply,
I guess, integrations more of a pick and choose
or I like to find which thing applies, but still,
you know, instead of going deep, deep, deep, deep,
deep into that on the first year stuff, yeah.
- Yeah, absolutely.
You had someone on the show a little while ago
called Allen Downey.
- Yeah.
- And he writes a lot of really great books.
And I remember what he said on your show was about,
he likes to think of teaching these things in the other way
that code allows you to do these things immediately.
So instead of waiting through and waiting
and having to have faith that there's gonna be used to it,
you can just flip that around, you know,
and understand what it is.
I think there's also a lot of that
in what we should be doing.
- Yeah, that's a very good point.
I totally agree.
All right, wrap this one up.
You were able to find that it would be better
to increase the number of barbers by one
than increase the waiting room capacity by two.
'Cause I think the chance of the shop being full
is like 23% if you had--
- Something like that, yeah.
- Yeah, yeah, versus like 8%, you know,
those kinds of things.
And those are really concrete numbers
that you could go to a business and say,
you hired us to analyze these things.
We gathered all the necessary information
like average time, how much that might vary
and those kinds of things.
And here are your choices
and here are what are the rough expected outcomes.
Like that's really, really valuable information.
And I am very sure that most barbershops
even chains don't do this kind of analysis.
- Not enough linear algebra going on in barbershops.
That's an interest, that's a bold thing.
- I know. (laughs)
- And this question, so a lot of the research
Geraint and I do is within healthcare.
So we work with hospitals solving essentially
very similar problems to the one that we've just discussed.
We're keen to avoid too much healthcare
because that's what we do so much of.
But yeah, if you get rid of the idea of a barber shop,
you talk about a ward, how many beds are on the ward,
how big's the parking lot for ambulances to wait in,
et cetera, et cetera, that becomes very important
and very powerful and not something you need to spend
thousands of pounds or dollars on licenses for,
not necessarily even have a huge understanding
of underlying mathematics.
It's all right there.
It's all right there available to anyone.
- Yeah, but there's way more at stake
in a hospital and healthcare than there is for--
- Of course, of course.
- A haircut. - Of course.
- Yeah, of course.
- Okay, let's go on to the next one.
So the next topic is discrete events.
So I'll find my way over to that one.
And you'll introduce this one, discrete event simulation.
This sounds like this might be somewhat
in your wheelhouse, Garin.
- Yeah, so I think discrete event simulation
is probably, if you're used to programming,
this is probably the one that probably makes the most sense
because it's moving virtual things
around the place essentially.
- Yeah.
- So the question we're asking here
is very similar to the question
we just answered with Markov chains.
But sometimes things are too complicated
to be able to formulate that correct matrix
or the matrix doesn't quite work
because the situation is a bit too complicated.
But what we can do is build a virtual representation
of our system.
And by sampling random numbers,
we can move virtual people around that barbershop
and look at what would happen, right?
Just observe, collect data, crunch that data.
And this is what discrete event simulation is.
It's building a virtual representation of the system,
using random numbers to play
that virtual representation system,
and then collect some data about what just happened virtually.
Right.
OK, excellent.
So this one-- let me set the problem that you all
laid out here.
This is a bicycle repair shop.
And there's a set of gates or actions or, I guess,
events, you might say, that it flows through.
So a bike arrives randomly at a rate of 15 bikes per hour.
They show up and they say, hey, there's
something wrong with my bike.
And the staff member looks at it and helps them.
That takes about three minutes.
But it turns out about 20% of the time,
the bikes don't need repair.
There's like, did you know that there's a thing
to just twist here and tighten it?
All right, go away.
You're fixed, you're cured.
But 80% of the time, there's something wrong,
flat tire, broken chain, whatever.
And that gets put into a place
where they're waiting to work on it.
And that takes about six minutes, right?
And then you've got to collect it back.
The question is, I guess the difference that I see here is there's not an uncertainty of
whether or not you want a haircut when you show up at a barbershop.
There's just amount of time and where the people are and actions they might take if
it's too full.
But here you may show up and you may need help or you may not need help and so on.
So is that the difference?
>> I suppose that is the difference between these two problems.
but that's not the reason we would use simulation over Markov chains.
In fact, the situation we're looking at here could be formulated as a Markov chain.
The difference I see is that in this situation,
you could have an infinite amount of bicycles in your bicycle shop.
So if we were to formulate that as a Markov chain, we'd have an infinite matrix,
which is not terrible to deal with,
but you wouldn't be able to deal with it in the way we just did.
- Got it, okay.
- Yeah.
- There's also, sorry, Gary.
- No, go on.
- There's also, talking about the states
that I did kind of awkwardly in the previous chapter,
it's just how many people are in the shop.
So there's three people in the shop.
Well, there's a probability that the next thing
that happens is four, or the probability
that the next thing that happens is two.
So it's a very straightforward set of states
that is finite, as Garen said.
Here, there's two things to keep track of.
There's the number of bikes waiting to be seen
by the inspector, the number of bikes waiting
to be seen by the repair shop.
And that just immediately, the fact that now you've got
these two dimensions that can change,
just again adds to the complexity and the fact
that it's an amount of them.
- Yeah, okay.
- Yeah.
- So the reason we would use, no.
So the reason we would use simulation for this
is because it's a more complicated situation
for a variety of reasons than what we had before.
It would take a really, really big,
if not infinite matrix to be able to solve this.
So why would we go to that effort
when we can get quick, easy answers
just by virtually playing the system?
- Right.
- And these two chapters are paired.
Sorry, Michael, I'm, please.
- No, please go.
- These two chapters are paired,
and actually there's this,
all the chapters are paired in the book
the reason that they are kind of the two similar types of problems from opposite angles and
correct me, Garen, if you don't agree, but I think that the two opposite angles about
numerical tractability, how much you can do with an exact method, which is what the previous
chapter was, versus a more approximative method.
And it's not always easy to say exactly what you should use one or the other.
Sometimes it's absolutely clear, but sometimes it's not.
But there's these pairs of chapters that kind of reflect the size of the problem, for want
of a better word.
Very interesting.
Okay, the fundamental Python bit here is the CIW, or you pronounce that Q?
Yeah, I call that Q.
CIW, Q, it's the Welsh word for Q, Q-U-E-U-E.
It's the Welsh word for that, and that's why I call it the library, though.
- Yeah, fantastic.
All right, and so the idea is basically
you state the problem in terms of
the arrival rate of events,
the inspection or the service distributions
in terms of how those flow through there,
the number of things that can be servicing it,
and the routing and so on.
You create a network with the Q library
and then you could just ask it questions, right?
You can tell it to run a simulation,
you can run it for some amount of time,
like eight seconds or so on.
And it's interesting that it simulates it,
that it doesn't exactly solve it, right?
- Yeah, so that's the point of simulation,
not the point, but like one of the uses of simulation
is that we can quite easily simulate something
that's too complicated to solve exactly,
or would take too long to solve exactly,
but we can quite easily simulate it.
The trade-off then is,
because assimilation is essentially
streams of random numbers that we do things to,
you could get extreme values, you could get flukes.
So you lose accuracy, but you gain efficiency.
Yeah, so the idea is you run it over and over and over again
with the randomness playing out there.
And then you just say, well, what is the most common set
of outcomes look like?
Yeah, yeah, essentially, yeah.
Nice.
Very cool.
So what did we learn from this one?
That we could add an extra inspector at the front,
or we could add an extra repairer.
And what was the better outcome here?
- So we were measuring was the maximum amount of time
a bicycle spends in the shop.
We wanted to get bicycles out as quick as possible.
So if we added an extra bicycle inspector,
we would reduce our time,
maximum time in the shop by quite a lot.
But if we added an extra repairer,
we'd reduce it, but not quite so much.
So we essentially found that the bottleneck
was the inspection desk and not the repairing.
- How interesting.
And I suspect, maybe this is not true,
but I feel like if I was a business,
knowing that would not just help me hire the right person,
but I think having an inspector type of person
would be easier to get, right?
You don't have to have as great of a skillset
to actually be the mechanic that does the repairs.
So that's really valuable as somebody who runs a bike shop.
I will also throw out another statement here
that's probably true.
I also suspect bike shops don't employ these techniques
but should.
(all laughing)
- Oh, they can now.
- They can, well, and they can now, honestly.
- I believe larger companies probably would.
Stuff like supermarkets and airports probably would
a team of people behind them doing this, but smaller shops probably not.
Yeah. Oh gosh, I don't remember the name.
Not always, right, would be a little caveat. We certainly know of lots of examples of big
entities, companies, things that make very expensive decisions without doing these sorts of
investigations. Yeah.
- Yeah, well, you mentioned grocery stores and stuff.
I had the folks from Kroger, which I think, if not,
is the largest, one of the largest chains in the US.
They have basically an analytics and data science
subsidiary called 8451.
They employ 200 data scientists who do R and Python.
That's not joking around in terms of,
where do the bananas go?
Yeah, all those types of things, right?
There.
So you're right.
The larger companies definitely, definitely invest in that.
It's interesting thing about what is data science, right?
So my data scientists, in that, are they looking at historic
data and identifying things?
And I suspect there's no answer to my question here as well.
I suspect different data scientists do different things.
Um, but when you hear a data science department, that doesn't necessarily
mean they're doing this type of work? It might be, absolutely might be. But they might be diving
into the data and understanding and observing as opposed to necessarily modeling. It's possible.
Right. It could be AI, it could be AI and ML, it could be data ETL, just bringing in data,
just getting into system. Yeah, there's all these different aspects. I wonder more about the data
science on when do I go from being somebody that uses say Python for computation to becoming a data
science right when do I where would you cross the boundary from being a mathematician or a biologist
to becoming a data scientist that has some understanding of biology for example. I would
argue it kind of goes back to what we were saying earlier about what is an applied mathematician and
And at what point are these labels helpful
and at what point are they not, right?
- Yeah, yeah, exactly.
Out in the audience,
Preetam says, "Yeah, data science
is such a catch-all phrase right now."
I would agree, also a buzzword.
So if you're looking for a job,
so it's a good one to employ for that.
All right.
Let's go on to another one.
We'll get through as many of these as we can.
And then we have time for anyway.
So those two problem areas were paired together.
- Yeah.
- The next area that you focused on
is fairly different with differential equations.
- Yeah, so those two areas that we were just in,
the book's got parts and there's two chapters in every part
and that was in the probabilistic modeling
is what we'd call it,
modeling these probabilistic type situations.
And here in the dynamical systems section
and so specifically here,
we're looking at a problem
where differential equations can be used.
And we wrote, we started writing this book
before the world changed, before COVID.
And I remember coming into one of our writing sessions,
I have to say I really enjoyed writing the book.
Very glad to have it finished, but I enjoyed writing the book
and I said to Garren, Garren,
do we need to change this chapter?
Because this chapter is, there's a disease going around,
I think we call it a cold.
Yeah, everyone's caught a cold in a small community.
There's a cost to not being able to work.
- It's a point now to have a cold.
Right?
And the cost, financial cost associated
with not being able to work,
financial costs with a possible cure
that would double the rate at which you get better
from the cure.
Is it worth, from the cold, pardon me,
is it worth financially to get this cure was the problem.
And yeah, we were like, should we make this about COVID?
You know, should we just essentially all the graphs
we spent so long looking at that was on the news
the time, this is it. This is essentially the same thing. Should we make it about COVID?
We decided not to, didn't we, Garry? We decided to just keep it somewhat abstract. But yeah,
that's the problem here. And the model is a textbook, for want of a better word, a textbook
model that's often taught when you start learning about systems of differential equations called
the SIR model. And the SIR model has a population in three groups, susceptible people.
Sorry, Vint, I think you're talking about a different chapter.
I am talking about the wrong chapter. Thank you. Sorry, I went off on that one. Thank you.
That's the next chapter.
They're similar. Yeah, they're similar.
Very similar.
- This one, you can write down a differential equation
that dictates not a system,
but just the number of people that are infected over time.
And so you can write that down, that differential equation,
you can get a solution for it.
Sorry, thank you, Geraint, thank you.
- Yeah, no worries.
This one actually uses an interesting library
that I am just fascinated with.
- Awesome library, yeah.
- Yeah, I just had the folks on back in May,
not too long ago for this one,
Andre and Aaron for this.
I did not expect this from Python
because I expected NumPy
and the linear algebra type things,
but not, here's the integral of this solution
of this differential equation.
And here were the steps that I took to get there.
It is so neat.
SimPy is an incredible library that just gives you
so much of what in mathematics is thought
to have to be commercial
in that you can have all your symbolic stuff.
So, if you wanna know what is X plus X,
you don't need to tell the computer what value X has.
With SimPy, the computer can just understand the concept
that X is this mathematical idea, and it's incredible.
And it's a fantastic, fantastic library.
I think it was when I started teaching programming
through our first year cohort,
I actually taught SageMath
and SageMath is built on top of SimPy.
But at some point I decided to just teach with SimPy.
And so that's what we use here.
That's what we use here.
We build up the actual numeric solution
for the differential equations.
We find the expression that if you put it
into this differential equation,
that would be the solution.
So again, the exact solution here.
- Yeah.
It's pretty wild.
You say, the way you set it up,
I mean, everything about Senpai is wild,
but the way you set it up here is you say,
I'm gonna create some symbols like time T
or a constant alpha or so on, or initial conditions.
And then you say, I'm gonna have a function.
You don't say what the function is.
You say there is a function called I.
And then what you define for the thing you're going to work with is you say,
uh, sim dot equation.
And you say the equation is going to be given the derivative of this function
and, of T and T.
And then it has this, this expression in there.
Negative.
I guess that's probably the, you, what you're specifying here is this is the
derivative, not the equation itself, which is negative alpha times I have T.
Right.
This is one of those parts of SymPy that when we teach this to our first years, it allows
us to help identify misunderstandings about the mathematics.
I think programming can often help you understand mathematics better and likewise and vice versa,
but people often come to study mathematics and don't know the difference between a formula
and an equation, and an equation has two sides.
An equation has an equal side in the middle.
And so what you're creating here is sim.eq takes two variables, a left-hand side and
a right-hand side.
That's actually the name of the parameters.
So the left-hand side is the derivative of i.
So it's saying that the speed at which the number of infected people changes over time,
that's the left-hand side of your equation, is equal to, and then the right-hand side
of your equation, some constant alpha minus some constant alpha, sorry, times the number.
So the speed is related to the number.
And that's really the relationship that defines the differential equations.
Right.
And in this scenario, you can change the alpha, the rate of recovery by providing medicine
or just letting people do their thing, you know?
Naturally recover, yes.
Yeah, yeah, exactly.
So in here you say, you know, given these constants, we're going to, you know, get the
equation and then you solve it.
You come up with the actual symbolic solution, which is always, always blows my mind.
- And then when you say sim.dsolve,
like does that solve the differential equation?
I'm guessing you give it the equation
and the initial conditions and stuff,
and you're like, where the initial condition is,
we defined a function i, we didn't say what it is, really.
What we said at zero, it's the initial condition
of like how many people are sick
or whatever that turned out to be, which is just amazing.
- All of which is simple.
Everything here is just an idea.
Nothing's a number.
Everything is just an idea.
- Yeah, yeah, we don't know anything about it.
We just know that these are the constraints of the equation
and out it gives you, let's see.
You get the solution and it says an equation.
Let's see, I gotta go further down.
- So that's it.
- Yeah, so it gives you the actual solution
and then you start plugging in like the numbers,
the different conditions and it gives you the answers, right?
- Exactly.
- Yeah, you plug in the numbers at the end.
- Yeah.
So we get the cost if you were to purchase the cure
would be 700 and the cost without purchasing the cure
would be 500.
And since the overall costing into account productivity
offsetting all the impacts of, you know,
the similar type of decisions that have been,
you know, governments all over the world have been making.
And in this particular case, for this cold data is,
no, it is not worth getting the cure.
(laughing)
- Don't tell that to the people who are suffering
with a fever or whatever.
- It's a cold.
- It's a little like, yeah, yeah, it's a cold.
- This is not like a common cold, this is a cold.
(laughing)
- Well, this chapter, I think was,
we found this chapter really, really neat
because when we started doing exactly the same thing,
going, how do we solve this in R,
we realized we had to use a completely different approach.
- Well, you know what, you skip,
skipping a step, right? - It was a bad choice.
- Yeah, 'cause, sorry, again,
Sorry. So we went looking in R for an equivalent symbolic mathematics package in R. And there
is one, but really it's a wrapper for Python's SymPy. And so we found ourselves writing strings
of Python that you pass to R that R passes to SymPy. And yeah, sorry.
- Yeah, I think that's--
- That doesn't sound like really like solving it in R.
You know?
- Exactly, exactly.
So then, so we decided, well, no,
if we were just to solve this in R, how would you do it?
And as there is no symbolic library,
a library for doing symbolic mathematics in R,
we solved it numerically.
And that just shows like exactly the same problem.
We get exactly the same answers out,
but the bit in the middle, the solution steps,
were completely different in both Python and R
for those reasons, which I think is really neat.
I think that shows that you're concentrating
on the problem rather than the solution steps.
Yeah, I like that.
- Yeah, absolutely.
And what you've learned here is that this function
that expresses the cost of people being sick,
IFT that we talked about is 100 for a hundred people sick.
E to the negative alpha of T, which is, it's pretty awesome.
I mean, there's never,
there's almost always a reason to marvel at the number E.
Have you all read the book, E, the story of a number?
- I don't think I have, no.
- This, this book is so good.
It just goes back to the history literally of just,
- Oh yeah, I totally can recommend it.
So people can check that out.
- E is a ridiculously cool number, right?
In terms of, I know pi gets all the press,
but E is a ridiculously cool number.
- It's way cooler than pi.
It's way cooler than pi.
I mean, pi is cool, but if you are into
the change of anything, differential equations,
integrations, like, there's nothing like it, yeah.
- Recommendation, I haven't heard about that one.
- We are going pretty long on time here,
so I wanna be respectful of your time and everything.
So maybe, is there any other of the problems
or areas or libraries that you've made?
Actually, let me pick one here.
Which chapter is the game theory one?
(laughing)
Wait for it, no, it's nine.
- Just one before that, yeah.
- Yeah, or yeah, no, it was one before that.
Yeah, got it, yep.
The missing one is the linear programming one.
All right, game theory.
And the reason I want to pull this up is we talked about Q,
Grant's library and his application.
How about some Nash pie?
I mean, there was John Nash won the Nobel Prize in economics
for coming up with basically proving in every game
there was an equilibrium position, which is insane.
- In a 17 page PhD thesis.
Yeah, incredible.
- Not bad, not bad.
So, quick game theory, what is it?
People maybe have heard of it, but they don't really know,
or maybe they do know.
I know I fumbled through the previous one,
but it probably makes sense for me to take this one.
Yeah, yeah, sure.
Right.
So game theory is the study of emergent interactions,
of if you can kind of make some decisions,
Describe a system at the lower level, how individuals interact, what happens at the
higher level. That's the study of game theory. It's interactive decision-making. A lot of the
time you can think of decision-making, for example, with that barbershop problem we discussed at the
first stage of, well, if I had this many chairs or this much waiting room, what's the best thing for
me, but there you're the only thing that your decision gets hit back with. If you think of a
game of tennis, it's playing against a wall, it's playing against that probability. Game theory is
just taking it to a slightly different dimension where you have two barbershops. What if you had
two barbershops and if one does this and the other does that, where are the customers going to go?
Right. Both barbershops could say expand their waiting room. Both barbershops could hire more
barbers or one could say, "You know what? If you hire barbers, I'm getting a bigger waiting room."
Yeah.
- Right, it's like that trade-off, yeah.
- Exactly, a very famous problem in game theory
is the prisoner's dilemma.
Two prisoners are,
two criminals are convicted
and are separated and are questioned,
and they can give evidence about the other prisoner.
If they both stay quiet, they both give no evidence,
the police won't have enough evidence
to send them to prison for a very long time.
they'll have to stay for a very short amount of time.
So that's in their interest,
but the police offer me a deal.
And if I give evidence about Geraint,
then I'll go free and Geraint will go to prison
for far longer.
Geraint gets offered the same deal
and that immediately creates a dilemma
that we will both end up getting to prison for a long time
because we will both give evidence one about the other.
And so that's a very simple model.
And that's actually what I came on the show
a long time ago to talk about was that actual model
and all the interesting stuff you could do with that.
But this game theoretic chapter is paired with another chapter
called agent-based modeling. It's again one of those points
where labeling is interesting because for some, agent-based
modeling is a subset of game theory. In a way, it doesn't
really matter. And so this chapter is all about emergent
behavior. If we can define how decision makers in our system
interact, what happens based on what they do, and they being
plural, then what is the emergent behavior that you can measure? And so the game that
we model in this chapter is there are two taxi firms that serve a community, again, a small town,
and they get to decide how many taxis they're going to have. And the town wants to incentivize
these two taxi firms, essentially not to collude.
They want to incentivize the taxi firms to give better service to the population.
And so we create the game that allows the taxi firms to decide how many taxis they're
going to have, one, two, or three.
And we build a little model that describes what happens if each taxi firm has one taxi,
what happens if one has one, the other has two, what happens if one has one, the other
as three, et cetera, et cetera.
And then we obtain what is called the Nash equilibrium,
which is a measure of if both tax firms know all this
and act rationally, what would we expect to happen?
- It's just amazing how these outcomes,
it's just so clear, you know,
if what's the likelihood of the other person doing
or the other player participant taking this particular
choice and if I take this choice,
what are they most likely to do and where are things going to settle in?
It's really neat how this all sets up here.
Let me find the section.
Yeah. So what you do is you create
a NumPy array that represents one of the participants outcomes and choices.
And this one, I guess it's a, what is this?
A zero sum game?
'Cause you can--
- Not quite, not quite.
It's not zero sum, it's symmetric.
So--
- Oh, symmetric, okay.
So you can pass in the transpose of that,
which says, here's the other player.
They get basically the opposite of that.
And then you just say, Nash.game, and off you go, right?
- Yeah, Nash.game creates this game.
It creates the game object, and in the game object,
the Nash Pi library has all sorts of different algorithms
that could be used and the one we use
is something called support enumeration,
which is essentially the basic by definition idea.
And the thing that we do do here is we say,
okay, if we are the town hall trying to decide
how to let these two tax firms compete,
is we essentially are deciding not how we regulate them,
but how we incentivize them.
So what game should we make the two taxi firms play?
And we do that by essentially saying,
I guess we will tax you slightly less
if you put on this amount of taxis
or give you an extra bonus if not.
And the idea is to, what is the smallest amount
that the city hall has to kind of guarantee
to the taxi firms to ensure that their rational behavior
would be to give best service to the population?
- Yeah. - So yeah,
then it's just those three lines
you got on the screen right there,
it's a little while loop
where it just increments that offset.
Yep, and you just say, you know, get the equilibrium from it and off it goes.
Yeah, very nice.
Okay.
This is really great and I guess it's worth pointing out.
I've heard of the guy.
Yeah, that's me.
I should say that.
The Nash Pi Library is myself and a former undergraduate.
Actually, they went on to start their PhD
at the School of Mathematics.
We started this library,
and it's grown quite nicely now.
To be honest, probably the part of the library
I'm the most proud of is the documentation.
There's arguably a game theory textbook in there,
if anyone's interested,
as well as all the functionality
for two-player games. >> Yeah, fantastic.
[laughter]
Yeah, it looks really great. I'm a big fan of game theory.
I think it's very simple,
and yet it reveals pretty amazing answers and behaviors. So, you know, as opposed to something
like partial differential equation, which can also reveal amazing answers, but the journey to get
through it is extremely long versus this. You could sit down in an afternoon and explain it to somebody,
you know, like that prisoner's dilemma and so on. We could all kind of relate to a lot of what's
happening there. I mean, to be honest, if you, this is one of those things, right, you know, you
you wonder, you realize that you're aging.
But I remember when I first started teaching game theory,
everyone had seen the movie "A Beautiful Mind."
But if people listening
and haven't, are slightly interested in game theory
and haven't seen "A Beautiful Mind,"
I'd definitely recommend it.
It's a Hollywood movie,
so have the correct level of expectations.
But it's a really nice movie.
It's Russell Crowe plays John Nash.
And it's more about the,
not necessarily about the mathematics,
about his life, which is incredibly interesting.
And there's a couple of scenes in that movie
that arguably if you just spend a little bit of time
talking about a couple of scenes in that movie,
you could explain most of the game theory.
- Yeah, absolutely.
All right, well, I get the sense we're a tad bit over time,
which is fine, but maybe we should wrap it up here.
There's a bunch of other areas that we haven't covered
that are lots of fun that you go into.
So yeah, very, very neat.
But let's kind of wrap it up.
Maybe we'll ask you the final two questions
and then we can--
(laughing)
- I want to thank you for having us,
but I know that you got these two questions coming.
So like, oh, do I thank you now?
Do I thank you later?
But no, I really do appreciate you having us on, Mike.
I really do appreciate it.
- Yeah, you're very welcome.
It's been great to talk about it.
All right, now maybe a lightning round.
Vint, if you're gonna write some Python code,
what editor are you using these days?
- I use Vim.
I use Vim.
- Vim, right on.
Grant?
- I use Sublime.
I really like having no features.
I just like typing.
I don't want features.
- Right on.
Perfect.
And then a notable PyPI package that you've come across
that you're thinking is worth giving a shout out to?
- I was thinking about this before,
and the one I'm gonna suggest is one called Python Ternary.
You've got it on the screen there.
Yeah, Python Ternary is a library for plotting on simplexes.
So for plotting on triangles, want a better word.
It's type of plots you draw a lot
in evolutionary game theory, as well as other things.
Actually a collaborator on another project of mine
that maintains this.
And it's a very nice library
for plotting different looking plots.
- Yeah, there's some, I'm sure people think about,
well, you could have these different plots
on triangle surfaces or how much are you,
how close are each three different states
or something like that.
- Yeah.
- You've got, some of these are really amazing,
like this M cherry one on there where it's got
kind of crawling graphs through these triangle
different states and so on.
Yeah, I haven't seen any one like that before.
- It's very cool.
It's very cool.
And it's built on top of MapPlotLib.
So it's essentially the layer to do all the correct scaling
and things like that is there for you.
So yeah, Python ternary is really cool.
- Yeah, fantastic.
Grant.
- So I think the library I would choose is,
it's called Traces.
It's a small library.
I don't know if you've heard of it before.
I got the link here.
It's one thing, yeah, it does one thing and one thing good.
it takes moving averages over irregular time intervals.
And it was a problem which I thought I had
until I found this, and then it just made life easier.
I liked it.
- Yeah, very cool.
Okay.
- I think it's the next link.
- Fantastic.
This one right here?
Not that one.
This one?
- There it is.
- Yes, the uneven time series.
Yeah, perfect.
Okay.
Oh yeah, that's great.
- It does one thing quite well.
Yeah.
And yeah, that's often just what you need.
Put a link to both of those in the show notes.
Thank you so much.
So people, people are interested in your book or maybe exploring some of these problems.
What do you tell them?
Where do they go?
I think if they're interested in the book, that's, that's great.
I think I, I, I'd more kind of emphasize just, just mathematics is so incredibly powerful
And it really, really is.
And it's kind of like, I feel that code, open source code,
Python, kind of an order of how important each other is,
with mathematics really, really gives you superpowers.
And so, I'd encourage people to investigate those things
and you can do amazing things with mathematics
and a bit of code.
That'd be mine.
I don't know what you think, Gary.
- No, no, yeah, I second that.
I think my main message is the maths and the software
are separate things and they complement each other
really, really well, but they are separate things.
- They are separate things, yeah, that's the thing.
- Yeah, fantastic.
All right, like I said, your book is on GitHub,
so people can go poke around some of the examples
and some of the notebooks and see what we're talking about.
Make it a little more concrete.
Make it a little more applied, maybe.
- Yes.
(laughing)
- All right guys, thanks for being here.
Thanks for sharing your work.
- Thank you so much.
- It's been a lot of fun.
Yeah.
- Thank you very much.
- Thank you.
- You bet.