Linear Systems and Sparse Matrices with Numpy and Scipy
A lot of problems in numerical methods boil down to solving systems of linear equations. In many cases, the matrices involved are very large. Fortunately, often one finds most of the entries of these are zero. These are referred to as sparse matrices. There are many advantages to being able to use sparse systems. Memory storage is greatly reduced as well as the ability to use algorithms custom built to perform fasted than with dense matrices. I’ll show Numpy’s basic ability to solve systems of linear equations, the I’ll revisit the stock monte carlo code from a previous video and treat it as a system of linear equations and use sparse matrix capabilities that are built in to Scipy to generate a simulated run of stock prices.
We’ve talked at length about sparse matrices in the previous videos. It turns out that a lot of practical problems produce systems that have a fairly specific shape- a banded matrix. I had assumed Scipy’s sparse solver would reconginze the matrix is banded and use an appropriate algorithm. Evidently, this is not the case. There is, however, a banded solver that can be called specifically. In this video, I briefly go over how to use it and show the performance enhancement over the generic spsolve command.
Part 1: https://youtu.be/v_S7cOL5ZWU
Part 2: https://youtu.be/qo-WzsVnXGE
Part 3: https://youtu.be/qSBqgKVD6EA
Github: https://github.com/kpmooney/numerical_methods_youtube/blob/master/prob_50/More%20on%20Linear%20Systems.ipynb
Donate: https://paypal.me/kpmooney
...
https://www.youtube.com/watch?v=qSBqgKVD6EA
Recently, we did a video where we repurposed our Newton's method code where we solved for the implied volatility and used it to calculate a stock price assuming we already knew the volatility. Someone requested that I do the same calculation with a spreadsheet. So this is a quick video demonstrating how to do the same calculation with a spreadsheet and also at the end I will briefly show how to use goal seek to solve the same problem.
Original Newton’s Method Video: https://youtu.be/Jpy3iCsijIU
Original Newton’s Method Spreadsheet Video: https://youtu.be/ljlqGWz1xDY
Original Stock Calculation Video: https://youtu.be/NF-XMP8RjJo
Github:https://github.com/kpmooney/numerical_methods_youtube/tree/master/root_finding/implied_volatility
Spreadsheet: https://github.com/kpmooney/numerical_methods_youtube/blob/master/root_finding/implied_volatility/find_stock.xlsx
Donate: https://paypal.me/kpmooney
...
https://www.youtube.com/watch?v=8wlJZ8UouB8
NOTE: WHEN RECORDING THIS, i ACCIDENTALLY FORGOT TO USE THE SPARSE IDENTITY MATRIX IN MATLAB. THE GITHUB CODE HAS BEEN UPDATED AND IS CORRECT. THE 10000-DIMENSIONAL SYSTEM IS NOW SOLVED IN 0.9 SECONDS INSTEAD OF TWO MINUTES!
In answer to a question, we look at using sparse matrices in the solution of boundary value problems. For nonlinear problems, things become more complicated, but we can still use fsolve to solve the underlying system of equations. The issue with Python is the solver cannot handle sparse Jacobian matrices so this limits the utility of this method to small problems. For solvers that can handle sparse Jaboians such as MATLAB’s, we can still get good performance despite having to use a nonlinear solver.
Wikipedia Article on the Jacobian Matrix: https://en.wikipedia.org/wiki/Jacobian_matrix_and_determinant
Github Python Notebook: https://github.com/kpmooney/numerical_methods_youtube/blob/master/bvp/Nonlinear%20BVP.ipynb
Github MATLAB Code: https://github.com/kpmooney/numerical_methods_youtube/blob/master/bvp/Nonlinear.m
Tip Jar: https://paypal.me/kpmooney
...
https://www.youtube.com/watch?v=JYxUh5h_g-M
In this straightforward video, I extend the code previously built to calculate the probability of making 50% of maximum profit on an option spread, in this case, a short strangle.
Previous Monte Carlo Video: https://youtu.be/yGlkRpqMDVk
Github: https://github.com/kpmooney/numerical_methods_youtube/tree/master/prob_50
...
https://www.youtube.com/watch?v=Vz7w2HiuU6A
This video is meant as a companion to the recent videos in the series of how to vectorize the implied volatility calculation so as to be able to process several option contracts at once. But it also supports some ideas we’ve used in the post where I have mentioned that writing code in a vectorized manner will speed up the calculation. As I said in the introduction, this is more for the sake of completeness as I just wanted a live demonstration of the performance benefits one can get structuring code in this way.
Videos referenced:
Probability of making 50% on a short option trade: https://youtu.be/yGlkRpqMDVk
Why Newton's Method Might Fail (Part 1): https://youtu.be/Q6COHive9CY
Why Newton’s method might Fail (Part 2): https://youtu.be/1UXDpPrAyl0
Donate: http://paypal.me/kpmooney
...
https://www.youtube.com/watch?v=EdyOEX4WZoo
I wanted to do a quick video following up with the binomial option model we looked at in an earlier video. I’ll quickly show how we can use Scipy’s function fsolve to solve for the IV. This is a simple extension on concepts we’ve already discussed using Black-Scholes as the p[ricing model.
Binomial Model: https://youtu.be/d7wa16RNRCI
Github: https://github.com/kpmooney/numerical_methods_youtube/blob/master/binomial_model/Binomial%20Model.ipynb
Tip Jar: https://paypal.me/kpmooney
...
https://www.youtube.com/watch?v=W_f3UL8xHDw
I had a deep in-the-money short call on an ETF that was going ex-dividend. Risk of assignment depends on the value of the dividend compared to the extrinsic value left in the option. How do we determine the dividend value of an ETF which does not announce the amount beforehand?
Donate: http://paypal.me/kpmooney
...
https://www.youtube.com/watch?v=vKX371HcbXc
We return to the topic of curve fitting. We will fit a complicated nonlinear differential algebraic equation system to experimental data. This problem is compounded by the fact the parameters we are searching for vary by over 10 orders of magnitude. We can fit such a system in the usual way using the curve_fit or minimize function, but the vast difference in the parameters will cause problems with the algorithms. One needs to rescale these numbers in such a way as to be roughly the same order of magnitude. We will accomplish this (for this case), by coding to fit the log of the parameters instead.
Github: https://github.com/kpmooney/numerical_methods_youtube/blob/master/curve_fitting/battery_model/Curve%20Fitting%20Battery%20Model.ipynb
Paper Referenced in this Video: https://iopscience.iop.org/article/10.1149/1.2422896
Donate: https://paypal.me/kpmooney
...
https://www.youtube.com/watch?v=V8zfHNeQUpE
This is a somewhat spur-of-the-moment video. I was revisiting some old MATLAB code that involved the singular value decomposition and since we are using Python more and more, I wanted to familiarize myself with the Numpy implementation thereof. Normally, the two languages are pretty similar, but in this case, there are enough differences that I wanted to note them down for future reference. So this video is essentially me detailing some of the major differences them.
Github: https://github.com/kpmooney/numerical_methods_youtube/tree/master/svd
Donate: paypal.me/kpmooney
...
https://www.youtube.com/watch?v=nnFCuFz2KL0