Gradient Descent (Part 3)
This is part 3 of our gradient descent series. We look at a linear regression and a logistic regression. We extend what we did in the previous two videos in multi-dimensional systems.
Part 1: https://youtu.be/trvgzYjUr-Y
Part 2: https://youtu.be/J1ghebX8XGY
Github: https://github.com/kpmooney/numerical_methods_youtube/tree/master/gradient_descent
Tip Jar: https://paypal.me/kpmooney
10
views
Simple Portfolio Optimization with Python
We have done various optimization problems in past videos including using it to fit regression models, find the optimal angle to hit a baseball to maximize distance traveled, gradient descent, and probably others that I can’t recall. Here, we will try to optimize a portfolio-- at least in a backwards looking sort of way. We will do two optimizations: first is a simple, almost trivial case of maximizing returns, the second is using a Sharpe ratio.
It should be obvious, but I will say it anyway. This is for teaching purposes only and one shouldn’t build a portfolio on this without substantial backtesting.
Github: https://github.com/kpmooney/numerical_methods_youtube/blob/master/portfolio_optimization/Portfolio%20Optimization.ipynb
Tip Jar: paypal.me/kpmooney
27
views
How to Calculate Beta-Weighted Delta
When managing a portfolio with many option positions, it is useful to recast the individual deltas so as to compare each position to a common underlying, say SPY. This is referred to as beta-weighting your deltas. Some trading platforms will do this for you, but still many more do not. In this video, I will show how to do this calculation yourself.
Calculation a stock’s beta coefficient: https://youtu.be/jmKfDvk4k6g
Github: https://github.com/kpmooney/numerical_methods_youtube/tree/master/beta_wieghted_delta
Tip Jar: http://paypal.me/kpmooney
23
views
Why Vectorize Your Code?
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
Tip Jar: http://paypal.me/kpmooney
16
views
More on Root Finding: The Bisection method Using Python
In previous videos, we have used the Newton’s method to find the roots of various functions. In particular, we inverted the Black-Scholes model to solve for the implied volatility of an option. There are, however, other algorithms that can do this. Here, I present the bisection method. This is a quick video showing the basics of the technique. I don’t go into detail with the volatility as we have done that in several previous videos.
Troubleshooting Newton’s mMethod: https://youtu.be/Q6COHive9CY
Original IV Video: https://youtu.be/Jpy3iCsijIU
Github: https://github.com/kpmooney/numerical_methods_youtube/tree/master/root_finding/bisection
Tip Jar: http://paypal.me/kpmooney
3
views
Revisiting the Implied Volatility Calculation: Possible Pitfalls of Newton’s Method (Part 2)
In part 2, we will look specifically at calculating the implied volatility and look at situations where Newton's method might fail.
Part 1: https://youtu.be/Q6COHive9CY
Original Video: https://youtu.be/Jpy3iCsijIU
IMT Options and Lack of a Root: https://youtu.be/2UJ9kFqQF-s
Github: https://github.com/kpmooney/numerical_methods_youtube/blob/master/root_finding/implied_volatility/Root%20Finding.ipynb
Tip Jar: http://paypal.me/kpmooney
44
views
Revisiting the Implied Volatility Calculation: Possible Pitfalls of Newton’s Method (Part 1)
In our original video on calculating the implied volatility of an option from its price, we made use of Newton’s method. This is generally a fairly robust algorithm, but can fail in certain situations. In part one of this video, we will look at situations where problems might arise with this technique of finding the root of a function.
Part 2: https://youtu.be/1UXDpPrAyl0
Original Video: https://youtu.be/Jpy3iCsijIU
IMT Options and Lack of a Root: https://youtu.be/2UJ9kFqQF-s
Github: https://github.com/kpmooney/numerical_methods_youtube/blob/master/root_finding/implied_volatility/Root%20Finding.ipynb
Tip Jar: http://paypal.me/kpmooney
13
views
Differential Algebraic Equations: Solving constrained differential equations in Python
In practical problems involving differential equations, we often find situations where we have a system of ODEs constrained by a number of algebraic equations. Fortunately, there are solvers available to handle these differential algebraic equations. We will look at solving a simple model of a battery using Pythons ODE solver. We will see how problems arise dealing with the constraints and how a DAE solver (We will use Assimulo here) can simplify things.
Wikipedia article on differential algebraic equations: https://en.wikipedia.org/wiki/Differential-algebraic_system_of_equations
Assimulo Website: https://jmodelica.org/assimulo/
Sundials: https://computing.llnl.gov/projects/sundials
IDA Documentation: https://computing.llnl.gov/projects/sundials/ida
Github: https://github.com/kpmooney/numerical_methods_youtube/tree/master/dae
Tip Jar: http://paypal.me/kpmooney
111
views
Adjusting a Short Strangle That Has Gone Against You
Since a lot of people like the trading-related stuff, this is a quick video on adjusting a short strangle that has gone bad. Obviously, the markets have been crazy the last few months, and a lot of short premium trades have gone south. I have a spreadsheet detailing my initial trades and subsequent roles and discuss the reason for each one.
The spreadsheet can be found here: https://github.com/kpmooney/numerical_methods_youtube/tree/master/tlt
The video detailing dividend risk is here: https://youtu.be/vKX371HcbXc
Tip Jar: http://paypal.me/kpmooney
12
views
Removing Duplicate Entries from Arrays in Numpy and MATLAB
So this is something of an impromptu video. I came across a problem trying to process some data yesterday and the solution turned out to be much easier than expected. Hopefully someone will find this of value. Since the code is very simple I won't bother with uploading to gGithub.
Tip Jar: http://paypal.me/kpmooney
22
views
Calculating Option Greeks Using a Spreadsheet (or Python)
In answer to a question in the comments, I show how to use a spreadsheet (as well as Python) to calculate the option greeks. This is straightforward. I’ll calculate delta, theta and gamma as an example.
The spreadsheet and Python notebook are both available in the github repo.
Wikipedia article on Greeks: https://en.wikipedia.org/wiki/Greeks_(finance)
Wikipedia article on the Black-Scholes model: https://en.wikipedia.org/wiki/Black%E2%80%93Scholes_model
Github: https://github.com/kpmooney/numerical_methods_youtube/tree/master/option_greeks
Donate: http://paypal.me/kpmooney
Tip Jar: https://paypal.me/kpmooney
22
views
Kernel Density Estimation with Python: Estimate a Density Function from Data
We return to generating samples from the distribution of daily percent changes in the S&P 500 index. In the last video, we looked at using a histogram to do this. Here, we look at a technique called kernel density estimation to do something similar.
Wikipedia: https://en.wikipedia.org/wiki/Kernel_density_estimation
Github: https://github.com/kpmooney/numerical_methods_youtube/blob/master/market_analysis/kernel_density_estimation.ipynb
Tip Jar: https://paypal.me/kpmooney
18
views
Random Sampling from a Histogram: Using S%P 500 Data
In the last video, we generated a histogram of SPX daily returns. I briefly touched on how to come up with an approximate probability density. I wan to elaborate on that a bit more, and show a very simple way to sample from out custom density function. There are other ways to build an approximate density function, but this is straightforward and probably easier to understand.
As this video is rather basic and doing things as described here depends a lot on how you build the histogram and do the analysis, this will likely be part one of two.
Wikipedia Article: https://en.wikipedia.org/wiki/Inverse_transform_sampling
Github Notebook: https://github.com/kpmooney/numerical_methods_youtube/blob/master/market_analysis/sample_using_cdf.ipynb
Tip Jar: https://paypal.me/kpmooney
15
views
Calculating Simple Statistics with Python and Pandas: Stock Market Data
A really simple video this time. We use Numpy/Scipy and Pandas to calculate some simple statistics on the daily returns of the S&P 500 index. Specifically, we calculate the mean, standard deviation, skewness, and kurtosis. I also talk about generating a histogram and modifying the plot to represent a density.
Github: https://github.com/kpmooney/numerical_methods_youtube/tree/master/market_analysis
Tip Jar: https://paypal.me/kpmooney
51
views
Assessing Dividend Assignment Risk: Estimate the Dividend Amount
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?
Tip Jar: http://paypal.me/kpmooney
27
views
Transforming Ordinary Differential Equations to A simple Algebraic System Using SciPy (Part 2)
This part two dealing with transforming a system of differential equations into a system of algebraic equations. In part one, we had the advantage of dealing with linear equations and solving the problem amounted to finding the inverse of a sparse matrix. In this video we will look at the nonlinear case and adapt our techniques to create a system of equations we can solve using Scipy's fsolve function.
Original Video Using solve_ivp: https://youtu.be/XPT3_L13RFM
Part 1, Linear Case: https://youtu.be/gXBPPY7TzNo
Github: https://github.com/kpmooney/numerical_methods_youtube/tree/master/free_fall
Tip Jar: http://paypal.me/kpmooney
18
views
Another Impromptu Trading Video: Why do I always (almost always) maintain a short bias my portfolio.
In the video I probably should have mentioned that I have not done well in the market down move. I was fine with my short deltas for the first few hundred S&P points, but this move has been too big, and too fast. The only hedge would have been “static” deltas such as short stock or short S&P futures which I didn’t have. I don’t want to give the appearance that I’ve killed it during this move. The opposite is true. I just haven’t gotten it as been as others.
Tip Jar: https://paypal.me/kpmooney
21
views
Impromptu Video: Setting up Iron Condors and Strangles and Some Position Adjustments
This was somewhat of an impromptu video for friends and family on Facebook, but since a lot of folks here seem to like the trading-related videos, perhaps some will find it useful.
Setting up strangles and iron condors: I also mention adjusting bad stock trades towards the end, but this is not really meant for people with just cash accounts or 401ks. I’ll try to write up something more general later, though to be honest, if you have those types of accounts your options are rather limited.
I tried to keep the jargon to a minimum, but I am assuming a basic knowledge of options here.
Iron condors can be thought of as two different things depending on your strikes and what you intend for the trade. The first is what you’d find in a book on trading, basically a defined-risk, delta-neutral short premium position. These are going to have relatively narrow wings to keep the potential losses small. The second way is to think of them as an approximation of a strangle. Here, you’d have wide wings, only buying the long options to cut down on buying power requirements or to synthetically do a strangle in an account that is not permissioned for naked options. The iron condor I did is closer to the first case. Obviously, there is a continuum between the two extremes. Generally, with the first type of condor, I’m looking for a credit that is ⅓ the width of the wings. I collected a little more than that on this particular trade.
Expirations: Regular expirations not weeklies, ideally closest to 45 days to expiration. I tend to roll the position out to the next cycle if there are 21 days or less left. I did these with 28 days left, which is borderline. I figured with the higher volatility, it was worth the additional gamma risk. I look to close trades when they reach 50% of max profit. This cuts down on portfolio P&L volatility.
One thing I forgot to mention in the video, or only did so obliquely, was that I have my options screen set up to save the delta and probability of expiring in-the-money displayed next to the prices.
So as of now, I don’t have many positions on. I usually have three to four times the number of trades on. Assuming some of the chaos abates a little, I will begin to put on more trades to take advantage of the high implied volatility. Normally, I want my portfolio to have a small amount of short bias to it though I am pretty close to delta neutral. The short deltas are a partial hedge against expanding volatility.
Tip Jar: http://paypal.me/kpmooney
38
views
Candlestick Charts and Technical Studies Using Python and mplfinance
We use Matplotlib and Mplfinance to make some candlestick charts and add some technical studies, in this case, moving averages. We also show how to calculate moving averages in Pandas.
This is a simple, quick video. Hopefully if you're into charting and technical analysis (I’m not) this will point you in a potential direction for coding up your own stuff.
mplfinance: https://github.com/matplotlib/mplfinance
Github: https://github.com/kpmooney/numerical_methods_youtube/tree/master/market_analysis
Tip Jar: https://paypal.me/kpmooney
28
views
Transforming Ordinary Differential Equations to A simple Algebraic System Using SciPy (Part 1)
We return to solving differential equations back going back to the free fall/parachutist problem from a previous video. We will solve this problem using a finite difference method (Euler’s method) in two ways: First, will we use simple loops. Second, we will transform the system of equations into a sparse matrix representation. In the simple linear case in this video, we can solve for the position and velocity simply by inverting a matrix.
The next video in this series will deal with the nonlinear case which is more challenging to solve via matrix manipulation.
This may seem overkill for such a simple problem, especially given that Scipy comes with its own ODE solver, but we will use a similar method when we move on to partial differential equations. There, we will transform a PDE into a system of ODEs that can be solved with regular ODE solvers.
Edit: There was an error in both the code and the matrix as written out in the notebook. The first row should not have a -1 in it. Instead it should be a zero. It doesn''t make a difference in the result as the initial velocity is zero, but it is technically incorrect. I have fixed this in the notebook on Github.
Original Video: https://youtu.be/XPT3_L13RFM
Github: https://github.com/kpmooney/numerical_methods_youtube/tree/master/free_fall
Tip Jar: https://paypal.me/kpmooney
24
views
Calculating Probability of Making 50% of Max Profit on a Short Strangle Using Python
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
Tip Jar: https://paypal.me/kpmooney
44
views
Generating Option Payoff Plots in Python
In answer to a question, I show how we can repurpose our Black-Scholes code to work out option payouts at expiration and generate diagrams for individual options as well as dad's.
Github: https://github.com/kpmooney/numerical_methods_youtube/tree/master/payout
Tip Jar: http://paypal.me/kpmooney
107
views
More on Ordinary Differential Equations in Python: The Assimulo Package
In the last video, we pointed out that Scipy’s ODE solver doesn’t return the function values at the event time when an event is detected. Assimulo is a Python wrapper written around the Sundials library allowing for the solution of both ODEs and differential algebraic equations (DAEs). Assimulo does return the function values at events so we demonstrate this with the no-drag projectile motion problem that we’ve looked at in previous videos.
We will cover Assimulo and DAEs in later videos in more depth, but this is a quick video demonstrating some of its ODE capabilities.
Projectile Motion and Optimization (part 1): https://youtu.be/DSJj9rfHKPk
Projectile Motion and Optimization (part 2): https://youtu.be/-hJc2zktxCs
Projectile Motion (part 3): https://youtu.be/qjb8hMoDrLI
Github: https://github.com/kpmooney/numerical_methods_youtube/tree/master/physics/projectile_motion
Tip Jar: http://paypal.me/kpmooney
44
views
Refining Our Projectile Motion Code: Some differences between MATLAB's and SciPy's ODE Solvers
There is a known issue-- but not known to me, apparently-- with the way Scipy’s ODE solver solve_ivp handles event detection. This will affect our projectile motion problem from the last two videos. I compare this with MAYLAB and come up with a workaround though the video is just a quick code snippet showing how one might handle the issue.
Tip Jar: https://paypal.me/kpmooney
22
views