Advanced Financial Modeling¶
Gives a quick overview of more advanced material that we didn’t have time to cover. Includes resources to go out an learn the advanced material, so this lecture can be viewed as a road-map for future learning. This is also a jumping off point for the Advanced Financial Modeling with Python course
Introduction to Advanced Financial Modeling¶
Notes¶
This lecture gives an overview of what was covered in the Financial Modeling with Python and Excel course and the types of topics that will be covered in the Advanced Financial Modeling with Python course
If you have not completed the Financial Modeling with Python and Excel course, I would recommend doing that first, or at least looking through the topics to make sure you know them
The Advanced Financial Modeling with Python course will assume knowledge of the topics in the basic course so that we can move more quickly
It will take me a while to cover all the topics mentioned here, so I have provided external resources you can use to learn the topics in the meantime
Please subscribe on YouTube to get notified about future course updates
Transcript¶
- hey everyone
- this is nick dear verdict teaching you
- financial modeling today
- we're going to be doing an introduction
- to advanced financial modeling
- so this lecture series has
- two main purposes one is
- to provide an ending to
- the first course of financial modeling
- with python and excel
- and give kind of a roadmap of where you
- can go
- in the future to learn more advanced
- topics
- then the other reason is that this
- serves as a jumping-off point for
- a new course that i'm creating advanced
- financial modeling
- with python so
- it's going to be a little bit slower
- releasing
- all the content for that course because
- the first course
- was developed as part of university of
- florida
- and now this advanced course is just
- on my own doing it um
- so i'm going to be releasing videos
- hopefully
- uh weekly or so um on additional more
- advanced topics
- but in the meantime i also give you a
- lot of resources
- in this lecture series so that you can
- go out and learn these topics on your
- own
- as well and then we'll have
- videos looking into each of the
- particular areas that we're going to
- introduce
- in this lecture series
- so we're also going to just quickly
- recap here what we covered in
- the first course so that if anyone is
- coming in to try and do the advanced
- course they can get a good idea of
- what they didn't get from the first
- course
- so we covered both excel in python
- basics
- in in the first course um
- and that includes you know just general
- python programming as well as building
- full-on financial models
- and as well as excel we kind of
- took the basics for granted and then
- focused on building the financial models
- and also in both tools how to structure
- things appropriately
- such that for larger models you're not
- going to end up with this
- mess you're going to be able to stay
- organized
- um so some of the areas that we covered
- in that
- in the first course are
- [Music]
- how to do a couple different kinds of
- financial modeling looking at cash flow
- modeling
- and probability modeling as well as
- different ways that we can
- extend the base model using sensitivity
- analysis
- scenario analysis and monte carlo
- simulations
- and looking at the particular types of
- financial models that we covered
- in the first course uh we looked at a
- retirement
- model how much you need to save to
- retire
- uh with a certain amount at a certain
- time we looked at capital budgeting
- in order to analyze a project and find
- out
- whether it's a profitable project
- we looked at also
- evaluating giving a loan to someone else
- and
- factoring in the probability of default
- into that and bankruptcy and recovery
- and all of that
- and the last part of the course
- developed the discounted cash flow
- valuation of a stock both the estimation
- of the weighted average cost of capital
- as well as the forecasting of the future
- free cash flows
- but there's a lot more to financial
- modeling
- um really that course is still kind of
- just hitting on the basics of everything
- um and so this lecture series
- we're going to look at a number of
- different areas where you can continue
- to expand
- in the future with your skills so that
- you can
- become an advanced financial modeler
- so those areas
- include additional types of financial
- models
- it includes also
- [Music]
- pipelines how we get data into the model
- in an automated way
- it's going to cover some additional math
- tools
- in terms of um you know additional
- mathematical capabilities we can build
- into our models such as
- symbolic computation and optimization
- and a few more things we can look at
- there
- advanced statistical models
- we're going to look at particularly
- for python how we can present the
- results from the model because
- you know with excel it's it's fairly
- straightforward you just have the
- spreadsheet you format it well
- with python there's a lot more to the
- story but there's a lot more possibility
- there as well
- we're also going to look at
- additional programming topics that you
- can learn both
- uh python specifically as well as more
- general
- programming topics which are helpful
- and we'll also just cover some you know
- general extra resources that you can use
- to continue learning
- so that's the overview of
- all the areas that we're going to cover
- in the advanced financial modeling with
- python course
- and for the rest of this lecture series
- we're just going to go through each area
- and give kind of the highlights of what
- we're going to be looking at
- as well as resources that you can go out
- and learn on your own
- and then going forward i will be
- releasing more lectures
- on each of these topics as well
- so if you want to get notified about
- when i'm going to be releasing those
- feature videos please just go ahead and
- subscribe
- on youtube and you will be notified
- so that's a quick overview of the
- introduction to advanced financial
- modeling so thanks for listening
- and see you next time
Additional Types of Financial Models¶
Notes¶
This lecture does not seek to give a comprehensive list of financial models, but instead highlights a selection of common models
In future lectures, we will look at implementing some of these in Python
Resources are listed for both Excel and Python models. The free Python resources I have been able to find outside this course do not have very good coding standards, though, so bring what you have learned from the Financial Modeling with Python and Excel course when looking at these
Transcript¶
- hey everyone
- this is nick duraburtis teaching you
- financial modeling today we're going to
- be taking a look
- at some more types of financial models
- that we did not cover in the
- financial modeling with python and excel
- course but that
- we may look at in the advanced financial
- modeling with python course
- um and so the point of this video is not
- to list every possible financial model
- but instead to look at what some of the
- most common
- models are and just briefly
- discuss them so
- um the first model that we're looking at
- here
- is uh portfolio models
- it's a general class of models portfolio
- evaluation portfolio optimization
- so these are all about putting together
- a portfolio of different assets
- and determining the risk and return on
- that portfolio
- and oftentimes you want to choose
- how to allocate the assets such that you
- will
- maximize the amount of return that
- you're getting for the amount of risk
- that you're taking on
- then we have additional funds needed
- models
- so this is about trying to forecast
- about uh how much money how much capital
- the company is going to need to raise
- in order to satisfy operations in the
- next period
- so we actually already in the
- original course covered a lot of what we
- need here
- because in order to do the dcf model
- we had to learn how to work with the
- financial statements and forecast the
- financial statements
- and that's exactly what you're doing
- here as well you're going to forecast
- the financial statements
- and then you are determining uh based
- upon that
- how much cash you're going to need to
- raise
- through either debt or equity so
- not too much of an extension beyond what
- we've already done there but it is a
- common class of models
- and then there are lease or owned models
- where
- you're evaluating some asset and the
- rental rate
- on that asset and how much it costs to
- purchase it and trying to make a
- decision about that um
- we have event studies um are looking at
- something that
- occurs at some point in time and then
- trying to understand
- what changed because of that event so
- it's kind of looking through
- historical data in order to
- look for a pattern that was caused by
- the event
- we have m a merger and acquisition
- models so this is
- looking at one company evaluating
- purchasing another company and how that
- is going to
- affect the financials of the combined
- company
- and so it's basically
- again the dcf model um
- got us a lot of the way towards building
- this out
- it's basically um
- you take the the parent company
- and the the company that's being
- purchased and you kind of combine their
- financials together
- and do a dcf on the combined financials
- um and that allows you to determine
- the value of the combined company and
- you compare that to the value of
- the parent company um and that
- difference there
- is going to be the value of the
- company that's going to be acquired to
- the acquirer
- um and so that can
- help in setting the price in an m a
- transaction
- then we have lbo leveraged buyout models
- so this is a
- particular type of m a model which is
- focused on uh taking out a large amount
- of debt and using that
- to purchase the firm so
- again the m a model is a dcf model
- with a little bit extra added to it
- the lbo is an m a model with a little
- bit extra added to it as well
- and so it's basically just
- modeling the debt side of this model in
- a lot more detail
- saying that you know you're going to
- have different tranches of debt
- with different seniority and what are
- going to be the cost of each of those
- and using that to evaluate
- the again purchase price of the
- targeted firm
- and ensuring that you're going to be
- able to pay back the debt and all of
- that
- um and there are lots of models
- around valuing derivatives
- um you know options swaps forwards
- uh any kind of derivative um
- forward and future models tend to be
- quite simple
- um swaps are not
- that complicated options are where
- things tend to get
- fairly complicated in the models with
- with quite a bit of math
- going on but
- if you're interested in going into
- derivatives it's definitely makes sense
- to
- check these out
- debt models we did cover um you know
- evaluating a loan
- uh from the perspective of a lender um
- in the in the first course but there are
- a lot of debt models that we didn't
- cover
- um so one example here is
- immunization models which uh are about
- saving about uh having the right amount
- of cash at the right time
- in the future uh such that you're gonna
- invest um in low enough for a securities
- in order to meet that objective
- uh but you wanna try to maximize your
- return
- while still ensuring that you're gonna
- meet that objective
- term structure models try to explain
- the yields on different maturities of
- bonds
- and default adjusted
- return models uh are for evaluating the
- returns on
- debt instruments once you consider the
- default as well so
- the the model that we looked at in the
- course is
- uh you know sort of in this regard of of
- trying to adjust for the default
- and the value of debt
- and then value at risk is another
- type of uh
- yeah i guess we can call it a model it's
- you know i don't know if it's much more
- than a calculation but
- um it's trying to quantify
- what's the maximum that you're going to
- lose
- with a certain probability like i'm 95
- confident i'm not going to lose more
- than a thousand dollars in one day
- are the kind of statements that you can
- make with the
- vir analysis we did actually look
- at monte carlo simulation and within
- monte carlo simulation we looked at
- evaluating the different quantiles of
- the distribution of the outcome and that
- actually will achieve
- this same value at risk concept
- but there are also ways to go about that
- without doing a monte carlo simulation
- in certain contexts such as portfolio
- settings
- and again that's not a comprehensive
- list of financial models that's just
- kind of the most main ones that you see
- commonly out there
- that we didn't cover in the first course
- um and so i'm giving some
- links here in the slides so definitely
- download the slides from the course
- website to look at
- these resources and we have a few here
- that are excel based resources
- so right now as far as free financial
- modeling resources out there
- the vast majority are focused on excel
- um
- so it can be useful just to get an idea
- about the models and learn about
- you know the the finance side of things
- that goes into the model
- and then you can try to go and recreate
- it in python if you're interested in
- that
- or wait for when i'll be releasing
- videos on doing some of these things in
- python
- um i also found a couple free python
- financial
- modeling related resources um
- what i've seen in these is that they
- don't tend to have very good
- coding standards um that
- the authors of these courses are
- certainly know what they're doing they
- certainly
- have good finance experience but they're
- maybe uh you know not as advanced
- working with python and so
- don't follow as as good of coding
- conventions
- as we do in this course
- so that's a quick overview of some of
- the main types of financial models that
- we didn't cover in the first course but
- i hope to cover
- in the advanced course
- and some resources for you to go out and
- learn these things on your own as well
- so thanks for listening and see you next
- time
Data Pipelines for Financial Modeling¶
Notes¶
Data pipelines are a very common application for automation in a business setting
Models need data, and most models get updated over time with new data. Data pipelines are all about streamlining getting the data into your model
First you must gather the data (data collection), then get it into the format ready to be consumed by your model (data cleaning/wrangling)
In deciding whether to build an automated data pipeline or to manually complete the steps, consider how often the data will need to be updated and how long it takes you to complete the manual process
There are additional advantages beyond time savings: elimination of mistakes and allowing new opportunities (e.g. historically the report is run weekly, now that it is automated it is run every hour)
Web scraping, API access, and SQL databases will be where you get most of your data beyond Excel spreadsheets
Data wrangling means cleaning the data of errors and unneeded values as well as reformatting it to be used in your model, such as combining multiple sources, aggregating, resampling, etc.
Requests is typically used for basic HTML scraping, while Selenium is typically used for for web pages that use JavaScript to display the necessary data
Helium is an easier way to use Selenium
Transcript¶
- hey everyone
- this is nick nirovertis teaching you
- financial modeling
- today we're going to be learning about
- data pipelines
- for financial modeling this is part of
- our introduction
- to the advanced financial modeling with
- python course
- so pipelines
- uh basically we can think of data
- pipeline
- as what gets the data into your model
- your model has all the logic on the
- inside but you still need
- the inputs into the model the data in
- order to
- get some kind of conclusion out of the
- model um
- and so sometimes your model is very
- hypothetical and you're just putting
- inputs in
- and then you don't really need a data
- pipeline but sometimes
- you are pulling in financial data
- stock prices uh you know other
- other sources of data and you need to
- process them
- maybe you need to put different sources
- together maybe you need to clean things
- up
- maybe you need to aggregate things in
- some way
- in order to actually work with your
- model
- and the data pipeline
- is there whether you automate it or not
- uh you can do a manual data pipeline
- um but there are definitely advantages
- to
- automating it so we we think about
- generally
- two main uh sections
- of a data pipeline one is the collection
- of the data how do you get it in the
- first place
- and the other is what we call
- wrangling the data which is a process of
- cleaning the data up
- and reformatting it in such a way that
- it's ready to be used
- by your model
- so again you can do all this manually
- um and that's what a lot of people are
- doing
- especially with excel models
- um but you can
- save yourself a lot of time by
- automating these steps
- um but you do have to consider how this
- model is being used
- if you're you know using this model
- to generate a report every single week
- and every week there's 30 minutes
- of manual work to do this data pipeline
- to go
- get the data that you need and then uh
- put things together and modify it in a
- way that's
- ready for your model you're spending a
- half an hour every week doing this
- um well maybe it makes sense to automate
- that
- maybe spend a few hours one time to
- automate it
- and the results of that are going to pay
- off over time because then you're not
- spending that 30 minutes every week
- you put the cost in one time to automate
- it and then
- it just goes and it runs on its own you
- click button and you've got the report
- you don't have to worry about the data
- um but uh
- you know if this is a model you're just
- going to use one time
- or you're not re-running it very often
- then maybe it doesn't make sense to go
- through and automate all this
- i mean if you want to do it for a
- learning exercise sure go ahead
- but ultimately the main the main thing
- with the automation here is you're
- trying to save time
- and so if you're going to spend way
- longer automating it than just doing it
- manually
- then probably just do it manually
- but there are other advantages to the
- automated approach
- beyond just saving time
- one is that you are going to eliminate
- the possibility for
- human error you know as long as you
- write the code correctly
- then it's going to do the exact same
- thing with your data every time
- and so you're not going to have to worry
- about accidentally missing a step in
- your process
- or you know handling one one row of the
- data
- accidentally differently than all the
- others or something like that
- which could cause a mistake in your your
- model and and
- the report from your model so if it's
- all automated
- you're gonna expect to get the same
- results every time
- and another uh good advantage is that it
- can open up new possibilities
- because maybe you know you're running
- this report every week and
- um you know everyone only considered
- ever doing it weekly because it does
- take 30 minutes of manual effort to put
- it together
- but now that you could click a button
- and you get the report
- well maybe you just set that up on a
- schedule and now
- every hour that report is generated and
- everyone always has
- the most timely information possible
- and nobody would have ever have dreamed
- of doing that before because
- you know basically it would become
- someone's full-time job to keep
- generating that report
- and now it's just automated it happens
- automatically in the background
- um so there's a lot of value
- in automating your data pipeline
- but again it there's trade-offs and if
- you're going to spend 10 hours to
- automate
- to save you know five minutes that
- you're going to do a few times
- it probably doesn't make sense
- so looking at the data collection side
- um
- we can get data into our models from a
- number of different ways
- usually you're going to be accessing the
- internet in some way to get the data
- whether that's from a website or it's
- from you know
- shared server where someone has an excel
- file or it's from a database or from an
- api
- you know all these are ultimately going
- over the internet at some point to get
- the data
- [Music]
- um
- and when we think about data which is on
- websites um if you can manually get that
- data somehow
- through a website then you can automate
- that process
- via web scraping so whether that's you
- know
- going to some site and downloading some
- file
- or it's actually you know there's a
- table on some site and you want to
- extract the data from that table
- um or even just you know random piece of
- information on a web page
- and you want to um you know look at 3
- 000
- different of these similar web pages and
- get that same information out of each
- one
- you can do all these things with web
- scraping
- um so
- it doesn't even have to be structured um
- you know you could um
- go to say yahoo finance and just grab
- some of the key statistics
- and then do it for every every company
- that yahoo finance has and have
- thousands and thousands
- of companies in your data set all
- through this
- web scraping process though
- i'm not going to endorse specifically
- using yahoo finance they do have
- terms of service against this but i'm
- just
- giving that as a possible example
- um another source of
- common source of data is through apis
- so there you send a web request to the
- api
- and it responds with the data
- usually in either a json or an xml
- format and then you can bring that into
- your model
- um and for
- um both web scraping
- and or accessing apis
- you can use the request package in
- python
- it's an easy way to send web requests um
- so if you have a website that's very
- simple
- it doesn't uh use javascript in order to
- load and display the data then request
- is a good fit in order to
- get that web page and extract the
- information and
- if it's a site that does use a lot of
- javascript to display the data
- or you just really prefer to work
- with a browser instead of like the
- command line interface
- selenium is a package which is useful
- for that because
- it actually drives a web browser so you
- can have chrome or firefox
- just up on the screen and you run some
- code and you see
- uh that the page is changing in firefox
- or you're pulling information out
- etc and so it's a nice like kind of
- visual way to go through it you can
- actually see what happens in the browser
- as you run the code
- [Music]
- and then databases
- are another general way that you're
- going to get
- information into your model
- and typically sql databases are the most
- common
- business databases out there sometimes
- it's nosql databases and there
- are packages which help work with those
- as well
- depends on which database but sql
- is more general more common
- and you can use the same approaches
- regardless of
- what type of sql database it is
- so you're you're not going to get 100
- away from sql like sql
- is a query language
- that lets you write
- these queries which are going to extract
- information from a database
- um or update the database and things
- like that
- um so you are still going to have to
- know a little bit of sql
- or at least understand the basics
- but python can help you
- work with these sql databases such that
- you don't actually have to write
- sql code you can use python
- you can use sql alchemy which lets you
- create
- sql queries with python code
- and pandas also has a read sql
- function so if you just want to bring in
- one database table and bring it into
- your model
- then pandas is going to be a good
- approach for that you can just
- read it in and if you need to do more
- complex queries
- across multiple tables and things like
- that then
- sql alchemy can be a good choice to help
- you with that
- also if you um need to take the result
- of your model and store it into a
- database then
- sql alchemy is also more helpful for
- that than panas
- so that's the data cleaning side and
- then looking at the data wrangling side
- of data pipelines
- so we have already worked with pandas
- in the first course and that is going to
- be your best
- general purpose uh package to use for
- wrangling data because
- you can do so many different
- manipulations of the data with it
- um so that's kind of the gold standard
- in python for wrangling data
- but we didn't cover it in the necessary
- detail
- to cover you know everything that you
- would expect to run into in data
- cleaning
- so i'll give some resources on where you
- can learn more
- about pandas to cover all these cases
- some of the main areas we didn't cover
- but are useful
- for this are um
- we covered a little bit on selecting
- data but you know more advanced
- selecting of data merging different data
- sets together um
- grouping and doing aggregations within
- those groups
- like you know maybe you have sales for
- each
- uh you know store in your company and
- you want to get
- sales by region so group
- by the region and and some across the
- stores
- and reshaping the data in different ways
- and that includes
- resampling if you have some time series
- and you need to change the frequency of
- the time series
- um so the reason this is not called just
- data cleaning
- is because cleaning generally refers to
- well you know if there's missing values
- in there you need to remove
- or there's uh you know invalid data in
- there
- like errors or there's other outliers
- you want to remove
- that part is generally referred to as
- cleaning
- um whereas there's the other side which
- is
- you might have perfectly cleaned data
- but it's still not
- in the structure that you need for your
- model
- so then this kind of reformatting of the
- data
- um by combining it with other data
- aggregating
- um selecting different parts of it etc
- um that's kind of the data reformatting
- side of things and together they make up
- the data wrangling
- one other thing i just want to mention
- in here that helps with
- the data cleaning side of things
- is regular expressions so regular
- expressions are actually
- their own kind of mini programming
- language on their own
- uh exactly they're actually supported in
- a lot of different
- programming languages with the same
- syntax
- um and their ways of matching different
- strings and extracting parts out of
- those strings
- so especially in the context of
- web scraping where you're just pulling
- in a you know
- big html file and you want to extract
- certain parts of it
- these are really useful and also
- sometimes you just
- get you know choosing your data
- which this can help with like
- you know maybe there's a percent sign on
- the end you got to remove that and then
- um you know convert it into decimal
- um so this can help with things like
- that
- and so i'm giving you a number of
- different
- resources here to help you learn these
- topics
- in the meantime until i'm able to post
- additional lectures
- on uh focusing on these topics
- so um there's a couple resources which
- are related to the web scraping
- with browser um so just an introduction
- to selenium
- um and then there's this other python
- package called helium
- which makes a little bit easier to use
- selenium
- so i would kind of look at the first one
- just to get an understanding of
- what all this is and then look at the
- second for maybe an easier way to do it
- um and then also looking at
- um requests which which helps you with
- this kind of more basic web scraping as
- well as
- accessing apis
- and couple resources on pamus
- so the introduction to pandas which
- uh has a lot of stuff we've already
- covered with pandas but also
- gives quick intros on the remaining
- topics and then
- you know the full um
- uh cookbook in pandas which gives you
- strategies for for a bunch of more
- advanced uh data pipeline related tasks
- a couple resources on regular
- expressions
- i think it's useful to go through an
- intro just to understand them
- but then they are somewhat complicated
- i myself even generally pull up the
- reference
- when i write regular expressions so it's
- nice to have that
- um and then
- a few resources on sql alchemy
- i'm giving an overview um
- as uh you know getting started with it
- and then the
- full documentation as well
- so that covers an introduction
- to data pipelines and financial models
- and i hope in the future to release
- more dedicated videos on
- both uh data collection and
- uh wrangling and looking at web scraping
- and all of these more individual topics
- but you have these resources to go off
- of
- in the meantime so thanks for listening
- and see you next time
Advanced Mathematical Tools for Financial Modeling¶
Notes¶
We have already covered one library typically used for high-performance computation: numpy
SciPy is the gold-standard for optimization in Python
Sympy lets you do algebra and calculus with symbolic math, so it is great for working with lots of or very complex equations. You can convert the symbolic formula into a function directly
fuzzywuzzy is a library that makes working Levenshtein distance very simple
There are a lot more statistical models you can access through statsmodels beyond just OLS. There is also linearmodels for working with panel data and SciPy has some additional tools
Scikit-Learn is the standard for high-level machine learning in Python, though its performance is limited
Keras helps with building deep learning models and can be more performant than Scikit-Learn
Transcript¶
- hey everyone
- this is nick dear brotis teaching you
- financial modeling
- today we're going to be talking about
- advanced mathematical tools
- for financial modeling this is part of
- our
- lecture series that introduces the new
- advanced
- financial modeling with python course
- and wraps up the prior financial
- modeling with python and excel course
- so we
- um already covered um you know kind of
- the basic
- mathematical tools for financial
- modeling
- in the first course um
- that includes algebra some basic
- probability theory
- and some basic kind of statistical tools
- um including regressions standard
- deviation etc
- um and in general
- this is going to get you through most
- financial models
- most financial models don't have very
- complicated math
- in them um it's typically mostly algebra
- and a little bit of probability and
- statistics
- um now some of them do
- definitely take more math um derivatives
- models
- are a good example especially looking at
- options
- um and you know any more exotic
- derivatives
- um they certainly take more complicated
- math
- um then also if you're going to build
- some more
- really custom financial models you might
- end up doing
- quite a bit of math but for
- most of the models that you're going to
- see you know thinking about
- most of the models we talked about in
- the other main financial
- models we didn't cover um
- most of those are not going to really
- take any additional math tools beyond
- what we've learned
- but i think it is still useful to cover
- these basic tools
- or more advanced tools
- in case people do end up working on
- those models
- which are more mathematical
- so one which which is um
- useful in a lot of different settings
- is optimization so optimization
- lets you maximize or minimize some
- quantity in your model and
- often in finance we want to do that
- right we want to
- get the most profit or mpv
- we want to have the least risk or least
- loss
- and so optimization out of all the math
- tools is definitely
- the most relevant to the most financial
- models
- and in python we can use the scipy
- library to help us with optimization
- it's it's kind of the standard library
- for doing optimization in python
- um and
- in general when you think about
- optimization
- and we're saying maximize or minimize
- here
- but when you look you'll find generally
- just uh people talking about minimizing
- and not about maximizing
- and similarly in scipy you'll see
- minimization functions but
- not maximization functions
- and that's because minimization and
- maximization are actually the same
- thing
- and you can
- um make them equivalent to each other by
- just adding a negative sign
- um you just take your your
- everything that you want to maximize
- function you want to maximize you know
- your model function which outputs the
- mpv
- you want to maximize that mpv well you
- just
- write one more function which takes the
- negative
- of that result and returns it
- and now you want to max minimize that
- and that's going to maximize the mpv
- so you always minimize if you want to
- maximize something
- just add a minus sign for it
- um then
- [Music]
- where i was mentioning the you know
- really custom complex
- financial models that's where computer
- symbolic algebra system is helpful
- so if you have a lot of complicated
- algebra
- going on in your model or calculus you
- know you need to take derivatives
- integrals
- of symbolic equations uh
- you can use senpai in python to help
- with that so you can actually
- have equations as code and
- you can you know uh
- combine different equations solve
- systems of equations you can
- take derivatives and integrals um
- etc so and you just work with these
- things as python objects
- so you can like have a list of equations
- and iterate through that list and do
- things with each of the equations
- um and you can also take any of these
- uh equations symbolic equations
- and convert it into a python function
- which is going to you know take all the
- inputs of the
- variables in the equation and give you
- uh
- the result of that so
- very useful if you have some complicated
- symbolic math
- that you need to do
- uh then we also have
- levenshtein distance so this
- is a calculation of how similar
- two different strings are um
- so you know it would say like cat
- is similar to bat but cat
- is not similar to remote
- um so it gives you a numeric score to
- quantify this
- um so this is useful for financial
- models mainly in the context of the data
- pipelines
- in that you can
- say extract
- you know maybe there's some some noise
- in your whatever data you're collecting
- such that um you know say it's like
- uh you scanned in some financial
- statements on paper
- and now you've done um optical character
- recognition ocr
- and now you've got all that
- represented electronically but there
- were some errors and transferring that
- over
- so this
- edit distance leverage time distance you
- know you could um
- use that on say the statement items and
- if somebody doesn't match up
- well maybe it still has a 95 similarity
- and so you can still match it up
- um a lot of different settings where
- that's useful
- but mainly in the data pipeline process
- in the context of financial models
- and then looking more specifically at
- statistics
- additional tools so we already covered
- ordinarily squares ols
- regressions and if someone just says run
- a regression
- that's generally what they're talking
- about um
- so that covers the majority of cases
- but there are a lot of other regression
- models
- out there
- you know there are full courses out
- there econometrics courses on
- all the different types of models that
- you can run and how you can
- get conclusions and predictions out of
- them um
- it's definitely outside the scope of
- this course to discuss all the different
- possible ways
- um but i'll just mention a couple here
- which are useful
- uh logistic regression uh
- is useful when your dependent variable
- whatever you're trying to explain is a
- probability
- itself like say you're trying to
- determine what affects the probability
- of default
- um then using a logistic regression
- would make sense
- and then panel regression with fixed
- effects
- is a very nice technique
- for when you have multiple different
- things that you observe
- over time so if you have
- multiple different companies financials
- over time
- multiple different stock prices over
- time et cetera
- and you want to run a regression with
- those data
- then panel regression is generally a
- good fit
- and fix effects basically allow you to
- control
- for whatever is not changing
- within these companies over time
- or whatever you know
- whatever happens in a certain time
- period that affects all the companies in
- that time period
- it can also help you control for that um
- so definitely take a look at that if
- you've got multiple different things
- that you're tracking over time and you
- want to do a regression on that
- i mentioned back in the forecasting
- lecture
- that we didn't cover a lot of different
- time series
- models so you can go back and look at
- that lecture i gave some resources there
- on how you can go
- learn about additional models there
- and then um machine learning and ai
- are useful to classify and predict
- different quantities so classification
- is about
- you know assigning into groups um
- you know is this is this picture
- of a cat or is it not of a cat uh that's
- classification and then predicting
- prediction is about trying to determine
- the specific quantitative value of
- something
- so you might be trying to predict
- tomorrow's stock price
- and you can use a machine learning model
- for that
- or you could be trying to classify is
- the stock going to go
- up in the next period or is it going to
- go down in the next period
- so and there's a lot of different
- applications
- for this and it
- sounds a lot more complicated than it is
- to carry out certainly it's a whole
- field
- there's a lot of complication there but
- there are
- high-level packages out there that make
- this process fairly easy
- for the vast majority of tasks that you
- would
- want to apply this to if you need to
- have a really really high performance
- model
- or it's for a very specialized task
- then um you know there's going to be a
- lot of complicated work to go into it
- but
- in general uh for probably 99
- of of potential applications
- it really is not difficult to build
- these if you can run an ols
- regression with stats models you can run
- their machine learning model with
- scikit-learn
- now it does take a little more knowledge
- to understand when you should apply
- each model in what situation but there
- are also
- auto machine learning packages which
- kind of
- go through that process for you
- so that can reduce the entire machine
- learning process
- once you have your data prepared and
- ready to go
- then just down to like four or five
- lines of code
- um so a lot of people get threatened by
- just
- hearing ai it sounds really complicated
- um but
- people have automated so much of this
- process that
- it's not that difficult as an average
- user of machine learning
- so then um the resources i'm showing
- here
- that you can use to learn these topics
- in the meantime until i'm able to
- post any more particular lectures on
- them we have
- uh how to approach optimization using
- scipy
- how to use senpai in order to do
- computer algebra
- and calculus and things like that
- we can use the fuzzy wuzzy package in
- order to calculate levenshtein distance
- in an easy way
- we can look at
- doing logistic regression using stats
- models and this is kind of a softer
- introduction
- to that same thing with panel regression
- so there we can use a package called
- linear models
- which is focused almost exclusively on
- panel
- approaches and
- there's a uh basically a full list of
- the models that are available in staff
- models which is
- quite a few um and then
- linear models also has additional models
- beyond the basic panel regression
- um and there's some additional
- statistics related tools in scipy that i
- like there
- um and then i added um a few different
- resources here on machine learning
- um so just kind of a general um
- you know light introduction to it a
- couple different resources for that
- and then looking at
- using a couple different libraries so
- generally
- scikit-learn is the kind of
- gold standard for easy machine learning
- in python
- as long as you don't need it to be
- really high performance
- um and then keras has emerged as
- the um you know easy high-level library
- for doing
- deep learning which is what people
- generally refer to
- uh are referring to when they say ai
- and did i mention this
- auto machine learning where
- [Music]
- you don't even have to pick what model
- you're going to use like that part is
- also automated um
- there's this package called auto sklearn
- where you just put a few lines and you
- have a well fitted uh
- machine learning model so um
- pretty interesting that we've hit that
- point in development
- in that field that you really don't
- have to know much at all and you can go
- and apply machine learning
- so that's a quick introduction
- to more advanced mathematical tools
- which are relevant to financial modeling
- and again i hope to be able to post
- additional lectures
- on these topics more specifically but
- you have these resources to go off for
- for now so thanks for listening and see
- you next time
Better Presentation of Python Financial Models¶
Notes¶
Presentation from a Python model is a more complex story than doing the same with an Excel model. But you also have many more possibilities
The easiest presentation method is to use the Jupyter notebook itself. You can offload code into separate .py files and just import it so that there is not much code in the notebook
You can create reports from your Python model. You could output to Excel, HTML/CSS, LaTeX, or PDF to create the report.
If you go with HTML/CSS, Jinja will be useful for templating. HTML/CSS can also be converted into a PDF with pdfkit
If you go with LaTeX, pyexlatex allows you to create Python objects which compile into LaTeX and PDFs
You can go direct to PDF with reportlab
You can have the most polished presentation of your model by building an app, which is not as hard as it sounds. Voila can directly convert your Jupyter notebook into an app. Panel will let you create a more customized app, by adding onto your existing Jupyter notebook
You can learn about building full web applications from scratch with Flask, but that is more complicated than the other approaches
Interactive plots allow the consumer of the model to view more information in a single plot by adding some dynamic behavior to it. There are many libraries for this such as Bokeh, Altair, Plotly, and Holoviews
Transcript¶
- hey everyone
- this is nick dearbortis teaching you
- financial modeling
- today we're going to be learning about
- how to
- improve the presentation of python
- models this is part of our lecture
- segment that introduces
- the advanced financial modeling with
- python course
- and closes out the financial modeling
- with python and excel course
- so we um
- in the first course the financial
- modeling with python excel course
- we have just been focusing on um
- having the model in a jupyter notebook
- and
- uh doing a little formatting on the
- output within the jupiter notebook
- so turning out strings that
- explain the result rather than just
- showing a number
- using data frames and styling the data
- frames
- um using number formatting to get the
- numbers formatted appropriately
- using plots in order to add some visuals
- to it
- but we didn't really go beyond that
- um and when we think about the excel
- side of things
- it's very straightforward right like
- uh the model
- is what you see um and so if you just
- kind of organize things well in the
- model
- and you format it well um in excel
- then that's gonna be all you really need
- to do and that's kind of the end of the
- story
- but for python it's definitely more
- complicated um
- in terms of how you're going to present
- your results
- because there's a lot of different
- possibilities
- so on one hand it can be
- you know some additional steps to get
- your model ready
- results from your model ready to present
- to someone else
- but on the other hand you have such a
- larger
- uh array of options and possible
- ways that you can present your model
- and some of them don't
- really take uh much work at all like i
- mean
- you can already go with the way that we
- covered in in the first course of just
- you know having everything in the
- jupiter notebook as long as you
- have all the clear sections uh and you
- have the markdown in there kind of
- explaining what's going on
- that's still a pretty good presentation
- medium it's not bad
- and it doesn't take much extra effort um
- that's kind of the easiest
- method um
- but at least at least i guess ideal
- for the end consumer of your model if
- they're a non-technical
- consumer um because
- you know when you look at an excel model
- all the formulas are hidden unless you
- click into cells
- but when you look at a jupiter notebook
- all the code is there
- no matter what um and so someone
- who has no idea about python or what
- you're doing in this model is just
- trying to read through and get the
- results um
- you know they're gonna have to go
- through that code as well and you can
- tell them just you know ignore the code
- but it's still in there
- and it can be distracting from the
- results
- um so we'll discuss a few different
- approaches we can take with python
- to present your results um
- going from the extremes of you know just
- have everything in one jupiter notebook
- they can just skip over the code uh to
- all the way to
- uh creating actually a web application
- where they can interact with your model
- by just changing the inputs and seeing
- all the outputs right there
- dynamically updating and everything um
- but of course that takes the most effort
- to get there
- um and so we'll cover some options in
- between as well
- so one thing which is quite simple
- that we can do um so this is just going
- one step beyond everything in the
- jupiter notebook
- um is to try to just separate out
- some of that logic um so
- you um just take
- all the main classes and functions in
- the model
- and you put them into separate dot py
- files
- and the python modules and that way you
- can
- import those into the jupyter notebook
- but it's not going to
- show all of the functioning class
- definitions
- in the notebook um so then it's
- you know just you import a couple things
- maybe it's even just one function which
- is just
- you know run your model and then it
- spits out all of the
- output from it um and then you know
- there's only a couple lines of code in
- there so it's really not distracting at
- all
- to a non-technical person all your code
- is just offloaded into these separate
- python files
- so this already goes a long way
- to making your model more readable
- for a non-technical consumer but one
- drawback you still have here is it's
- still a jupiter notebook
- and so they still have to have python in
- their system
- they still have to have jupiter set up
- and everything
- and have to understand how to open a
- jupiter notebook which
- um you know at this if you've gone
- through the first course it's
- straightforward at this point right but
- you know when you got started with that
- course you probably didn't know how to
- open jupiter notebook either
- and so you know you would then have to
- go explain this to your boss or whoever
- how to even open this thing um
- and so there's still drawbacks with this
- approach
- so kind of the next way going towards
- making it easier and cleaner for the
- non-technical person
- but taking a little bit more work to get
- there is going to creating reports
- um so if the
- um consumers of your model don't need to
- play around with it at all they just
- want to get you know what are the
- results
- from you running the model then reports
- are a good fit
- and there are three different
- um general ways that you
- can go to making kind of a
- prepared report um in addition to
- outputting to an excel file so you can
- always output it to an excel file
- and um you can
- even do some formatting from the python
- code into the excel file
- so that's one route
- and then you can go where you don't even
- need to have excel
- to view the results which the main
- approaches there would be
- html and css latex
- and going directly to a pdf
- so if your ultimate goal is just to get
- a pdf
- then you can just go directly to that
- but also
- both the html and css approach and the
- latex approach
- are going to allow conversion into a pdf
- as well
- so you know generally personally i have
- not
- tended to go for the direct pdf
- solutions
- because i can go for one of the other
- formats
- and then i'm not only able to have it in
- a pdf but also in other formats as well
- um so html css
- natively can be loaded up in a browser
- um
- so you can view it like a web page and
- then
- uh latex can also be converted to html
- and so you can
- convert it to pdf view it that way or
- convert it to html css
- and view it in a web browser
- and
- in building this report basically you're
- going to have some kind of
- layout in the report and then you're
- going to want to drop your data
- into it your results into the certain
- spots and the report
- and this kind of general structure is
- referred to as templating
- and those are generally easier to go
- through doing
- html and latex approaches than with the
- direct
- pdf approach so another reason that
- i don't generally recommend going direct
- to pdf
- but if that is all that you you know
- that's all you're ever going to need is
- a pdf
- and you're not comfortable with html and
- css or latex
- then it can make sense to do that
- um and when we get to the resources
- section i'll talk about
- um some of the packages that are
- generally used for these different
- approaches
- um so then we get all the way to the
- other extreme of the best experience
- for the non-technical user of your model
- um but also the most work on your end
- as the creator of the model to set this
- up
- and that is creating an app out of your
- model
- um which some people
- watching this right now may think i'm
- crazy to say create an
- app out of your financial model um
- it sounds on the service like a huge
- amount of work
- but it's really not um
- there are ways you can actually just
- take an entire jupiter notebook and
- convert it into an app
- with no effort on your end um
- and then there's also ways to extend
- your jupiter notebook
- to make it into an app in a more custom
- way that you
- want um so
- with neither of these approaches do you
- have to go and just you know completely
- like go and learn you know full-on
- software engineering to create full
- stack apps and then totally recreate
- your model
- like that's not what i'm talking about
- here it's
- using some packages which are going to
- help you basically add on to your model
- to convert it into an app
- which is easier than it sounds
- now you can go all the way to creating a
- web app from scratch
- um certainly that's a route to go but
- that's like going even further in that
- extreme direction that's
- probably going to be a lot more work
- than you want to
- uh you know do for this model ultimately
- you know you've you've got your model
- done you just need to show off the
- results
- you don't want to be spending days or
- weeks on end to
- build out a full-on scalable web app
- you can just use these packages to
- convert your jupyter notebook instead
- um the the big advantage of
- publishing this as an app is that the
- uh someone who knows absolutely nothing
- about python
- does not have python on their computer
- um
- and maybe doesn't even know anything
- about financial modeling
- they can play around with your model
- they can adjust the inputs
- they can see how it changes the outputs
- um
- try different things you're basically
- building like a dashboard they can play
- around with
- and try out different things and see the
- results
- um so this could end up saving you time
- if uh
- you have a working relationship with
- your boss where
- you have a model and then you bring the
- results and they say well you know try
- it with these different things and you
- run back and
- change your model around and then
- generate another report and bring them
- that and
- says oh we'll try these other things and
- you're just going back and forth well
- you know
- you could eliminate that whole cycle if
- there's an app that your
- boss could just play around with and see
- all the results
- um so this is definitely the most
- polished way to go as far as presenting
- the results
- but it is uh you know certainly more
- complicated to do that
- so not that complicated to convert your
- jupiter notebook directly to an
- app
- and one other thing i want to talk about
- on
- presenting your results in python is
- um looking at interactive
- plots because we did some some basic
- plots with pandas
- and well even before getting the
- interactivity
- we can make more advanced plots
- customize them in lots of different ways
- take a look at
- the matplotlib library which
- is what pandas uses in the background to
- generate the plots
- so you can do lots and lots of different
- customizations there basically anything
- you can imagine
- you can create a maplotlib very flexible
- um but there's also these interactive
- plots
- where you see the plot but then you can
- zoom into it
- you can select certain points out of it
- and get more information
- and you can interact with the different
- ways maybe there are drop downs to
- change and look at different things in
- the plot
- etc so
- there are a number of libraries out
- there which help with this such as
- bokeh altair plotly
- holoviews there are a lot lots of them
- out there
- and they can make a really
- nice experience for using your model and
- kind of really drilling into the results
- now of course uh the interactivity
- only makes sense if there's actually um
- you know code running behind it if uh
- you're just generating a pdf report you
- can't have that interactivity
- um but if you're if if the
- person viewing your model is running the
- jupyter notebook itself
- then it can work or if you go and create
- an app
- then it can also work um
- and i mentioned hollow views
- specifically
- um has a really interesting plotting
- library
- where not only does it support these
- interactive plots
- uh but also it's it's kind of a
- whole new way to do plotting you
- basically
- just kind of um you tell it things about
- your data
- and then it can kind of understand your
- data to be able to generate the right
- interactive plots
- um so you can still customize things
- but um i find that it can
- generate these interactive plots with um
- very little effort
- in general so i definitely recommend
- checking that out
- and as far as the resources which you
- can go and
- use to learn these topics in the
- meantime before i'm able to go out
- and uh produce lecture videos on them
- there's a couple resources here
- on creating reports html css reports
- and getting that to a pdf
- and also a guide specifically looking at
- jinja to help with this
- templating jinja is a library that's
- made specifically for templating
- regardless of um what the ultimate
- output format is um
- and then i mentioned going directly to
- the pdf
- so you can use report lab for that
- and you can go direct to latex
- using uh the pi ex latex
- library which i am actually the author
- of
- so if you run into any issues with that
- library
- definitely just reach out to me and i'll
- be happy to help
- but it's it's very nice for um
- i use it a lot for generating pdfs from
- my models
- um because you can just create python
- objects
- which ultimately render into latex and
- then
- generate the pdf from it so you don't
- have to think about latex you don't have
- to think about how you generate a pdf
- from latex
- you just create some python objects and
- you ultimately get a pdf
- so it's a pretty nice library to work
- with
- um and then some more resources on
- matplotlib looking at holo views
- as well and then
- some resources on building the apps
- from your model so panel is a library
- that you can use if you want to
- do a little more customization on what
- your app is going to look like how you
- interact with it
- it allows you to you know add little
- widgets and stuff
- to your jupyter model
- tutorial on that as well
- and then voila is
- what i was talking about which helps you
- just
- take your existing jupiter notebook and
- just
- convert it into a web app like
- essentially zero effort required
- um so
- [Music]
- that that's definitely useful if you
- want to get to a web app with as little
- effort as possible
- um and anvil
- and bill is another option for that that
- is not completely free i think that's a
- paid service
- um and then a couple of resources here
- um should anyone want to go and learn
- more full-on full-stack web development
- um to build really custom things
- um which again that's a lot more work i
- would
- not recommend that for the majority of
- people that just want to
- display the results of their model um
- but it is an interesting direction to go
- down if you want to do really custom
- things
- or have a really scalable application
- so that's an overview on
- how you can present your model results
- in
- python and i hope in the future to
- release more videos on each of these
- particular topics
- in this area so thanks for listening and
- see you next time
Programming Skills for Advanced Financial Models¶
Notes¶
If you are going to take one suggestion from the general programming practices, make it version control. This is an essential skill in programming to be able to work with others. It will also open up options for yourself, giving greater freedom to tear apart the code and not worry about losing anything
Automated testing also allows greater freedom as you are not worried about breaking existing functionality when adding new features of the model. pytest is a useful package to help
Once you are offloading your code into separate .py files, it is useful to work with an IDE such as PyCharm or VS Code. In general, I recommend PyCharm if you are working only or primarily in Python and VS Code if you need a multi-language editor
CI/CD allows you to automate the lifecycle of your code and remove all the manual actions you take to maintain it besides writing the code itself
If you do go to working in an IDE, it is useful to start using type annotations as it will give super powers to your IDE to know exactly what you can and cannot do in the code, giving you suggestions and highlighting issues
Experienced Python programmers use virtual environments religiously because they have been bitten in the past by package conflicts across projects. Once you are managing multiple Python projects, it becomes all but a necessity. They will allow you to keep the dependencies of each project isolated
Transcript¶
- hey everyone
- this is nick dearmartis teaching you
- financial modeling
- today we're going to be talking about
- programming skills
- that are useful for more advanced
- financial models
- this is part of our lecture series that
- introduces the
- new course on advanced financial
- modeling with python
- and also closes out the first course on
- financial modeling with python and excel
- so in the first
- course we certainly cover uh you know
- basic
- python programming everyone completing
- that course should
- feel comfortable that um they have kind
- of a handle on basic python
- but we
- still are only getting really kind of a
- quick brush of everything there's still
- a number of different areas of python
- that we haven't explored
- which would be used fairly commonly in
- more general
- programming settings um
- and some of these are going to be useful
- as you go to expand to
- building additional models and data
- pipelines for those models and
- everything like that
- thankfully python is one of those
- programming languages
- where you don't need to be an expert to
- get things done
- as long as you have a handle on basic
- python
- you can manage to work your way through
- most tasks
- and you don't have to you know
- understand
- really advanced um code structures
- which are good for scalability and
- maintenance
- it's good to learn those over time but
- you can just get started just kind of
- throwing some code in there and it'll
- work
- but if you do go and learn better
- structure for your code and better
- and more advanced programming techniques
- then all of a sudden new things become
- possible and things which were difficult
- before become easy
- so there's a lot more
- to learn beyond what we've already
- looked at in the first course
- and beyond just thinking about python
- programming there are also
- general software engineering practices
- that we can look at and learn from
- in order to become better uh financial
- modelers as well
- so looking at some of those um
- general programming things we can learn
- from
- um so these are the kinds of things that
- um you know software engineers that that
- build software all day
- are doing on a day-to-day basis
- and that you don't see as often and
- the finance industry but um
- it's good to learn from these things um
- to become better programmers
- and build better financial models
- because of that
- so um
- if you're going to take one thing away
- from this lecture
- it should be that you should use a
- version control
- version control is a way
- of tracking the code over time in a
- project
- you track not only
- the code over time but all the changes
- that are being made to the code
- and it also allows multiple people to
- work on the code
- a lot easier without
- going into a lot of detail here um
- you basically can create separate
- branches
- of the code which means that
- people can start from the same code and
- go in different directions with it
- and then you can merge those branches
- back together later so both people can
- be working on the code at the same time
- changing different things about the code
- and then you ultimately bring it
- together
- without having to go through a big
- manual process to do that
- most this is going to be automated
- through the version control software
- and because you have this full history
- um you can do things like um
- well i'm gonna just try something really
- experimental
- i'm gonna tear apart the code totally
- change it and then if it doesn't work
- out well i'll just go back to where i
- was before
- you have basically the entire history of
- the project every change that's been
- made
- throughout um so
- this is a really really useful skill to
- learn for programming
- um i highly highly recommend that you
- learn this
- especially if you're going to start
- working in teams for
- your models then it becomes even more
- necessary
- but even as an individual working on a
- project
- it's still very useful because
- uh you can always revert changes if they
- don't work out
- and you can go and try things and you
- know maybe you're you're trying
- something and it's not working out but
- you want to come back to it later
- well you just leave it in this branch
- over here you go back onto the main code
- you can keep uh you know doing whatever
- changes and then
- you know when you have some free time
- come back to this branch and try to get
- it to work out
- and you can even bring over the changes
- that you've made in the meantime from
- the main branch
- um so becomes way way more flexible to
- work with the code
- and you don't worry about losing
- anything
- so i use this for every single uh
- programming
- project that i do no matter how big or
- small
- so i highly recommend that you do this
- and git is
- the gold standard here there used to be
- other options which which people used uh
- you know fairly commonly like mercurial
- and subversion
- uh but git is nowadays what everyone is
- using
- so it's really not even a decision
- anymore just use git
- for your version control um
- and then git
- itself will work just locally on your
- machine not being hooked up to anything
- um but you can kind of uh
- supercharge git by um
- sharing that um repository that git
- repository into some kind of platform
- github is by far the most common
- place and if you look up basically any
- open source project it's on github
- the git repository is shared through
- github
- um and what this allows
- is it makes your code public or you can
- have private repositories as well
- um it allows much easier sharing
- of the code with others so then once you
- have the code on github
- someone else can come and pull that code
- down and make some changes and
- make a request to you know merge their
- changes back into the main code
- and so it enables a lot easier
- collaboration
- in a very organized way
- automated testing is another
- general area of
- a general pattern that that we can use
- that we can
- draw from the software engineers
- and automated testing is a little bit
- meta
- it's writing code which checks that your
- code
- is working properly um so you're
- you know you might think with your model
- you go and you run it and you check and
- you look at the results
- and make sure the results are sensible
- right and that's your
- your testing um but you could also
- say you know i know if i put in this
- input in the model i know i'm supposed
- to get this output
- now you just write some code that says
- hey
- run the model uh get the output from the
- model
- compare that output to my expected
- output
- if those things don't match up then
- throw an error the test fails
- uh if they match up then do nothing the
- test pass is fine
- so then you just run this code which
- tests your code
- anytime you make changes and then you
- can ensure that you haven't broken
- the functionality so this is
- you know basically essential for
- building large applications and so
- software engineers do this all the time
- um but we can bring that over to our
- financial models
- as well especially in larger more
- complex
- financial models that have a lot of
- different parts you can kind of test
- each part
- individually to ensure that nothing is
- breaking as you go and add additional
- changes
- input validation is basically checking
- that the inputs are uh
- in the way that you expect to get them
- so are they the correct data types
- are they in the the type of range that
- you would expect
- you know if it's a retirement model and
- you're putting starting salary if it's a
- negative number
- then you throw an error saying oh uh
- starting salary can't be negative
- or if it's uh you know they try to put
- abc the string for the starting salary
- then you're going to also throw an error
- starting salary should be a number not a
- string
- so it's not you know
- necessary to do this for your model but
- it's nice for when
- other people if you have other people
- just using your model
- directly because they may not
- totally understand what the
- all the inputs are supposed to be and so
- they make made mistakes
- in setting up the inputs and what this
- validation does is it will just
- immediately tell them exactly what that
- mistake is
- you know you you gave a string when it
- should have been a number for the
- starting salary
- versus if you just let it fail in your
- model well it's like
- now you're trying to like add a string
- and a number and it's like oh can't
- add type str to type float
- and then you just see this long stack
- trace and that's what uh
- what the user of your model sees is uh
- this weird error
- and they they think that your model is
- broken they don't think
- that they use it incorrectly they just
- think it's broken because they see this
- weird error they don't understand coming
- out of it
- so if you have that input validation
- then the error is going to say
- exactly what they did wrong and so
- they'll be able to
- fix that rather than coming to you and
- saying hey this thing broke
- can you help me because it broke uh and
- then you go and say oh well you used it
- incorrectly
- here's how you use it uh just you know
- saves you that
- that whole issue
- um and then if you're starting to go to
- this
- you know in the fir in the financial
- modeling with python and excel course we
- generally cover just having everything
- in one jupyter notebook
- and so jupiter is serving as your ide um
- but if you start to go to the structure
- where you're going to offload code
- into separate python files and have a
- really
- more mature project structure um
- then i would recommend going to using an
- integrated development
- environment ide in order to help you
- write that and maintain that code
- um so pycharm and vs code
- are the two that i would recommend they
- are by far the most popular
- in the python space and generally i
- would say
- if you're really focused on writing
- python code
- go with pycharm it's it's really built
- for python
- if you know you're going to be writing
- python code but you're also going to be
- writing in other languages as well
- then vs code can be a better choice
- because it's you know more natively
- multi-language support
- and also if you have very limited system
- resources on your machine then
- probably go for vs code because it's a
- more lightweight editor
- but pycharm definitely does do a better
- job
- specifically with python so that's why i
- recommend it
- if that's primarily what you're doing
- um and then we have this whole concept
- of continuous integration continuous
- deployment or ci cd for short
- and that's basically thinking about all
- the things that you might have to do in
- your project
- like you know for your financial model
- it might be
- running the data pipeline to get the
- data and then running the model
- and then generating the report from the
- model and then you know maybe emailing
- that report to somebody
- you can automate all these steps and
- that's referred to as a ci cd pipeline
- um
- so you can um you know
- do all those things i mentioned for a
- financial model or if you're building a
- python package you can also
- you know deploy that that python package
- or if it's a web app you can deploy the
- web app
- you can have it automatically format
- your code so it looks nicer you can have
- it
- run all the tests the automated tests
- that you've written
- make sure that nothing breaks so this
- is just all about automating as much as
- possible in the process
- so that's the general programming topics
- and then we have some additional python
- topics as well
- um so one thing that we didn't really
- cover
- [Music]
- is is working with files on the file
- system we covered working with excel
- files
- loading them in through pandas and excel
- wings
- but you can just work with more
- generally any kind of file
- using the built-in python modules os
- pathlib and shoe till
- so check those out one basic data type
- we didn't cover in python are sets
- so sets um have
- unique items and they're not ordered
- um so it's just you know you have a
- bunch of
- different things it takes the unique
- ones of those
- but you can also do intersections
- between sets you can do unions
- um you know intersection being what are
- the overlapping things between these two
- sets
- union being you know take all the things
- from both sets
- put them into a new set um et cetera
- so those that definitely have certain
- applications
- um and then there's you know
- we covered a little bit working with
- classes um
- but we didn't cover at all how to
- customize the built-in behavior of
- python objects
- using dunder methods or double
- underscore methods
- um so that's um
- like you know how we we use the fin
- statement
- library in the class and when you look
- at a financial statements object from
- that library
- it prints out the whole income statement
- and the balance sheet right
- so how you know me as the creator of the
- library how did i get it to do that
- when you just look at the financial
- statements in a jupiter cell
- it was setting one of these dunder
- methods
- uh particularly there it's underscore
- underscore
- repr wrapper uh
- is able to uh set that and for jupiter
- you have
- wrapper underscore html which is going
- to be the html representation
- of your object um but this is just an
- example
- you can override basically any behavior
- of a python object
- so you want to add two things together
- and make
- wanna make it display an animation sure
- you can do that
- you override the underscore underscore
- add method
- and that's gonna make it run whatever
- you logic you define there when you add
- two of these objects together um
- so actually in the fin statement library
- you can take two financial statements
- and add them together and it will go and
- add all the different financial
- statement items together
- so i have that logic implemented on the
- financial statements class
- in the dunder add method
- there it has the logic to go through
- each of the statement items and add them
- together
- and then create a new financial
- statements object and return that
- um so you can basically
- totally change the way that you work
- with python by creating really
- interesting packages
- by overriding these thunder methods
- um we
- [Music]
- only looked at one decorator in the
- course that was the data class decorator
- which converts
- a regular class into a data class
- but decorators allow you to modify
- a class or a function so you just place
- it above the definition
- and it will modify it
- so it's definitely a more advanced
- pattern
- but it can be useful in certain cases
- like maybe
- you know you have a bunch of um
- functions in your model
- and normally they don't really print
- anything out you just
- ultimately get the output at the end um
- but you're debugging the model and so
- you want to see
- you know what's getting spit out for
- each step of the model
- maybe you have a decorator which you put
- on the top of every function
- and depending on the value of some
- variable it either
- prints the output or doesn't print the
- output and then you just change this one
- variable
- and then that will make it all of a
- sudden that every function
- is printing its output you switch it
- back and now
- none of them are printing the output
- so lots of different applications
- there's just an example
- um then we have this concept of pickling
- so a pickling is a way to take a python
- object
- and put it into a file such that you can
- later
- in a new python session load that
- file and get your python object back
- so if you have some kind of custom
- classes in your model that you need to
- store
- this can be a good way to do it
- um and then type annotations
- are a way of
- adding a little bit to your python code
- to say what type
- everything is is it a string is it a
- float is it
- a list is a list of floats is it a list
- of strings
- etc um
- and this will make it easier to read
- your code
- because it's clear what type of data is
- flowing through
- and when you use an ide it's going to
- really
- um kind of uh
- make it a lot better experience because
- you'll see in your ide well if you say
- you know this thing is a string
- and then you put a dot on the end of it
- well it will suggest all the string
- methods for you right there
- or if you then try and add that to
- something which you've said as a number
- it will say it'll highlight that
- immediately you haven't even run the
- code but it highlights it immediately
- and says oh you can't add a string to a
- number
- because it understands that one is a
- string and the other is a number it's
- going to know that that will raise an
- error once you do ultimately run it
- and it's going to highlight it before
- you ever even get there
- so you can catch your mistakes a lot
- sooner uh with the combination of an ide
- and type annotations so
- definitely recommend going in that
- direction
- um and then you know again in the first
- course we
- discussed you know just having
- everything in one jupiter notebook
- that's not really a good long-term
- structure you want to split out your
- code into modules and packages so
- so modules are just um text files and in
- in dot py that have python code in them
- and packages are just folders
- which have a certain python module
- called
- init.pui
- and then it has modules inside of it as
- well
- um so nothing too complicated there
- but it helps you keep things really well
- separated and reuse code between
- different models and projects
- and then virtual environments are a way
- of basically
- you know isolating everything for one
- project all the
- packages that you're using in that
- project keeping them separate from
- another project
- so it may not be clear why you would
- want to do this as someone who's just
- getting into python
- but any experienced python programmer is
- going to be using these things
- religiously because they have run into
- issues when they haven't used them
- that's kind of the natural progression
- you just have all these projects going
- everywhere you're not really thinking
- about the environment and then something
- breaks in one project and it affects
- other projects and all of a sudden all
- your things are broken you're just like
- what is going on
- nothing works anymore everything is
- broken on all my projects
- like this is a nightmare and that's why
- virtual environments are really
- necessary here
- if you're only ever going to have one
- python project on your computer
- you don't need a virtual environment but
- as soon as you think about having
- multiple projects
- uh you should definitely consider using
- virtual environments because it totally
- isolates one project
- from another project
- and then looking at um the resources
- so i'm providing a lot of links on all
- these things so you can go out and learn
- them on your own
- i do want to post videos on some of
- these topics
- but in the meantime you'll have these
- resources
- so here's want to get started with git
- and github
- and then there are apps out there that
- make
- working with git and github easier
- github desktop is one of those
- an introduction to automated testing and
- input validation
- [Music]
- the two different ides that i mentioned
- pycharm and vs code
- an introduction to cicd
- and then looking at actually doing ci cd
- using github and github's built-in ci cd
- system github actions
- working with files in python um
- both in intro and going to more advanced
- usage
- um then looking at how to use sets
- um how to work with these dunder methods
- to
- to customize object behavior
- some more resources on data class
- additional things we can do with data
- classes um
- how i can basically achieve uh you know
- the financial statement display
- in the fin statement package in jupiter
- how you can specifically add that
- or whatever you want to display for your
- particular classes
- how you can get started working with the
- decorators
- to modify existing uh classes and
- functions
- um how you can pickle these
- python objects to store them for later
- with pickle and dill
- a introduction to type annotations in
- python
- how to better structure your python
- projects with packages and modules
- an introduction to virtual environments
- and
- why you might want to use them what they
- are um as well as a package called pipi
- and v
- that makes working with virtual
- environments easier
- so that's an overview on additional
- programming topics that are useful as
- you start to get more advanced
- um with financial modeling and python
- so thanks for listening and see you next
- time
Extra Resources for Python Financial Modeling¶
Notes¶
Hitchhiker’s Guide to Python goes through many general Python topics and also has sections dedicated to specific applications of Python
Real Python regularly posts articles containing in-depth explanations and tutorials. It is generally targeted at intermediate Python programmers, and will give lots of detail
Automate the Boring Stuff is a book that goes through general practical tasks you might want to automate and shows how to do it in Python along with explanations of the underlying code
Practical Business Python is a similar article structure to Real Python but focuses more on common business tasks and is generally targeted at less-experienced programmers
Transcript¶
- hey everyone
- this is nick dear burtis teaching you
- financial modeling
- today we're going to be talking about
- some extra resources that you can use to
- learn
- about python and financial modeling
- topics
- this is part of our lecture segment
- which gives an introduction
- to the new advanced financial modeling
- with python course
- and also closes out the financial
- modeling with python and excel course
- so um
- here we're just looking at a few
- different resources
- that give general
- help with python as
- well as applying it to financial
- modeling
- so the first one here is hitchhiker's
- guide to python
- so this is useful
- just to give an overview on a lot of
- different python topics
- um so some of this stuff is already
- gonna be covered
- um in the initial course um but it also
- definitely goes further um and gives a
- lot of different basics
- and then also has different areas
- should you need to go into any
- particular
- area of development so you know we
- already talked about how
- web scraping is useful for data
- pipelines and financial
- models um so some of that is there
- here's a section on cicd
- which we also talked about
- machine learning they have some
- information on that as well
- um and some more just good general
- recommendations on how to do things um
- so then we have real python
- real python is uh basically a set of
- like tutorial
- tutorial articles um so they
- you know regularly release these
- articles
- um which have a lot of good content on
- them
- so generally it's kind of targeted at
- like an intermediate
- python programmer and
- they have very in-depth tutorials on
- particular topics and there's a lot of
- stuff on here
- so i really like this as a resource
- then we have automate the boring stuff
- with python so that's actually a book
- but it's available free online
- you you can um just go through and find
- everything here
- online um and
- it covers python basics so you know a
- good amount of this you can probably
- mostly skip through after doing the the
- first course
- um but it's it's really applied to
- you know kind of general things that you
- might want to automate
- um and how to write the particular
- python code that
- does that um so
- really good to get started with with
- just automating
- um you know day-to-day tasks
- this is a really good resource
- and then we have practical business
- python so this is a little bit similar
- to real python and that it's kind of
- like an article
- um format just uh you know
- this uh is created by chris moffitt um
- he just keeps posting articles um
- and it's you know particularly about
- you know applying python in a business
- setting
- um and is generally a little bit
- more introductory focused so you don't
- need to be
- as comfortable in python to read through
- the articles
- so it's another really nice general
- resource
- for python financial modeling
- um so that
- covers the additional just general
- resources and
- this also wraps up our introduction to
- the
- advanced financial modeling course and
- the um
- end of the financial modeling with
- python and excel course
- so going forward i want to
- release videos on all the different
- topics
- that we covered in this introduction
- um but i don't necessarily
- have a particular order yet so i'm
- definitely
- looking for feedback on what people
- would like to learn about first
- so please go ahead and just leave a
- comment about
- anything that you're interested on me
- expanding on
- and any of these topics and you know
- based on that feedback
- i can look at those areas first
- for the lecture topics um
- because we kind of you know hit on all
- the basics now with the first course
- as going into more advanced stuff we
- really can
- can go in any order uh the order is not
- so important so
- i just want to see what people want to
- learn the most first
- and i can hem that direction
- and please subscribe on youtube so that
- you can be notified
- as i release these videos on the
- advanced
- financial modeling topics
- [Music]
- and i hope that everyone enjoys the the
- new course
- and i hope that you enjoy the first
- chords
- as well so i would love to to hear your
- feedback
- um on the first course as well so feel
- free to comment
- about that and yeah thanks
- thanks everyone for listening and i'm
- excited to
- branch off into all these more advanced
- topics with you
- thanks for listening and see you next
- time